blob: c94715159b9bb3ef42a90d6b298d642d958c9f47 [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() */
Guoqing Jiangfccb60a2016-08-12 13:42:41 +080028 wait_queue_head_t sync_locking; /* wait queue for synchronized locking */
29 bool sync_locking_done;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050030 void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
31 struct mddev *mddev; /* pointing back to mddev. */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -050032 int mode;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050033};
34
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050035struct suspend_info {
36 int slot;
37 sector_t lo;
38 sector_t hi;
39 struct list_head list;
40};
41
42struct resync_info {
43 __le64 lo;
44 __le64 hi;
45};
46
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060047/* md_cluster_info flags */
48#define MD_CLUSTER_WAITING_FOR_NEWDISK 1
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -050049#define MD_CLUSTER_SUSPEND_READ_BALANCING 2
Guoqing Jiangeece0752015-07-10 17:01:21 +080050#define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060051
Guoqing Jiang8b9277c2015-12-21 10:51:00 +110052/* Lock the send communication. This is done through
53 * bit manipulation as opposed to a mutex in order to
54 * accomodate lock and hold. See next comment.
55 */
56#define MD_CLUSTER_SEND_LOCK 4
Guoqing Jiange19508f2015-12-21 10:51:01 +110057/* If cluster operations (such as adding a disk) must lock the
58 * communication channel, so as to perform extra operations
59 * (update metadata) and no other operation is allowed on the
60 * MD. Token needs to be locked and held until the operation
61 * completes witha md_update_sb(), which would eventually release
62 * the lock.
Guoqing Jiang8b9277c2015-12-21 10:51:00 +110063 */
64#define MD_CLUSTER_SEND_LOCKED_ALREADY 5
Guoqing Jiang51e453a2016-05-04 02:17:09 -040065/* We should receive message after node joined cluster and
66 * set up all the related infos such as bitmap and personality */
67#define MD_CLUSTER_ALREADY_IN_CLUSTER 6
68#define MD_CLUSTER_PENDING_RECV_EVENT 7
Guoqing Jiang8b9277c2015-12-21 10:51:00 +110069
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060070
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050071struct md_cluster_info {
72 /* dlm lock space and resources for clustered raid. */
73 dlm_lockspace_t *lockspace;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -050074 int slot_number;
75 struct completion completion;
Guoqing Jiang8b9277c2015-12-21 10:51:00 +110076 struct mutex recv_mutex;
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -050077 struct dlm_lock_resource *bitmap_lockres;
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +110078 struct dlm_lock_resource **other_bitmap_lockres;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -050079 struct dlm_lock_resource *resync_lockres;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050080 struct list_head suspend_list;
81 spinlock_t suspend_lock;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050082 struct md_thread *recovery_thread;
83 unsigned long recovery_map;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050084 /* communication loc resources */
85 struct dlm_lock_resource *ack_lockres;
86 struct dlm_lock_resource *message_lockres;
87 struct dlm_lock_resource *token_lockres;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050088 struct dlm_lock_resource *no_new_dev_lockres;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050089 struct md_thread *recv_thread;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050090 struct completion newdisk_completion;
Guoqing Jiang8b9277c2015-12-21 10:51:00 +110091 wait_queue_head_t wait;
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060092 unsigned long state;
Guoqing Jiang18c9ff72016-05-02 11:50:12 -040093 /* record the region in RESYNCING message */
94 sector_t sync_low;
95 sector_t sync_hi;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050096};
97
98enum msg_type {
99 METADATA_UPDATED = 0,
100 RESYNCING,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500101 NEWDISK,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500102 REMOVE,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500103 RE_ADD,
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800104 BITMAP_NEEDS_SYNC,
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500105};
106
107struct cluster_msg {
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800108 __le32 type;
109 __le32 slot;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500110 /* TODO: Unionize this for smaller footprint */
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800111 __le64 low;
112 __le64 high;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500113 char uuid[16];
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800114 __le32 raid_slot;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600115};
116
117static void sync_ast(void *arg)
118{
119 struct dlm_lock_resource *res;
120
NeilBrown2e2a7cd2015-10-19 15:42:18 +1100121 res = arg;
Guoqing Jiangfccb60a2016-08-12 13:42:41 +0800122 res->sync_locking_done = true;
123 wake_up(&res->sync_locking);
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600124}
125
126static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
127{
128 int ret = 0;
129
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600130 ret = dlm_lock(res->ls, mode, &res->lksb,
131 res->flags, res->name, strlen(res->name),
132 0, sync_ast, res, res->bast);
133 if (ret)
134 return ret;
Guoqing Jiangfccb60a2016-08-12 13:42:41 +0800135 wait_event(res->sync_locking, res->sync_locking_done);
136 res->sync_locking_done = false;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500137 if (res->lksb.sb_status == 0)
138 res->mode = mode;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600139 return res->lksb.sb_status;
140}
141
142static int dlm_unlock_sync(struct dlm_lock_resource *res)
143{
144 return dlm_lock_sync(res, DLM_LOCK_NL);
145}
146
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500147static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600148 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
149{
150 struct dlm_lock_resource *res = NULL;
151 int ret, namelen;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500152 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600153
154 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
155 if (!res)
156 return NULL;
Guoqing Jiangfccb60a2016-08-12 13:42:41 +0800157 init_waitqueue_head(&res->sync_locking);
158 res->sync_locking_done = false;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500159 res->ls = cinfo->lockspace;
160 res->mddev = mddev;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500161 res->mode = DLM_LOCK_IV;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600162 namelen = strlen(name);
163 res->name = kzalloc(namelen + 1, GFP_KERNEL);
164 if (!res->name) {
165 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
166 goto out_err;
167 }
168 strlcpy(res->name, name, namelen + 1);
169 if (with_lvb) {
170 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
171 if (!res->lksb.sb_lvbptr) {
172 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
173 goto out_err;
174 }
175 res->flags = DLM_LKF_VALBLK;
176 }
177
178 if (bastfn)
179 res->bast = bastfn;
180
181 res->flags |= DLM_LKF_EXPEDITE;
182
183 ret = dlm_lock_sync(res, DLM_LOCK_NL);
184 if (ret) {
185 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
186 goto out_err;
187 }
188 res->flags &= ~DLM_LKF_EXPEDITE;
189 res->flags |= DLM_LKF_CONVERT;
190
191 return res;
192out_err:
193 kfree(res->lksb.sb_lvbptr);
194 kfree(res->name);
195 kfree(res);
196 return NULL;
197}
198
199static void lockres_free(struct dlm_lock_resource *res)
200{
Guoqing Jiang400cb452016-08-12 13:42:35 +0800201 int ret = 0;
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800202
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600203 if (!res)
204 return;
205
Guoqing Jiang400cb452016-08-12 13:42:35 +0800206 /*
207 * use FORCEUNLOCK flag, so we can unlock even the lock is on the
208 * waiting or convert queue
209 */
210 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_FORCEUNLOCK,
211 &res->lksb, res);
212 if (unlikely(ret != 0))
213 pr_err("failed to unlock %s return %d\n", res->name, ret);
214 else
Guoqing Jiangfccb60a2016-08-12 13:42:41 +0800215 wait_event(res->sync_locking, res->sync_locking_done);
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600216
217 kfree(res->name);
218 kfree(res->lksb.sb_lvbptr);
219 kfree(res);
220}
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600221
NeilBrown30661b42015-10-19 15:44:00 +1100222static void add_resync_info(struct dlm_lock_resource *lockres,
223 sector_t lo, sector_t hi)
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500224{
225 struct resync_info *ri;
226
227 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
228 ri->lo = cpu_to_le64(lo);
229 ri->hi = cpu_to_le64(hi);
230}
231
232static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
233{
234 struct resync_info ri;
235 struct suspend_info *s = NULL;
236 sector_t hi = 0;
237
238 dlm_lock_sync(lockres, DLM_LOCK_CR);
239 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
240 hi = le64_to_cpu(ri.hi);
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800241 if (hi > 0) {
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500242 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
243 if (!s)
244 goto out;
245 s->hi = hi;
246 s->lo = le64_to_cpu(ri.lo);
247 }
248 dlm_unlock_sync(lockres);
249out:
250 return s;
251}
252
kbuild test robot6dc69c92015-02-28 07:04:37 +0800253static void recover_bitmaps(struct md_thread *thread)
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500254{
255 struct mddev *mddev = thread->mddev;
256 struct md_cluster_info *cinfo = mddev->cluster_info;
257 struct dlm_lock_resource *bm_lockres;
258 char str[64];
259 int slot, ret;
260 struct suspend_info *s, *tmp;
261 sector_t lo, hi;
262
263 while (cinfo->recovery_map) {
264 slot = fls64((u64)cinfo->recovery_map) - 1;
265
266 /* Clear suspend_area associated with the bitmap */
267 spin_lock_irq(&cinfo->suspend_lock);
268 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
269 if (slot == s->slot) {
270 list_del(&s->list);
271 kfree(s);
272 }
273 spin_unlock_irq(&cinfo->suspend_lock);
274
275 snprintf(str, 64, "bitmap%04d", slot);
276 bm_lockres = lockres_init(mddev, str, NULL, 1);
277 if (!bm_lockres) {
278 pr_err("md-cluster: Cannot initialize bitmaps\n");
279 goto clear_bit;
280 }
281
282 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
283 if (ret) {
284 pr_err("md-cluster: Could not DLM lock %s: %d\n",
285 str, ret);
286 goto clear_bit;
287 }
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500288 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500289 if (ret) {
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500290 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
Guoqing Jiange3f924d2016-08-12 13:42:36 +0800291 goto clear_bit;
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500292 }
293 if (hi > 0) {
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500294 if (lo < mddev->recovery_cp)
295 mddev->recovery_cp = lo;
Guoqing Jiangeb315cd2016-05-02 11:33:10 -0400296 /* wake up thread to continue resync in case resync
297 * is not finished */
298 if (mddev->recovery_cp != MaxSector) {
299 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
300 md_wakeup_thread(mddev->thread);
301 }
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500302 }
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500303clear_bit:
Shaohua Li4ac7a652016-01-22 15:54:42 -0800304 lockres_free(bm_lockres);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500305 clear_bit(slot, &cinfo->recovery_map);
306 }
307}
308
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500309static void recover_prep(void *arg)
310{
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500311 struct mddev *mddev = arg;
312 struct md_cluster_info *cinfo = mddev->cluster_info;
313 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500314}
315
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800316static void __recover_slot(struct mddev *mddev, int slot)
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500317{
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500318 struct md_cluster_info *cinfo = mddev->cluster_info;
319
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800320 set_bit(slot, &cinfo->recovery_map);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500321 if (!cinfo->recovery_thread) {
322 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
323 mddev, "recover");
324 if (!cinfo->recovery_thread) {
325 pr_warn("md-cluster: Could not create recovery thread\n");
326 return;
327 }
328 }
329 md_wakeup_thread(cinfo->recovery_thread);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500330}
331
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800332static void recover_slot(void *arg, struct dlm_slot *slot)
333{
334 struct mddev *mddev = arg;
335 struct md_cluster_info *cinfo = mddev->cluster_info;
336
337 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
338 mddev->bitmap_info.cluster_name,
339 slot->nodeid, slot->slot,
340 cinfo->slot_number);
341 /* deduct one since dlm slot starts from one while the num of
342 * cluster-md begins with 0 */
343 __recover_slot(mddev, slot->slot - 1);
344}
345
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500346static void recover_done(void *arg, struct dlm_slot *slots,
347 int num_slots, int our_slot,
348 uint32_t generation)
349{
350 struct mddev *mddev = arg;
351 struct md_cluster_info *cinfo = mddev->cluster_info;
352
353 cinfo->slot_number = our_slot;
Guoqing Jiangeece0752015-07-10 17:01:21 +0800354 /* completion is only need to be complete when node join cluster,
355 * it doesn't need to run during another node's failure */
356 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
357 complete(&cinfo->completion);
358 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
359 }
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500360 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500361}
362
Guoqing Jiangeece0752015-07-10 17:01:21 +0800363/* the ops is called when node join the cluster, and do lock recovery
364 * if node failure occurs */
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500365static const struct dlm_lockspace_ops md_ls_ops = {
366 .recover_prep = recover_prep,
367 .recover_slot = recover_slot,
368 .recover_done = recover_done,
369};
370
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500371/*
372 * The BAST function for the ack lock resource
373 * This function wakes up the receive thread in
374 * order to receive and process the message.
375 */
376static void ack_bast(void *arg, int mode)
377{
NeilBrown2e2a7cd2015-10-19 15:42:18 +1100378 struct dlm_lock_resource *res = arg;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500379 struct md_cluster_info *cinfo = res->mddev->cluster_info;
380
Guoqing Jiang51e453a2016-05-04 02:17:09 -0400381 if (mode == DLM_LOCK_EX) {
382 if (test_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state))
383 md_wakeup_thread(cinfo->recv_thread);
384 else
385 set_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state);
386 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500387}
388
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500389static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
390{
391 struct suspend_info *s, *tmp;
392
393 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
394 if (slot == s->slot) {
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500395 list_del(&s->list);
396 kfree(s);
397 break;
398 }
399}
400
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500401static void remove_suspend_info(struct mddev *mddev, int slot)
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500402{
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500403 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500404 spin_lock_irq(&cinfo->suspend_lock);
405 __remove_suspend_info(cinfo, slot);
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
410
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500411static void process_suspend_info(struct mddev *mddev,
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500412 int slot, sector_t lo, sector_t hi)
413{
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500414 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500415 struct suspend_info *s;
416
417 if (!hi) {
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500418 remove_suspend_info(mddev, slot);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500419 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
420 md_wakeup_thread(mddev->thread);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500421 return;
422 }
Guoqing Jiang18c9ff72016-05-02 11:50:12 -0400423
424 /*
425 * The bitmaps are not same for different nodes
426 * if RESYNCING is happening in one node, then
427 * the node which received the RESYNCING message
428 * probably will perform resync with the region
429 * [lo, hi] again, so we could reduce resync time
430 * a lot if we can ensure that the bitmaps among
431 * different nodes are match up well.
432 *
433 * sync_low/hi is used to record the region which
434 * arrived in the previous RESYNCING message,
435 *
436 * Call bitmap_sync_with_cluster to clear
437 * NEEDED_MASK and set RESYNC_MASK since
438 * resync thread is running in another node,
439 * so we don't need to do the resync again
440 * with the same section */
441 bitmap_sync_with_cluster(mddev, cinfo->sync_low,
442 cinfo->sync_hi,
443 lo, hi);
444 cinfo->sync_low = lo;
445 cinfo->sync_hi = hi;
446
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500447 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
448 if (!s)
449 return;
450 s->slot = slot;
451 s->lo = lo;
452 s->hi = hi;
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500453 mddev->pers->quiesce(mddev, 1);
454 mddev->pers->quiesce(mddev, 0);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500455 spin_lock_irq(&cinfo->suspend_lock);
456 /* Remove existing entry (if exists) before adding */
457 __remove_suspend_info(cinfo, slot);
458 list_add(&s->list, &cinfo->suspend_list);
459 spin_unlock_irq(&cinfo->suspend_lock);
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500460 mddev->pers->quiesce(mddev, 2);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500461}
462
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500463static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
464{
465 char disk_uuid[64];
466 struct md_cluster_info *cinfo = mddev->cluster_info;
467 char event_name[] = "EVENT=ADD_DEVICE";
468 char raid_slot[16];
469 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
470 int len;
471
472 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800473 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800474 snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500475 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
476 init_completion(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600477 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500478 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
479 wait_for_completion_timeout(&cinfo->newdisk_completion,
480 NEW_DEV_TIMEOUT);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600481 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500482}
483
484
485static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
486{
487 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiang15858fa2015-12-21 10:51:00 +1100488 mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
489 set_bit(MD_RELOAD_SB, &mddev->flags);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500490 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
Guoqing Jiang15858fa2015-12-21 10:51:00 +1100491 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500492}
493
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500494static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
495{
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800496 struct md_rdev *rdev;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500497
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800498 rcu_read_lock();
499 rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
Guoqing Jiang659b2542015-12-21 10:50:59 +1100500 if (rdev) {
501 set_bit(ClusterRemove, &rdev->flags);
502 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
503 md_wakeup_thread(mddev->thread);
504 }
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500505 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800506 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
507 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800508 rcu_read_unlock();
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500509}
510
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500511static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
512{
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800513 struct md_rdev *rdev;
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500514
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800515 rcu_read_lock();
516 rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500517 if (rdev && test_bit(Faulty, &rdev->flags))
518 clear_bit(Faulty, &rdev->flags);
519 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800520 pr_warn("%s: %d Could not find disk(%d) which is faulty",
521 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Guoqing Jiang5f0aa212016-08-12 13:42:39 +0800522 rcu_read_unlock();
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500523}
524
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400525static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500526{
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400527 int ret = 0;
528
Guoqing Jiang256f5b22015-10-12 17:21:23 +0800529 if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
530 "node %d received it's own msg\n", le32_to_cpu(msg->slot)))
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400531 return -1;
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800532 switch (le32_to_cpu(msg->type)) {
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500533 case METADATA_UPDATED:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500534 process_metadata_update(mddev, msg);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500535 break;
536 case RESYNCING:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800537 process_suspend_info(mddev, le32_to_cpu(msg->slot),
538 le64_to_cpu(msg->low),
539 le64_to_cpu(msg->high));
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500540 break;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500541 case NEWDISK:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500542 process_add_new_disk(mddev, msg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500543 break;
544 case REMOVE:
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500545 process_remove_disk(mddev, msg);
546 break;
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500547 case RE_ADD:
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500548 process_readd_disk(mddev, msg);
549 break;
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800550 case BITMAP_NEEDS_SYNC:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800551 __recover_slot(mddev, le32_to_cpu(msg->slot));
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800552 break;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500553 default:
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400554 ret = -1;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500555 pr_warn("%s:%d Received unknown message from %d\n",
556 __func__, __LINE__, msg->slot);
kbuild test robot09dd1af2015-02-28 09:16:08 +0800557 }
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400558 return ret;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500559}
560
561/*
562 * thread for receiving message
563 */
564static void recv_daemon(struct md_thread *thread)
565{
566 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
567 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
568 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
569 struct cluster_msg msg;
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800570 int ret;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500571
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100572 mutex_lock(&cinfo->recv_mutex);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500573 /*get CR on Message*/
574 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
575 pr_err("md/raid1:failed to get CR on MESSAGE\n");
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100576 mutex_unlock(&cinfo->recv_mutex);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500577 return;
578 }
579
580 /* read lvb and wake up thread to process this message_lockres */
581 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400582 ret = process_recvd_msg(thread->mddev, &msg);
583 if (ret)
584 goto out;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500585
586 /*release CR on ack_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800587 ret = dlm_unlock_sync(ack_lockres);
588 if (unlikely(ret != 0))
589 pr_info("unlock ack failed return %d\n", ret);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800590 /*up-convert to PR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800591 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
592 if (unlikely(ret != 0))
593 pr_info("lock PR on msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500594 /*get CR on ack_lockres again*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800595 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
596 if (unlikely(ret != 0))
597 pr_info("lock CR on ack failed return %d\n", ret);
Guoqing Jiang1fa9a1a2016-05-03 22:22:15 -0400598out:
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500599 /*release CR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800600 ret = dlm_unlock_sync(message_lockres);
601 if (unlikely(ret != 0))
602 pr_info("unlock msg failed return %d\n", ret);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100603 mutex_unlock(&cinfo->recv_mutex);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500604}
605
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100606/* lock_token()
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500607 * Takes the lock on the TOKEN lock resource so no other
608 * node can communicate while the operation is underway.
609 */
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100610static int lock_token(struct md_cluster_info *cinfo)
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500611{
612 int error;
613
614 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
615 if (error)
616 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
617 __func__, __LINE__, error);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100618
619 /* Lock the receive sequence */
620 mutex_lock(&cinfo->recv_mutex);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500621 return error;
622}
623
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100624/* lock_comm()
625 * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
626 */
627static int lock_comm(struct md_cluster_info *cinfo)
628{
629 wait_event(cinfo->wait,
630 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
631
632 return lock_token(cinfo);
633}
634
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500635static void unlock_comm(struct md_cluster_info *cinfo)
636{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500637 WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100638 mutex_unlock(&cinfo->recv_mutex);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500639 dlm_unlock_sync(cinfo->token_lockres);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100640 clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
641 wake_up(&cinfo->wait);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500642}
643
644/* __sendmsg()
645 * This function performs the actual sending of the message. This function is
646 * usually called after performing the encompassing operation
647 * The function:
648 * 1. Grabs the message lockresource in EX mode
649 * 2. Copies the message to the message LVB
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800650 * 3. Downconverts message lockresource to CW
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500651 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
652 * and the other nodes read the message. The thread will wait here until all other
653 * nodes have released ack lock resource.
654 * 5. Downconvert ack lockresource to CR
655 */
656static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
657{
658 int error;
659 int slot = cinfo->slot_number - 1;
660
661 cmsg->slot = cpu_to_le32(slot);
662 /*get EX on Message*/
663 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
664 if (error) {
665 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
666 goto failed_message;
667 }
668
669 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
670 sizeof(struct cluster_msg));
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800671 /*down-convert EX to CW on Message*/
672 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500673 if (error) {
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800674 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500675 error);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800676 goto failed_ack;
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500677 }
678
679 /*up-convert CR to EX on Ack*/
680 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
681 if (error) {
682 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
683 error);
684 goto failed_ack;
685 }
686
687 /*down-convert EX to CR on Ack*/
688 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
689 if (error) {
690 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
691 error);
692 goto failed_ack;
693 }
694
695failed_ack:
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800696 error = dlm_unlock_sync(cinfo->message_lockres);
697 if (unlikely(error != 0)) {
698 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
699 error);
700 /* in case the message can't be released due to some reason */
701 goto failed_ack;
702 }
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500703failed_message:
704 return error;
705}
706
707static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
708{
709 int ret;
710
711 lock_comm(cinfo);
712 ret = __sendmsg(cinfo, cmsg);
713 unlock_comm(cinfo);
714 return ret;
715}
716
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500717static int gather_all_resync_info(struct mddev *mddev, int total_slots)
718{
719 struct md_cluster_info *cinfo = mddev->cluster_info;
720 int i, ret = 0;
721 struct dlm_lock_resource *bm_lockres;
722 struct suspend_info *s;
723 char str[64];
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800724 sector_t lo, hi;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500725
726
727 for (i = 0; i < total_slots; i++) {
728 memset(str, '\0', 64);
729 snprintf(str, 64, "bitmap%04d", i);
730 bm_lockres = lockres_init(mddev, str, NULL, 1);
731 if (!bm_lockres)
732 return -ENOMEM;
Shaohua Li4ac7a652016-01-22 15:54:42 -0800733 if (i == (cinfo->slot_number - 1)) {
734 lockres_free(bm_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500735 continue;
Shaohua Li4ac7a652016-01-22 15:54:42 -0800736 }
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500737
738 bm_lockres->flags |= DLM_LKF_NOQUEUE;
739 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
740 if (ret == -EAGAIN) {
741 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
742 s = read_resync_info(mddev, bm_lockres);
743 if (s) {
744 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
745 __func__, __LINE__,
746 (unsigned long long) s->lo,
747 (unsigned long long) s->hi, i);
748 spin_lock_irq(&cinfo->suspend_lock);
749 s->slot = i;
750 list_add(&s->list, &cinfo->suspend_list);
751 spin_unlock_irq(&cinfo->suspend_lock);
752 }
753 ret = 0;
754 lockres_free(bm_lockres);
755 continue;
756 }
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800757 if (ret) {
758 lockres_free(bm_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500759 goto out;
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800760 }
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800761
762 /* Read the disk bitmap sb and check if it needs recovery */
763 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
764 if (ret) {
765 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
766 lockres_free(bm_lockres);
767 continue;
768 }
769 if ((hi > 0) && (lo < mddev->recovery_cp)) {
770 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
771 mddev->recovery_cp = lo;
772 md_check_recovery(mddev);
773 }
774
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500775 lockres_free(bm_lockres);
776 }
777out:
778 return ret;
779}
780
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500781static int join(struct mddev *mddev, int nodes)
782{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500783 struct md_cluster_info *cinfo;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500784 int ret, ops_rv;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500785 char str[64];
786
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500787 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
788 if (!cinfo)
789 return -ENOMEM;
790
Guoqing Jiang9e3072e2015-07-10 17:01:18 +0800791 INIT_LIST_HEAD(&cinfo->suspend_list);
792 spin_lock_init(&cinfo->suspend_lock);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500793 init_completion(&cinfo->completion);
Guoqing Jiangeece0752015-07-10 17:01:21 +0800794 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100795 init_waitqueue_head(&cinfo->wait);
796 mutex_init(&cinfo->recv_mutex);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500797
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500798 mddev->cluster_info = cinfo;
799
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500800 memset(str, 0, 64);
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800801 sprintf(str, "%pU", mddev->uuid);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500802 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
803 DLM_LSFL_FS, LVB_SIZE,
804 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500805 if (ret)
806 goto err;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500807 wait_for_completion(&cinfo->completion);
Guoqing Jiang8c58f022015-04-21 11:25:52 -0500808 if (nodes < cinfo->slot_number) {
809 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
810 cinfo->slot_number, nodes);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500811 ret = -ERANGE;
812 goto err;
813 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500814 /* Initiate the communication resources */
815 ret = -ENOMEM;
816 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
817 if (!cinfo->recv_thread) {
818 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
819 goto err;
820 }
821 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
822 if (!cinfo->message_lockres)
823 goto err;
824 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
825 if (!cinfo->token_lockres)
826 goto err;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500827 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
828 if (!cinfo->no_new_dev_lockres)
829 goto err;
830
Guoqing Jiang15352122016-05-02 11:33:12 -0400831 ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
832 if (ret) {
833 ret = -EAGAIN;
834 pr_err("md-cluster: can't join cluster to avoid lock issue\n");
835 goto err;
836 }
837 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000838 if (!cinfo->ack_lockres) {
839 ret = -ENOMEM;
Guoqing Jiang15352122016-05-02 11:33:12 -0400840 goto err;
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000841 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500842 /* get sync CR lock on ACK. */
843 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
844 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
845 ret);
Guoqing Jiang15352122016-05-02 11:33:12 -0400846 dlm_unlock_sync(cinfo->token_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500847 /* get sync CR lock on no-new-dev. */
848 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
849 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
850
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500851
852 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
853 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
854 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000855 if (!cinfo->bitmap_lockres) {
856 ret = -ENOMEM;
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500857 goto err;
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000858 }
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500859 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
860 pr_err("Failed to get bitmap lock\n");
861 ret = -EINVAL;
862 goto err;
863 }
864
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500865 cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000866 if (!cinfo->resync_lockres) {
867 ret = -ENOMEM;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500868 goto err;
Wei Yongjun0f6187d2016-08-21 14:42:25 +0000869 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500870
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500871 return 0;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500872err:
Guoqing Jiang5b0fb332016-05-02 11:33:11 -0400873 md_unregister_thread(&cinfo->recovery_thread);
874 md_unregister_thread(&cinfo->recv_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500875 lockres_free(cinfo->message_lockres);
876 lockres_free(cinfo->token_lockres);
877 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500878 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500879 lockres_free(cinfo->resync_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500880 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500881 if (cinfo->lockspace)
882 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500883 mddev->cluster_info = NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500884 kfree(cinfo);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500885 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500886}
887
Guoqing Jiang51e453a2016-05-04 02:17:09 -0400888static void load_bitmaps(struct mddev *mddev, int total_slots)
889{
890 struct md_cluster_info *cinfo = mddev->cluster_info;
891
892 /* load all the node's bitmap info for resync */
893 if (gather_all_resync_info(mddev, total_slots))
894 pr_err("md-cluster: failed to gather all resyn infos\n");
895 set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
896 /* wake up recv thread in case something need to be handled */
897 if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
898 md_wakeup_thread(cinfo->recv_thread);
899}
900
Guoqing Jiang09995412015-10-01 00:09:18 +0800901static void resync_bitmap(struct mddev *mddev)
902{
903 struct md_cluster_info *cinfo = mddev->cluster_info;
904 struct cluster_msg cmsg = {0};
905 int err;
906
907 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
908 err = sendmsg(cinfo, &cmsg);
909 if (err)
910 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
911 __func__, __LINE__, err);
912}
913
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +1100914static void unlock_all_bitmaps(struct mddev *mddev);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500915static int leave(struct mddev *mddev)
916{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500917 struct md_cluster_info *cinfo = mddev->cluster_info;
918
919 if (!cinfo)
920 return 0;
Guoqing Jiang09995412015-10-01 00:09:18 +0800921
922 /* BITMAP_NEEDS_SYNC message should be sent when node
923 * is leaving the cluster with dirty bitmap, also we
924 * can only deliver it when dlm connection is available */
925 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
926 resync_bitmap(mddev);
927
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500928 md_unregister_thread(&cinfo->recovery_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500929 md_unregister_thread(&cinfo->recv_thread);
930 lockres_free(cinfo->message_lockres);
931 lockres_free(cinfo->token_lockres);
932 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500933 lockres_free(cinfo->no_new_dev_lockres);
Shaohua Li4ac7a652016-01-22 15:54:42 -0800934 lockres_free(cinfo->resync_lockres);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500935 lockres_free(cinfo->bitmap_lockres);
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +1100936 unlock_all_bitmaps(mddev);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500937 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500938 return 0;
939}
940
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500941/* slot_number(): Returns the MD slot number to use
942 * DLM starts the slot numbers from 1, wheras cluster-md
943 * wants the number to be from zero, so we deduct one
944 */
945static int slot_number(struct mddev *mddev)
946{
947 struct md_cluster_info *cinfo = mddev->cluster_info;
948
949 return cinfo->slot_number - 1;
950}
951
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100952/*
953 * Check if the communication is already locked, else lock the communication
954 * channel.
955 * If it is already locked, token is in EX mode, and hence lock_token()
956 * should not be called.
957 */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500958static int metadata_update_start(struct mddev *mddev)
959{
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100960 struct md_cluster_info *cinfo = mddev->cluster_info;
961
962 wait_event(cinfo->wait,
963 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
964 test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
965
966 /* If token is already locked, return 0 */
967 if (cinfo->token_lockres->mode == DLM_LOCK_EX)
968 return 0;
969
970 return lock_token(cinfo);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500971}
972
973static int metadata_update_finish(struct mddev *mddev)
974{
975 struct md_cluster_info *cinfo = mddev->cluster_info;
976 struct cluster_msg cmsg;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500977 struct md_rdev *rdev;
978 int ret = 0;
NeilBrownba2746b2015-10-16 13:48:35 +1100979 int raid_slot = -1;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500980
981 memset(&cmsg, 0, sizeof(cmsg));
982 cmsg.type = cpu_to_le32(METADATA_UPDATED);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500983 /* Pick up a good active device number to send.
984 */
985 rdev_for_each(rdev, mddev)
986 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
NeilBrownba2746b2015-10-16 13:48:35 +1100987 raid_slot = rdev->desc_nr;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500988 break;
989 }
NeilBrownba2746b2015-10-16 13:48:35 +1100990 if (raid_slot >= 0) {
991 cmsg.raid_slot = cpu_to_le32(raid_slot);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500992 ret = __sendmsg(cinfo, &cmsg);
NeilBrownba2746b2015-10-16 13:48:35 +1100993 } else
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500994 pr_warn("md-cluster: No good device id found to send\n");
Guoqing Jiang8b9277c2015-12-21 10:51:00 +1100995 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500996 unlock_comm(cinfo);
997 return ret;
998}
999
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001000static void metadata_update_cancel(struct mddev *mddev)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05001001{
1002 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiang8b9277c2015-12-21 10:51:00 +11001003 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001004 unlock_comm(cinfo);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05001005}
1006
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001007static int resync_start(struct mddev *mddev)
1008{
1009 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001010 return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX);
1011}
1012
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10001013static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05001014{
1015 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesac277c62015-12-21 10:50:59 +11001016 struct resync_info ri;
Guoqing Jiangaee177a2015-10-12 17:21:24 +08001017 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05001018
Goldwyn Rodriguesac277c62015-12-21 10:50:59 +11001019 /* do not send zero again, if we have sent before */
1020 if (hi == 0) {
1021 memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
1022 if (le64_to_cpu(ri.hi) == 0)
1023 return 0;
1024 }
1025
NeilBrown30661b42015-10-19 15:44:00 +11001026 add_resync_info(cinfo->bitmap_lockres, lo, hi);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10001027 /* Re-acquire the lock to refresh LVB */
1028 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10001029 cmsg.type = cpu_to_le32(RESYNCING);
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05001030 cmsg.low = cpu_to_le64(lo);
1031 cmsg.high = cpu_to_le64(hi);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001032
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05001033 return sendmsg(cinfo, &cmsg);
1034}
1035
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001036static int resync_finish(struct mddev *mddev)
1037{
1038 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001039 dlm_unlock_sync(cinfo->resync_lockres);
1040 return resync_info_update(mddev, 0, 0);
1041}
1042
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001043static int area_resyncing(struct mddev *mddev, int direction,
1044 sector_t lo, sector_t hi)
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001045{
1046 struct md_cluster_info *cinfo = mddev->cluster_info;
1047 int ret = 0;
1048 struct suspend_info *s;
1049
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001050 if ((direction == READ) &&
1051 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
1052 return 1;
1053
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001054 spin_lock_irq(&cinfo->suspend_lock);
1055 if (list_empty(&cinfo->suspend_list))
1056 goto out;
1057 list_for_each_entry(s, &cinfo->suspend_list, list)
1058 if (hi > s->lo && lo < s->hi) {
1059 ret = 1;
1060 break;
1061 }
1062out:
1063 spin_unlock_irq(&cinfo->suspend_lock);
1064 return ret;
1065}
1066
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001067/* add_new_disk() - initiates a disk add
1068 * However, if this fails before writing md_update_sb(),
1069 * add_new_disk_cancel() must be called to release token lock
1070 */
1071static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001072{
1073 struct md_cluster_info *cinfo = mddev->cluster_info;
1074 struct cluster_msg cmsg;
1075 int ret = 0;
1076 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1077 char *uuid = sb->device_uuid;
1078
1079 memset(&cmsg, 0, sizeof(cmsg));
1080 cmsg.type = cpu_to_le32(NEWDISK);
1081 memcpy(cmsg.uuid, uuid, 16);
Guoqing Jiangfaeff832015-10-12 17:21:21 +08001082 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001083 lock_comm(cinfo);
1084 ret = __sendmsg(cinfo, &cmsg);
1085 if (ret)
1086 return ret;
1087 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
1088 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
1089 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
1090 /* Some node does not "see" the device */
1091 if (ret == -EAGAIN)
1092 ret = -ENOENT;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001093 if (ret)
1094 unlock_comm(cinfo);
Guoqing Jiang8b9277c2015-12-21 10:51:00 +11001095 else {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001096 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
Guoqing Jiange19508f2015-12-21 10:51:01 +11001097 /* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
1098 * will run soon after add_new_disk, the below path will be
1099 * invoked:
1100 * md_wakeup_thread(mddev->thread)
1101 * -> conf->thread (raid1d)
1102 * -> md_check_recovery -> md_update_sb
1103 * -> metadata_update_start/finish
1104 * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
1105 *
1106 * For other failure cases, metadata_update_cancel and
1107 * add_new_disk_cancel also clear below bit as well.
1108 * */
Guoqing Jiang8b9277c2015-12-21 10:51:00 +11001109 set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1110 wake_up(&cinfo->wait);
1111 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001112 return ret;
1113}
1114
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001115static void add_new_disk_cancel(struct mddev *mddev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001116{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001117 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiang8b9277c2015-12-21 10:51:00 +11001118 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001119 unlock_comm(cinfo);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001120}
1121
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06001122static int new_disk_ack(struct mddev *mddev, bool ack)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001123{
1124 struct md_cluster_info *cinfo = mddev->cluster_info;
1125
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06001126 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
1127 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
1128 return -EINVAL;
1129 }
1130
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001131 if (ack)
1132 dlm_unlock_sync(cinfo->no_new_dev_lockres);
1133 complete(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06001134 return 0;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001135}
1136
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001137static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1138{
Guoqing Jiangaee177a2015-10-12 17:21:24 +08001139 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001140 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiangfaeff832015-10-12 17:21:21 +08001141 cmsg.type = cpu_to_le32(REMOVE);
1142 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11001143 return sendmsg(cinfo, &cmsg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001144}
1145
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11001146static int lock_all_bitmaps(struct mddev *mddev)
1147{
1148 int slot, my_slot, ret, held = 1, i = 0;
1149 char str[64];
1150 struct md_cluster_info *cinfo = mddev->cluster_info;
1151
1152 cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) *
1153 sizeof(struct dlm_lock_resource *),
1154 GFP_KERNEL);
1155 if (!cinfo->other_bitmap_lockres) {
1156 pr_err("md: can't alloc mem for other bitmap locks\n");
1157 return 0;
1158 }
1159
1160 my_slot = slot_number(mddev);
1161 for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
1162 if (slot == my_slot)
1163 continue;
1164
1165 memset(str, '\0', 64);
1166 snprintf(str, 64, "bitmap%04d", slot);
1167 cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
1168 if (!cinfo->other_bitmap_lockres[i])
1169 return -ENOMEM;
1170
1171 cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
1172 ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
1173 if (ret)
1174 held = -1;
1175 i++;
1176 }
1177
1178 return held;
1179}
1180
1181static void unlock_all_bitmaps(struct mddev *mddev)
1182{
1183 struct md_cluster_info *cinfo = mddev->cluster_info;
1184 int i;
1185
1186 /* release other node's bitmap lock if they are existed */
1187 if (cinfo->other_bitmap_lockres) {
1188 for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
1189 if (cinfo->other_bitmap_lockres[i]) {
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11001190 lockres_free(cinfo->other_bitmap_lockres[i]);
1191 }
1192 }
1193 kfree(cinfo->other_bitmap_lockres);
1194 }
1195}
1196
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001197static int gather_bitmaps(struct md_rdev *rdev)
1198{
1199 int sn, err;
1200 sector_t lo, hi;
Guoqing Jiangaee177a2015-10-12 17:21:24 +08001201 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001202 struct mddev *mddev = rdev->mddev;
1203 struct md_cluster_info *cinfo = mddev->cluster_info;
1204
Guoqing Jiangfaeff832015-10-12 17:21:21 +08001205 cmsg.type = cpu_to_le32(RE_ADD);
1206 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001207 err = sendmsg(cinfo, &cmsg);
1208 if (err)
1209 goto out;
1210
1211 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
1212 if (sn == (cinfo->slot_number - 1))
1213 continue;
1214 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
1215 if (err) {
1216 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
1217 goto out;
1218 }
1219 if ((hi > 0) && (lo < mddev->recovery_cp))
1220 mddev->recovery_cp = lo;
1221 }
1222out:
1223 return err;
1224}
1225
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001226static struct md_cluster_operations cluster_ops = {
1227 .join = join,
1228 .leave = leave,
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05001229 .slot_number = slot_number,
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001230 .resync_start = resync_start,
1231 .resync_finish = resync_finish,
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05001232 .resync_info_update = resync_info_update,
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05001233 .metadata_update_start = metadata_update_start,
1234 .metadata_update_finish = metadata_update_finish,
1235 .metadata_update_cancel = metadata_update_cancel,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001236 .area_resyncing = area_resyncing,
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001237 .add_new_disk = add_new_disk,
1238 .add_new_disk_cancel = add_new_disk_cancel,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001239 .new_disk_ack = new_disk_ack,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001240 .remove_disk = remove_disk,
Guoqing Jiang51e453a2016-05-04 02:17:09 -04001241 .load_bitmaps = load_bitmaps,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001242 .gather_bitmaps = gather_bitmaps,
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11001243 .lock_all_bitmaps = lock_all_bitmaps,
1244 .unlock_all_bitmaps = unlock_all_bitmaps,
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001245};
1246
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001247static int __init cluster_init(void)
1248{
1249 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1250 pr_info("Registering Cluster MD functions\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001251 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001252 return 0;
1253}
1254
1255static void cluster_exit(void)
1256{
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001257 unregister_md_cluster_operations();
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001258}
1259
1260module_init(cluster_init);
1261module_exit(cluster_exit);
Guoqing Jiang86b57272015-10-12 17:21:25 +08001262MODULE_AUTHOR("SUSE");
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001263MODULE_LICENSE("GPL");
1264MODULE_DESCRIPTION("Clustering support for MD");