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