ocfs2_dlm: fix cluster-wide refcounting of lock resources
[deliverable/linux.git] / fs / ocfs2 / dlm / dlmrecovery.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmrecovery.c
5 *
6 * recovery stuff
7 *
8 * Copyright (C) 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 *
25 */
26
27
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/highmem.h>
33 #include <linux/utsname.h>
34 #include <linux/init.h>
35 #include <linux/sysctl.h>
36 #include <linux/random.h>
37 #include <linux/blkdev.h>
38 #include <linux/socket.h>
39 #include <linux/inet.h>
40 #include <linux/timer.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43
44
45 #include "cluster/heartbeat.h"
46 #include "cluster/nodemanager.h"
47 #include "cluster/tcp.h"
48
49 #include "dlmapi.h"
50 #include "dlmcommon.h"
51 #include "dlmdomain.h"
52
53 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
54 #include "cluster/masklog.h"
55
56 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
57
58 static int dlm_recovery_thread(void *data);
59 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
60 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
61 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
62 static int dlm_do_recovery(struct dlm_ctxt *dlm);
63
64 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
65 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
66 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
67 static int dlm_request_all_locks(struct dlm_ctxt *dlm,
68 u8 request_from, u8 dead_node);
69 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
70
71 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
72 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
73 const char *lockname, int namelen,
74 int total_locks, u64 cookie,
75 u8 flags, u8 master);
76 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
77 struct dlm_migratable_lockres *mres,
78 u8 send_to,
79 struct dlm_lock_resource *res,
80 int total_locks);
81 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
82 struct dlm_lock_resource *res,
83 struct dlm_migratable_lockres *mres);
84 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
85 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
86 u8 dead_node, u8 send_to);
87 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
88 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
89 struct list_head *list, u8 dead_node);
90 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
91 u8 dead_node, u8 new_master);
92 static void dlm_reco_ast(void *astdata);
93 static void dlm_reco_bast(void *astdata, int blocked_type);
94 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
95 static void dlm_request_all_locks_worker(struct dlm_work_item *item,
96 void *data);
97 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
98 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
99 struct dlm_lock_resource *res,
100 u8 *real_master);
101
102 static u64 dlm_get_next_mig_cookie(void);
103
104 static DEFINE_SPINLOCK(dlm_reco_state_lock);
105 static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
106 static u64 dlm_mig_cookie = 1;
107
108 static u64 dlm_get_next_mig_cookie(void)
109 {
110 u64 c;
111 spin_lock(&dlm_mig_cookie_lock);
112 c = dlm_mig_cookie;
113 if (dlm_mig_cookie == (~0ULL))
114 dlm_mig_cookie = 1;
115 else
116 dlm_mig_cookie++;
117 spin_unlock(&dlm_mig_cookie_lock);
118 return c;
119 }
120
121 static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
122 u8 dead_node)
123 {
124 assert_spin_locked(&dlm->spinlock);
125 if (dlm->reco.dead_node != dead_node)
126 mlog(0, "%s: changing dead_node from %u to %u\n",
127 dlm->name, dlm->reco.dead_node, dead_node);
128 dlm->reco.dead_node = dead_node;
129 }
130
131 static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
132 u8 master)
133 {
134 assert_spin_locked(&dlm->spinlock);
135 mlog(0, "%s: changing new_master from %u to %u\n",
136 dlm->name, dlm->reco.new_master, master);
137 dlm->reco.new_master = master;
138 }
139
140 static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
141 {
142 assert_spin_locked(&dlm->spinlock);
143 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
144 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
145 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
146 }
147
148 static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
149 {
150 spin_lock(&dlm->spinlock);
151 __dlm_reset_recovery(dlm);
152 spin_unlock(&dlm->spinlock);
153 }
154
155 /* Worker function used during recovery. */
156 void dlm_dispatch_work(struct work_struct *work)
157 {
158 struct dlm_ctxt *dlm =
159 container_of(work, struct dlm_ctxt, dispatched_work);
160 LIST_HEAD(tmp_list);
161 struct list_head *iter, *iter2;
162 struct dlm_work_item *item;
163 dlm_workfunc_t *workfunc;
164 int tot=0;
165
166 if (!dlm_joined(dlm))
167 return;
168
169 spin_lock(&dlm->work_lock);
170 list_splice_init(&dlm->work_list, &tmp_list);
171 spin_unlock(&dlm->work_lock);
172
173 list_for_each_safe(iter, iter2, &tmp_list) {
174 tot++;
175 }
176 mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
177
178 list_for_each_safe(iter, iter2, &tmp_list) {
179 item = list_entry(iter, struct dlm_work_item, list);
180 workfunc = item->func;
181 list_del_init(&item->list);
182
183 /* already have ref on dlm to avoid having
184 * it disappear. just double-check. */
185 BUG_ON(item->dlm != dlm);
186
187 /* this is allowed to sleep and
188 * call network stuff */
189 workfunc(item, item->data);
190
191 dlm_put(dlm);
192 kfree(item);
193 }
194 }
195
196 /*
197 * RECOVERY THREAD
198 */
199
200 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
201 {
202 /* wake the recovery thread
203 * this will wake the reco thread in one of three places
204 * 1) sleeping with no recovery happening
205 * 2) sleeping with recovery mastered elsewhere
206 * 3) recovery mastered here, waiting on reco data */
207
208 wake_up(&dlm->dlm_reco_thread_wq);
209 }
210
211 /* Launch the recovery thread */
212 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
213 {
214 mlog(0, "starting dlm recovery thread...\n");
215
216 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
217 "dlm_reco_thread");
218 if (IS_ERR(dlm->dlm_reco_thread_task)) {
219 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
220 dlm->dlm_reco_thread_task = NULL;
221 return -EINVAL;
222 }
223
224 return 0;
225 }
226
227 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
228 {
229 if (dlm->dlm_reco_thread_task) {
230 mlog(0, "waiting for dlm recovery thread to exit\n");
231 kthread_stop(dlm->dlm_reco_thread_task);
232 dlm->dlm_reco_thread_task = NULL;
233 }
234 }
235
236
237
238 /*
239 * this is lame, but here's how recovery works...
240 * 1) all recovery threads cluster wide will work on recovering
241 * ONE node at a time
242 * 2) negotiate who will take over all the locks for the dead node.
243 * thats right... ALL the locks.
244 * 3) once a new master is chosen, everyone scans all locks
245 * and moves aside those mastered by the dead guy
246 * 4) each of these locks should be locked until recovery is done
247 * 5) the new master collects up all of secondary lock queue info
248 * one lock at a time, forcing each node to communicate back
249 * before continuing
250 * 6) each secondary lock queue responds with the full known lock info
251 * 7) once the new master has run all its locks, it sends a ALLDONE!
252 * message to everyone
253 * 8) upon receiving this message, the secondary queue node unlocks
254 * and responds to the ALLDONE
255 * 9) once the new master gets responses from everyone, he unlocks
256 * everything and recovery for this dead node is done
257 *10) go back to 2) while there are still dead nodes
258 *
259 */
260
261 static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
262 {
263 struct dlm_reco_node_data *ndata;
264 struct dlm_lock_resource *res;
265
266 mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
267 dlm->name, dlm->dlm_reco_thread_task->pid,
268 dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
269 dlm->reco.dead_node, dlm->reco.new_master);
270
271 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
272 char *st = "unknown";
273 switch (ndata->state) {
274 case DLM_RECO_NODE_DATA_INIT:
275 st = "init";
276 break;
277 case DLM_RECO_NODE_DATA_REQUESTING:
278 st = "requesting";
279 break;
280 case DLM_RECO_NODE_DATA_DEAD:
281 st = "dead";
282 break;
283 case DLM_RECO_NODE_DATA_RECEIVING:
284 st = "receiving";
285 break;
286 case DLM_RECO_NODE_DATA_REQUESTED:
287 st = "requested";
288 break;
289 case DLM_RECO_NODE_DATA_DONE:
290 st = "done";
291 break;
292 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
293 st = "finalize-sent";
294 break;
295 default:
296 st = "bad";
297 break;
298 }
299 mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
300 dlm->name, ndata->node_num, st);
301 }
302 list_for_each_entry(res, &dlm->reco.resources, recovering) {
303 mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
304 dlm->name, res->lockname.len, res->lockname.name);
305 }
306 }
307
308 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
309
310 static int dlm_recovery_thread(void *data)
311 {
312 int status;
313 struct dlm_ctxt *dlm = data;
314 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
315
316 mlog(0, "dlm thread running for %s...\n", dlm->name);
317
318 while (!kthread_should_stop()) {
319 if (dlm_joined(dlm)) {
320 status = dlm_do_recovery(dlm);
321 if (status == -EAGAIN) {
322 /* do not sleep, recheck immediately. */
323 continue;
324 }
325 if (status < 0)
326 mlog_errno(status);
327 }
328
329 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
330 kthread_should_stop(),
331 timeout);
332 }
333
334 mlog(0, "quitting DLM recovery thread\n");
335 return 0;
336 }
337
338 /* returns true when the recovery master has contacted us */
339 static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
340 {
341 int ready;
342 spin_lock(&dlm->spinlock);
343 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
344 spin_unlock(&dlm->spinlock);
345 return ready;
346 }
347
348 /* returns true if node is no longer in the domain
349 * could be dead or just not joined */
350 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
351 {
352 int dead;
353 spin_lock(&dlm->spinlock);
354 dead = !test_bit(node, dlm->domain_map);
355 spin_unlock(&dlm->spinlock);
356 return dead;
357 }
358
359 /* returns true if node is no longer in the domain
360 * could be dead or just not joined */
361 static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
362 {
363 int recovered;
364 spin_lock(&dlm->spinlock);
365 recovered = !test_bit(node, dlm->recovery_map);
366 spin_unlock(&dlm->spinlock);
367 return recovered;
368 }
369
370
371 int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
372 {
373 if (timeout) {
374 mlog(ML_NOTICE, "%s: waiting %dms for notification of "
375 "death of node %u\n", dlm->name, timeout, node);
376 wait_event_timeout(dlm->dlm_reco_thread_wq,
377 dlm_is_node_dead(dlm, node),
378 msecs_to_jiffies(timeout));
379 } else {
380 mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
381 "of death of node %u\n", dlm->name, node);
382 wait_event(dlm->dlm_reco_thread_wq,
383 dlm_is_node_dead(dlm, node));
384 }
385 /* for now, return 0 */
386 return 0;
387 }
388
389 int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
390 {
391 if (timeout) {
392 mlog(0, "%s: waiting %dms for notification of "
393 "recovery of node %u\n", dlm->name, timeout, node);
394 wait_event_timeout(dlm->dlm_reco_thread_wq,
395 dlm_is_node_recovered(dlm, node),
396 msecs_to_jiffies(timeout));
397 } else {
398 mlog(0, "%s: waiting indefinitely for notification "
399 "of recovery of node %u\n", dlm->name, node);
400 wait_event(dlm->dlm_reco_thread_wq,
401 dlm_is_node_recovered(dlm, node));
402 }
403 /* for now, return 0 */
404 return 0;
405 }
406
407 /* callers of the top-level api calls (dlmlock/dlmunlock) should
408 * block on the dlm->reco.event when recovery is in progress.
409 * the dlm recovery thread will set this state when it begins
410 * recovering a dead node (as the new master or not) and clear
411 * the state and wake as soon as all affected lock resources have
412 * been marked with the RECOVERY flag */
413 static int dlm_in_recovery(struct dlm_ctxt *dlm)
414 {
415 int in_recovery;
416 spin_lock(&dlm->spinlock);
417 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
418 spin_unlock(&dlm->spinlock);
419 return in_recovery;
420 }
421
422
423 void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
424 {
425 if (dlm_in_recovery(dlm)) {
426 mlog(0, "%s: reco thread %d in recovery: "
427 "state=%d, master=%u, dead=%u\n",
428 dlm->name, dlm->dlm_reco_thread_task->pid,
429 dlm->reco.state, dlm->reco.new_master,
430 dlm->reco.dead_node);
431 }
432 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
433 }
434
435 static void dlm_begin_recovery(struct dlm_ctxt *dlm)
436 {
437 spin_lock(&dlm->spinlock);
438 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
439 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
440 spin_unlock(&dlm->spinlock);
441 }
442
443 static void dlm_end_recovery(struct dlm_ctxt *dlm)
444 {
445 spin_lock(&dlm->spinlock);
446 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
447 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
448 spin_unlock(&dlm->spinlock);
449 wake_up(&dlm->reco.event);
450 }
451
452 static int dlm_do_recovery(struct dlm_ctxt *dlm)
453 {
454 int status = 0;
455 int ret;
456
457 spin_lock(&dlm->spinlock);
458
459 /* check to see if the new master has died */
460 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
461 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
462 mlog(0, "new master %u died while recovering %u!\n",
463 dlm->reco.new_master, dlm->reco.dead_node);
464 /* unset the new_master, leave dead_node */
465 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
466 }
467
468 /* select a target to recover */
469 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
470 int bit;
471
472 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
473 if (bit >= O2NM_MAX_NODES || bit < 0)
474 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
475 else
476 dlm_set_reco_dead_node(dlm, bit);
477 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
478 /* BUG? */
479 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
480 dlm->reco.dead_node);
481 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
482 }
483
484 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
485 // mlog(0, "nothing to recover! sleeping now!\n");
486 spin_unlock(&dlm->spinlock);
487 /* return to main thread loop and sleep. */
488 return 0;
489 }
490 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
491 dlm->name, dlm->dlm_reco_thread_task->pid,
492 dlm->reco.dead_node);
493 spin_unlock(&dlm->spinlock);
494
495 /* take write barrier */
496 /* (stops the list reshuffling thread, proxy ast handling) */
497 dlm_begin_recovery(dlm);
498
499 if (dlm->reco.new_master == dlm->node_num)
500 goto master_here;
501
502 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
503 /* choose a new master, returns 0 if this node
504 * is the master, -EEXIST if it's another node.
505 * this does not return until a new master is chosen
506 * or recovery completes entirely. */
507 ret = dlm_pick_recovery_master(dlm);
508 if (!ret) {
509 /* already notified everyone. go. */
510 goto master_here;
511 }
512 mlog(0, "another node will master this recovery session.\n");
513 }
514 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
515 dlm->name, dlm->dlm_reco_thread_task->pid, dlm->reco.new_master,
516 dlm->node_num, dlm->reco.dead_node);
517
518 /* it is safe to start everything back up here
519 * because all of the dead node's lock resources
520 * have been marked as in-recovery */
521 dlm_end_recovery(dlm);
522
523 /* sleep out in main dlm_recovery_thread loop. */
524 return 0;
525
526 master_here:
527 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
528 dlm->dlm_reco_thread_task->pid,
529 dlm->name, dlm->reco.dead_node, dlm->node_num);
530
531 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
532 if (status < 0) {
533 /* we should never hit this anymore */
534 mlog(ML_ERROR, "error %d remastering locks for node %u, "
535 "retrying.\n", status, dlm->reco.dead_node);
536 /* yield a bit to allow any final network messages
537 * to get handled on remaining nodes */
538 msleep(100);
539 } else {
540 /* success! see if any other nodes need recovery */
541 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
542 dlm->name, dlm->reco.dead_node, dlm->node_num);
543 dlm_reset_recovery(dlm);
544 }
545 dlm_end_recovery(dlm);
546
547 /* continue and look for another dead node */
548 return -EAGAIN;
549 }
550
551 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
552 {
553 int status = 0;
554 struct dlm_reco_node_data *ndata;
555 struct list_head *iter;
556 int all_nodes_done;
557 int destroy = 0;
558 int pass = 0;
559
560 do {
561 /* we have become recovery master. there is no escaping
562 * this, so just keep trying until we get it. */
563 status = dlm_init_recovery_area(dlm, dead_node);
564 if (status < 0) {
565 mlog(ML_ERROR, "%s: failed to alloc recovery area, "
566 "retrying\n", dlm->name);
567 msleep(1000);
568 }
569 } while (status != 0);
570
571 /* safe to access the node data list without a lock, since this
572 * process is the only one to change the list */
573 list_for_each(iter, &dlm->reco.node_data) {
574 ndata = list_entry (iter, struct dlm_reco_node_data, list);
575 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
576 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
577
578 mlog(0, "requesting lock info from node %u\n",
579 ndata->node_num);
580
581 if (ndata->node_num == dlm->node_num) {
582 ndata->state = DLM_RECO_NODE_DATA_DONE;
583 continue;
584 }
585
586 do {
587 status = dlm_request_all_locks(dlm, ndata->node_num,
588 dead_node);
589 if (status < 0) {
590 mlog_errno(status);
591 if (dlm_is_host_down(status)) {
592 /* node died, ignore it for recovery */
593 status = 0;
594 ndata->state = DLM_RECO_NODE_DATA_DEAD;
595 /* wait for the domain map to catch up
596 * with the network state. */
597 wait_event_timeout(dlm->dlm_reco_thread_wq,
598 dlm_is_node_dead(dlm,
599 ndata->node_num),
600 msecs_to_jiffies(1000));
601 mlog(0, "waited 1 sec for %u, "
602 "dead? %s\n", ndata->node_num,
603 dlm_is_node_dead(dlm, ndata->node_num) ?
604 "yes" : "no");
605 } else {
606 /* -ENOMEM on the other node */
607 mlog(0, "%s: node %u returned "
608 "%d during recovery, retrying "
609 "after a short wait\n",
610 dlm->name, ndata->node_num,
611 status);
612 msleep(100);
613 }
614 }
615 } while (status != 0);
616
617 switch (ndata->state) {
618 case DLM_RECO_NODE_DATA_INIT:
619 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
620 case DLM_RECO_NODE_DATA_REQUESTED:
621 BUG();
622 break;
623 case DLM_RECO_NODE_DATA_DEAD:
624 mlog(0, "node %u died after requesting "
625 "recovery info for node %u\n",
626 ndata->node_num, dead_node);
627 /* fine. don't need this node's info.
628 * continue without it. */
629 break;
630 case DLM_RECO_NODE_DATA_REQUESTING:
631 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
632 mlog(0, "now receiving recovery data from "
633 "node %u for dead node %u\n",
634 ndata->node_num, dead_node);
635 break;
636 case DLM_RECO_NODE_DATA_RECEIVING:
637 mlog(0, "already receiving recovery data from "
638 "node %u for dead node %u\n",
639 ndata->node_num, dead_node);
640 break;
641 case DLM_RECO_NODE_DATA_DONE:
642 mlog(0, "already DONE receiving recovery data "
643 "from node %u for dead node %u\n",
644 ndata->node_num, dead_node);
645 break;
646 }
647 }
648
649 mlog(0, "done requesting all lock info\n");
650
651 /* nodes should be sending reco data now
652 * just need to wait */
653
654 while (1) {
655 /* check all the nodes now to see if we are
656 * done, or if anyone died */
657 all_nodes_done = 1;
658 spin_lock(&dlm_reco_state_lock);
659 list_for_each(iter, &dlm->reco.node_data) {
660 ndata = list_entry (iter, struct dlm_reco_node_data, list);
661
662 mlog(0, "checking recovery state of node %u\n",
663 ndata->node_num);
664 switch (ndata->state) {
665 case DLM_RECO_NODE_DATA_INIT:
666 case DLM_RECO_NODE_DATA_REQUESTING:
667 mlog(ML_ERROR, "bad ndata state for "
668 "node %u: state=%d\n",
669 ndata->node_num, ndata->state);
670 BUG();
671 break;
672 case DLM_RECO_NODE_DATA_DEAD:
673 mlog(0, "node %u died after "
674 "requesting recovery info for "
675 "node %u\n", ndata->node_num,
676 dead_node);
677 break;
678 case DLM_RECO_NODE_DATA_RECEIVING:
679 case DLM_RECO_NODE_DATA_REQUESTED:
680 mlog(0, "%s: node %u still in state %s\n",
681 dlm->name, ndata->node_num,
682 ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
683 "receiving" : "requested");
684 all_nodes_done = 0;
685 break;
686 case DLM_RECO_NODE_DATA_DONE:
687 mlog(0, "%s: node %u state is done\n",
688 dlm->name, ndata->node_num);
689 break;
690 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
691 mlog(0, "%s: node %u state is finalize\n",
692 dlm->name, ndata->node_num);
693 break;
694 }
695 }
696 spin_unlock(&dlm_reco_state_lock);
697
698 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
699 all_nodes_done?"yes":"no");
700 if (all_nodes_done) {
701 int ret;
702
703 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
704 * just send a finalize message to everyone and
705 * clean up */
706 mlog(0, "all nodes are done! send finalize\n");
707 ret = dlm_send_finalize_reco_message(dlm);
708 if (ret < 0)
709 mlog_errno(ret);
710
711 spin_lock(&dlm->spinlock);
712 dlm_finish_local_lockres_recovery(dlm, dead_node,
713 dlm->node_num);
714 spin_unlock(&dlm->spinlock);
715 mlog(0, "should be done with recovery!\n");
716
717 mlog(0, "finishing recovery of %s at %lu, "
718 "dead=%u, this=%u, new=%u\n", dlm->name,
719 jiffies, dlm->reco.dead_node,
720 dlm->node_num, dlm->reco.new_master);
721 destroy = 1;
722 status = 0;
723 /* rescan everything marked dirty along the way */
724 dlm_kick_thread(dlm, NULL);
725 break;
726 }
727 /* wait to be signalled, with periodic timeout
728 * to check for node death */
729 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
730 kthread_should_stop(),
731 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
732
733 }
734
735 if (destroy)
736 dlm_destroy_recovery_area(dlm, dead_node);
737
738 mlog_exit(status);
739 return status;
740 }
741
742 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
743 {
744 int num=0;
745 struct dlm_reco_node_data *ndata;
746
747 spin_lock(&dlm->spinlock);
748 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
749 /* nodes can only be removed (by dying) after dropping
750 * this lock, and death will be trapped later, so this should do */
751 spin_unlock(&dlm->spinlock);
752
753 while (1) {
754 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
755 if (num >= O2NM_MAX_NODES) {
756 break;
757 }
758 BUG_ON(num == dead_node);
759
760 ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
761 if (!ndata) {
762 dlm_destroy_recovery_area(dlm, dead_node);
763 return -ENOMEM;
764 }
765 ndata->node_num = num;
766 ndata->state = DLM_RECO_NODE_DATA_INIT;
767 spin_lock(&dlm_reco_state_lock);
768 list_add_tail(&ndata->list, &dlm->reco.node_data);
769 spin_unlock(&dlm_reco_state_lock);
770 num++;
771 }
772
773 return 0;
774 }
775
776 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
777 {
778 struct list_head *iter, *iter2;
779 struct dlm_reco_node_data *ndata;
780 LIST_HEAD(tmplist);
781
782 spin_lock(&dlm_reco_state_lock);
783 list_splice_init(&dlm->reco.node_data, &tmplist);
784 spin_unlock(&dlm_reco_state_lock);
785
786 list_for_each_safe(iter, iter2, &tmplist) {
787 ndata = list_entry (iter, struct dlm_reco_node_data, list);
788 list_del_init(&ndata->list);
789 kfree(ndata);
790 }
791 }
792
793 static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
794 u8 dead_node)
795 {
796 struct dlm_lock_request lr;
797 enum dlm_status ret;
798
799 mlog(0, "\n");
800
801
802 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
803 "to %u\n", dead_node, request_from);
804
805 memset(&lr, 0, sizeof(lr));
806 lr.node_idx = dlm->node_num;
807 lr.dead_node = dead_node;
808
809 // send message
810 ret = DLM_NOLOCKMGR;
811 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
812 &lr, sizeof(lr), request_from, NULL);
813
814 /* negative status is handled by caller */
815 if (ret < 0)
816 mlog_errno(ret);
817
818 // return from here, then
819 // sleep until all received or error
820 return ret;
821
822 }
823
824 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
825 {
826 struct dlm_ctxt *dlm = data;
827 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
828 char *buf = NULL;
829 struct dlm_work_item *item = NULL;
830
831 if (!dlm_grab(dlm))
832 return -EINVAL;
833
834 if (lr->dead_node != dlm->reco.dead_node) {
835 mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
836 "dead_node is %u\n", dlm->name, lr->node_idx,
837 lr->dead_node, dlm->reco.dead_node);
838 dlm_print_reco_node_status(dlm);
839 /* this is a hack */
840 dlm_put(dlm);
841 return -ENOMEM;
842 }
843 BUG_ON(lr->dead_node != dlm->reco.dead_node);
844
845 item = kzalloc(sizeof(*item), GFP_NOFS);
846 if (!item) {
847 dlm_put(dlm);
848 return -ENOMEM;
849 }
850
851 /* this will get freed by dlm_request_all_locks_worker */
852 buf = (char *) __get_free_page(GFP_NOFS);
853 if (!buf) {
854 kfree(item);
855 dlm_put(dlm);
856 return -ENOMEM;
857 }
858
859 /* queue up work for dlm_request_all_locks_worker */
860 dlm_grab(dlm); /* get an extra ref for the work item */
861 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
862 item->u.ral.reco_master = lr->node_idx;
863 item->u.ral.dead_node = lr->dead_node;
864 spin_lock(&dlm->work_lock);
865 list_add_tail(&item->list, &dlm->work_list);
866 spin_unlock(&dlm->work_lock);
867 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
868
869 dlm_put(dlm);
870 return 0;
871 }
872
873 static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
874 {
875 struct dlm_migratable_lockres *mres;
876 struct dlm_lock_resource *res;
877 struct dlm_ctxt *dlm;
878 LIST_HEAD(resources);
879 struct list_head *iter;
880 int ret;
881 u8 dead_node, reco_master;
882 int skip_all_done = 0;
883
884 dlm = item->dlm;
885 dead_node = item->u.ral.dead_node;
886 reco_master = item->u.ral.reco_master;
887 mres = (struct dlm_migratable_lockres *)data;
888
889 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
890 dlm->name, dead_node, reco_master);
891
892 if (dead_node != dlm->reco.dead_node ||
893 reco_master != dlm->reco.new_master) {
894 /* worker could have been created before the recovery master
895 * died. if so, do not continue, but do not error. */
896 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
897 mlog(ML_NOTICE, "%s: will not send recovery state, "
898 "recovery master %u died, thread=(dead=%u,mas=%u)"
899 " current=(dead=%u,mas=%u)\n", dlm->name,
900 reco_master, dead_node, reco_master,
901 dlm->reco.dead_node, dlm->reco.new_master);
902 } else {
903 mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
904 "master=%u), request(dead=%u, master=%u)\n",
905 dlm->name, dlm->reco.dead_node,
906 dlm->reco.new_master, dead_node, reco_master);
907 }
908 goto leave;
909 }
910
911 /* lock resources should have already been moved to the
912 * dlm->reco.resources list. now move items from that list
913 * to a temp list if the dead owner matches. note that the
914 * whole cluster recovers only one node at a time, so we
915 * can safely move UNKNOWN lock resources for each recovery
916 * session. */
917 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
918
919 /* now we can begin blasting lockreses without the dlm lock */
920
921 /* any errors returned will be due to the new_master dying,
922 * the dlm_reco_thread should detect this */
923 list_for_each(iter, &resources) {
924 res = list_entry (iter, struct dlm_lock_resource, recovering);
925 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
926 DLM_MRES_RECOVERY);
927 if (ret < 0) {
928 mlog(ML_ERROR, "%s: node %u went down while sending "
929 "recovery state for dead node %u, ret=%d\n", dlm->name,
930 reco_master, dead_node, ret);
931 skip_all_done = 1;
932 break;
933 }
934 }
935
936 /* move the resources back to the list */
937 spin_lock(&dlm->spinlock);
938 list_splice_init(&resources, &dlm->reco.resources);
939 spin_unlock(&dlm->spinlock);
940
941 if (!skip_all_done) {
942 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
943 if (ret < 0) {
944 mlog(ML_ERROR, "%s: node %u went down while sending "
945 "recovery all-done for dead node %u, ret=%d\n",
946 dlm->name, reco_master, dead_node, ret);
947 }
948 }
949 leave:
950 free_page((unsigned long)data);
951 }
952
953
954 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
955 {
956 int ret, tmpret;
957 struct dlm_reco_data_done done_msg;
958
959 memset(&done_msg, 0, sizeof(done_msg));
960 done_msg.node_idx = dlm->node_num;
961 done_msg.dead_node = dead_node;
962 mlog(0, "sending DATA DONE message to %u, "
963 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
964 done_msg.dead_node);
965
966 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
967 sizeof(done_msg), send_to, &tmpret);
968 if (ret < 0) {
969 if (!dlm_is_host_down(ret)) {
970 mlog_errno(ret);
971 mlog(ML_ERROR, "%s: unknown error sending data-done "
972 "to %u\n", dlm->name, send_to);
973 BUG();
974 }
975 } else
976 ret = tmpret;
977 return ret;
978 }
979
980
981 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
982 {
983 struct dlm_ctxt *dlm = data;
984 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
985 struct list_head *iter;
986 struct dlm_reco_node_data *ndata = NULL;
987 int ret = -EINVAL;
988
989 if (!dlm_grab(dlm))
990 return -EINVAL;
991
992 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
993 "node_idx=%u, this node=%u\n", done->dead_node,
994 dlm->reco.dead_node, done->node_idx, dlm->node_num);
995
996 mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
997 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
998 "node_idx=%u, this node=%u\n", done->dead_node,
999 dlm->reco.dead_node, done->node_idx, dlm->node_num);
1000
1001 spin_lock(&dlm_reco_state_lock);
1002 list_for_each(iter, &dlm->reco.node_data) {
1003 ndata = list_entry (iter, struct dlm_reco_node_data, list);
1004 if (ndata->node_num != done->node_idx)
1005 continue;
1006
1007 switch (ndata->state) {
1008 /* should have moved beyond INIT but not to FINALIZE yet */
1009 case DLM_RECO_NODE_DATA_INIT:
1010 case DLM_RECO_NODE_DATA_DEAD:
1011 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
1012 mlog(ML_ERROR, "bad ndata state for node %u:"
1013 " state=%d\n", ndata->node_num,
1014 ndata->state);
1015 BUG();
1016 break;
1017 /* these states are possible at this point, anywhere along
1018 * the line of recovery */
1019 case DLM_RECO_NODE_DATA_DONE:
1020 case DLM_RECO_NODE_DATA_RECEIVING:
1021 case DLM_RECO_NODE_DATA_REQUESTED:
1022 case DLM_RECO_NODE_DATA_REQUESTING:
1023 mlog(0, "node %u is DONE sending "
1024 "recovery data!\n",
1025 ndata->node_num);
1026
1027 ndata->state = DLM_RECO_NODE_DATA_DONE;
1028 ret = 0;
1029 break;
1030 }
1031 }
1032 spin_unlock(&dlm_reco_state_lock);
1033
1034 /* wake the recovery thread, some node is done */
1035 if (!ret)
1036 dlm_kick_recovery_thread(dlm);
1037
1038 if (ret < 0)
1039 mlog(ML_ERROR, "failed to find recovery node data for node "
1040 "%u\n", done->node_idx);
1041 dlm_put(dlm);
1042
1043 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
1044 return ret;
1045 }
1046
1047 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
1048 struct list_head *list,
1049 u8 dead_node)
1050 {
1051 struct dlm_lock_resource *res;
1052 struct list_head *iter, *iter2;
1053 struct dlm_lock *lock;
1054
1055 spin_lock(&dlm->spinlock);
1056 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1057 res = list_entry (iter, struct dlm_lock_resource, recovering);
1058 /* always prune any $RECOVERY entries for dead nodes,
1059 * otherwise hangs can occur during later recovery */
1060 if (dlm_is_recovery_lock(res->lockname.name,
1061 res->lockname.len)) {
1062 spin_lock(&res->spinlock);
1063 list_for_each_entry(lock, &res->granted, list) {
1064 if (lock->ml.node == dead_node) {
1065 mlog(0, "AHA! there was "
1066 "a $RECOVERY lock for dead "
1067 "node %u (%s)!\n",
1068 dead_node, dlm->name);
1069 list_del_init(&lock->list);
1070 dlm_lock_put(lock);
1071 break;
1072 }
1073 }
1074 spin_unlock(&res->spinlock);
1075 continue;
1076 }
1077
1078 if (res->owner == dead_node) {
1079 mlog(0, "found lockres owned by dead node while "
1080 "doing recovery for node %u. sending it.\n",
1081 dead_node);
1082 list_move_tail(&res->recovering, list);
1083 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1084 mlog(0, "found UNKNOWN owner while doing recovery "
1085 "for node %u. sending it.\n", dead_node);
1086 list_move_tail(&res->recovering, list);
1087 }
1088 }
1089 spin_unlock(&dlm->spinlock);
1090 }
1091
1092 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1093 {
1094 int total_locks = 0;
1095 struct list_head *iter, *queue = &res->granted;
1096 int i;
1097
1098 for (i=0; i<3; i++) {
1099 list_for_each(iter, queue)
1100 total_locks++;
1101 queue++;
1102 }
1103 return total_locks;
1104 }
1105
1106
1107 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1108 struct dlm_migratable_lockres *mres,
1109 u8 send_to,
1110 struct dlm_lock_resource *res,
1111 int total_locks)
1112 {
1113 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1114 int mres_total_locks = be32_to_cpu(mres->total_locks);
1115 int sz, ret = 0, status = 0;
1116 u8 orig_flags = mres->flags,
1117 orig_master = mres->master;
1118
1119 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1120 if (!mres->num_locks)
1121 return 0;
1122
1123 sz = sizeof(struct dlm_migratable_lockres) +
1124 (mres->num_locks * sizeof(struct dlm_migratable_lock));
1125
1126 /* add an all-done flag if we reached the last lock */
1127 orig_flags = mres->flags;
1128 BUG_ON(total_locks > mres_total_locks);
1129 if (total_locks == mres_total_locks)
1130 mres->flags |= DLM_MRES_ALL_DONE;
1131
1132 mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1133 dlm->name, res->lockname.len, res->lockname.name,
1134 orig_flags & DLM_MRES_MIGRATION ? "migrate" : "recovery",
1135 send_to);
1136
1137 /* send it */
1138 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1139 sz, send_to, &status);
1140 if (ret < 0) {
1141 /* XXX: negative status is not handled.
1142 * this will end up killing this node. */
1143 mlog_errno(ret);
1144 } else {
1145 /* might get an -ENOMEM back here */
1146 ret = status;
1147 if (ret < 0) {
1148 mlog_errno(ret);
1149
1150 if (ret == -EFAULT) {
1151 mlog(ML_ERROR, "node %u told me to kill "
1152 "myself!\n", send_to);
1153 BUG();
1154 }
1155 }
1156 }
1157
1158 /* zero and reinit the message buffer */
1159 dlm_init_migratable_lockres(mres, res->lockname.name,
1160 res->lockname.len, mres_total_locks,
1161 mig_cookie, orig_flags, orig_master);
1162 return ret;
1163 }
1164
1165 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1166 const char *lockname, int namelen,
1167 int total_locks, u64 cookie,
1168 u8 flags, u8 master)
1169 {
1170 /* mres here is one full page */
1171 memset(mres, 0, PAGE_SIZE);
1172 mres->lockname_len = namelen;
1173 memcpy(mres->lockname, lockname, namelen);
1174 mres->num_locks = 0;
1175 mres->total_locks = cpu_to_be32(total_locks);
1176 mres->mig_cookie = cpu_to_be64(cookie);
1177 mres->flags = flags;
1178 mres->master = master;
1179 }
1180
1181
1182 /* returns 1 if this lock fills the network structure,
1183 * 0 otherwise */
1184 static int dlm_add_lock_to_array(struct dlm_lock *lock,
1185 struct dlm_migratable_lockres *mres, int queue)
1186 {
1187 struct dlm_migratable_lock *ml;
1188 int lock_num = mres->num_locks;
1189
1190 ml = &(mres->ml[lock_num]);
1191 ml->cookie = lock->ml.cookie;
1192 ml->type = lock->ml.type;
1193 ml->convert_type = lock->ml.convert_type;
1194 ml->highest_blocked = lock->ml.highest_blocked;
1195 ml->list = queue;
1196 if (lock->lksb) {
1197 ml->flags = lock->lksb->flags;
1198 /* send our current lvb */
1199 if (ml->type == LKM_EXMODE ||
1200 ml->type == LKM_PRMODE) {
1201 /* if it is already set, this had better be a PR
1202 * and it has to match */
1203 if (!dlm_lvb_is_empty(mres->lvb) &&
1204 (ml->type == LKM_EXMODE ||
1205 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
1206 mlog(ML_ERROR, "mismatched lvbs!\n");
1207 __dlm_print_one_lock_resource(lock->lockres);
1208 BUG();
1209 }
1210 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1211 }
1212 }
1213 ml->node = lock->ml.node;
1214 mres->num_locks++;
1215 /* we reached the max, send this network message */
1216 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1217 return 1;
1218 return 0;
1219 }
1220
1221 static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
1222 struct dlm_migratable_lockres *mres)
1223 {
1224 struct dlm_lock dummy;
1225 memset(&dummy, 0, sizeof(dummy));
1226 dummy.ml.cookie = 0;
1227 dummy.ml.type = LKM_IVMODE;
1228 dummy.ml.convert_type = LKM_IVMODE;
1229 dummy.ml.highest_blocked = LKM_IVMODE;
1230 dummy.lksb = NULL;
1231 dummy.ml.node = dlm->node_num;
1232 dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
1233 }
1234
1235 static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
1236 struct dlm_migratable_lock *ml,
1237 u8 *nodenum)
1238 {
1239 if (unlikely(ml->cookie == 0 &&
1240 ml->type == LKM_IVMODE &&
1241 ml->convert_type == LKM_IVMODE &&
1242 ml->highest_blocked == LKM_IVMODE &&
1243 ml->list == DLM_BLOCKED_LIST)) {
1244 *nodenum = ml->node;
1245 return 1;
1246 }
1247 return 0;
1248 }
1249
1250 int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1251 struct dlm_migratable_lockres *mres,
1252 u8 send_to, u8 flags)
1253 {
1254 struct list_head *queue, *iter;
1255 int total_locks, i;
1256 u64 mig_cookie = 0;
1257 struct dlm_lock *lock;
1258 int ret = 0;
1259
1260 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1261
1262 mlog(0, "sending to %u\n", send_to);
1263
1264 total_locks = dlm_num_locks_in_lockres(res);
1265 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1266 /* rare, but possible */
1267 mlog(0, "argh. lockres has %d locks. this will "
1268 "require more than one network packet to "
1269 "migrate\n", total_locks);
1270 mig_cookie = dlm_get_next_mig_cookie();
1271 }
1272
1273 dlm_init_migratable_lockres(mres, res->lockname.name,
1274 res->lockname.len, total_locks,
1275 mig_cookie, flags, res->owner);
1276
1277 total_locks = 0;
1278 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1279 queue = dlm_list_idx_to_ptr(res, i);
1280 list_for_each(iter, queue) {
1281 lock = list_entry (iter, struct dlm_lock, list);
1282
1283 /* add another lock. */
1284 total_locks++;
1285 if (!dlm_add_lock_to_array(lock, mres, i))
1286 continue;
1287
1288 /* this filled the lock message,
1289 * we must send it immediately. */
1290 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1291 res, total_locks);
1292 if (ret < 0)
1293 goto error;
1294 }
1295 }
1296 if (total_locks == 0) {
1297 /* send a dummy lock to indicate a mastery reference only */
1298 mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
1299 dlm->name, res->lockname.len, res->lockname.name,
1300 send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
1301 "migration");
1302 dlm_add_dummy_lock(dlm, mres);
1303 }
1304 /* flush any remaining locks */
1305 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
1306 if (ret < 0)
1307 goto error;
1308 return ret;
1309
1310 error:
1311 mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1312 dlm->name, ret);
1313 if (!dlm_is_host_down(ret))
1314 BUG();
1315 mlog(0, "%s: node %u went down while sending %s "
1316 "lockres %.*s\n", dlm->name, send_to,
1317 flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
1318 res->lockname.len, res->lockname.name);
1319 return ret;
1320 }
1321
1322
1323
1324 /*
1325 * this message will contain no more than one page worth of
1326 * recovery data, and it will work on only one lockres.
1327 * there may be many locks in this page, and we may need to wait
1328 * for additional packets to complete all the locks (rare, but
1329 * possible).
1330 */
1331 /*
1332 * NOTE: the allocation error cases here are scary
1333 * we really cannot afford to fail an alloc in recovery
1334 * do we spin? returning an error only delays the problem really
1335 */
1336
1337 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
1338 {
1339 struct dlm_ctxt *dlm = data;
1340 struct dlm_migratable_lockres *mres =
1341 (struct dlm_migratable_lockres *)msg->buf;
1342 int ret = 0;
1343 u8 real_master;
1344 char *buf = NULL;
1345 struct dlm_work_item *item = NULL;
1346 struct dlm_lock_resource *res = NULL;
1347
1348 if (!dlm_grab(dlm))
1349 return -EINVAL;
1350
1351 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1352
1353 real_master = mres->master;
1354 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1355 /* cannot migrate a lockres with no master */
1356 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1357 }
1358
1359 mlog(0, "%s message received from node %u\n",
1360 (mres->flags & DLM_MRES_RECOVERY) ?
1361 "recovery" : "migration", mres->master);
1362 if (mres->flags & DLM_MRES_ALL_DONE)
1363 mlog(0, "all done flag. all lockres data received!\n");
1364
1365 ret = -ENOMEM;
1366 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1367 item = kzalloc(sizeof(*item), GFP_NOFS);
1368 if (!buf || !item)
1369 goto leave;
1370
1371 /* lookup the lock to see if we have a secondary queue for this
1372 * already... just add the locks in and this will have its owner
1373 * and RECOVERY flag changed when it completes. */
1374 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1375 if (res) {
1376 /* this will get a ref on res */
1377 /* mark it as recovering/migrating and hash it */
1378 spin_lock(&res->spinlock);
1379 if (mres->flags & DLM_MRES_RECOVERY) {
1380 res->state |= DLM_LOCK_RES_RECOVERING;
1381 } else {
1382 if (res->state & DLM_LOCK_RES_MIGRATING) {
1383 /* this is at least the second
1384 * lockres message */
1385 mlog(0, "lock %.*s is already migrating\n",
1386 mres->lockname_len,
1387 mres->lockname);
1388 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1389 /* caller should BUG */
1390 mlog(ML_ERROR, "node is attempting to migrate "
1391 "lock %.*s, but marked as recovering!\n",
1392 mres->lockname_len, mres->lockname);
1393 ret = -EFAULT;
1394 spin_unlock(&res->spinlock);
1395 goto leave;
1396 }
1397 res->state |= DLM_LOCK_RES_MIGRATING;
1398 }
1399 spin_unlock(&res->spinlock);
1400 } else {
1401 /* need to allocate, just like if it was
1402 * mastered here normally */
1403 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1404 if (!res)
1405 goto leave;
1406
1407 /* to match the ref that we would have gotten if
1408 * dlm_lookup_lockres had succeeded */
1409 dlm_lockres_get(res);
1410
1411 /* mark it as recovering/migrating and hash it */
1412 if (mres->flags & DLM_MRES_RECOVERY)
1413 res->state |= DLM_LOCK_RES_RECOVERING;
1414 else
1415 res->state |= DLM_LOCK_RES_MIGRATING;
1416
1417 spin_lock(&dlm->spinlock);
1418 __dlm_insert_lockres(dlm, res);
1419 spin_unlock(&dlm->spinlock);
1420
1421 /* now that the new lockres is inserted,
1422 * make it usable by other processes */
1423 spin_lock(&res->spinlock);
1424 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1425 spin_unlock(&res->spinlock);
1426
1427 /* add an extra ref for just-allocated lockres
1428 * otherwise the lockres will be purged immediately */
1429 dlm_lockres_get(res);
1430 }
1431
1432 /* at this point we have allocated everything we need,
1433 * and we have a hashed lockres with an extra ref and
1434 * the proper res->state flags. */
1435 ret = 0;
1436 spin_lock(&res->spinlock);
1437 /* drop this either when master requery finds a different master
1438 * or when a lock is added by the recovery worker */
1439 dlm_lockres_grab_inflight_ref(dlm, res);
1440 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1441 /* migration cannot have an unknown master */
1442 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1443 mlog(0, "recovery has passed me a lockres with an "
1444 "unknown owner.. will need to requery: "
1445 "%.*s\n", mres->lockname_len, mres->lockname);
1446 } else {
1447 /* take a reference now to pin the lockres, drop it
1448 * when locks are added in the worker */
1449 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1450 }
1451 spin_unlock(&res->spinlock);
1452
1453 /* queue up work for dlm_mig_lockres_worker */
1454 dlm_grab(dlm); /* get an extra ref for the work item */
1455 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1456 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1457 item->u.ml.lockres = res; /* already have a ref */
1458 item->u.ml.real_master = real_master;
1459 spin_lock(&dlm->work_lock);
1460 list_add_tail(&item->list, &dlm->work_list);
1461 spin_unlock(&dlm->work_lock);
1462 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
1463
1464 leave:
1465 dlm_put(dlm);
1466 if (ret < 0) {
1467 if (buf)
1468 kfree(buf);
1469 if (item)
1470 kfree(item);
1471 }
1472
1473 mlog_exit(ret);
1474 return ret;
1475 }
1476
1477
1478 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1479 {
1480 struct dlm_ctxt *dlm = data;
1481 struct dlm_migratable_lockres *mres;
1482 int ret = 0;
1483 struct dlm_lock_resource *res;
1484 u8 real_master;
1485
1486 dlm = item->dlm;
1487 mres = (struct dlm_migratable_lockres *)data;
1488
1489 res = item->u.ml.lockres;
1490 real_master = item->u.ml.real_master;
1491
1492 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1493 /* this case is super-rare. only occurs if
1494 * node death happens during migration. */
1495 again:
1496 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1497 if (ret < 0) {
1498 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1499 ret);
1500 goto again;
1501 }
1502 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1503 mlog(0, "lockres %.*s not claimed. "
1504 "this node will take it.\n",
1505 res->lockname.len, res->lockname.name);
1506 } else {
1507 spin_lock(&res->spinlock);
1508 dlm_lockres_drop_inflight_ref(dlm, res);
1509 spin_unlock(&res->spinlock);
1510 mlog(0, "master needs to respond to sender "
1511 "that node %u still owns %.*s\n",
1512 real_master, res->lockname.len,
1513 res->lockname.name);
1514 /* cannot touch this lockres */
1515 goto leave;
1516 }
1517 }
1518
1519 ret = dlm_process_recovery_data(dlm, res, mres);
1520 if (ret < 0)
1521 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1522 else
1523 mlog(0, "dlm_process_recovery_data succeeded\n");
1524
1525 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1526 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1527 ret = dlm_finish_migration(dlm, res, mres->master);
1528 if (ret < 0)
1529 mlog_errno(ret);
1530 }
1531
1532 leave:
1533 kfree(data);
1534 mlog_exit(ret);
1535 }
1536
1537
1538
1539 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1540 struct dlm_lock_resource *res,
1541 u8 *real_master)
1542 {
1543 struct dlm_node_iter iter;
1544 int nodenum;
1545 int ret = 0;
1546
1547 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1548
1549 /* we only reach here if one of the two nodes in a
1550 * migration died while the migration was in progress.
1551 * at this point we need to requery the master. we
1552 * know that the new_master got as far as creating
1553 * an mle on at least one node, but we do not know
1554 * if any nodes had actually cleared the mle and set
1555 * the master to the new_master. the old master
1556 * is supposed to set the owner to UNKNOWN in the
1557 * event of a new_master death, so the only possible
1558 * responses that we can get from nodes here are
1559 * that the master is new_master, or that the master
1560 * is UNKNOWN.
1561 * if all nodes come back with UNKNOWN then we know
1562 * the lock needs remastering here.
1563 * if any node comes back with a valid master, check
1564 * to see if that master is the one that we are
1565 * recovering. if so, then the new_master died and
1566 * we need to remaster this lock. if not, then the
1567 * new_master survived and that node will respond to
1568 * other nodes about the owner.
1569 * if there is an owner, this node needs to dump this
1570 * lockres and alert the sender that this lockres
1571 * was rejected. */
1572 spin_lock(&dlm->spinlock);
1573 dlm_node_iter_init(dlm->domain_map, &iter);
1574 spin_unlock(&dlm->spinlock);
1575
1576 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1577 /* do not send to self */
1578 if (nodenum == dlm->node_num)
1579 continue;
1580 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1581 if (ret < 0) {
1582 mlog_errno(ret);
1583 if (!dlm_is_host_down(ret))
1584 BUG();
1585 /* host is down, so answer for that node would be
1586 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1587 }
1588 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1589 mlog(0, "lock master is %u\n", *real_master);
1590 break;
1591 }
1592 }
1593 return ret;
1594 }
1595
1596
1597 int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1598 u8 nodenum, u8 *real_master)
1599 {
1600 int ret = -EINVAL;
1601 struct dlm_master_requery req;
1602 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1603
1604 memset(&req, 0, sizeof(req));
1605 req.node_idx = dlm->node_num;
1606 req.namelen = res->lockname.len;
1607 memcpy(req.name, res->lockname.name, res->lockname.len);
1608
1609 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1610 &req, sizeof(req), nodenum, &status);
1611 /* XXX: negative status not handled properly here. */
1612 if (ret < 0)
1613 mlog_errno(ret);
1614 else {
1615 BUG_ON(status < 0);
1616 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1617 *real_master = (u8) (status & 0xff);
1618 mlog(0, "node %u responded to master requery with %u\n",
1619 nodenum, *real_master);
1620 ret = 0;
1621 }
1622 return ret;
1623 }
1624
1625
1626 /* this function cannot error, so unless the sending
1627 * or receiving of the message failed, the owner can
1628 * be trusted */
1629 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
1630 {
1631 struct dlm_ctxt *dlm = data;
1632 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1633 struct dlm_lock_resource *res = NULL;
1634 unsigned int hash;
1635 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1636 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1637
1638 if (!dlm_grab(dlm)) {
1639 /* since the domain has gone away on this
1640 * node, the proper response is UNKNOWN */
1641 return master;
1642 }
1643
1644 hash = dlm_lockid_hash(req->name, req->namelen);
1645
1646 spin_lock(&dlm->spinlock);
1647 res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
1648 if (res) {
1649 spin_lock(&res->spinlock);
1650 master = res->owner;
1651 if (master == dlm->node_num) {
1652 int ret = dlm_dispatch_assert_master(dlm, res,
1653 0, 0, flags);
1654 if (ret < 0) {
1655 mlog_errno(-ENOMEM);
1656 /* retry!? */
1657 BUG();
1658 }
1659 }
1660 spin_unlock(&res->spinlock);
1661 }
1662 spin_unlock(&dlm->spinlock);
1663
1664 dlm_put(dlm);
1665 return master;
1666 }
1667
1668 static inline struct list_head *
1669 dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1670 {
1671 struct list_head *ret;
1672 BUG_ON(list_num < 0);
1673 BUG_ON(list_num > 2);
1674 ret = &(res->granted);
1675 ret += list_num;
1676 return ret;
1677 }
1678 /* TODO: do ast flush business
1679 * TODO: do MIGRATING and RECOVERING spinning
1680 */
1681
1682 /*
1683 * NOTE about in-flight requests during migration:
1684 *
1685 * Before attempting the migrate, the master has marked the lockres as
1686 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1687 * requests either got queued before the MIGRATING flag got set, in which
1688 * case the lock data will reflect the change and a return message is on
1689 * the way, or the request failed to get in before MIGRATING got set. In
1690 * this case, the caller will be told to spin and wait for the MIGRATING
1691 * flag to be dropped, then recheck the master.
1692 * This holds true for the convert, cancel and unlock cases, and since lvb
1693 * updates are tied to these same messages, it applies to lvb updates as
1694 * well. For the lock case, there is no way a lock can be on the master
1695 * queue and not be on the secondary queue since the lock is always added
1696 * locally first. This means that the new target node will never be sent
1697 * a lock that he doesn't already have on the list.
1698 * In total, this means that the local lock is correct and should not be
1699 * updated to match the one sent by the master. Any messages sent back
1700 * from the master before the MIGRATING flag will bring the lock properly
1701 * up-to-date, and the change will be ordered properly for the waiter.
1702 * We will *not* attempt to modify the lock underneath the waiter.
1703 */
1704
1705 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1706 struct dlm_lock_resource *res,
1707 struct dlm_migratable_lockres *mres)
1708 {
1709 struct dlm_migratable_lock *ml;
1710 struct list_head *queue;
1711 struct dlm_lock *newlock = NULL;
1712 struct dlm_lockstatus *lksb = NULL;
1713 int ret = 0;
1714 int i, bad;
1715 struct list_head *iter;
1716 struct dlm_lock *lock = NULL;
1717 u8 from = O2NM_MAX_NODES;
1718 unsigned int added = 0;
1719
1720 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1721 for (i=0; i<mres->num_locks; i++) {
1722 ml = &(mres->ml[i]);
1723
1724 if (dlm_is_dummy_lock(dlm, ml, &from)) {
1725 /* placeholder, just need to set the refmap bit */
1726 BUG_ON(mres->num_locks != 1);
1727 mlog(0, "%s:%.*s: dummy lock for %u\n",
1728 dlm->name, mres->lockname_len, mres->lockname,
1729 from);
1730 spin_lock(&res->spinlock);
1731 dlm_lockres_set_refmap_bit(from, res);
1732 spin_unlock(&res->spinlock);
1733 added++;
1734 break;
1735 }
1736 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1737 newlock = NULL;
1738 lksb = NULL;
1739
1740 queue = dlm_list_num_to_pointer(res, ml->list);
1741
1742 /* if the lock is for the local node it needs to
1743 * be moved to the proper location within the queue.
1744 * do not allocate a new lock structure. */
1745 if (ml->node == dlm->node_num) {
1746 /* MIGRATION ONLY! */
1747 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1748
1749 spin_lock(&res->spinlock);
1750 list_for_each(iter, queue) {
1751 lock = list_entry (iter, struct dlm_lock, list);
1752 if (lock->ml.cookie != ml->cookie)
1753 lock = NULL;
1754 else
1755 break;
1756 }
1757
1758 /* lock is always created locally first, and
1759 * destroyed locally last. it must be on the list */
1760 if (!lock) {
1761 u64 c = ml->cookie;
1762 mlog(ML_ERROR, "could not find local lock "
1763 "with cookie %u:%llu!\n",
1764 dlm_get_lock_cookie_node(c),
1765 dlm_get_lock_cookie_seq(c));
1766 BUG();
1767 }
1768 BUG_ON(lock->ml.node != ml->node);
1769
1770 /* see NOTE above about why we do not update
1771 * to match the master here */
1772
1773 /* move the lock to its proper place */
1774 /* do not alter lock refcount. switching lists. */
1775 list_move_tail(&lock->list, queue);
1776 spin_unlock(&res->spinlock);
1777 added++;
1778
1779 mlog(0, "just reordered a local lock!\n");
1780 continue;
1781 }
1782
1783 /* lock is for another node. */
1784 newlock = dlm_new_lock(ml->type, ml->node,
1785 be64_to_cpu(ml->cookie), NULL);
1786 if (!newlock) {
1787 ret = -ENOMEM;
1788 goto leave;
1789 }
1790 lksb = newlock->lksb;
1791 dlm_lock_attach_lockres(newlock, res);
1792
1793 if (ml->convert_type != LKM_IVMODE) {
1794 BUG_ON(queue != &res->converting);
1795 newlock->ml.convert_type = ml->convert_type;
1796 }
1797 lksb->flags |= (ml->flags &
1798 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1799
1800 if (ml->type == LKM_NLMODE)
1801 goto skip_lvb;
1802
1803 if (!dlm_lvb_is_empty(mres->lvb)) {
1804 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1805 /* other node was trying to update
1806 * lvb when node died. recreate the
1807 * lksb with the updated lvb. */
1808 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1809 /* the lock resource lvb update must happen
1810 * NOW, before the spinlock is dropped.
1811 * we no longer wait for the AST to update
1812 * the lvb. */
1813 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1814 } else {
1815 /* otherwise, the node is sending its
1816 * most recent valid lvb info */
1817 BUG_ON(ml->type != LKM_EXMODE &&
1818 ml->type != LKM_PRMODE);
1819 if (!dlm_lvb_is_empty(res->lvb) &&
1820 (ml->type == LKM_EXMODE ||
1821 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1822 int i;
1823 mlog(ML_ERROR, "%s:%.*s: received bad "
1824 "lvb! type=%d\n", dlm->name,
1825 res->lockname.len,
1826 res->lockname.name, ml->type);
1827 printk("lockres lvb=[");
1828 for (i=0; i<DLM_LVB_LEN; i++)
1829 printk("%02x", res->lvb[i]);
1830 printk("]\nmigrated lvb=[");
1831 for (i=0; i<DLM_LVB_LEN; i++)
1832 printk("%02x", mres->lvb[i]);
1833 printk("]\n");
1834 dlm_print_one_lock_resource(res);
1835 BUG();
1836 }
1837 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1838 }
1839 }
1840 skip_lvb:
1841
1842 /* NOTE:
1843 * wrt lock queue ordering and recovery:
1844 * 1. order of locks on granted queue is
1845 * meaningless.
1846 * 2. order of locks on converting queue is
1847 * LOST with the node death. sorry charlie.
1848 * 3. order of locks on the blocked queue is
1849 * also LOST.
1850 * order of locks does not affect integrity, it
1851 * just means that a lock request may get pushed
1852 * back in line as a result of the node death.
1853 * also note that for a given node the lock order
1854 * for its secondary queue locks is preserved
1855 * relative to each other, but clearly *not*
1856 * preserved relative to locks from other nodes.
1857 */
1858 bad = 0;
1859 spin_lock(&res->spinlock);
1860 list_for_each_entry(lock, queue, list) {
1861 if (lock->ml.cookie == ml->cookie) {
1862 u64 c = lock->ml.cookie;
1863 mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1864 "exists on this lockres!\n", dlm->name,
1865 res->lockname.len, res->lockname.name,
1866 dlm_get_lock_cookie_node(c),
1867 dlm_get_lock_cookie_seq(c));
1868
1869 mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1870 "node=%u, cookie=%u:%llu, queue=%d\n",
1871 ml->type, ml->convert_type, ml->node,
1872 dlm_get_lock_cookie_node(ml->cookie),
1873 dlm_get_lock_cookie_seq(ml->cookie),
1874 ml->list);
1875
1876 __dlm_print_one_lock_resource(res);
1877 bad = 1;
1878 break;
1879 }
1880 }
1881 if (!bad) {
1882 dlm_lock_get(newlock);
1883 list_add_tail(&newlock->list, queue);
1884 mlog(0, "%s:%.*s: added lock for node %u, "
1885 "setting refmap bit\n", dlm->name,
1886 res->lockname.len, res->lockname.name, ml->node);
1887 dlm_lockres_set_refmap_bit(ml->node, res);
1888 added++;
1889 }
1890 spin_unlock(&res->spinlock);
1891 }
1892 mlog(0, "done running all the locks\n");
1893
1894 leave:
1895 /* balance the ref taken when the work was queued */
1896 if (added > 0) {
1897 spin_lock(&res->spinlock);
1898 dlm_lockres_drop_inflight_ref(dlm, res);
1899 spin_unlock(&res->spinlock);
1900 }
1901
1902 if (ret < 0) {
1903 mlog_errno(ret);
1904 if (newlock)
1905 dlm_lock_put(newlock);
1906 }
1907
1908 mlog_exit(ret);
1909 return ret;
1910 }
1911
1912 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1913 struct dlm_lock_resource *res)
1914 {
1915 int i;
1916 struct list_head *queue, *iter, *iter2;
1917 struct dlm_lock *lock;
1918
1919 res->state |= DLM_LOCK_RES_RECOVERING;
1920 if (!list_empty(&res->recovering)) {
1921 mlog(0,
1922 "Recovering res %s:%.*s, is already on recovery list!\n",
1923 dlm->name, res->lockname.len, res->lockname.name);
1924 list_del_init(&res->recovering);
1925 }
1926 /* We need to hold a reference while on the recovery list */
1927 dlm_lockres_get(res);
1928 list_add_tail(&res->recovering, &dlm->reco.resources);
1929
1930 /* find any pending locks and put them back on proper list */
1931 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1932 queue = dlm_list_idx_to_ptr(res, i);
1933 list_for_each_safe(iter, iter2, queue) {
1934 lock = list_entry (iter, struct dlm_lock, list);
1935 dlm_lock_get(lock);
1936 if (lock->convert_pending) {
1937 /* move converting lock back to granted */
1938 BUG_ON(i != DLM_CONVERTING_LIST);
1939 mlog(0, "node died with convert pending "
1940 "on %.*s. move back to granted list.\n",
1941 res->lockname.len, res->lockname.name);
1942 dlm_revert_pending_convert(res, lock);
1943 lock->convert_pending = 0;
1944 } else if (lock->lock_pending) {
1945 /* remove pending lock requests completely */
1946 BUG_ON(i != DLM_BLOCKED_LIST);
1947 mlog(0, "node died with lock pending "
1948 "on %.*s. remove from blocked list and skip.\n",
1949 res->lockname.len, res->lockname.name);
1950 /* lock will be floating until ref in
1951 * dlmlock_remote is freed after the network
1952 * call returns. ok for it to not be on any
1953 * list since no ast can be called
1954 * (the master is dead). */
1955 dlm_revert_pending_lock(res, lock);
1956 lock->lock_pending = 0;
1957 } else if (lock->unlock_pending) {
1958 /* if an unlock was in progress, treat as
1959 * if this had completed successfully
1960 * before sending this lock state to the
1961 * new master. note that the dlm_unlock
1962 * call is still responsible for calling
1963 * the unlockast. that will happen after
1964 * the network call times out. for now,
1965 * just move lists to prepare the new
1966 * recovery master. */
1967 BUG_ON(i != DLM_GRANTED_LIST);
1968 mlog(0, "node died with unlock pending "
1969 "on %.*s. remove from blocked list and skip.\n",
1970 res->lockname.len, res->lockname.name);
1971 dlm_commit_pending_unlock(res, lock);
1972 lock->unlock_pending = 0;
1973 } else if (lock->cancel_pending) {
1974 /* if a cancel was in progress, treat as
1975 * if this had completed successfully
1976 * before sending this lock state to the
1977 * new master */
1978 BUG_ON(i != DLM_CONVERTING_LIST);
1979 mlog(0, "node died with cancel pending "
1980 "on %.*s. move back to granted list.\n",
1981 res->lockname.len, res->lockname.name);
1982 dlm_commit_pending_cancel(res, lock);
1983 lock->cancel_pending = 0;
1984 }
1985 dlm_lock_put(lock);
1986 }
1987 }
1988 }
1989
1990
1991
1992 /* removes all recovered locks from the recovery list.
1993 * sets the res->owner to the new master.
1994 * unsets the RECOVERY flag and wakes waiters. */
1995 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1996 u8 dead_node, u8 new_master)
1997 {
1998 int i;
1999 struct list_head *iter, *iter2;
2000 struct hlist_node *hash_iter;
2001 struct hlist_head *bucket;
2002
2003 struct dlm_lock_resource *res;
2004
2005 mlog_entry_void();
2006
2007 assert_spin_locked(&dlm->spinlock);
2008
2009 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
2010 res = list_entry (iter, struct dlm_lock_resource, recovering);
2011 if (res->owner == dead_node) {
2012 list_del_init(&res->recovering);
2013 spin_lock(&res->spinlock);
2014 /* new_master has our reference from
2015 * the lock state sent during recovery */
2016 dlm_change_lockres_owner(dlm, res, new_master);
2017 res->state &= ~DLM_LOCK_RES_RECOVERING;
2018 if (__dlm_lockres_has_locks(res))
2019 __dlm_dirty_lockres(dlm, res);
2020 spin_unlock(&res->spinlock);
2021 wake_up(&res->wq);
2022 dlm_lockres_put(res);
2023 }
2024 }
2025
2026 /* this will become unnecessary eventually, but
2027 * for now we need to run the whole hash, clear
2028 * the RECOVERING state and set the owner
2029 * if necessary */
2030 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2031 bucket = dlm_lockres_hash(dlm, i);
2032 hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
2033 if (res->state & DLM_LOCK_RES_RECOVERING) {
2034 if (res->owner == dead_node) {
2035 mlog(0, "(this=%u) res %.*s owner=%u "
2036 "was not on recovering list, but "
2037 "clearing state anyway\n",
2038 dlm->node_num, res->lockname.len,
2039 res->lockname.name, new_master);
2040 } else if (res->owner == dlm->node_num) {
2041 mlog(0, "(this=%u) res %.*s owner=%u "
2042 "was not on recovering list, "
2043 "owner is THIS node, clearing\n",
2044 dlm->node_num, res->lockname.len,
2045 res->lockname.name, new_master);
2046 } else
2047 continue;
2048
2049 if (!list_empty(&res->recovering)) {
2050 mlog(0, "%s:%.*s: lockres was "
2051 "marked RECOVERING, owner=%u\n",
2052 dlm->name, res->lockname.len,
2053 res->lockname.name, res->owner);
2054 list_del_init(&res->recovering);
2055 dlm_lockres_put(res);
2056 }
2057 spin_lock(&res->spinlock);
2058 /* new_master has our reference from
2059 * the lock state sent during recovery */
2060 dlm_change_lockres_owner(dlm, res, new_master);
2061 res->state &= ~DLM_LOCK_RES_RECOVERING;
2062 if (__dlm_lockres_has_locks(res))
2063 __dlm_dirty_lockres(dlm, res);
2064 spin_unlock(&res->spinlock);
2065 wake_up(&res->wq);
2066 }
2067 }
2068 }
2069 }
2070
2071 static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
2072 {
2073 if (local) {
2074 if (lock->ml.type != LKM_EXMODE &&
2075 lock->ml.type != LKM_PRMODE)
2076 return 1;
2077 } else if (lock->ml.type == LKM_EXMODE)
2078 return 1;
2079 return 0;
2080 }
2081
2082 static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
2083 struct dlm_lock_resource *res, u8 dead_node)
2084 {
2085 struct list_head *iter, *queue;
2086 struct dlm_lock *lock;
2087 int blank_lvb = 0, local = 0;
2088 int i;
2089 u8 search_node;
2090
2091 assert_spin_locked(&dlm->spinlock);
2092 assert_spin_locked(&res->spinlock);
2093
2094 if (res->owner == dlm->node_num)
2095 /* if this node owned the lockres, and if the dead node
2096 * had an EX when he died, blank out the lvb */
2097 search_node = dead_node;
2098 else {
2099 /* if this is a secondary lockres, and we had no EX or PR
2100 * locks granted, we can no longer trust the lvb */
2101 search_node = dlm->node_num;
2102 local = 1; /* check local state for valid lvb */
2103 }
2104
2105 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
2106 queue = dlm_list_idx_to_ptr(res, i);
2107 list_for_each(iter, queue) {
2108 lock = list_entry (iter, struct dlm_lock, list);
2109 if (lock->ml.node == search_node) {
2110 if (dlm_lvb_needs_invalidation(lock, local)) {
2111 /* zero the lksb lvb and lockres lvb */
2112 blank_lvb = 1;
2113 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
2114 }
2115 }
2116 }
2117 }
2118
2119 if (blank_lvb) {
2120 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2121 res->lockname.len, res->lockname.name, dead_node);
2122 memset(res->lvb, 0, DLM_LVB_LEN);
2123 }
2124 }
2125
2126 static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
2127 struct dlm_lock_resource *res, u8 dead_node)
2128 {
2129 struct list_head *iter, *tmpiter;
2130 struct dlm_lock *lock;
2131 unsigned int freed = 0;
2132
2133 /* this node is the lockres master:
2134 * 1) remove any stale locks for the dead node
2135 * 2) if the dead node had an EX when he died, blank out the lvb
2136 */
2137 assert_spin_locked(&dlm->spinlock);
2138 assert_spin_locked(&res->spinlock);
2139
2140 /* TODO: check pending_asts, pending_basts here */
2141 list_for_each_safe(iter, tmpiter, &res->granted) {
2142 lock = list_entry (iter, struct dlm_lock, list);
2143 if (lock->ml.node == dead_node) {
2144 list_del_init(&lock->list);
2145 dlm_lock_put(lock);
2146 freed++;
2147 }
2148 }
2149 list_for_each_safe(iter, tmpiter, &res->converting) {
2150 lock = list_entry (iter, struct dlm_lock, list);
2151 if (lock->ml.node == dead_node) {
2152 list_del_init(&lock->list);
2153 dlm_lock_put(lock);
2154 freed++;
2155 }
2156 }
2157 list_for_each_safe(iter, tmpiter, &res->blocked) {
2158 lock = list_entry (iter, struct dlm_lock, list);
2159 if (lock->ml.node == dead_node) {
2160 list_del_init(&lock->list);
2161 dlm_lock_put(lock);
2162 freed++;
2163 }
2164 }
2165
2166 if (freed) {
2167 mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2168 "dropping ref from lockres\n", dlm->name,
2169 res->lockname.len, res->lockname.name, freed, dead_node);
2170 BUG_ON(!test_bit(dead_node, res->refmap));
2171 dlm_lockres_clear_refmap_bit(dead_node, res);
2172 } else if (test_bit(dead_node, res->refmap)) {
2173 mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2174 "no locks and had not purged before dying\n", dlm->name,
2175 res->lockname.len, res->lockname.name, dead_node);
2176 dlm_lockres_clear_refmap_bit(dead_node, res);
2177 }
2178
2179 /* do not kick thread yet */
2180 __dlm_dirty_lockres(dlm, res);
2181 }
2182
2183 /* if this node is the recovery master, and there are no
2184 * locks for a given lockres owned by this node that are in
2185 * either PR or EX mode, zero out the lvb before requesting.
2186 *
2187 */
2188
2189
2190 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2191 {
2192 struct hlist_node *iter;
2193 struct dlm_lock_resource *res;
2194 int i;
2195 struct hlist_head *bucket;
2196 struct dlm_lock *lock;
2197
2198
2199 /* purge any stale mles */
2200 dlm_clean_master_list(dlm, dead_node);
2201
2202 /*
2203 * now clean up all lock resources. there are two rules:
2204 *
2205 * 1) if the dead node was the master, move the lockres
2206 * to the recovering list. set the RECOVERING flag.
2207 * this lockres needs to be cleaned up before it can
2208 * be used further.
2209 *
2210 * 2) if this node was the master, remove all locks from
2211 * each of the lockres queues that were owned by the
2212 * dead node. once recovery finishes, the dlm thread
2213 * can be kicked again to see if any ASTs or BASTs
2214 * need to be fired as a result.
2215 */
2216 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2217 bucket = dlm_lockres_hash(dlm, i);
2218 hlist_for_each_entry(res, iter, bucket, hash_node) {
2219 /* always prune any $RECOVERY entries for dead nodes,
2220 * otherwise hangs can occur during later recovery */
2221 if (dlm_is_recovery_lock(res->lockname.name,
2222 res->lockname.len)) {
2223 spin_lock(&res->spinlock);
2224 list_for_each_entry(lock, &res->granted, list) {
2225 if (lock->ml.node == dead_node) {
2226 mlog(0, "AHA! there was "
2227 "a $RECOVERY lock for dead "
2228 "node %u (%s)!\n",
2229 dead_node, dlm->name);
2230 list_del_init(&lock->list);
2231 dlm_lock_put(lock);
2232 break;
2233 }
2234 }
2235 spin_unlock(&res->spinlock);
2236 continue;
2237 }
2238 spin_lock(&res->spinlock);
2239 /* zero the lvb if necessary */
2240 dlm_revalidate_lvb(dlm, res, dead_node);
2241 if (res->owner == dead_node) {
2242 if (res->state & DLM_LOCK_RES_DROPPING_REF)
2243 mlog(0, "%s:%.*s: owned by "
2244 "dead node %u, this node was "
2245 "dropping its ref when it died. "
2246 "continue, dropping the flag.\n",
2247 dlm->name, res->lockname.len,
2248 res->lockname.name, dead_node);
2249
2250 /* the wake_up for this will happen when the
2251 * RECOVERING flag is dropped later */
2252 res->state &= ~DLM_LOCK_RES_DROPPING_REF;
2253
2254 dlm_move_lockres_to_recovery_list(dlm, res);
2255 } else if (res->owner == dlm->node_num) {
2256 dlm_free_dead_locks(dlm, res, dead_node);
2257 __dlm_lockres_calc_usage(dlm, res);
2258 }
2259 spin_unlock(&res->spinlock);
2260 }
2261 }
2262
2263 }
2264
2265 static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2266 {
2267 assert_spin_locked(&dlm->spinlock);
2268
2269 if (dlm->reco.new_master == idx) {
2270 mlog(0, "%s: recovery master %d just died\n",
2271 dlm->name, idx);
2272 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2273 /* finalize1 was reached, so it is safe to clear
2274 * the new_master and dead_node. that recovery
2275 * is complete. */
2276 mlog(0, "%s: dead master %d had reached "
2277 "finalize1 state, clearing\n", dlm->name, idx);
2278 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2279 __dlm_reset_recovery(dlm);
2280 }
2281 }
2282
2283 /* check to see if the node is already considered dead */
2284 if (!test_bit(idx, dlm->live_nodes_map)) {
2285 mlog(0, "for domain %s, node %d is already dead. "
2286 "another node likely did recovery already.\n",
2287 dlm->name, idx);
2288 return;
2289 }
2290
2291 /* check to see if we do not care about this node */
2292 if (!test_bit(idx, dlm->domain_map)) {
2293 /* This also catches the case that we get a node down
2294 * but haven't joined the domain yet. */
2295 mlog(0, "node %u already removed from domain!\n", idx);
2296 return;
2297 }
2298
2299 clear_bit(idx, dlm->live_nodes_map);
2300
2301 /* Clean up join state on node death. */
2302 if (dlm->joining_node == idx) {
2303 mlog(0, "Clearing join state for node %u\n", idx);
2304 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2305 }
2306
2307 /* make sure local cleanup occurs before the heartbeat events */
2308 if (!test_bit(idx, dlm->recovery_map))
2309 dlm_do_local_recovery_cleanup(dlm, idx);
2310
2311 /* notify anything attached to the heartbeat events */
2312 dlm_hb_event_notify_attached(dlm, idx, 0);
2313
2314 mlog(0, "node %u being removed from domain map!\n", idx);
2315 clear_bit(idx, dlm->domain_map);
2316 /* wake up migration waiters if a node goes down.
2317 * perhaps later we can genericize this for other waiters. */
2318 wake_up(&dlm->migration_wq);
2319
2320 if (test_bit(idx, dlm->recovery_map))
2321 mlog(0, "domain %s, node %u already added "
2322 "to recovery map!\n", dlm->name, idx);
2323 else
2324 set_bit(idx, dlm->recovery_map);
2325 }
2326
2327 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2328 {
2329 struct dlm_ctxt *dlm = data;
2330
2331 if (!dlm_grab(dlm))
2332 return;
2333
2334 spin_lock(&dlm->spinlock);
2335 __dlm_hb_node_down(dlm, idx);
2336 spin_unlock(&dlm->spinlock);
2337
2338 dlm_put(dlm);
2339 }
2340
2341 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2342 {
2343 struct dlm_ctxt *dlm = data;
2344
2345 if (!dlm_grab(dlm))
2346 return;
2347
2348 spin_lock(&dlm->spinlock);
2349 set_bit(idx, dlm->live_nodes_map);
2350 /* do NOT notify mle attached to the heartbeat events.
2351 * new nodes are not interesting in mastery until joined. */
2352 spin_unlock(&dlm->spinlock);
2353
2354 dlm_put(dlm);
2355 }
2356
2357 static void dlm_reco_ast(void *astdata)
2358 {
2359 struct dlm_ctxt *dlm = astdata;
2360 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2361 dlm->node_num, dlm->name);
2362 }
2363 static void dlm_reco_bast(void *astdata, int blocked_type)
2364 {
2365 struct dlm_ctxt *dlm = astdata;
2366 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2367 dlm->node_num, dlm->name);
2368 }
2369 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2370 {
2371 mlog(0, "unlockast for recovery lock fired!\n");
2372 }
2373
2374 /*
2375 * dlm_pick_recovery_master will continually attempt to use
2376 * dlmlock() on the special "$RECOVERY" lockres with the
2377 * LKM_NOQUEUE flag to get an EX. every thread that enters
2378 * this function on each node racing to become the recovery
2379 * master will not stop attempting this until either:
2380 * a) this node gets the EX (and becomes the recovery master),
2381 * or b) dlm->reco.new_master gets set to some nodenum
2382 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2383 * so each time a recovery master is needed, the entire cluster
2384 * will sync at this point. if the new master dies, that will
2385 * be detected in dlm_do_recovery */
2386 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2387 {
2388 enum dlm_status ret;
2389 struct dlm_lockstatus lksb;
2390 int status = -EINVAL;
2391
2392 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2393 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2394 again:
2395 memset(&lksb, 0, sizeof(lksb));
2396
2397 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2398 DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
2399 dlm_reco_ast, dlm, dlm_reco_bast);
2400
2401 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2402 dlm->name, ret, lksb.status);
2403
2404 if (ret == DLM_NORMAL) {
2405 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2406 dlm->name, dlm->node_num);
2407
2408 /* got the EX lock. check to see if another node
2409 * just became the reco master */
2410 if (dlm_reco_master_ready(dlm)) {
2411 mlog(0, "%s: got reco EX lock, but %u will "
2412 "do the recovery\n", dlm->name,
2413 dlm->reco.new_master);
2414 status = -EEXIST;
2415 } else {
2416 status = 0;
2417
2418 /* see if recovery was already finished elsewhere */
2419 spin_lock(&dlm->spinlock);
2420 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2421 status = -EINVAL;
2422 mlog(0, "%s: got reco EX lock, but "
2423 "node got recovered already\n", dlm->name);
2424 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2425 mlog(ML_ERROR, "%s: new master is %u "
2426 "but no dead node!\n",
2427 dlm->name, dlm->reco.new_master);
2428 BUG();
2429 }
2430 }
2431 spin_unlock(&dlm->spinlock);
2432 }
2433
2434 /* if this node has actually become the recovery master,
2435 * set the master and send the messages to begin recovery */
2436 if (!status) {
2437 mlog(0, "%s: dead=%u, this=%u, sending "
2438 "begin_reco now\n", dlm->name,
2439 dlm->reco.dead_node, dlm->node_num);
2440 status = dlm_send_begin_reco_message(dlm,
2441 dlm->reco.dead_node);
2442 /* this always succeeds */
2443 BUG_ON(status);
2444
2445 /* set the new_master to this node */
2446 spin_lock(&dlm->spinlock);
2447 dlm_set_reco_master(dlm, dlm->node_num);
2448 spin_unlock(&dlm->spinlock);
2449 }
2450
2451 /* recovery lock is a special case. ast will not get fired,
2452 * so just go ahead and unlock it. */
2453 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2454 if (ret == DLM_DENIED) {
2455 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2456 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2457 }
2458 if (ret != DLM_NORMAL) {
2459 /* this would really suck. this could only happen
2460 * if there was a network error during the unlock
2461 * because of node death. this means the unlock
2462 * is actually "done" and the lock structure is
2463 * even freed. we can continue, but only
2464 * because this specific lock name is special. */
2465 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
2466 }
2467 } else if (ret == DLM_NOTQUEUED) {
2468 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2469 dlm->name, dlm->node_num);
2470 /* another node is master. wait on
2471 * reco.new_master != O2NM_INVALID_NODE_NUM
2472 * for at most one second */
2473 wait_event_timeout(dlm->dlm_reco_thread_wq,
2474 dlm_reco_master_ready(dlm),
2475 msecs_to_jiffies(1000));
2476 if (!dlm_reco_master_ready(dlm)) {
2477 mlog(0, "%s: reco master taking awhile\n",
2478 dlm->name);
2479 goto again;
2480 }
2481 /* another node has informed this one that it is reco master */
2482 mlog(0, "%s: reco master %u is ready to recover %u\n",
2483 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
2484 status = -EEXIST;
2485 } else if (ret == DLM_RECOVERING) {
2486 mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2487 dlm->name, dlm->node_num);
2488 goto again;
2489 } else {
2490 struct dlm_lock_resource *res;
2491
2492 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2493 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2494 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2495 dlm_errname(lksb.status));
2496 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2497 DLM_RECOVERY_LOCK_NAME_LEN);
2498 if (res) {
2499 dlm_print_one_lock_resource(res);
2500 dlm_lockres_put(res);
2501 } else {
2502 mlog(ML_ERROR, "recovery lock not found\n");
2503 }
2504 BUG();
2505 }
2506
2507 return status;
2508 }
2509
2510 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2511 {
2512 struct dlm_begin_reco br;
2513 int ret = 0;
2514 struct dlm_node_iter iter;
2515 int nodenum;
2516 int status;
2517
2518 mlog_entry("%u\n", dead_node);
2519
2520 mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
2521
2522 spin_lock(&dlm->spinlock);
2523 dlm_node_iter_init(dlm->domain_map, &iter);
2524 spin_unlock(&dlm->spinlock);
2525
2526 clear_bit(dead_node, iter.node_map);
2527
2528 memset(&br, 0, sizeof(br));
2529 br.node_idx = dlm->node_num;
2530 br.dead_node = dead_node;
2531
2532 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2533 ret = 0;
2534 if (nodenum == dead_node) {
2535 mlog(0, "not sending begin reco to dead node "
2536 "%u\n", dead_node);
2537 continue;
2538 }
2539 if (nodenum == dlm->node_num) {
2540 mlog(0, "not sending begin reco to self\n");
2541 continue;
2542 }
2543 retry:
2544 ret = -EINVAL;
2545 mlog(0, "attempting to send begin reco msg to %d\n",
2546 nodenum);
2547 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2548 &br, sizeof(br), nodenum, &status);
2549 /* negative status is handled ok by caller here */
2550 if (ret >= 0)
2551 ret = status;
2552 if (dlm_is_host_down(ret)) {
2553 /* node is down. not involved in recovery
2554 * so just keep going */
2555 mlog(0, "%s: node %u was down when sending "
2556 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2557 ret = 0;
2558 }
2559 if (ret < 0) {
2560 struct dlm_lock_resource *res;
2561 /* this is now a serious problem, possibly ENOMEM
2562 * in the network stack. must retry */
2563 mlog_errno(ret);
2564 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2565 " returned %d\n", dlm->name, nodenum, ret);
2566 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2567 DLM_RECOVERY_LOCK_NAME_LEN);
2568 if (res) {
2569 dlm_print_one_lock_resource(res);
2570 dlm_lockres_put(res);
2571 } else {
2572 mlog(ML_ERROR, "recovery lock not found\n");
2573 }
2574 /* sleep for a bit in hopes that we can avoid
2575 * another ENOMEM */
2576 msleep(100);
2577 goto retry;
2578 } else if (ret == EAGAIN) {
2579 mlog(0, "%s: trying to start recovery of node "
2580 "%u, but node %u is waiting for last recovery "
2581 "to complete, backoff for a bit\n", dlm->name,
2582 dead_node, nodenum);
2583 /* TODO Look into replacing msleep with cond_resched() */
2584 msleep(100);
2585 goto retry;
2586 }
2587 }
2588
2589 return ret;
2590 }
2591
2592 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2593 {
2594 struct dlm_ctxt *dlm = data;
2595 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2596
2597 /* ok to return 0, domain has gone away */
2598 if (!dlm_grab(dlm))
2599 return 0;
2600
2601 spin_lock(&dlm->spinlock);
2602 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2603 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2604 "but this node is in finalize state, waiting on finalize2\n",
2605 dlm->name, br->node_idx, br->dead_node,
2606 dlm->reco.dead_node, dlm->reco.new_master);
2607 spin_unlock(&dlm->spinlock);
2608 return EAGAIN;
2609 }
2610 spin_unlock(&dlm->spinlock);
2611
2612 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2613 dlm->name, br->node_idx, br->dead_node,
2614 dlm->reco.dead_node, dlm->reco.new_master);
2615
2616 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2617
2618 spin_lock(&dlm->spinlock);
2619 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2620 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2621 mlog(0, "%s: new_master %u died, changing "
2622 "to %u\n", dlm->name, dlm->reco.new_master,
2623 br->node_idx);
2624 } else {
2625 mlog(0, "%s: new_master %u NOT DEAD, changing "
2626 "to %u\n", dlm->name, dlm->reco.new_master,
2627 br->node_idx);
2628 /* may not have seen the new master as dead yet */
2629 }
2630 }
2631 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2632 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2633 "node %u changing it to %u\n", dlm->name,
2634 dlm->reco.dead_node, br->node_idx, br->dead_node);
2635 }
2636 dlm_set_reco_master(dlm, br->node_idx);
2637 dlm_set_reco_dead_node(dlm, br->dead_node);
2638 if (!test_bit(br->dead_node, dlm->recovery_map)) {
2639 mlog(0, "recovery master %u sees %u as dead, but this "
2640 "node has not yet. marking %u as dead\n",
2641 br->node_idx, br->dead_node, br->dead_node);
2642 if (!test_bit(br->dead_node, dlm->domain_map) ||
2643 !test_bit(br->dead_node, dlm->live_nodes_map))
2644 mlog(0, "%u not in domain/live_nodes map "
2645 "so setting it in reco map manually\n",
2646 br->dead_node);
2647 /* force the recovery cleanup in __dlm_hb_node_down
2648 * both of these will be cleared in a moment */
2649 set_bit(br->dead_node, dlm->domain_map);
2650 set_bit(br->dead_node, dlm->live_nodes_map);
2651 __dlm_hb_node_down(dlm, br->dead_node);
2652 }
2653 spin_unlock(&dlm->spinlock);
2654
2655 dlm_kick_recovery_thread(dlm);
2656
2657 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2658 dlm->name, br->node_idx, br->dead_node,
2659 dlm->reco.dead_node, dlm->reco.new_master);
2660
2661 dlm_put(dlm);
2662 return 0;
2663 }
2664
2665 #define DLM_FINALIZE_STAGE2 0x01
2666 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2667 {
2668 int ret = 0;
2669 struct dlm_finalize_reco fr;
2670 struct dlm_node_iter iter;
2671 int nodenum;
2672 int status;
2673 int stage = 1;
2674
2675 mlog(0, "finishing recovery for node %s:%u, "
2676 "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
2677
2678 spin_lock(&dlm->spinlock);
2679 dlm_node_iter_init(dlm->domain_map, &iter);
2680 spin_unlock(&dlm->spinlock);
2681
2682 stage2:
2683 memset(&fr, 0, sizeof(fr));
2684 fr.node_idx = dlm->node_num;
2685 fr.dead_node = dlm->reco.dead_node;
2686 if (stage == 2)
2687 fr.flags |= DLM_FINALIZE_STAGE2;
2688
2689 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2690 if (nodenum == dlm->node_num)
2691 continue;
2692 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2693 &fr, sizeof(fr), nodenum, &status);
2694 if (ret >= 0)
2695 ret = status;
2696 if (ret < 0) {
2697 mlog_errno(ret);
2698 if (dlm_is_host_down(ret)) {
2699 /* this has no effect on this recovery
2700 * session, so set the status to zero to
2701 * finish out the last recovery */
2702 mlog(ML_ERROR, "node %u went down after this "
2703 "node finished recovery.\n", nodenum);
2704 ret = 0;
2705 continue;
2706 }
2707 break;
2708 }
2709 }
2710 if (stage == 1) {
2711 /* reset the node_iter back to the top and send finalize2 */
2712 iter.curnode = -1;
2713 stage = 2;
2714 goto stage2;
2715 }
2716
2717 return ret;
2718 }
2719
2720 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2721 {
2722 struct dlm_ctxt *dlm = data;
2723 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2724 int stage = 1;
2725
2726 /* ok to return 0, domain has gone away */
2727 if (!dlm_grab(dlm))
2728 return 0;
2729
2730 if (fr->flags & DLM_FINALIZE_STAGE2)
2731 stage = 2;
2732
2733 mlog(0, "%s: node %u finalizing recovery stage%d of "
2734 "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2735 fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2736
2737 spin_lock(&dlm->spinlock);
2738
2739 if (dlm->reco.new_master != fr->node_idx) {
2740 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2741 "%u is supposed to be the new master, dead=%u\n",
2742 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2743 BUG();
2744 }
2745 if (dlm->reco.dead_node != fr->dead_node) {
2746 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2747 "node %u, but node %u is supposed to be dead\n",
2748 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2749 BUG();
2750 }
2751
2752 switch (stage) {
2753 case 1:
2754 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2755 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2756 mlog(ML_ERROR, "%s: received finalize1 from "
2757 "new master %u for dead node %u, but "
2758 "this node has already received it!\n",
2759 dlm->name, fr->node_idx, fr->dead_node);
2760 dlm_print_reco_node_status(dlm);
2761 BUG();
2762 }
2763 dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2764 spin_unlock(&dlm->spinlock);
2765 break;
2766 case 2:
2767 if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2768 mlog(ML_ERROR, "%s: received finalize2 from "
2769 "new master %u for dead node %u, but "
2770 "this node did not have finalize1!\n",
2771 dlm->name, fr->node_idx, fr->dead_node);
2772 dlm_print_reco_node_status(dlm);
2773 BUG();
2774 }
2775 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2776 spin_unlock(&dlm->spinlock);
2777 dlm_reset_recovery(dlm);
2778 dlm_kick_recovery_thread(dlm);
2779 break;
2780 default:
2781 BUG();
2782 }
2783
2784 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2785 dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2786
2787 dlm_put(dlm);
2788 return 0;
2789 }
This page took 0.094865 seconds and 5 git commands to generate.