blob: b58374daff32b05f0be4456c48d562061acd5513 [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 Rodrigues54519c52014-06-06 12:12:32 -050057 struct dlm_lock_resource *bitmap_lockres;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -050058 struct dlm_lock_resource *resync_lockres;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050059 struct list_head suspend_list;
60 spinlock_t suspend_lock;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050061 struct md_thread *recovery_thread;
62 unsigned long recovery_map;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050063 /* communication loc resources */
64 struct dlm_lock_resource *ack_lockres;
65 struct dlm_lock_resource *message_lockres;
66 struct dlm_lock_resource *token_lockres;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050067 struct dlm_lock_resource *no_new_dev_lockres;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050068 struct md_thread *recv_thread;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050069 struct completion newdisk_completion;
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060070 unsigned long state;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050071};
72
73enum msg_type {
74 METADATA_UPDATED = 0,
75 RESYNCING,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050076 NEWDISK,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -050077 REMOVE,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -050078 RE_ADD,
Guoqing Jiangdc737d72015-07-10 16:54:04 +080079 BITMAP_NEEDS_SYNC,
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050080};
81
82struct cluster_msg {
Guoqing Jiangcf97a342015-10-16 15:40:22 +080083 __le32 type;
84 __le32 slot;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050085 /* TODO: Unionize this for smaller footprint */
Guoqing Jiangcf97a342015-10-16 15:40:22 +080086 __le64 low;
87 __le64 high;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050088 char uuid[16];
Guoqing Jiangcf97a342015-10-16 15:40:22 +080089 __le32 raid_slot;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060090};
91
92static void sync_ast(void *arg)
93{
94 struct dlm_lock_resource *res;
95
NeilBrown2e2a7cd2015-10-19 15:42:18 +110096 res = arg;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060097 complete(&res->completion);
98}
99
100static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
101{
102 int ret = 0;
103
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600104 ret = dlm_lock(res->ls, mode, &res->lksb,
105 res->flags, res->name, strlen(res->name),
106 0, sync_ast, res, res->bast);
107 if (ret)
108 return ret;
109 wait_for_completion(&res->completion);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500110 if (res->lksb.sb_status == 0)
111 res->mode = mode;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600112 return res->lksb.sb_status;
113}
114
115static int dlm_unlock_sync(struct dlm_lock_resource *res)
116{
117 return dlm_lock_sync(res, DLM_LOCK_NL);
118}
119
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500120static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600121 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
122{
123 struct dlm_lock_resource *res = NULL;
124 int ret, namelen;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500125 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600126
127 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
128 if (!res)
129 return NULL;
Guoqing Jiangb83d51c2015-07-10 17:01:16 +0800130 init_completion(&res->completion);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500131 res->ls = cinfo->lockspace;
132 res->mddev = mddev;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500133 res->mode = DLM_LOCK_IV;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600134 namelen = strlen(name);
135 res->name = kzalloc(namelen + 1, GFP_KERNEL);
136 if (!res->name) {
137 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
138 goto out_err;
139 }
140 strlcpy(res->name, name, namelen + 1);
141 if (with_lvb) {
142 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
143 if (!res->lksb.sb_lvbptr) {
144 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
145 goto out_err;
146 }
147 res->flags = DLM_LKF_VALBLK;
148 }
149
150 if (bastfn)
151 res->bast = bastfn;
152
153 res->flags |= DLM_LKF_EXPEDITE;
154
155 ret = dlm_lock_sync(res, DLM_LOCK_NL);
156 if (ret) {
157 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
158 goto out_err;
159 }
160 res->flags &= ~DLM_LKF_EXPEDITE;
161 res->flags |= DLM_LKF_CONVERT;
162
163 return res;
164out_err:
165 kfree(res->lksb.sb_lvbptr);
166 kfree(res->name);
167 kfree(res);
168 return NULL;
169}
170
171static void lockres_free(struct dlm_lock_resource *res)
172{
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800173 int ret;
174
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600175 if (!res)
176 return;
177
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800178 /* cancel a lock request or a conversion request that is blocked */
179 res->flags |= DLM_LKF_CANCEL;
180retry:
181 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
182 if (unlikely(ret != 0)) {
183 pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret);
184
185 /* if a lock conversion is cancelled, then the lock is put
186 * back to grant queue, need to ensure it is unlocked */
187 if (ret == -DLM_ECANCEL)
188 goto retry;
189 }
190 res->flags &= ~DLM_LKF_CANCEL;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600191 wait_for_completion(&res->completion);
192
193 kfree(res->name);
194 kfree(res->lksb.sb_lvbptr);
195 kfree(res);
196}
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600197
NeilBrown30661b42015-10-19 15:44:00 +1100198static void add_resync_info(struct dlm_lock_resource *lockres,
199 sector_t lo, sector_t hi)
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500200{
201 struct resync_info *ri;
202
203 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
204 ri->lo = cpu_to_le64(lo);
205 ri->hi = cpu_to_le64(hi);
206}
207
208static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
209{
210 struct resync_info ri;
211 struct suspend_info *s = NULL;
212 sector_t hi = 0;
213
214 dlm_lock_sync(lockres, DLM_LOCK_CR);
215 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
216 hi = le64_to_cpu(ri.hi);
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800217 if (hi > 0) {
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500218 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
219 if (!s)
220 goto out;
221 s->hi = hi;
222 s->lo = le64_to_cpu(ri.lo);
223 }
224 dlm_unlock_sync(lockres);
225out:
226 return s;
227}
228
kbuild test robot6dc69c9c2015-02-28 07:04:37 +0800229static void recover_bitmaps(struct md_thread *thread)
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500230{
231 struct mddev *mddev = thread->mddev;
232 struct md_cluster_info *cinfo = mddev->cluster_info;
233 struct dlm_lock_resource *bm_lockres;
234 char str[64];
235 int slot, ret;
236 struct suspend_info *s, *tmp;
237 sector_t lo, hi;
238
239 while (cinfo->recovery_map) {
240 slot = fls64((u64)cinfo->recovery_map) - 1;
241
242 /* Clear suspend_area associated with the bitmap */
243 spin_lock_irq(&cinfo->suspend_lock);
244 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
245 if (slot == s->slot) {
246 list_del(&s->list);
247 kfree(s);
248 }
249 spin_unlock_irq(&cinfo->suspend_lock);
250
251 snprintf(str, 64, "bitmap%04d", slot);
252 bm_lockres = lockres_init(mddev, str, NULL, 1);
253 if (!bm_lockres) {
254 pr_err("md-cluster: Cannot initialize bitmaps\n");
255 goto clear_bit;
256 }
257
258 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
259 if (ret) {
260 pr_err("md-cluster: Could not DLM lock %s: %d\n",
261 str, ret);
262 goto clear_bit;
263 }
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500264 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500265 if (ret) {
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500266 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500267 goto dlm_unlock;
268 }
269 if (hi > 0) {
270 /* TODO:Wait for current resync to get over */
271 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
272 if (lo < mddev->recovery_cp)
273 mddev->recovery_cp = lo;
274 md_check_recovery(mddev);
275 }
276dlm_unlock:
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500277 dlm_unlock_sync(bm_lockres);
278clear_bit:
279 clear_bit(slot, &cinfo->recovery_map);
280 }
281}
282
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500283static void recover_prep(void *arg)
284{
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500285 struct mddev *mddev = arg;
286 struct md_cluster_info *cinfo = mddev->cluster_info;
287 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500288}
289
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800290static void __recover_slot(struct mddev *mddev, int slot)
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500291{
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500292 struct md_cluster_info *cinfo = mddev->cluster_info;
293
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800294 set_bit(slot, &cinfo->recovery_map);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500295 if (!cinfo->recovery_thread) {
296 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
297 mddev, "recover");
298 if (!cinfo->recovery_thread) {
299 pr_warn("md-cluster: Could not create recovery thread\n");
300 return;
301 }
302 }
303 md_wakeup_thread(cinfo->recovery_thread);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500304}
305
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800306static void recover_slot(void *arg, struct dlm_slot *slot)
307{
308 struct mddev *mddev = arg;
309 struct md_cluster_info *cinfo = mddev->cluster_info;
310
311 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
312 mddev->bitmap_info.cluster_name,
313 slot->nodeid, slot->slot,
314 cinfo->slot_number);
315 /* deduct one since dlm slot starts from one while the num of
316 * cluster-md begins with 0 */
317 __recover_slot(mddev, slot->slot - 1);
318}
319
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500320static void recover_done(void *arg, struct dlm_slot *slots,
321 int num_slots, int our_slot,
322 uint32_t generation)
323{
324 struct mddev *mddev = arg;
325 struct md_cluster_info *cinfo = mddev->cluster_info;
326
327 cinfo->slot_number = our_slot;
Guoqing Jiangeece0752015-07-10 17:01:21 +0800328 /* completion is only need to be complete when node join cluster,
329 * it doesn't need to run during another node's failure */
330 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
331 complete(&cinfo->completion);
332 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
333 }
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500334 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500335}
336
Guoqing Jiangeece0752015-07-10 17:01:21 +0800337/* the ops is called when node join the cluster, and do lock recovery
338 * if node failure occurs */
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500339static const struct dlm_lockspace_ops md_ls_ops = {
340 .recover_prep = recover_prep,
341 .recover_slot = recover_slot,
342 .recover_done = recover_done,
343};
344
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500345/*
346 * The BAST function for the ack lock resource
347 * This function wakes up the receive thread in
348 * order to receive and process the message.
349 */
350static void ack_bast(void *arg, int mode)
351{
NeilBrown2e2a7cd2015-10-19 15:42:18 +1100352 struct dlm_lock_resource *res = arg;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500353 struct md_cluster_info *cinfo = res->mddev->cluster_info;
354
355 if (mode == DLM_LOCK_EX)
356 md_wakeup_thread(cinfo->recv_thread);
357}
358
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500359static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
360{
361 struct suspend_info *s, *tmp;
362
363 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
364 if (slot == s->slot) {
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500365 list_del(&s->list);
366 kfree(s);
367 break;
368 }
369}
370
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500371static void remove_suspend_info(struct mddev *mddev, int slot)
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500372{
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500373 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500374 spin_lock_irq(&cinfo->suspend_lock);
375 __remove_suspend_info(cinfo, slot);
376 spin_unlock_irq(&cinfo->suspend_lock);
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500377 mddev->pers->quiesce(mddev, 2);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500378}
379
380
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500381static void process_suspend_info(struct mddev *mddev,
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500382 int slot, sector_t lo, sector_t hi)
383{
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500384 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500385 struct suspend_info *s;
386
387 if (!hi) {
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500388 remove_suspend_info(mddev, slot);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500389 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
390 md_wakeup_thread(mddev->thread);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500391 return;
392 }
393 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
394 if (!s)
395 return;
396 s->slot = slot;
397 s->lo = lo;
398 s->hi = hi;
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500399 mddev->pers->quiesce(mddev, 1);
400 mddev->pers->quiesce(mddev, 0);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500401 spin_lock_irq(&cinfo->suspend_lock);
402 /* Remove existing entry (if exists) before adding */
403 __remove_suspend_info(cinfo, slot);
404 list_add(&s->list, &cinfo->suspend_list);
405 spin_unlock_irq(&cinfo->suspend_lock);
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500406 mddev->pers->quiesce(mddev, 2);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500407}
408
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500409static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
410{
411 char disk_uuid[64];
412 struct md_cluster_info *cinfo = mddev->cluster_info;
413 char event_name[] = "EVENT=ADD_DEVICE";
414 char raid_slot[16];
415 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
416 int len;
417
418 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800419 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800420 snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500421 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
422 init_completion(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600423 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500424 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
425 wait_for_completion_timeout(&cinfo->newdisk_completion,
426 NEW_DEV_TIMEOUT);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600427 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500428}
429
430
431static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
432{
433 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500434 md_reload_sb(mddev, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500435 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
436}
437
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500438static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
439{
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800440 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
441 le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500442
Guoqing Jiang659b2542015-12-21 10:50:59 +1100443 if (rdev) {
444 set_bit(ClusterRemove, &rdev->flags);
445 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
446 md_wakeup_thread(mddev->thread);
447 }
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500448 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800449 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
450 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500451}
452
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500453static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
454{
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800455 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
456 le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500457
458 if (rdev && test_bit(Faulty, &rdev->flags))
459 clear_bit(Faulty, &rdev->flags);
460 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800461 pr_warn("%s: %d Could not find disk(%d) which is faulty",
462 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500463}
464
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500465static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
466{
Guoqing Jiang256f5b22015-10-12 17:21:23 +0800467 if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
468 "node %d received it's own msg\n", le32_to_cpu(msg->slot)))
469 return;
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800470 switch (le32_to_cpu(msg->type)) {
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500471 case METADATA_UPDATED:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500472 process_metadata_update(mddev, msg);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500473 break;
474 case RESYNCING:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800475 process_suspend_info(mddev, le32_to_cpu(msg->slot),
476 le64_to_cpu(msg->low),
477 le64_to_cpu(msg->high));
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500478 break;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500479 case NEWDISK:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500480 process_add_new_disk(mddev, msg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500481 break;
482 case REMOVE:
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500483 process_remove_disk(mddev, msg);
484 break;
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500485 case RE_ADD:
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500486 process_readd_disk(mddev, msg);
487 break;
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800488 case BITMAP_NEEDS_SYNC:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800489 __recover_slot(mddev, le32_to_cpu(msg->slot));
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800490 break;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500491 default:
492 pr_warn("%s:%d Received unknown message from %d\n",
493 __func__, __LINE__, msg->slot);
kbuild test robot09dd1af2015-02-28 09:16:08 +0800494 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500495}
496
497/*
498 * thread for receiving message
499 */
500static void recv_daemon(struct md_thread *thread)
501{
502 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
503 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
504 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
505 struct cluster_msg msg;
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800506 int ret;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500507
508 /*get CR on Message*/
509 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
510 pr_err("md/raid1:failed to get CR on MESSAGE\n");
511 return;
512 }
513
514 /* read lvb and wake up thread to process this message_lockres */
515 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
516 process_recvd_msg(thread->mddev, &msg);
517
518 /*release CR on ack_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800519 ret = dlm_unlock_sync(ack_lockres);
520 if (unlikely(ret != 0))
521 pr_info("unlock ack failed return %d\n", ret);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800522 /*up-convert to PR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800523 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
524 if (unlikely(ret != 0))
525 pr_info("lock PR on msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500526 /*get CR on ack_lockres again*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800527 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
528 if (unlikely(ret != 0))
529 pr_info("lock CR on ack failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500530 /*release CR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800531 ret = dlm_unlock_sync(message_lockres);
532 if (unlikely(ret != 0))
533 pr_info("unlock msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500534}
535
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500536/* lock_comm()
537 * Takes the lock on the TOKEN lock resource so no other
538 * node can communicate while the operation is underway.
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500539 * If called again, and the TOKEN lock is alread in EX mode
540 * return success. However, care must be taken that unlock_comm()
541 * is called only once.
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500542 */
543static int lock_comm(struct md_cluster_info *cinfo)
544{
545 int error;
546
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500547 if (cinfo->token_lockres->mode == DLM_LOCK_EX)
548 return 0;
549
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500550 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
551 if (error)
552 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
553 __func__, __LINE__, error);
554 return error;
555}
556
557static void unlock_comm(struct md_cluster_info *cinfo)
558{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500559 WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500560 dlm_unlock_sync(cinfo->token_lockres);
561}
562
563/* __sendmsg()
564 * This function performs the actual sending of the message. This function is
565 * usually called after performing the encompassing operation
566 * The function:
567 * 1. Grabs the message lockresource in EX mode
568 * 2. Copies the message to the message LVB
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800569 * 3. Downconverts message lockresource to CW
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500570 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
571 * and the other nodes read the message. The thread will wait here until all other
572 * nodes have released ack lock resource.
573 * 5. Downconvert ack lockresource to CR
574 */
575static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
576{
577 int error;
578 int slot = cinfo->slot_number - 1;
579
580 cmsg->slot = cpu_to_le32(slot);
581 /*get EX on Message*/
582 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
583 if (error) {
584 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
585 goto failed_message;
586 }
587
588 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
589 sizeof(struct cluster_msg));
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800590 /*down-convert EX to CW on Message*/
591 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500592 if (error) {
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800593 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500594 error);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800595 goto failed_ack;
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500596 }
597
598 /*up-convert CR to EX on Ack*/
599 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
600 if (error) {
601 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
602 error);
603 goto failed_ack;
604 }
605
606 /*down-convert EX to CR on Ack*/
607 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
608 if (error) {
609 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
610 error);
611 goto failed_ack;
612 }
613
614failed_ack:
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800615 error = dlm_unlock_sync(cinfo->message_lockres);
616 if (unlikely(error != 0)) {
617 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
618 error);
619 /* in case the message can't be released due to some reason */
620 goto failed_ack;
621 }
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500622failed_message:
623 return error;
624}
625
626static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
627{
628 int ret;
629
630 lock_comm(cinfo);
631 ret = __sendmsg(cinfo, cmsg);
632 unlock_comm(cinfo);
633 return ret;
634}
635
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500636static int gather_all_resync_info(struct mddev *mddev, int total_slots)
637{
638 struct md_cluster_info *cinfo = mddev->cluster_info;
639 int i, ret = 0;
640 struct dlm_lock_resource *bm_lockres;
641 struct suspend_info *s;
642 char str[64];
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800643 sector_t lo, hi;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500644
645
646 for (i = 0; i < total_slots; i++) {
647 memset(str, '\0', 64);
648 snprintf(str, 64, "bitmap%04d", i);
649 bm_lockres = lockres_init(mddev, str, NULL, 1);
650 if (!bm_lockres)
651 return -ENOMEM;
652 if (i == (cinfo->slot_number - 1))
653 continue;
654
655 bm_lockres->flags |= DLM_LKF_NOQUEUE;
656 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
657 if (ret == -EAGAIN) {
658 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
659 s = read_resync_info(mddev, bm_lockres);
660 if (s) {
661 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
662 __func__, __LINE__,
663 (unsigned long long) s->lo,
664 (unsigned long long) s->hi, i);
665 spin_lock_irq(&cinfo->suspend_lock);
666 s->slot = i;
667 list_add(&s->list, &cinfo->suspend_list);
668 spin_unlock_irq(&cinfo->suspend_lock);
669 }
670 ret = 0;
671 lockres_free(bm_lockres);
672 continue;
673 }
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800674 if (ret) {
675 lockres_free(bm_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500676 goto out;
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800677 }
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800678
679 /* Read the disk bitmap sb and check if it needs recovery */
680 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
681 if (ret) {
682 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
683 lockres_free(bm_lockres);
684 continue;
685 }
686 if ((hi > 0) && (lo < mddev->recovery_cp)) {
687 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
688 mddev->recovery_cp = lo;
689 md_check_recovery(mddev);
690 }
691
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500692 dlm_unlock_sync(bm_lockres);
693 lockres_free(bm_lockres);
694 }
695out:
696 return ret;
697}
698
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500699static int join(struct mddev *mddev, int nodes)
700{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500701 struct md_cluster_info *cinfo;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500702 int ret, ops_rv;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500703 char str[64];
704
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500705 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
706 if (!cinfo)
707 return -ENOMEM;
708
Guoqing Jiang9e3072e2015-07-10 17:01:18 +0800709 INIT_LIST_HEAD(&cinfo->suspend_list);
710 spin_lock_init(&cinfo->suspend_lock);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500711 init_completion(&cinfo->completion);
Guoqing Jiangeece0752015-07-10 17:01:21 +0800712 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500713
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500714 mddev->cluster_info = cinfo;
715
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500716 memset(str, 0, 64);
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800717 sprintf(str, "%pU", mddev->uuid);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500718 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
719 DLM_LSFL_FS, LVB_SIZE,
720 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500721 if (ret)
722 goto err;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500723 wait_for_completion(&cinfo->completion);
Guoqing Jiang8c58f022015-04-21 11:25:52 -0500724 if (nodes < cinfo->slot_number) {
725 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
726 cinfo->slot_number, nodes);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500727 ret = -ERANGE;
728 goto err;
729 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500730 /* Initiate the communication resources */
731 ret = -ENOMEM;
732 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
733 if (!cinfo->recv_thread) {
734 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
735 goto err;
736 }
737 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
738 if (!cinfo->message_lockres)
739 goto err;
740 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
741 if (!cinfo->token_lockres)
742 goto err;
743 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
744 if (!cinfo->ack_lockres)
745 goto err;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500746 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
747 if (!cinfo->no_new_dev_lockres)
748 goto err;
749
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500750 /* get sync CR lock on ACK. */
751 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
752 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
753 ret);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500754 /* get sync CR lock on no-new-dev. */
755 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
756 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
757
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500758
759 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
760 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
761 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
762 if (!cinfo->bitmap_lockres)
763 goto err;
764 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
765 pr_err("Failed to get bitmap lock\n");
766 ret = -EINVAL;
767 goto err;
768 }
769
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500770 cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
771 if (!cinfo->resync_lockres)
772 goto err;
773
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500774 ret = gather_all_resync_info(mddev, nodes);
775 if (ret)
776 goto err;
777
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500778 return 0;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500779err:
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500780 lockres_free(cinfo->message_lockres);
781 lockres_free(cinfo->token_lockres);
782 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500783 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500784 lockres_free(cinfo->resync_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500785 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500786 if (cinfo->lockspace)
787 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500788 mddev->cluster_info = NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500789 kfree(cinfo);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500790 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500791}
792
Guoqing Jiang09995412015-10-01 00:09:18 +0800793static void resync_bitmap(struct mddev *mddev)
794{
795 struct md_cluster_info *cinfo = mddev->cluster_info;
796 struct cluster_msg cmsg = {0};
797 int err;
798
799 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
800 err = sendmsg(cinfo, &cmsg);
801 if (err)
802 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
803 __func__, __LINE__, err);
804}
805
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500806static int leave(struct mddev *mddev)
807{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500808 struct md_cluster_info *cinfo = mddev->cluster_info;
809
810 if (!cinfo)
811 return 0;
Guoqing Jiang09995412015-10-01 00:09:18 +0800812
813 /* BITMAP_NEEDS_SYNC message should be sent when node
814 * is leaving the cluster with dirty bitmap, also we
815 * can only deliver it when dlm connection is available */
816 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
817 resync_bitmap(mddev);
818
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500819 md_unregister_thread(&cinfo->recovery_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500820 md_unregister_thread(&cinfo->recv_thread);
821 lockres_free(cinfo->message_lockres);
822 lockres_free(cinfo->token_lockres);
823 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500824 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500825 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500826 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500827 return 0;
828}
829
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500830/* slot_number(): Returns the MD slot number to use
831 * DLM starts the slot numbers from 1, wheras cluster-md
832 * wants the number to be from zero, so we deduct one
833 */
834static int slot_number(struct mddev *mddev)
835{
836 struct md_cluster_info *cinfo = mddev->cluster_info;
837
838 return cinfo->slot_number - 1;
839}
840
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500841static int metadata_update_start(struct mddev *mddev)
842{
843 return lock_comm(mddev->cluster_info);
844}
845
846static int metadata_update_finish(struct mddev *mddev)
847{
848 struct md_cluster_info *cinfo = mddev->cluster_info;
849 struct cluster_msg cmsg;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500850 struct md_rdev *rdev;
851 int ret = 0;
NeilBrownba2746b2015-10-16 13:48:35 +1100852 int raid_slot = -1;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500853
854 memset(&cmsg, 0, sizeof(cmsg));
855 cmsg.type = cpu_to_le32(METADATA_UPDATED);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500856 /* Pick up a good active device number to send.
857 */
858 rdev_for_each(rdev, mddev)
859 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
NeilBrownba2746b2015-10-16 13:48:35 +1100860 raid_slot = rdev->desc_nr;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500861 break;
862 }
NeilBrownba2746b2015-10-16 13:48:35 +1100863 if (raid_slot >= 0) {
864 cmsg.raid_slot = cpu_to_le32(raid_slot);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500865 ret = __sendmsg(cinfo, &cmsg);
NeilBrownba2746b2015-10-16 13:48:35 +1100866 } else
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500867 pr_warn("md-cluster: No good device id found to send\n");
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500868 unlock_comm(cinfo);
869 return ret;
870}
871
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500872static void metadata_update_cancel(struct mddev *mddev)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500873{
874 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500875 unlock_comm(cinfo);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500876}
877
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500878static int resync_start(struct mddev *mddev)
879{
880 struct md_cluster_info *cinfo = mddev->cluster_info;
881 cinfo->resync_lockres->flags |= DLM_LKF_NOQUEUE;
882 return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX);
883}
884
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000885static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500886{
887 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesac277c62015-12-21 10:50:59 +1100888 struct resync_info ri;
Guoqing Jiangaee177a2015-10-12 17:21:24 +0800889 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500890
Goldwyn Rodriguesac277c62015-12-21 10:50:59 +1100891 /* do not send zero again, if we have sent before */
892 if (hi == 0) {
893 memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
894 if (le64_to_cpu(ri.hi) == 0)
895 return 0;
896 }
897
NeilBrown30661b42015-10-19 15:44:00 +1100898 add_resync_info(cinfo->bitmap_lockres, lo, hi);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000899 /* Re-acquire the lock to refresh LVB */
900 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000901 cmsg.type = cpu_to_le32(RESYNCING);
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500902 cmsg.low = cpu_to_le64(lo);
903 cmsg.high = cpu_to_le64(hi);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500904
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500905 return sendmsg(cinfo, &cmsg);
906}
907
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500908static int resync_finish(struct mddev *mddev)
909{
910 struct md_cluster_info *cinfo = mddev->cluster_info;
911 cinfo->resync_lockres->flags &= ~DLM_LKF_NOQUEUE;
912 dlm_unlock_sync(cinfo->resync_lockres);
913 return resync_info_update(mddev, 0, 0);
914}
915
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500916static int area_resyncing(struct mddev *mddev, int direction,
917 sector_t lo, sector_t hi)
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500918{
919 struct md_cluster_info *cinfo = mddev->cluster_info;
920 int ret = 0;
921 struct suspend_info *s;
922
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500923 if ((direction == READ) &&
924 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
925 return 1;
926
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500927 spin_lock_irq(&cinfo->suspend_lock);
928 if (list_empty(&cinfo->suspend_list))
929 goto out;
930 list_for_each_entry(s, &cinfo->suspend_list, list)
931 if (hi > s->lo && lo < s->hi) {
932 ret = 1;
933 break;
934 }
935out:
936 spin_unlock_irq(&cinfo->suspend_lock);
937 return ret;
938}
939
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500940/* add_new_disk() - initiates a disk add
941 * However, if this fails before writing md_update_sb(),
942 * add_new_disk_cancel() must be called to release token lock
943 */
944static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500945{
946 struct md_cluster_info *cinfo = mddev->cluster_info;
947 struct cluster_msg cmsg;
948 int ret = 0;
949 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
950 char *uuid = sb->device_uuid;
951
952 memset(&cmsg, 0, sizeof(cmsg));
953 cmsg.type = cpu_to_le32(NEWDISK);
954 memcpy(cmsg.uuid, uuid, 16);
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800955 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500956 lock_comm(cinfo);
957 ret = __sendmsg(cinfo, &cmsg);
958 if (ret)
959 return ret;
960 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
961 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
962 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
963 /* Some node does not "see" the device */
964 if (ret == -EAGAIN)
965 ret = -ENOENT;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500966 if (ret)
967 unlock_comm(cinfo);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500968 else
969 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
970 return ret;
971}
972
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500973static void add_new_disk_cancel(struct mddev *mddev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500974{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500975 struct md_cluster_info *cinfo = mddev->cluster_info;
976 unlock_comm(cinfo);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500977}
978
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600979static int new_disk_ack(struct mddev *mddev, bool ack)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500980{
981 struct md_cluster_info *cinfo = mddev->cluster_info;
982
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600983 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
984 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
985 return -EINVAL;
986 }
987
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500988 if (ack)
989 dlm_unlock_sync(cinfo->no_new_dev_lockres);
990 complete(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600991 return 0;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500992}
993
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500994static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
995{
Guoqing Jiangaee177a2015-10-12 17:21:24 +0800996 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500997 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800998 cmsg.type = cpu_to_le32(REMOVE);
999 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11001000 return sendmsg(cinfo, &cmsg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001001}
1002
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001003static int gather_bitmaps(struct md_rdev *rdev)
1004{
1005 int sn, err;
1006 sector_t lo, hi;
Guoqing Jiangaee177a2015-10-12 17:21:24 +08001007 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001008 struct mddev *mddev = rdev->mddev;
1009 struct md_cluster_info *cinfo = mddev->cluster_info;
1010
Guoqing Jiangfaeff832015-10-12 17:21:21 +08001011 cmsg.type = cpu_to_le32(RE_ADD);
1012 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001013 err = sendmsg(cinfo, &cmsg);
1014 if (err)
1015 goto out;
1016
1017 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
1018 if (sn == (cinfo->slot_number - 1))
1019 continue;
1020 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
1021 if (err) {
1022 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
1023 goto out;
1024 }
1025 if ((hi > 0) && (lo < mddev->recovery_cp))
1026 mddev->recovery_cp = lo;
1027 }
1028out:
1029 return err;
1030}
1031
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001032static struct md_cluster_operations cluster_ops = {
1033 .join = join,
1034 .leave = leave,
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05001035 .slot_number = slot_number,
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001036 .resync_start = resync_start,
1037 .resync_finish = resync_finish,
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05001038 .resync_info_update = resync_info_update,
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05001039 .metadata_update_start = metadata_update_start,
1040 .metadata_update_finish = metadata_update_finish,
1041 .metadata_update_cancel = metadata_update_cancel,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001042 .area_resyncing = area_resyncing,
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001043 .add_new_disk = add_new_disk,
1044 .add_new_disk_cancel = add_new_disk_cancel,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001045 .new_disk_ack = new_disk_ack,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001046 .remove_disk = remove_disk,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001047 .gather_bitmaps = gather_bitmaps,
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001048};
1049
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001050static int __init cluster_init(void)
1051{
1052 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1053 pr_info("Registering Cluster MD functions\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001054 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001055 return 0;
1056}
1057
1058static void cluster_exit(void)
1059{
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001060 unregister_md_cluster_operations();
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001061}
1062
1063module_init(cluster_init);
1064module_exit(cluster_exit);
Guoqing Jiang86b57272015-10-12 17:21:25 +08001065MODULE_AUTHOR("SUSE");
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001066MODULE_LICENSE("GPL");
1067MODULE_DESCRIPTION("Clustering support for MD");