blob: de68e498bfadfa875b291c68d6ec1c9858f4f67a [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- 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>
Adrian Bunkb4c7f532006-01-14 20:55:10 +010042#include <linux/delay.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080043
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
56static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
57
58static int dlm_recovery_thread(void *data);
59void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
60int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
Kurt Hackelc03872f2006-03-06 14:08:49 -080061void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080062static int dlm_do_recovery(struct dlm_ctxt *dlm);
63
64static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
65static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
66static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
67static int dlm_request_all_locks(struct dlm_ctxt *dlm,
68 u8 request_from, u8 dead_node);
69static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
70
71static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
72static 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);
76static 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);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080081static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
82 struct dlm_lock_resource *res,
83 struct dlm_migratable_lockres *mres);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080084static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
85static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
86 u8 dead_node, u8 send_to);
87static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
88static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
89 struct list_head *list, u8 dead_node);
90static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
91 u8 dead_node, u8 new_master);
92static void dlm_reco_ast(void *astdata);
93static void dlm_reco_bast(void *astdata, int blocked_type);
94static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
95static void dlm_request_all_locks_worker(struct dlm_work_item *item,
96 void *data);
97static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
98
99static u64 dlm_get_next_mig_cookie(void);
100
101static spinlock_t dlm_reco_state_lock = SPIN_LOCK_UNLOCKED;
102static spinlock_t dlm_mig_cookie_lock = SPIN_LOCK_UNLOCKED;
103static u64 dlm_mig_cookie = 1;
104
105static u64 dlm_get_next_mig_cookie(void)
106{
107 u64 c;
108 spin_lock(&dlm_mig_cookie_lock);
109 c = dlm_mig_cookie;
110 if (dlm_mig_cookie == (~0ULL))
111 dlm_mig_cookie = 1;
112 else
113 dlm_mig_cookie++;
114 spin_unlock(&dlm_mig_cookie_lock);
115 return c;
116}
117
Kurt Hackelab27eb62006-04-27 18:03:49 -0700118static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
119 u8 dead_node)
120{
121 assert_spin_locked(&dlm->spinlock);
122 if (dlm->reco.dead_node != dead_node)
123 mlog(0, "%s: changing dead_node from %u to %u\n",
124 dlm->name, dlm->reco.dead_node, dead_node);
125 dlm->reco.dead_node = dead_node;
126}
127
128static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
129 u8 master)
130{
131 assert_spin_locked(&dlm->spinlock);
132 mlog(0, "%s: changing new_master from %u to %u\n",
133 dlm->name, dlm->reco.new_master, master);
134 dlm->reco.new_master = master;
135}
136
Kurt Hackel466d1a42006-05-01 11:11:13 -0700137static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800138{
Kurt Hackel466d1a42006-05-01 11:11:13 -0700139 assert_spin_locked(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800140 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
Kurt Hackelab27eb62006-04-27 18:03:49 -0700141 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
142 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel466d1a42006-05-01 11:11:13 -0700143}
144
145static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
146{
147 spin_lock(&dlm->spinlock);
148 __dlm_reset_recovery(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800149 spin_unlock(&dlm->spinlock);
150}
151
152/* Worker function used during recovery. */
153void dlm_dispatch_work(void *data)
154{
155 struct dlm_ctxt *dlm = (struct dlm_ctxt *)data;
156 LIST_HEAD(tmp_list);
157 struct list_head *iter, *iter2;
158 struct dlm_work_item *item;
159 dlm_workfunc_t *workfunc;
160
161 spin_lock(&dlm->work_lock);
162 list_splice_init(&dlm->work_list, &tmp_list);
163 spin_unlock(&dlm->work_lock);
164
165 list_for_each_safe(iter, iter2, &tmp_list) {
166 item = list_entry(iter, struct dlm_work_item, list);
167 workfunc = item->func;
168 list_del_init(&item->list);
169
170 /* already have ref on dlm to avoid having
171 * it disappear. just double-check. */
172 BUG_ON(item->dlm != dlm);
173
174 /* this is allowed to sleep and
175 * call network stuff */
176 workfunc(item, item->data);
177
178 dlm_put(dlm);
179 kfree(item);
180 }
181}
182
183/*
184 * RECOVERY THREAD
185 */
186
Kurt Hackelc03872f2006-03-06 14:08:49 -0800187void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800188{
189 /* wake the recovery thread
190 * this will wake the reco thread in one of three places
191 * 1) sleeping with no recovery happening
192 * 2) sleeping with recovery mastered elsewhere
193 * 3) recovery mastered here, waiting on reco data */
194
195 wake_up(&dlm->dlm_reco_thread_wq);
196}
197
198/* Launch the recovery thread */
199int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
200{
201 mlog(0, "starting dlm recovery thread...\n");
202
203 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
204 "dlm_reco_thread");
205 if (IS_ERR(dlm->dlm_reco_thread_task)) {
206 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
207 dlm->dlm_reco_thread_task = NULL;
208 return -EINVAL;
209 }
210
211 return 0;
212}
213
214void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
215{
216 if (dlm->dlm_reco_thread_task) {
217 mlog(0, "waiting for dlm recovery thread to exit\n");
218 kthread_stop(dlm->dlm_reco_thread_task);
219 dlm->dlm_reco_thread_task = NULL;
220 }
221}
222
223
224
225/*
226 * this is lame, but here's how recovery works...
227 * 1) all recovery threads cluster wide will work on recovering
228 * ONE node at a time
229 * 2) negotiate who will take over all the locks for the dead node.
230 * thats right... ALL the locks.
231 * 3) once a new master is chosen, everyone scans all locks
232 * and moves aside those mastered by the dead guy
233 * 4) each of these locks should be locked until recovery is done
234 * 5) the new master collects up all of secondary lock queue info
235 * one lock at a time, forcing each node to communicate back
236 * before continuing
237 * 6) each secondary lock queue responds with the full known lock info
238 * 7) once the new master has run all its locks, it sends a ALLDONE!
239 * message to everyone
240 * 8) upon receiving this message, the secondary queue node unlocks
241 * and responds to the ALLDONE
242 * 9) once the new master gets responses from everyone, he unlocks
243 * everything and recovery for this dead node is done
244 *10) go back to 2) while there are still dead nodes
245 *
246 */
247
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700248static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
249{
250 struct dlm_reco_node_data *ndata;
251 struct dlm_lock_resource *res;
252
253 mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
254 dlm->name, dlm->dlm_reco_thread_task->pid,
255 dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
256 dlm->reco.dead_node, dlm->reco.new_master);
257
258 list_for_each_entry(ndata, &dlm->reco.node_data, list) {
259 char *st = "unknown";
260 switch (ndata->state) {
261 case DLM_RECO_NODE_DATA_INIT:
262 st = "init";
263 break;
264 case DLM_RECO_NODE_DATA_REQUESTING:
265 st = "requesting";
266 break;
267 case DLM_RECO_NODE_DATA_DEAD:
268 st = "dead";
269 break;
270 case DLM_RECO_NODE_DATA_RECEIVING:
271 st = "receiving";
272 break;
273 case DLM_RECO_NODE_DATA_REQUESTED:
274 st = "requested";
275 break;
276 case DLM_RECO_NODE_DATA_DONE:
277 st = "done";
278 break;
279 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
280 st = "finalize-sent";
281 break;
282 default:
283 st = "bad";
284 break;
285 }
286 mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
287 dlm->name, ndata->node_num, st);
288 }
289 list_for_each_entry(res, &dlm->reco.resources, recovering) {
290 mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
291 dlm->name, res->lockname.len, res->lockname.name);
292 }
293}
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800294
295#define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
296
297static int dlm_recovery_thread(void *data)
298{
299 int status;
300 struct dlm_ctxt *dlm = data;
301 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
302
303 mlog(0, "dlm thread running for %s...\n", dlm->name);
304
305 while (!kthread_should_stop()) {
306 if (dlm_joined(dlm)) {
307 status = dlm_do_recovery(dlm);
308 if (status == -EAGAIN) {
309 /* do not sleep, recheck immediately. */
310 continue;
311 }
312 if (status < 0)
313 mlog_errno(status);
314 }
315
316 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
317 kthread_should_stop(),
318 timeout);
319 }
320
321 mlog(0, "quitting DLM recovery thread\n");
322 return 0;
323}
324
Kurt Hackele2faea42006-01-12 14:24:55 -0800325/* returns true when the recovery master has contacted us */
326static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
327{
328 int ready;
329 spin_lock(&dlm->spinlock);
330 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
331 spin_unlock(&dlm->spinlock);
332 return ready;
333}
334
335/* returns true if node is no longer in the domain
336 * could be dead or just not joined */
337int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
338{
339 int dead;
340 spin_lock(&dlm->spinlock);
Kurt Hackelaba9aac2006-04-27 18:00:21 -0700341 dead = !test_bit(node, dlm->domain_map);
Kurt Hackele2faea42006-01-12 14:24:55 -0800342 spin_unlock(&dlm->spinlock);
343 return dead;
344}
345
Kurt Hackel44465a72006-01-18 17:05:38 -0800346int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
347{
348 if (timeout) {
349 mlog(ML_NOTICE, "%s: waiting %dms for notification of "
350 "death of node %u\n", dlm->name, timeout, node);
351 wait_event_timeout(dlm->dlm_reco_thread_wq,
352 dlm_is_node_dead(dlm, node),
353 msecs_to_jiffies(timeout));
354 } else {
355 mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
356 "of death of node %u\n", dlm->name, node);
357 wait_event(dlm->dlm_reco_thread_wq,
358 dlm_is_node_dead(dlm, node));
359 }
360 /* for now, return 0 */
361 return 0;
362}
363
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800364/* callers of the top-level api calls (dlmlock/dlmunlock) should
365 * block on the dlm->reco.event when recovery is in progress.
366 * the dlm recovery thread will set this state when it begins
367 * recovering a dead node (as the new master or not) and clear
368 * the state and wake as soon as all affected lock resources have
369 * been marked with the RECOVERY flag */
370static int dlm_in_recovery(struct dlm_ctxt *dlm)
371{
372 int in_recovery;
373 spin_lock(&dlm->spinlock);
374 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
375 spin_unlock(&dlm->spinlock);
376 return in_recovery;
377}
378
379
380void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
381{
382 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
383}
384
385static void dlm_begin_recovery(struct dlm_ctxt *dlm)
386{
387 spin_lock(&dlm->spinlock);
388 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
389 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
390 spin_unlock(&dlm->spinlock);
391}
392
393static void dlm_end_recovery(struct dlm_ctxt *dlm)
394{
395 spin_lock(&dlm->spinlock);
396 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
397 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
398 spin_unlock(&dlm->spinlock);
399 wake_up(&dlm->reco.event);
400}
401
402static int dlm_do_recovery(struct dlm_ctxt *dlm)
403{
404 int status = 0;
Kurt Hackele2faea42006-01-12 14:24:55 -0800405 int ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800406
407 spin_lock(&dlm->spinlock);
408
409 /* check to see if the new master has died */
410 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
411 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
412 mlog(0, "new master %u died while recovering %u!\n",
413 dlm->reco.new_master, dlm->reco.dead_node);
414 /* unset the new_master, leave dead_node */
Kurt Hackelab27eb62006-04-27 18:03:49 -0700415 dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800416 }
417
418 /* select a target to recover */
419 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
420 int bit;
421
422 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
423 if (bit >= O2NM_MAX_NODES || bit < 0)
Kurt Hackelab27eb62006-04-27 18:03:49 -0700424 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800425 else
Kurt Hackelab27eb62006-04-27 18:03:49 -0700426 dlm_set_reco_dead_node(dlm, bit);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800427 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
428 /* BUG? */
429 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
430 dlm->reco.dead_node);
Kurt Hackelab27eb62006-04-27 18:03:49 -0700431 dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800432 }
433
434 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
435 // mlog(0, "nothing to recover! sleeping now!\n");
436 spin_unlock(&dlm->spinlock);
437 /* return to main thread loop and sleep. */
438 return 0;
439 }
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700440 mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
441 dlm->name, dlm->dlm_reco_thread_task->pid,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800442 dlm->reco.dead_node);
443 spin_unlock(&dlm->spinlock);
444
445 /* take write barrier */
446 /* (stops the list reshuffling thread, proxy ast handling) */
447 dlm_begin_recovery(dlm);
448
449 if (dlm->reco.new_master == dlm->node_num)
450 goto master_here;
451
452 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -0800453 /* choose a new master, returns 0 if this node
454 * is the master, -EEXIST if it's another node.
455 * this does not return until a new master is chosen
456 * or recovery completes entirely. */
457 ret = dlm_pick_recovery_master(dlm);
458 if (!ret) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800459 /* already notified everyone. go. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800460 goto master_here;
461 }
462 mlog(0, "another node will master this recovery session.\n");
463 }
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700464 mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
465 dlm->name, dlm->dlm_reco_thread_task->pid, dlm->reco.new_master,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800466 dlm->node_num, dlm->reco.dead_node);
467
468 /* it is safe to start everything back up here
469 * because all of the dead node's lock resources
470 * have been marked as in-recovery */
471 dlm_end_recovery(dlm);
472
473 /* sleep out in main dlm_recovery_thread loop. */
474 return 0;
475
476master_here:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700477 mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
478 dlm->dlm_reco_thread_task->pid,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800479 dlm->name, dlm->reco.dead_node, dlm->node_num);
480
481 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
482 if (status < 0) {
483 mlog(ML_ERROR, "error %d remastering locks for node %u, "
484 "retrying.\n", status, dlm->reco.dead_node);
Kurt Hackele2faea42006-01-12 14:24:55 -0800485 /* yield a bit to allow any final network messages
486 * to get handled on remaining nodes */
487 msleep(100);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800488 } else {
489 /* success! see if any other nodes need recovery */
Kurt Hackele2faea42006-01-12 14:24:55 -0800490 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
491 dlm->name, dlm->reco.dead_node, dlm->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800492 dlm_reset_recovery(dlm);
493 }
494 dlm_end_recovery(dlm);
495
496 /* continue and look for another dead node */
497 return -EAGAIN;
498}
499
500static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
501{
502 int status = 0;
503 struct dlm_reco_node_data *ndata;
504 struct list_head *iter;
505 int all_nodes_done;
506 int destroy = 0;
507 int pass = 0;
508
509 status = dlm_init_recovery_area(dlm, dead_node);
510 if (status < 0)
511 goto leave;
512
513 /* safe to access the node data list without a lock, since this
514 * process is the only one to change the list */
515 list_for_each(iter, &dlm->reco.node_data) {
516 ndata = list_entry (iter, struct dlm_reco_node_data, list);
517 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
518 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
519
520 mlog(0, "requesting lock info from node %u\n",
521 ndata->node_num);
522
523 if (ndata->node_num == dlm->node_num) {
524 ndata->state = DLM_RECO_NODE_DATA_DONE;
525 continue;
526 }
527
528 status = dlm_request_all_locks(dlm, ndata->node_num, dead_node);
529 if (status < 0) {
530 mlog_errno(status);
531 if (dlm_is_host_down(status))
532 ndata->state = DLM_RECO_NODE_DATA_DEAD;
533 else {
534 destroy = 1;
535 goto leave;
536 }
537 }
538
539 switch (ndata->state) {
540 case DLM_RECO_NODE_DATA_INIT:
541 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
542 case DLM_RECO_NODE_DATA_REQUESTED:
543 BUG();
544 break;
545 case DLM_RECO_NODE_DATA_DEAD:
546 mlog(0, "node %u died after requesting "
547 "recovery info for node %u\n",
548 ndata->node_num, dead_node);
549 // start all over
550 destroy = 1;
551 status = -EAGAIN;
552 goto leave;
553 case DLM_RECO_NODE_DATA_REQUESTING:
554 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
555 mlog(0, "now receiving recovery data from "
556 "node %u for dead node %u\n",
557 ndata->node_num, dead_node);
558 break;
559 case DLM_RECO_NODE_DATA_RECEIVING:
560 mlog(0, "already receiving recovery data from "
561 "node %u for dead node %u\n",
562 ndata->node_num, dead_node);
563 break;
564 case DLM_RECO_NODE_DATA_DONE:
565 mlog(0, "already DONE receiving recovery data "
566 "from node %u for dead node %u\n",
567 ndata->node_num, dead_node);
568 break;
569 }
570 }
571
572 mlog(0, "done requesting all lock info\n");
573
574 /* nodes should be sending reco data now
575 * just need to wait */
576
577 while (1) {
578 /* check all the nodes now to see if we are
579 * done, or if anyone died */
580 all_nodes_done = 1;
581 spin_lock(&dlm_reco_state_lock);
582 list_for_each(iter, &dlm->reco.node_data) {
583 ndata = list_entry (iter, struct dlm_reco_node_data, list);
584
585 mlog(0, "checking recovery state of node %u\n",
586 ndata->node_num);
587 switch (ndata->state) {
588 case DLM_RECO_NODE_DATA_INIT:
589 case DLM_RECO_NODE_DATA_REQUESTING:
590 mlog(ML_ERROR, "bad ndata state for "
591 "node %u: state=%d\n",
592 ndata->node_num, ndata->state);
593 BUG();
594 break;
595 case DLM_RECO_NODE_DATA_DEAD:
Kurt Hackele2faea42006-01-12 14:24:55 -0800596 mlog(ML_NOTICE, "node %u died after "
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800597 "requesting recovery info for "
598 "node %u\n", ndata->node_num,
599 dead_node);
600 spin_unlock(&dlm_reco_state_lock);
601 // start all over
602 destroy = 1;
603 status = -EAGAIN;
Kurt Hackele2faea42006-01-12 14:24:55 -0800604 /* instead of spinning like crazy here,
605 * wait for the domain map to catch up
606 * with the network state. otherwise this
607 * can be hit hundreds of times before
608 * the node is really seen as dead. */
609 wait_event_timeout(dlm->dlm_reco_thread_wq,
610 dlm_is_node_dead(dlm,
611 ndata->node_num),
612 msecs_to_jiffies(1000));
613 mlog(0, "waited 1 sec for %u, "
614 "dead? %s\n", ndata->node_num,
615 dlm_is_node_dead(dlm, ndata->node_num) ?
616 "yes" : "no");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800617 goto leave;
618 case DLM_RECO_NODE_DATA_RECEIVING:
619 case DLM_RECO_NODE_DATA_REQUESTED:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700620 mlog(0, "%s: node %u still in state %s\n",
621 dlm->name, ndata->node_num,
622 ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
623 "receiving" : "requested");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800624 all_nodes_done = 0;
625 break;
626 case DLM_RECO_NODE_DATA_DONE:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700627 mlog(0, "%s: node %u state is done\n",
628 dlm->name, ndata->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800629 break;
630 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700631 mlog(0, "%s: node %u state is finalize\n",
632 dlm->name, ndata->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800633 break;
634 }
635 }
636 spin_unlock(&dlm_reco_state_lock);
637
638 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
639 all_nodes_done?"yes":"no");
640 if (all_nodes_done) {
641 int ret;
642
643 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
644 * just send a finalize message to everyone and
645 * clean up */
646 mlog(0, "all nodes are done! send finalize\n");
647 ret = dlm_send_finalize_reco_message(dlm);
648 if (ret < 0)
649 mlog_errno(ret);
650
651 spin_lock(&dlm->spinlock);
652 dlm_finish_local_lockres_recovery(dlm, dead_node,
653 dlm->node_num);
654 spin_unlock(&dlm->spinlock);
655 mlog(0, "should be done with recovery!\n");
656
657 mlog(0, "finishing recovery of %s at %lu, "
658 "dead=%u, this=%u, new=%u\n", dlm->name,
659 jiffies, dlm->reco.dead_node,
660 dlm->node_num, dlm->reco.new_master);
661 destroy = 1;
662 status = ret;
663 /* rescan everything marked dirty along the way */
664 dlm_kick_thread(dlm, NULL);
665 break;
666 }
667 /* wait to be signalled, with periodic timeout
668 * to check for node death */
669 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
670 kthread_should_stop(),
671 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
672
673 }
674
675leave:
676 if (destroy)
677 dlm_destroy_recovery_area(dlm, dead_node);
678
679 mlog_exit(status);
680 return status;
681}
682
683static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
684{
685 int num=0;
686 struct dlm_reco_node_data *ndata;
687
688 spin_lock(&dlm->spinlock);
689 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
690 /* nodes can only be removed (by dying) after dropping
691 * this lock, and death will be trapped later, so this should do */
692 spin_unlock(&dlm->spinlock);
693
694 while (1) {
695 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
696 if (num >= O2NM_MAX_NODES) {
697 break;
698 }
699 BUG_ON(num == dead_node);
700
701 ndata = kcalloc(1, sizeof(*ndata), GFP_KERNEL);
702 if (!ndata) {
703 dlm_destroy_recovery_area(dlm, dead_node);
704 return -ENOMEM;
705 }
706 ndata->node_num = num;
707 ndata->state = DLM_RECO_NODE_DATA_INIT;
708 spin_lock(&dlm_reco_state_lock);
709 list_add_tail(&ndata->list, &dlm->reco.node_data);
710 spin_unlock(&dlm_reco_state_lock);
711 num++;
712 }
713
714 return 0;
715}
716
717static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
718{
719 struct list_head *iter, *iter2;
720 struct dlm_reco_node_data *ndata;
721 LIST_HEAD(tmplist);
722
723 spin_lock(&dlm_reco_state_lock);
724 list_splice_init(&dlm->reco.node_data, &tmplist);
725 spin_unlock(&dlm_reco_state_lock);
726
727 list_for_each_safe(iter, iter2, &tmplist) {
728 ndata = list_entry (iter, struct dlm_reco_node_data, list);
729 list_del_init(&ndata->list);
730 kfree(ndata);
731 }
732}
733
734static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
735 u8 dead_node)
736{
737 struct dlm_lock_request lr;
738 enum dlm_status ret;
739
740 mlog(0, "\n");
741
742
743 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
744 "to %u\n", dead_node, request_from);
745
746 memset(&lr, 0, sizeof(lr));
747 lr.node_idx = dlm->node_num;
748 lr.dead_node = dead_node;
749
750 // send message
751 ret = DLM_NOLOCKMGR;
752 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
753 &lr, sizeof(lr), request_from, NULL);
754
755 /* negative status is handled by caller */
756 if (ret < 0)
757 mlog_errno(ret);
758
759 // return from here, then
760 // sleep until all received or error
761 return ret;
762
763}
764
765int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
766{
767 struct dlm_ctxt *dlm = data;
768 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
769 char *buf = NULL;
770 struct dlm_work_item *item = NULL;
771
772 if (!dlm_grab(dlm))
773 return -EINVAL;
774
Kurt Hackelc3187ce2006-04-27 18:05:41 -0700775 if (lr->dead_node != dlm->reco.dead_node) {
776 mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
777 "dead_node is %u\n", dlm->name, lr->node_idx,
778 lr->dead_node, dlm->reco.dead_node);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700779 dlm_print_reco_node_status(dlm);
Kurt Hackelc3187ce2006-04-27 18:05:41 -0700780 /* this is a hack */
781 dlm_put(dlm);
782 return -ENOMEM;
783 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800784 BUG_ON(lr->dead_node != dlm->reco.dead_node);
785
786 item = kcalloc(1, sizeof(*item), GFP_KERNEL);
787 if (!item) {
788 dlm_put(dlm);
789 return -ENOMEM;
790 }
791
792 /* this will get freed by dlm_request_all_locks_worker */
793 buf = (char *) __get_free_page(GFP_KERNEL);
794 if (!buf) {
795 kfree(item);
796 dlm_put(dlm);
797 return -ENOMEM;
798 }
799
800 /* queue up work for dlm_request_all_locks_worker */
801 dlm_grab(dlm); /* get an extra ref for the work item */
802 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
803 item->u.ral.reco_master = lr->node_idx;
804 item->u.ral.dead_node = lr->dead_node;
805 spin_lock(&dlm->work_lock);
806 list_add_tail(&item->list, &dlm->work_list);
807 spin_unlock(&dlm->work_lock);
808 schedule_work(&dlm->dispatched_work);
809
810 dlm_put(dlm);
811 return 0;
812}
813
814static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
815{
816 struct dlm_migratable_lockres *mres;
817 struct dlm_lock_resource *res;
818 struct dlm_ctxt *dlm;
819 LIST_HEAD(resources);
820 struct list_head *iter;
821 int ret;
822 u8 dead_node, reco_master;
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700823 int skip_all_done = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800824
825 dlm = item->dlm;
826 dead_node = item->u.ral.dead_node;
827 reco_master = item->u.ral.reco_master;
Kurt Hackele2faea42006-01-12 14:24:55 -0800828 mres = (struct dlm_migratable_lockres *)data;
829
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700830 mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
831 dlm->name, dead_node, reco_master);
832
Kurt Hackele2faea42006-01-12 14:24:55 -0800833 if (dead_node != dlm->reco.dead_node ||
834 reco_master != dlm->reco.new_master) {
835 /* show extra debug info if the recovery state is messed */
836 mlog(ML_ERROR, "%s: bad reco state: reco(dead=%u, master=%u), "
837 "request(dead=%u, master=%u)\n",
838 dlm->name, dlm->reco.dead_node, dlm->reco.new_master,
839 dead_node, reco_master);
840 mlog(ML_ERROR, "%s: name=%.*s master=%u locks=%u/%u flags=%u "
Kurt Hackel29004852006-03-02 16:43:36 -0800841 "entry[0]={c=%u:%llu,l=%u,f=%u,t=%d,ct=%d,hb=%d,n=%u}\n",
Kurt Hackele2faea42006-01-12 14:24:55 -0800842 dlm->name, mres->lockname_len, mres->lockname, mres->master,
843 mres->num_locks, mres->total_locks, mres->flags,
Kurt Hackel29004852006-03-02 16:43:36 -0800844 dlm_get_lock_cookie_node(mres->ml[0].cookie),
845 dlm_get_lock_cookie_seq(mres->ml[0].cookie),
846 mres->ml[0].list, mres->ml[0].flags,
Kurt Hackele2faea42006-01-12 14:24:55 -0800847 mres->ml[0].type, mres->ml[0].convert_type,
848 mres->ml[0].highest_blocked, mres->ml[0].node);
849 BUG();
850 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800851 BUG_ON(dead_node != dlm->reco.dead_node);
852 BUG_ON(reco_master != dlm->reco.new_master);
853
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800854 /* lock resources should have already been moved to the
855 * dlm->reco.resources list. now move items from that list
856 * to a temp list if the dead owner matches. note that the
857 * whole cluster recovers only one node at a time, so we
858 * can safely move UNKNOWN lock resources for each recovery
859 * session. */
860 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
861
862 /* now we can begin blasting lockreses without the dlm lock */
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700863
864 /* any errors returned will be due to the new_master dying,
865 * the dlm_reco_thread should detect this */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800866 list_for_each(iter, &resources) {
867 res = list_entry (iter, struct dlm_lock_resource, recovering);
868 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
869 DLM_MRES_RECOVERY);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700870 if (ret < 0) {
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700871 mlog(ML_ERROR, "%s: node %u went down while sending "
872 "recovery state for dead node %u, ret=%d\n", dlm->name,
873 reco_master, dead_node, ret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700874 skip_all_done = 1;
875 break;
876 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800877 }
878
879 /* move the resources back to the list */
880 spin_lock(&dlm->spinlock);
881 list_splice_init(&resources, &dlm->reco.resources);
882 spin_unlock(&dlm->spinlock);
883
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700884 if (!skip_all_done) {
885 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
886 if (ret < 0) {
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700887 mlog(ML_ERROR, "%s: node %u went down while sending "
888 "recovery all-done for dead node %u, ret=%d\n",
889 dlm->name, reco_master, dead_node, ret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700890 }
891 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800892
893 free_page((unsigned long)data);
894}
895
896
897static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
898{
899 int ret, tmpret;
900 struct dlm_reco_data_done done_msg;
901
902 memset(&done_msg, 0, sizeof(done_msg));
903 done_msg.node_idx = dlm->node_num;
904 done_msg.dead_node = dead_node;
905 mlog(0, "sending DATA DONE message to %u, "
906 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
907 done_msg.dead_node);
908
909 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
910 sizeof(done_msg), send_to, &tmpret);
Kurt Hackel29c0fa02006-04-27 18:06:58 -0700911 if (ret < 0) {
912 if (!dlm_is_host_down(ret)) {
913 mlog_errno(ret);
914 mlog(ML_ERROR, "%s: unknown error sending data-done "
915 "to %u\n", dlm->name, send_to);
916 BUG();
917 }
918 } else
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800919 ret = tmpret;
920 return ret;
921}
922
923
924int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
925{
926 struct dlm_ctxt *dlm = data;
927 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
928 struct list_head *iter;
929 struct dlm_reco_node_data *ndata = NULL;
930 int ret = -EINVAL;
931
932 if (!dlm_grab(dlm))
933 return -EINVAL;
934
935 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
936 "node_idx=%u, this node=%u\n", done->dead_node,
937 dlm->reco.dead_node, done->node_idx, dlm->node_num);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -0700938
939 mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
940 "Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
941 "node_idx=%u, this node=%u\n", done->dead_node,
942 dlm->reco.dead_node, done->node_idx, dlm->node_num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800943
944 spin_lock(&dlm_reco_state_lock);
945 list_for_each(iter, &dlm->reco.node_data) {
946 ndata = list_entry (iter, struct dlm_reco_node_data, list);
947 if (ndata->node_num != done->node_idx)
948 continue;
949
950 switch (ndata->state) {
Kurt Hackele2faea42006-01-12 14:24:55 -0800951 /* should have moved beyond INIT but not to FINALIZE yet */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800952 case DLM_RECO_NODE_DATA_INIT:
953 case DLM_RECO_NODE_DATA_DEAD:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800954 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
955 mlog(ML_ERROR, "bad ndata state for node %u:"
956 " state=%d\n", ndata->node_num,
957 ndata->state);
958 BUG();
959 break;
Kurt Hackele2faea42006-01-12 14:24:55 -0800960 /* these states are possible at this point, anywhere along
961 * the line of recovery */
962 case DLM_RECO_NODE_DATA_DONE:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800963 case DLM_RECO_NODE_DATA_RECEIVING:
964 case DLM_RECO_NODE_DATA_REQUESTED:
965 case DLM_RECO_NODE_DATA_REQUESTING:
966 mlog(0, "node %u is DONE sending "
967 "recovery data!\n",
968 ndata->node_num);
969
970 ndata->state = DLM_RECO_NODE_DATA_DONE;
971 ret = 0;
972 break;
973 }
974 }
975 spin_unlock(&dlm_reco_state_lock);
976
977 /* wake the recovery thread, some node is done */
978 if (!ret)
979 dlm_kick_recovery_thread(dlm);
980
981 if (ret < 0)
982 mlog(ML_ERROR, "failed to find recovery node data for node "
983 "%u\n", done->node_idx);
984 dlm_put(dlm);
985
986 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
987 return ret;
988}
989
990static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
991 struct list_head *list,
992 u8 dead_node)
993{
994 struct dlm_lock_resource *res;
995 struct list_head *iter, *iter2;
Kurt Hackele2faea42006-01-12 14:24:55 -0800996 struct dlm_lock *lock;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800997
998 spin_lock(&dlm->spinlock);
999 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1000 res = list_entry (iter, struct dlm_lock_resource, recovering);
Kurt Hackele2faea42006-01-12 14:24:55 -08001001 /* always prune any $RECOVERY entries for dead nodes,
1002 * otherwise hangs can occur during later recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001003 if (dlm_is_recovery_lock(res->lockname.name,
Kurt Hackele2faea42006-01-12 14:24:55 -08001004 res->lockname.len)) {
1005 spin_lock(&res->spinlock);
1006 list_for_each_entry(lock, &res->granted, list) {
1007 if (lock->ml.node == dead_node) {
1008 mlog(0, "AHA! there was "
1009 "a $RECOVERY lock for dead "
1010 "node %u (%s)!\n",
1011 dead_node, dlm->name);
1012 list_del_init(&lock->list);
1013 dlm_lock_put(lock);
1014 break;
1015 }
1016 }
1017 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001018 continue;
Kurt Hackele2faea42006-01-12 14:24:55 -08001019 }
1020
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001021 if (res->owner == dead_node) {
1022 mlog(0, "found lockres owned by dead node while "
1023 "doing recovery for node %u. sending it.\n",
1024 dead_node);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001025 list_move_tail(&res->recovering, list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001026 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1027 mlog(0, "found UNKNOWN owner while doing recovery "
1028 "for node %u. sending it.\n", dead_node);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001029 list_move_tail(&res->recovering, list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001030 }
1031 }
1032 spin_unlock(&dlm->spinlock);
1033}
1034
1035static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1036{
1037 int total_locks = 0;
1038 struct list_head *iter, *queue = &res->granted;
1039 int i;
1040
1041 for (i=0; i<3; i++) {
1042 list_for_each(iter, queue)
1043 total_locks++;
1044 queue++;
1045 }
1046 return total_locks;
1047}
1048
1049
1050static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1051 struct dlm_migratable_lockres *mres,
1052 u8 send_to,
1053 struct dlm_lock_resource *res,
1054 int total_locks)
1055{
1056 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1057 int mres_total_locks = be32_to_cpu(mres->total_locks);
1058 int sz, ret = 0, status = 0;
1059 u8 orig_flags = mres->flags,
1060 orig_master = mres->master;
1061
1062 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1063 if (!mres->num_locks)
1064 return 0;
1065
1066 sz = sizeof(struct dlm_migratable_lockres) +
1067 (mres->num_locks * sizeof(struct dlm_migratable_lock));
1068
1069 /* add an all-done flag if we reached the last lock */
1070 orig_flags = mres->flags;
1071 BUG_ON(total_locks > mres_total_locks);
1072 if (total_locks == mres_total_locks)
1073 mres->flags |= DLM_MRES_ALL_DONE;
1074
1075 /* send it */
1076 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1077 sz, send_to, &status);
1078 if (ret < 0) {
1079 /* XXX: negative status is not handled.
1080 * this will end up killing this node. */
1081 mlog_errno(ret);
1082 } else {
1083 /* might get an -ENOMEM back here */
1084 ret = status;
1085 if (ret < 0) {
1086 mlog_errno(ret);
1087
1088 if (ret == -EFAULT) {
1089 mlog(ML_ERROR, "node %u told me to kill "
1090 "myself!\n", send_to);
1091 BUG();
1092 }
1093 }
1094 }
1095
1096 /* zero and reinit the message buffer */
1097 dlm_init_migratable_lockres(mres, res->lockname.name,
1098 res->lockname.len, mres_total_locks,
1099 mig_cookie, orig_flags, orig_master);
1100 return ret;
1101}
1102
1103static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1104 const char *lockname, int namelen,
1105 int total_locks, u64 cookie,
1106 u8 flags, u8 master)
1107{
1108 /* mres here is one full page */
1109 memset(mres, 0, PAGE_SIZE);
1110 mres->lockname_len = namelen;
1111 memcpy(mres->lockname, lockname, namelen);
1112 mres->num_locks = 0;
1113 mres->total_locks = cpu_to_be32(total_locks);
1114 mres->mig_cookie = cpu_to_be64(cookie);
1115 mres->flags = flags;
1116 mres->master = master;
1117}
1118
1119
1120/* returns 1 if this lock fills the network structure,
1121 * 0 otherwise */
1122static int dlm_add_lock_to_array(struct dlm_lock *lock,
1123 struct dlm_migratable_lockres *mres, int queue)
1124{
1125 struct dlm_migratable_lock *ml;
1126 int lock_num = mres->num_locks;
1127
1128 ml = &(mres->ml[lock_num]);
1129 ml->cookie = lock->ml.cookie;
1130 ml->type = lock->ml.type;
1131 ml->convert_type = lock->ml.convert_type;
1132 ml->highest_blocked = lock->ml.highest_blocked;
1133 ml->list = queue;
1134 if (lock->lksb) {
1135 ml->flags = lock->lksb->flags;
1136 /* send our current lvb */
1137 if (ml->type == LKM_EXMODE ||
1138 ml->type == LKM_PRMODE) {
1139 /* if it is already set, this had better be a PR
1140 * and it has to match */
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001141 if (!dlm_lvb_is_empty(mres->lvb) &&
1142 (ml->type == LKM_EXMODE ||
1143 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001144 mlog(ML_ERROR, "mismatched lvbs!\n");
1145 __dlm_print_one_lock_resource(lock->lockres);
1146 BUG();
1147 }
1148 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1149 }
1150 }
1151 ml->node = lock->ml.node;
1152 mres->num_locks++;
1153 /* we reached the max, send this network message */
1154 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1155 return 1;
1156 return 0;
1157}
1158
1159
1160int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1161 struct dlm_migratable_lockres *mres,
1162 u8 send_to, u8 flags)
1163{
1164 struct list_head *queue, *iter;
1165 int total_locks, i;
1166 u64 mig_cookie = 0;
1167 struct dlm_lock *lock;
1168 int ret = 0;
1169
1170 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1171
1172 mlog(0, "sending to %u\n", send_to);
1173
1174 total_locks = dlm_num_locks_in_lockres(res);
1175 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1176 /* rare, but possible */
1177 mlog(0, "argh. lockres has %d locks. this will "
1178 "require more than one network packet to "
1179 "migrate\n", total_locks);
1180 mig_cookie = dlm_get_next_mig_cookie();
1181 }
1182
1183 dlm_init_migratable_lockres(mres, res->lockname.name,
1184 res->lockname.len, total_locks,
1185 mig_cookie, flags, res->owner);
1186
1187 total_locks = 0;
1188 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1189 queue = dlm_list_idx_to_ptr(res, i);
1190 list_for_each(iter, queue) {
1191 lock = list_entry (iter, struct dlm_lock, list);
1192
1193 /* add another lock. */
1194 total_locks++;
1195 if (!dlm_add_lock_to_array(lock, mres, i))
1196 continue;
1197
1198 /* this filled the lock message,
1199 * we must send it immediately. */
1200 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1201 res, total_locks);
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001202 if (ret < 0)
1203 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001204 }
1205 }
1206 /* flush any remaining locks */
1207 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001208 if (ret < 0)
1209 goto error;
1210 return ret;
1211
1212error:
1213 mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1214 dlm->name, ret);
1215 if (!dlm_is_host_down(ret))
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001216 BUG();
Kurt Hackel29c0fa02006-04-27 18:06:58 -07001217 mlog(0, "%s: node %u went down while sending %s "
1218 "lockres %.*s\n", dlm->name, send_to,
1219 flags & DLM_MRES_RECOVERY ? "recovery" : "migration",
1220 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001221 return ret;
1222}
1223
1224
1225
1226/*
1227 * this message will contain no more than one page worth of
1228 * recovery data, and it will work on only one lockres.
1229 * there may be many locks in this page, and we may need to wait
1230 * for additional packets to complete all the locks (rare, but
1231 * possible).
1232 */
1233/*
1234 * NOTE: the allocation error cases here are scary
1235 * we really cannot afford to fail an alloc in recovery
1236 * do we spin? returning an error only delays the problem really
1237 */
1238
1239int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
1240{
1241 struct dlm_ctxt *dlm = data;
1242 struct dlm_migratable_lockres *mres =
1243 (struct dlm_migratable_lockres *)msg->buf;
1244 int ret = 0;
1245 u8 real_master;
1246 char *buf = NULL;
1247 struct dlm_work_item *item = NULL;
1248 struct dlm_lock_resource *res = NULL;
1249
1250 if (!dlm_grab(dlm))
1251 return -EINVAL;
1252
1253 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1254
1255 real_master = mres->master;
1256 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1257 /* cannot migrate a lockres with no master */
1258 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1259 }
1260
1261 mlog(0, "%s message received from node %u\n",
1262 (mres->flags & DLM_MRES_RECOVERY) ?
1263 "recovery" : "migration", mres->master);
1264 if (mres->flags & DLM_MRES_ALL_DONE)
1265 mlog(0, "all done flag. all lockres data received!\n");
1266
1267 ret = -ENOMEM;
1268 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_KERNEL);
1269 item = kcalloc(1, sizeof(*item), GFP_KERNEL);
1270 if (!buf || !item)
1271 goto leave;
1272
1273 /* lookup the lock to see if we have a secondary queue for this
1274 * already... just add the locks in and this will have its owner
1275 * and RECOVERY flag changed when it completes. */
1276 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1277 if (res) {
1278 /* this will get a ref on res */
1279 /* mark it as recovering/migrating and hash it */
1280 spin_lock(&res->spinlock);
1281 if (mres->flags & DLM_MRES_RECOVERY) {
1282 res->state |= DLM_LOCK_RES_RECOVERING;
1283 } else {
1284 if (res->state & DLM_LOCK_RES_MIGRATING) {
1285 /* this is at least the second
1286 * lockres message */
1287 mlog(0, "lock %.*s is already migrating\n",
1288 mres->lockname_len,
1289 mres->lockname);
1290 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1291 /* caller should BUG */
1292 mlog(ML_ERROR, "node is attempting to migrate "
1293 "lock %.*s, but marked as recovering!\n",
1294 mres->lockname_len, mres->lockname);
1295 ret = -EFAULT;
1296 spin_unlock(&res->spinlock);
1297 goto leave;
1298 }
1299 res->state |= DLM_LOCK_RES_MIGRATING;
1300 }
1301 spin_unlock(&res->spinlock);
1302 } else {
1303 /* need to allocate, just like if it was
1304 * mastered here normally */
1305 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1306 if (!res)
1307 goto leave;
1308
1309 /* to match the ref that we would have gotten if
1310 * dlm_lookup_lockres had succeeded */
1311 dlm_lockres_get(res);
1312
1313 /* mark it as recovering/migrating and hash it */
1314 if (mres->flags & DLM_MRES_RECOVERY)
1315 res->state |= DLM_LOCK_RES_RECOVERING;
1316 else
1317 res->state |= DLM_LOCK_RES_MIGRATING;
1318
1319 spin_lock(&dlm->spinlock);
1320 __dlm_insert_lockres(dlm, res);
1321 spin_unlock(&dlm->spinlock);
1322
1323 /* now that the new lockres is inserted,
1324 * make it usable by other processes */
1325 spin_lock(&res->spinlock);
1326 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1327 spin_unlock(&res->spinlock);
1328
1329 /* add an extra ref for just-allocated lockres
1330 * otherwise the lockres will be purged immediately */
1331 dlm_lockres_get(res);
1332
1333 }
1334
1335 /* at this point we have allocated everything we need,
1336 * and we have a hashed lockres with an extra ref and
1337 * the proper res->state flags. */
1338 ret = 0;
1339 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1340 /* migration cannot have an unknown master */
1341 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1342 mlog(0, "recovery has passed me a lockres with an "
1343 "unknown owner.. will need to requery: "
1344 "%.*s\n", mres->lockname_len, mres->lockname);
1345 } else {
1346 spin_lock(&res->spinlock);
1347 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1348 spin_unlock(&res->spinlock);
1349 }
1350
1351 /* queue up work for dlm_mig_lockres_worker */
1352 dlm_grab(dlm); /* get an extra ref for the work item */
1353 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1354 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1355 item->u.ml.lockres = res; /* already have a ref */
1356 item->u.ml.real_master = real_master;
1357 spin_lock(&dlm->work_lock);
1358 list_add_tail(&item->list, &dlm->work_list);
1359 spin_unlock(&dlm->work_lock);
1360 schedule_work(&dlm->dispatched_work);
1361
1362leave:
1363 dlm_put(dlm);
1364 if (ret < 0) {
1365 if (buf)
1366 kfree(buf);
1367 if (item)
1368 kfree(item);
1369 }
1370
1371 mlog_exit(ret);
1372 return ret;
1373}
1374
1375
1376static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1377{
1378 struct dlm_ctxt *dlm = data;
1379 struct dlm_migratable_lockres *mres;
1380 int ret = 0;
1381 struct dlm_lock_resource *res;
1382 u8 real_master;
1383
1384 dlm = item->dlm;
1385 mres = (struct dlm_migratable_lockres *)data;
1386
1387 res = item->u.ml.lockres;
1388 real_master = item->u.ml.real_master;
1389
1390 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1391 /* this case is super-rare. only occurs if
1392 * node death happens during migration. */
1393again:
1394 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1395 if (ret < 0) {
Kurt Hackele2faea42006-01-12 14:24:55 -08001396 mlog(0, "dlm_lockres_master_requery ret=%d\n",
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001397 ret);
1398 goto again;
1399 }
1400 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1401 mlog(0, "lockres %.*s not claimed. "
1402 "this node will take it.\n",
1403 res->lockname.len, res->lockname.name);
1404 } else {
1405 mlog(0, "master needs to respond to sender "
1406 "that node %u still owns %.*s\n",
1407 real_master, res->lockname.len,
1408 res->lockname.name);
1409 /* cannot touch this lockres */
1410 goto leave;
1411 }
1412 }
1413
1414 ret = dlm_process_recovery_data(dlm, res, mres);
1415 if (ret < 0)
1416 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1417 else
1418 mlog(0, "dlm_process_recovery_data succeeded\n");
1419
1420 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1421 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1422 ret = dlm_finish_migration(dlm, res, mres->master);
1423 if (ret < 0)
1424 mlog_errno(ret);
1425 }
1426
1427leave:
1428 kfree(data);
1429 mlog_exit(ret);
1430}
1431
1432
1433
Kurt Hackelc03872f2006-03-06 14:08:49 -08001434int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1435 struct dlm_lock_resource *res, u8 *real_master)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001436{
1437 struct dlm_node_iter iter;
1438 int nodenum;
1439 int ret = 0;
1440
1441 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1442
1443 /* we only reach here if one of the two nodes in a
1444 * migration died while the migration was in progress.
1445 * at this point we need to requery the master. we
1446 * know that the new_master got as far as creating
1447 * an mle on at least one node, but we do not know
1448 * if any nodes had actually cleared the mle and set
1449 * the master to the new_master. the old master
1450 * is supposed to set the owner to UNKNOWN in the
1451 * event of a new_master death, so the only possible
1452 * responses that we can get from nodes here are
1453 * that the master is new_master, or that the master
1454 * is UNKNOWN.
1455 * if all nodes come back with UNKNOWN then we know
1456 * the lock needs remastering here.
1457 * if any node comes back with a valid master, check
1458 * to see if that master is the one that we are
1459 * recovering. if so, then the new_master died and
1460 * we need to remaster this lock. if not, then the
1461 * new_master survived and that node will respond to
1462 * other nodes about the owner.
1463 * if there is an owner, this node needs to dump this
1464 * lockres and alert the sender that this lockres
1465 * was rejected. */
1466 spin_lock(&dlm->spinlock);
1467 dlm_node_iter_init(dlm->domain_map, &iter);
1468 spin_unlock(&dlm->spinlock);
1469
1470 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1471 /* do not send to self */
1472 if (nodenum == dlm->node_num)
1473 continue;
1474 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1475 if (ret < 0) {
1476 mlog_errno(ret);
Kurt Hackelc03872f2006-03-06 14:08:49 -08001477 if (!dlm_is_host_down(ret))
1478 BUG();
1479 /* host is down, so answer for that node would be
1480 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001481 }
1482 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1483 mlog(0, "lock master is %u\n", *real_master);
1484 break;
1485 }
1486 }
1487 return ret;
1488}
1489
1490
Kurt Hackelc03872f2006-03-06 14:08:49 -08001491int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1492 u8 nodenum, u8 *real_master)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001493{
1494 int ret = -EINVAL;
1495 struct dlm_master_requery req;
1496 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1497
1498 memset(&req, 0, sizeof(req));
1499 req.node_idx = dlm->node_num;
1500 req.namelen = res->lockname.len;
1501 memcpy(req.name, res->lockname.name, res->lockname.len);
1502
1503 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1504 &req, sizeof(req), nodenum, &status);
1505 /* XXX: negative status not handled properly here. */
1506 if (ret < 0)
1507 mlog_errno(ret);
1508 else {
1509 BUG_ON(status < 0);
1510 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1511 *real_master = (u8) (status & 0xff);
1512 mlog(0, "node %u responded to master requery with %u\n",
1513 nodenum, *real_master);
1514 ret = 0;
1515 }
1516 return ret;
1517}
1518
1519
1520/* this function cannot error, so unless the sending
1521 * or receiving of the message failed, the owner can
1522 * be trusted */
1523int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
1524{
1525 struct dlm_ctxt *dlm = data;
1526 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1527 struct dlm_lock_resource *res = NULL;
Mark Fasheha3d33292006-03-09 17:55:56 -08001528 unsigned int hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001529 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1530 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1531
1532 if (!dlm_grab(dlm)) {
1533 /* since the domain has gone away on this
1534 * node, the proper response is UNKNOWN */
1535 return master;
1536 }
1537
Mark Fasheha3d33292006-03-09 17:55:56 -08001538 hash = dlm_lockid_hash(req->name, req->namelen);
1539
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001540 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08001541 res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001542 if (res) {
1543 spin_lock(&res->spinlock);
1544 master = res->owner;
1545 if (master == dlm->node_num) {
1546 int ret = dlm_dispatch_assert_master(dlm, res,
1547 0, 0, flags);
1548 if (ret < 0) {
1549 mlog_errno(-ENOMEM);
1550 /* retry!? */
1551 BUG();
1552 }
1553 }
1554 spin_unlock(&res->spinlock);
1555 }
1556 spin_unlock(&dlm->spinlock);
1557
1558 dlm_put(dlm);
1559 return master;
1560}
1561
1562static inline struct list_head *
1563dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1564{
1565 struct list_head *ret;
1566 BUG_ON(list_num < 0);
1567 BUG_ON(list_num > 2);
1568 ret = &(res->granted);
1569 ret += list_num;
1570 return ret;
1571}
1572/* TODO: do ast flush business
1573 * TODO: do MIGRATING and RECOVERING spinning
1574 */
1575
1576/*
1577* NOTE about in-flight requests during migration:
1578*
1579* Before attempting the migrate, the master has marked the lockres as
1580* MIGRATING and then flushed all of its pending ASTS. So any in-flight
1581* requests either got queued before the MIGRATING flag got set, in which
1582* case the lock data will reflect the change and a return message is on
1583* the way, or the request failed to get in before MIGRATING got set. In
1584* this case, the caller will be told to spin and wait for the MIGRATING
1585* flag to be dropped, then recheck the master.
1586* This holds true for the convert, cancel and unlock cases, and since lvb
1587* updates are tied to these same messages, it applies to lvb updates as
1588* well. For the lock case, there is no way a lock can be on the master
1589* queue and not be on the secondary queue since the lock is always added
1590* locally first. This means that the new target node will never be sent
1591* a lock that he doesn't already have on the list.
1592* In total, this means that the local lock is correct and should not be
1593* updated to match the one sent by the master. Any messages sent back
1594* from the master before the MIGRATING flag will bring the lock properly
1595* up-to-date, and the change will be ordered properly for the waiter.
1596* We will *not* attempt to modify the lock underneath the waiter.
1597*/
1598
1599static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1600 struct dlm_lock_resource *res,
1601 struct dlm_migratable_lockres *mres)
1602{
1603 struct dlm_migratable_lock *ml;
1604 struct list_head *queue;
1605 struct dlm_lock *newlock = NULL;
1606 struct dlm_lockstatus *lksb = NULL;
1607 int ret = 0;
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001608 int i, bad;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001609 struct list_head *iter;
1610 struct dlm_lock *lock = NULL;
1611
1612 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1613 for (i=0; i<mres->num_locks; i++) {
1614 ml = &(mres->ml[i]);
1615 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1616 newlock = NULL;
1617 lksb = NULL;
1618
1619 queue = dlm_list_num_to_pointer(res, ml->list);
1620
1621 /* if the lock is for the local node it needs to
1622 * be moved to the proper location within the queue.
1623 * do not allocate a new lock structure. */
1624 if (ml->node == dlm->node_num) {
1625 /* MIGRATION ONLY! */
1626 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1627
1628 spin_lock(&res->spinlock);
1629 list_for_each(iter, queue) {
1630 lock = list_entry (iter, struct dlm_lock, list);
1631 if (lock->ml.cookie != ml->cookie)
1632 lock = NULL;
1633 else
1634 break;
1635 }
1636
1637 /* lock is always created locally first, and
1638 * destroyed locally last. it must be on the list */
1639 if (!lock) {
Kurt Hackel29004852006-03-02 16:43:36 -08001640 u64 c = ml->cookie;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001641 mlog(ML_ERROR, "could not find local lock "
Kurt Hackel29004852006-03-02 16:43:36 -08001642 "with cookie %u:%llu!\n",
1643 dlm_get_lock_cookie_node(c),
1644 dlm_get_lock_cookie_seq(c));
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001645 BUG();
1646 }
1647 BUG_ON(lock->ml.node != ml->node);
1648
1649 /* see NOTE above about why we do not update
1650 * to match the master here */
1651
1652 /* move the lock to its proper place */
1653 /* do not alter lock refcount. switching lists. */
Akinobu Mitaf1166292006-06-26 00:24:46 -07001654 list_move_tail(&lock->list, queue);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001655 spin_unlock(&res->spinlock);
1656
1657 mlog(0, "just reordered a local lock!\n");
1658 continue;
1659 }
1660
1661 /* lock is for another node. */
1662 newlock = dlm_new_lock(ml->type, ml->node,
1663 be64_to_cpu(ml->cookie), NULL);
1664 if (!newlock) {
1665 ret = -ENOMEM;
1666 goto leave;
1667 }
1668 lksb = newlock->lksb;
1669 dlm_lock_attach_lockres(newlock, res);
1670
1671 if (ml->convert_type != LKM_IVMODE) {
1672 BUG_ON(queue != &res->converting);
1673 newlock->ml.convert_type = ml->convert_type;
1674 }
1675 lksb->flags |= (ml->flags &
1676 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1677
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001678 if (!dlm_lvb_is_empty(mres->lvb)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001679 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1680 /* other node was trying to update
1681 * lvb when node died. recreate the
1682 * lksb with the updated lvb. */
1683 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1684 } else {
1685 /* otherwise, the node is sending its
1686 * most recent valid lvb info */
1687 BUG_ON(ml->type != LKM_EXMODE &&
1688 ml->type != LKM_PRMODE);
Kurt Hackel8bc674c2006-04-27 18:02:10 -07001689 if (!dlm_lvb_is_empty(res->lvb) &&
1690 (ml->type == LKM_EXMODE ||
1691 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
Kurt Hackel343e26a2006-05-01 11:15:04 -07001692 int i;
1693 mlog(ML_ERROR, "%s:%.*s: received bad "
1694 "lvb! type=%d\n", dlm->name,
1695 res->lockname.len,
1696 res->lockname.name, ml->type);
1697 printk("lockres lvb=[");
1698 for (i=0; i<DLM_LVB_LEN; i++)
1699 printk("%02x", res->lvb[i]);
1700 printk("]\nmigrated lvb=[");
1701 for (i=0; i<DLM_LVB_LEN; i++)
1702 printk("%02x", mres->lvb[i]);
1703 printk("]\n");
1704 dlm_print_one_lock_resource(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001705 BUG();
1706 }
1707 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1708 }
1709 }
1710
1711
1712 /* NOTE:
1713 * wrt lock queue ordering and recovery:
1714 * 1. order of locks on granted queue is
1715 * meaningless.
1716 * 2. order of locks on converting queue is
1717 * LOST with the node death. sorry charlie.
1718 * 3. order of locks on the blocked queue is
1719 * also LOST.
1720 * order of locks does not affect integrity, it
1721 * just means that a lock request may get pushed
1722 * back in line as a result of the node death.
1723 * also note that for a given node the lock order
1724 * for its secondary queue locks is preserved
1725 * relative to each other, but clearly *not*
1726 * preserved relative to locks from other nodes.
1727 */
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001728 bad = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001729 spin_lock(&res->spinlock);
Kurt Hackelc3187ce2006-04-27 18:05:41 -07001730 list_for_each_entry(lock, queue, list) {
1731 if (lock->ml.cookie == ml->cookie) {
1732 u64 c = lock->ml.cookie;
1733 mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1734 "exists on this lockres!\n", dlm->name,
1735 res->lockname.len, res->lockname.name,
1736 dlm_get_lock_cookie_node(c),
1737 dlm_get_lock_cookie_seq(c));
1738
1739 mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1740 "node=%u, cookie=%u:%llu, queue=%d\n",
1741 ml->type, ml->convert_type, ml->node,
1742 dlm_get_lock_cookie_node(ml->cookie),
1743 dlm_get_lock_cookie_seq(ml->cookie),
1744 ml->list);
1745
1746 __dlm_print_one_lock_resource(res);
1747 bad = 1;
1748 break;
1749 }
1750 }
1751 if (!bad) {
1752 dlm_lock_get(newlock);
1753 list_add_tail(&newlock->list, queue);
1754 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001755 spin_unlock(&res->spinlock);
1756 }
1757 mlog(0, "done running all the locks\n");
1758
1759leave:
1760 if (ret < 0) {
1761 mlog_errno(ret);
1762 if (newlock)
1763 dlm_lock_put(newlock);
1764 }
1765
1766 mlog_exit(ret);
1767 return ret;
1768}
1769
1770void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1771 struct dlm_lock_resource *res)
1772{
1773 int i;
1774 struct list_head *queue, *iter, *iter2;
1775 struct dlm_lock *lock;
1776
1777 res->state |= DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001778 if (!list_empty(&res->recovering)) {
1779 mlog(0,
1780 "Recovering res %s:%.*s, is already on recovery list!\n",
1781 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001782 list_del_init(&res->recovering);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001783 }
1784 /* We need to hold a reference while on the recovery list */
1785 dlm_lockres_get(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001786 list_add_tail(&res->recovering, &dlm->reco.resources);
1787
1788 /* find any pending locks and put them back on proper list */
1789 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1790 queue = dlm_list_idx_to_ptr(res, i);
1791 list_for_each_safe(iter, iter2, queue) {
1792 lock = list_entry (iter, struct dlm_lock, list);
1793 dlm_lock_get(lock);
1794 if (lock->convert_pending) {
1795 /* move converting lock back to granted */
1796 BUG_ON(i != DLM_CONVERTING_LIST);
1797 mlog(0, "node died with convert pending "
1798 "on %.*s. move back to granted list.\n",
1799 res->lockname.len, res->lockname.name);
1800 dlm_revert_pending_convert(res, lock);
1801 lock->convert_pending = 0;
1802 } else if (lock->lock_pending) {
1803 /* remove pending lock requests completely */
1804 BUG_ON(i != DLM_BLOCKED_LIST);
1805 mlog(0, "node died with lock pending "
1806 "on %.*s. remove from blocked list and skip.\n",
1807 res->lockname.len, res->lockname.name);
1808 /* lock will be floating until ref in
1809 * dlmlock_remote is freed after the network
1810 * call returns. ok for it to not be on any
1811 * list since no ast can be called
1812 * (the master is dead). */
1813 dlm_revert_pending_lock(res, lock);
1814 lock->lock_pending = 0;
1815 } else if (lock->unlock_pending) {
1816 /* if an unlock was in progress, treat as
1817 * if this had completed successfully
1818 * before sending this lock state to the
1819 * new master. note that the dlm_unlock
1820 * call is still responsible for calling
1821 * the unlockast. that will happen after
1822 * the network call times out. for now,
1823 * just move lists to prepare the new
1824 * recovery master. */
1825 BUG_ON(i != DLM_GRANTED_LIST);
1826 mlog(0, "node died with unlock pending "
1827 "on %.*s. remove from blocked list and skip.\n",
1828 res->lockname.len, res->lockname.name);
1829 dlm_commit_pending_unlock(res, lock);
1830 lock->unlock_pending = 0;
1831 } else if (lock->cancel_pending) {
1832 /* if a cancel was in progress, treat as
1833 * if this had completed successfully
1834 * before sending this lock state to the
1835 * new master */
1836 BUG_ON(i != DLM_CONVERTING_LIST);
1837 mlog(0, "node died with cancel pending "
1838 "on %.*s. move back to granted list.\n",
1839 res->lockname.len, res->lockname.name);
1840 dlm_commit_pending_cancel(res, lock);
1841 lock->cancel_pending = 0;
1842 }
1843 dlm_lock_put(lock);
1844 }
1845 }
1846}
1847
1848
1849
1850/* removes all recovered locks from the recovery list.
1851 * sets the res->owner to the new master.
1852 * unsets the RECOVERY flag and wakes waiters. */
1853static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1854 u8 dead_node, u8 new_master)
1855{
1856 int i;
Mark Fasheh81f20942006-02-28 17:31:22 -08001857 struct list_head *iter, *iter2;
1858 struct hlist_node *hash_iter;
1859 struct hlist_head *bucket;
1860
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001861 struct dlm_lock_resource *res;
1862
1863 mlog_entry_void();
1864
1865 assert_spin_locked(&dlm->spinlock);
1866
1867 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1868 res = list_entry (iter, struct dlm_lock_resource, recovering);
1869 if (res->owner == dead_node) {
1870 list_del_init(&res->recovering);
1871 spin_lock(&res->spinlock);
1872 dlm_change_lockres_owner(dlm, res, new_master);
1873 res->state &= ~DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001874 if (!__dlm_lockres_unused(res))
1875 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001876 spin_unlock(&res->spinlock);
1877 wake_up(&res->wq);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001878 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001879 }
1880 }
1881
1882 /* this will become unnecessary eventually, but
1883 * for now we need to run the whole hash, clear
1884 * the RECOVERING state and set the owner
1885 * if necessary */
Mark Fasheh81f20942006-02-28 17:31:22 -08001886 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Daniel Phillips03d864c2006-03-10 18:08:16 -08001887 bucket = dlm_lockres_hash(dlm, i);
Mark Fasheh81f20942006-02-28 17:31:22 -08001888 hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001889 if (res->state & DLM_LOCK_RES_RECOVERING) {
1890 if (res->owner == dead_node) {
1891 mlog(0, "(this=%u) res %.*s owner=%u "
1892 "was not on recovering list, but "
1893 "clearing state anyway\n",
1894 dlm->node_num, res->lockname.len,
1895 res->lockname.name, new_master);
1896 } else if (res->owner == dlm->node_num) {
1897 mlog(0, "(this=%u) res %.*s owner=%u "
1898 "was not on recovering list, "
1899 "owner is THIS node, clearing\n",
1900 dlm->node_num, res->lockname.len,
1901 res->lockname.name, new_master);
1902 } else
1903 continue;
1904
Kurt Hackelc03872f2006-03-06 14:08:49 -08001905 if (!list_empty(&res->recovering)) {
1906 mlog(0, "%s:%.*s: lockres was "
1907 "marked RECOVERING, owner=%u\n",
1908 dlm->name, res->lockname.len,
1909 res->lockname.name, res->owner);
1910 list_del_init(&res->recovering);
Kurt Hackel69d72b02006-05-01 10:57:51 -07001911 dlm_lockres_put(res);
Kurt Hackelc03872f2006-03-06 14:08:49 -08001912 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001913 spin_lock(&res->spinlock);
1914 dlm_change_lockres_owner(dlm, res, new_master);
1915 res->state &= ~DLM_LOCK_RES_RECOVERING;
Kurt Hackel69d72b02006-05-01 10:57:51 -07001916 if (!__dlm_lockres_unused(res))
1917 __dlm_dirty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001918 spin_unlock(&res->spinlock);
1919 wake_up(&res->wq);
1920 }
1921 }
1922 }
1923}
1924
1925static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
1926{
1927 if (local) {
1928 if (lock->ml.type != LKM_EXMODE &&
1929 lock->ml.type != LKM_PRMODE)
1930 return 1;
1931 } else if (lock->ml.type == LKM_EXMODE)
1932 return 1;
1933 return 0;
1934}
1935
1936static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
1937 struct dlm_lock_resource *res, u8 dead_node)
1938{
1939 struct list_head *iter, *queue;
1940 struct dlm_lock *lock;
1941 int blank_lvb = 0, local = 0;
1942 int i;
1943 u8 search_node;
1944
1945 assert_spin_locked(&dlm->spinlock);
1946 assert_spin_locked(&res->spinlock);
1947
1948 if (res->owner == dlm->node_num)
1949 /* if this node owned the lockres, and if the dead node
1950 * had an EX when he died, blank out the lvb */
1951 search_node = dead_node;
1952 else {
1953 /* if this is a secondary lockres, and we had no EX or PR
1954 * locks granted, we can no longer trust the lvb */
1955 search_node = dlm->node_num;
1956 local = 1; /* check local state for valid lvb */
1957 }
1958
1959 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
1960 queue = dlm_list_idx_to_ptr(res, i);
1961 list_for_each(iter, queue) {
1962 lock = list_entry (iter, struct dlm_lock, list);
1963 if (lock->ml.node == search_node) {
1964 if (dlm_lvb_needs_invalidation(lock, local)) {
1965 /* zero the lksb lvb and lockres lvb */
1966 blank_lvb = 1;
1967 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
1968 }
1969 }
1970 }
1971 }
1972
1973 if (blank_lvb) {
1974 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
1975 res->lockname.len, res->lockname.name, dead_node);
1976 memset(res->lvb, 0, DLM_LVB_LEN);
1977 }
1978}
1979
1980static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
1981 struct dlm_lock_resource *res, u8 dead_node)
1982{
1983 struct list_head *iter, *tmpiter;
1984 struct dlm_lock *lock;
1985
1986 /* this node is the lockres master:
1987 * 1) remove any stale locks for the dead node
1988 * 2) if the dead node had an EX when he died, blank out the lvb
1989 */
1990 assert_spin_locked(&dlm->spinlock);
1991 assert_spin_locked(&res->spinlock);
1992
1993 /* TODO: check pending_asts, pending_basts here */
1994 list_for_each_safe(iter, tmpiter, &res->granted) {
1995 lock = list_entry (iter, struct dlm_lock, list);
1996 if (lock->ml.node == dead_node) {
1997 list_del_init(&lock->list);
1998 dlm_lock_put(lock);
1999 }
2000 }
2001 list_for_each_safe(iter, tmpiter, &res->converting) {
2002 lock = list_entry (iter, struct dlm_lock, list);
2003 if (lock->ml.node == dead_node) {
2004 list_del_init(&lock->list);
2005 dlm_lock_put(lock);
2006 }
2007 }
2008 list_for_each_safe(iter, tmpiter, &res->blocked) {
2009 lock = list_entry (iter, struct dlm_lock, list);
2010 if (lock->ml.node == dead_node) {
2011 list_del_init(&lock->list);
2012 dlm_lock_put(lock);
2013 }
2014 }
2015
2016 /* do not kick thread yet */
2017 __dlm_dirty_lockres(dlm, res);
2018}
2019
2020/* if this node is the recovery master, and there are no
2021 * locks for a given lockres owned by this node that are in
2022 * either PR or EX mode, zero out the lvb before requesting.
2023 *
2024 */
2025
2026
2027static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2028{
Mark Fasheh81f20942006-02-28 17:31:22 -08002029 struct hlist_node *iter;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002030 struct dlm_lock_resource *res;
2031 int i;
Mark Fasheh81f20942006-02-28 17:31:22 -08002032 struct hlist_head *bucket;
Kurt Hackele2faea42006-01-12 14:24:55 -08002033 struct dlm_lock *lock;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002034
2035
2036 /* purge any stale mles */
2037 dlm_clean_master_list(dlm, dead_node);
2038
2039 /*
2040 * now clean up all lock resources. there are two rules:
2041 *
2042 * 1) if the dead node was the master, move the lockres
2043 * to the recovering list. set the RECOVERING flag.
2044 * this lockres needs to be cleaned up before it can
2045 * be used further.
2046 *
2047 * 2) if this node was the master, remove all locks from
2048 * each of the lockres queues that were owned by the
2049 * dead node. once recovery finishes, the dlm thread
2050 * can be kicked again to see if any ASTs or BASTs
2051 * need to be fired as a result.
2052 */
Mark Fasheh81f20942006-02-28 17:31:22 -08002053 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Daniel Phillips03d864c2006-03-10 18:08:16 -08002054 bucket = dlm_lockres_hash(dlm, i);
Mark Fasheh81f20942006-02-28 17:31:22 -08002055 hlist_for_each_entry(res, iter, bucket, hash_node) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002056 /* always prune any $RECOVERY entries for dead nodes,
2057 * otherwise hangs can occur during later recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002058 if (dlm_is_recovery_lock(res->lockname.name,
Kurt Hackele2faea42006-01-12 14:24:55 -08002059 res->lockname.len)) {
2060 spin_lock(&res->spinlock);
2061 list_for_each_entry(lock, &res->granted, list) {
2062 if (lock->ml.node == dead_node) {
2063 mlog(0, "AHA! there was "
2064 "a $RECOVERY lock for dead "
2065 "node %u (%s)!\n",
2066 dead_node, dlm->name);
2067 list_del_init(&lock->list);
2068 dlm_lock_put(lock);
2069 break;
2070 }
2071 }
2072 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002073 continue;
Kurt Hackele2faea42006-01-12 14:24:55 -08002074 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002075 spin_lock(&res->spinlock);
2076 /* zero the lvb if necessary */
2077 dlm_revalidate_lvb(dlm, res, dead_node);
2078 if (res->owner == dead_node)
2079 dlm_move_lockres_to_recovery_list(dlm, res);
2080 else if (res->owner == dlm->node_num) {
2081 dlm_free_dead_locks(dlm, res, dead_node);
2082 __dlm_lockres_calc_usage(dlm, res);
2083 }
2084 spin_unlock(&res->spinlock);
2085 }
2086 }
2087
2088}
2089
2090static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2091{
2092 assert_spin_locked(&dlm->spinlock);
2093
Kurt Hackel466d1a42006-05-01 11:11:13 -07002094 if (dlm->reco.new_master == idx) {
2095 mlog(0, "%s: recovery master %d just died\n",
2096 dlm->name, idx);
2097 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2098 /* finalize1 was reached, so it is safe to clear
2099 * the new_master and dead_node. that recovery
2100 * is complete. */
2101 mlog(0, "%s: dead master %d had reached "
2102 "finalize1 state, clearing\n", dlm->name, idx);
2103 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2104 __dlm_reset_recovery(dlm);
2105 }
2106 }
2107
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002108 /* check to see if the node is already considered dead */
2109 if (!test_bit(idx, dlm->live_nodes_map)) {
2110 mlog(0, "for domain %s, node %d is already dead. "
2111 "another node likely did recovery already.\n",
2112 dlm->name, idx);
2113 return;
2114 }
2115
2116 /* check to see if we do not care about this node */
2117 if (!test_bit(idx, dlm->domain_map)) {
2118 /* This also catches the case that we get a node down
2119 * but haven't joined the domain yet. */
2120 mlog(0, "node %u already removed from domain!\n", idx);
2121 return;
2122 }
2123
2124 clear_bit(idx, dlm->live_nodes_map);
2125
2126 /* Clean up join state on node death. */
2127 if (dlm->joining_node == idx) {
2128 mlog(0, "Clearing join state for node %u\n", idx);
2129 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2130 }
2131
2132 /* make sure local cleanup occurs before the heartbeat events */
2133 if (!test_bit(idx, dlm->recovery_map))
2134 dlm_do_local_recovery_cleanup(dlm, idx);
2135
2136 /* notify anything attached to the heartbeat events */
2137 dlm_hb_event_notify_attached(dlm, idx, 0);
2138
2139 mlog(0, "node %u being removed from domain map!\n", idx);
2140 clear_bit(idx, dlm->domain_map);
2141 /* wake up migration waiters if a node goes down.
2142 * perhaps later we can genericize this for other waiters. */
2143 wake_up(&dlm->migration_wq);
2144
2145 if (test_bit(idx, dlm->recovery_map))
2146 mlog(0, "domain %s, node %u already added "
2147 "to recovery map!\n", dlm->name, idx);
2148 else
2149 set_bit(idx, dlm->recovery_map);
2150}
2151
2152void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2153{
2154 struct dlm_ctxt *dlm = data;
2155
2156 if (!dlm_grab(dlm))
2157 return;
2158
2159 spin_lock(&dlm->spinlock);
2160 __dlm_hb_node_down(dlm, idx);
2161 spin_unlock(&dlm->spinlock);
2162
2163 dlm_put(dlm);
2164}
2165
2166void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2167{
2168 struct dlm_ctxt *dlm = data;
2169
2170 if (!dlm_grab(dlm))
2171 return;
2172
2173 spin_lock(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002174 set_bit(idx, dlm->live_nodes_map);
Kurt Hackele2faea42006-01-12 14:24:55 -08002175 /* do NOT notify mle attached to the heartbeat events.
2176 * new nodes are not interesting in mastery until joined. */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002177 spin_unlock(&dlm->spinlock);
2178
2179 dlm_put(dlm);
2180}
2181
2182static void dlm_reco_ast(void *astdata)
2183{
2184 struct dlm_ctxt *dlm = astdata;
2185 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2186 dlm->node_num, dlm->name);
2187}
2188static void dlm_reco_bast(void *astdata, int blocked_type)
2189{
2190 struct dlm_ctxt *dlm = astdata;
2191 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2192 dlm->node_num, dlm->name);
2193}
2194static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2195{
2196 mlog(0, "unlockast for recovery lock fired!\n");
2197}
2198
Kurt Hackele2faea42006-01-12 14:24:55 -08002199/*
2200 * dlm_pick_recovery_master will continually attempt to use
2201 * dlmlock() on the special "$RECOVERY" lockres with the
2202 * LKM_NOQUEUE flag to get an EX. every thread that enters
2203 * this function on each node racing to become the recovery
2204 * master will not stop attempting this until either:
2205 * a) this node gets the EX (and becomes the recovery master),
2206 * or b) dlm->reco.new_master gets set to some nodenum
2207 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2208 * so each time a recovery master is needed, the entire cluster
2209 * will sync at this point. if the new master dies, that will
2210 * be detected in dlm_do_recovery */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002211static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2212{
2213 enum dlm_status ret;
2214 struct dlm_lockstatus lksb;
2215 int status = -EINVAL;
2216
2217 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2218 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002219again:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002220 memset(&lksb, 0, sizeof(lksb));
2221
2222 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2223 DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
2224
Kurt Hackele2faea42006-01-12 14:24:55 -08002225 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2226 dlm->name, ret, lksb.status);
2227
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002228 if (ret == DLM_NORMAL) {
2229 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2230 dlm->name, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002231
2232 /* got the EX lock. check to see if another node
2233 * just became the reco master */
2234 if (dlm_reco_master_ready(dlm)) {
2235 mlog(0, "%s: got reco EX lock, but %u will "
2236 "do the recovery\n", dlm->name,
2237 dlm->reco.new_master);
2238 status = -EEXIST;
2239 } else {
Kurt Hackel898effa2006-01-18 17:01:25 -08002240 status = 0;
2241
2242 /* see if recovery was already finished elsewhere */
2243 spin_lock(&dlm->spinlock);
2244 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2245 status = -EINVAL;
2246 mlog(0, "%s: got reco EX lock, but "
2247 "node got recovered already\n", dlm->name);
2248 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2249 mlog(ML_ERROR, "%s: new master is %u "
2250 "but no dead node!\n",
2251 dlm->name, dlm->reco.new_master);
2252 BUG();
2253 }
2254 }
2255 spin_unlock(&dlm->spinlock);
2256 }
2257
2258 /* if this node has actually become the recovery master,
2259 * set the master and send the messages to begin recovery */
2260 if (!status) {
2261 mlog(0, "%s: dead=%u, this=%u, sending "
2262 "begin_reco now\n", dlm->name,
2263 dlm->reco.dead_node, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002264 status = dlm_send_begin_reco_message(dlm,
2265 dlm->reco.dead_node);
2266 /* this always succeeds */
2267 BUG_ON(status);
2268
2269 /* set the new_master to this node */
2270 spin_lock(&dlm->spinlock);
Kurt Hackelab27eb62006-04-27 18:03:49 -07002271 dlm_set_reco_master(dlm, dlm->node_num);
Kurt Hackele2faea42006-01-12 14:24:55 -08002272 spin_unlock(&dlm->spinlock);
2273 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002274
2275 /* recovery lock is a special case. ast will not get fired,
2276 * so just go ahead and unlock it. */
2277 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
Kurt Hackele2faea42006-01-12 14:24:55 -08002278 if (ret == DLM_DENIED) {
2279 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2280 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2281 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002282 if (ret != DLM_NORMAL) {
2283 /* this would really suck. this could only happen
2284 * if there was a network error during the unlock
2285 * because of node death. this means the unlock
2286 * is actually "done" and the lock structure is
2287 * even freed. we can continue, but only
2288 * because this specific lock name is special. */
Kurt Hackele2faea42006-01-12 14:24:55 -08002289 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002290 }
2291 } else if (ret == DLM_NOTQUEUED) {
2292 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2293 dlm->name, dlm->node_num);
2294 /* another node is master. wait on
Kurt Hackele2faea42006-01-12 14:24:55 -08002295 * reco.new_master != O2NM_INVALID_NODE_NUM
2296 * for at most one second */
2297 wait_event_timeout(dlm->dlm_reco_thread_wq,
2298 dlm_reco_master_ready(dlm),
2299 msecs_to_jiffies(1000));
2300 if (!dlm_reco_master_ready(dlm)) {
2301 mlog(0, "%s: reco master taking awhile\n",
2302 dlm->name);
2303 goto again;
2304 }
2305 /* another node has informed this one that it is reco master */
2306 mlog(0, "%s: reco master %u is ready to recover %u\n",
2307 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002308 status = -EEXIST;
Kurt Hackele2faea42006-01-12 14:24:55 -08002309 } else {
2310 struct dlm_lock_resource *res;
2311
2312 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2313 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2314 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2315 dlm_errname(lksb.status));
2316 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2317 DLM_RECOVERY_LOCK_NAME_LEN);
2318 if (res) {
2319 dlm_print_one_lock_resource(res);
2320 dlm_lockres_put(res);
2321 } else {
2322 mlog(ML_ERROR, "recovery lock not found\n");
2323 }
2324 BUG();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002325 }
2326
2327 return status;
2328}
2329
2330static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2331{
2332 struct dlm_begin_reco br;
2333 int ret = 0;
2334 struct dlm_node_iter iter;
2335 int nodenum;
2336 int status;
2337
2338 mlog_entry("%u\n", dead_node);
2339
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002340 mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002341
2342 spin_lock(&dlm->spinlock);
2343 dlm_node_iter_init(dlm->domain_map, &iter);
2344 spin_unlock(&dlm->spinlock);
2345
2346 clear_bit(dead_node, iter.node_map);
2347
2348 memset(&br, 0, sizeof(br));
2349 br.node_idx = dlm->node_num;
2350 br.dead_node = dead_node;
2351
2352 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2353 ret = 0;
2354 if (nodenum == dead_node) {
2355 mlog(0, "not sending begin reco to dead node "
2356 "%u\n", dead_node);
2357 continue;
2358 }
2359 if (nodenum == dlm->node_num) {
2360 mlog(0, "not sending begin reco to self\n");
2361 continue;
2362 }
Kurt Hackele2faea42006-01-12 14:24:55 -08002363retry:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002364 ret = -EINVAL;
2365 mlog(0, "attempting to send begin reco msg to %d\n",
2366 nodenum);
2367 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2368 &br, sizeof(br), nodenum, &status);
2369 /* negative status is handled ok by caller here */
2370 if (ret >= 0)
2371 ret = status;
Kurt Hackele2faea42006-01-12 14:24:55 -08002372 if (dlm_is_host_down(ret)) {
2373 /* node is down. not involved in recovery
2374 * so just keep going */
2375 mlog(0, "%s: node %u was down when sending "
2376 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2377 ret = 0;
2378 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002379 if (ret < 0) {
2380 struct dlm_lock_resource *res;
Kurt Hackele2faea42006-01-12 14:24:55 -08002381 /* this is now a serious problem, possibly ENOMEM
2382 * in the network stack. must retry */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002383 mlog_errno(ret);
2384 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2385 " returned %d\n", dlm->name, nodenum, ret);
2386 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2387 DLM_RECOVERY_LOCK_NAME_LEN);
2388 if (res) {
2389 dlm_print_one_lock_resource(res);
2390 dlm_lockres_put(res);
2391 } else {
2392 mlog(ML_ERROR, "recovery lock not found\n");
2393 }
Kurt Hackele2faea42006-01-12 14:24:55 -08002394 /* sleep for a bit in hopes that we can avoid
2395 * another ENOMEM */
2396 msleep(100);
2397 goto retry;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002398 } else if (ret == EAGAIN) {
2399 mlog(0, "%s: trying to start recovery of node "
2400 "%u, but node %u is waiting for last recovery "
2401 "to complete, backoff for a bit\n", dlm->name,
2402 dead_node, nodenum);
2403 /* TODO Look into replacing msleep with cond_resched() */
2404 msleep(100);
2405 goto retry;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002406 }
2407 }
2408
2409 return ret;
2410}
2411
2412int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2413{
2414 struct dlm_ctxt *dlm = data;
2415 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2416
2417 /* ok to return 0, domain has gone away */
2418 if (!dlm_grab(dlm))
2419 return 0;
2420
Kurt Hackel466d1a42006-05-01 11:11:13 -07002421 spin_lock(&dlm->spinlock);
2422 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2423 mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2424 "but this node is in finalize state, waiting on finalize2\n",
2425 dlm->name, br->node_idx, br->dead_node,
2426 dlm->reco.dead_node, dlm->reco.new_master);
2427 spin_unlock(&dlm->spinlock);
2428 return EAGAIN;
2429 }
2430 spin_unlock(&dlm->spinlock);
2431
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002432 mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2433 dlm->name, br->node_idx, br->dead_node,
2434 dlm->reco.dead_node, dlm->reco.new_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002435
2436 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2437
2438 spin_lock(&dlm->spinlock);
2439 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002440 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2441 mlog(0, "%s: new_master %u died, changing "
2442 "to %u\n", dlm->name, dlm->reco.new_master,
2443 br->node_idx);
2444 } else {
2445 mlog(0, "%s: new_master %u NOT DEAD, changing "
2446 "to %u\n", dlm->name, dlm->reco.new_master,
2447 br->node_idx);
2448 /* may not have seen the new master as dead yet */
2449 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002450 }
2451 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002452 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2453 "node %u changing it to %u\n", dlm->name,
2454 dlm->reco.dead_node, br->node_idx, br->dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002455 }
Kurt Hackelab27eb62006-04-27 18:03:49 -07002456 dlm_set_reco_master(dlm, br->node_idx);
2457 dlm_set_reco_dead_node(dlm, br->dead_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002458 if (!test_bit(br->dead_node, dlm->recovery_map)) {
Kurt Hackele2faea42006-01-12 14:24:55 -08002459 mlog(0, "recovery master %u sees %u as dead, but this "
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002460 "node has not yet. marking %u as dead\n",
2461 br->node_idx, br->dead_node, br->dead_node);
Kurt Hackele2faea42006-01-12 14:24:55 -08002462 if (!test_bit(br->dead_node, dlm->domain_map) ||
2463 !test_bit(br->dead_node, dlm->live_nodes_map))
2464 mlog(0, "%u not in domain/live_nodes map "
2465 "so setting it in reco map manually\n",
2466 br->dead_node);
Kurt Hackelc03872f2006-03-06 14:08:49 -08002467 /* force the recovery cleanup in __dlm_hb_node_down
2468 * both of these will be cleared in a moment */
2469 set_bit(br->dead_node, dlm->domain_map);
2470 set_bit(br->dead_node, dlm->live_nodes_map);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002471 __dlm_hb_node_down(dlm, br->dead_node);
2472 }
2473 spin_unlock(&dlm->spinlock);
2474
2475 dlm_kick_recovery_thread(dlm);
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002476
2477 mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2478 dlm->name, br->node_idx, br->dead_node,
2479 dlm->reco.dead_node, dlm->reco.new_master);
2480
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002481 dlm_put(dlm);
2482 return 0;
2483}
2484
Kurt Hackel466d1a42006-05-01 11:11:13 -07002485#define DLM_FINALIZE_STAGE2 0x01
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002486static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2487{
2488 int ret = 0;
2489 struct dlm_finalize_reco fr;
2490 struct dlm_node_iter iter;
2491 int nodenum;
2492 int status;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002493 int stage = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002494
Kurt Hackel466d1a42006-05-01 11:11:13 -07002495 mlog(0, "finishing recovery for node %s:%u, "
2496 "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002497
2498 spin_lock(&dlm->spinlock);
2499 dlm_node_iter_init(dlm->domain_map, &iter);
2500 spin_unlock(&dlm->spinlock);
2501
Kurt Hackel466d1a42006-05-01 11:11:13 -07002502stage2:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002503 memset(&fr, 0, sizeof(fr));
2504 fr.node_idx = dlm->node_num;
2505 fr.dead_node = dlm->reco.dead_node;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002506 if (stage == 2)
2507 fr.flags |= DLM_FINALIZE_STAGE2;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002508
2509 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2510 if (nodenum == dlm->node_num)
2511 continue;
2512 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2513 &fr, sizeof(fr), nodenum, &status);
Kurt Hackel466d1a42006-05-01 11:11:13 -07002514 if (ret >= 0)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002515 ret = status;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002516 if (ret < 0) {
2517 mlog_errno(ret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002518 if (dlm_is_host_down(ret)) {
2519 /* this has no effect on this recovery
2520 * session, so set the status to zero to
2521 * finish out the last recovery */
2522 mlog(ML_ERROR, "node %u went down after this "
2523 "node finished recovery.\n", nodenum);
2524 ret = 0;
2525 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002526 break;
2527 }
2528 }
Kurt Hackel466d1a42006-05-01 11:11:13 -07002529 if (stage == 1) {
2530 /* reset the node_iter back to the top and send finalize2 */
2531 iter.curnode = -1;
2532 stage = 2;
2533 goto stage2;
2534 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002535
2536 return ret;
2537}
2538
2539int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2540{
2541 struct dlm_ctxt *dlm = data;
2542 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
Kurt Hackel466d1a42006-05-01 11:11:13 -07002543 int stage = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002544
2545 /* ok to return 0, domain has gone away */
2546 if (!dlm_grab(dlm))
2547 return 0;
2548
Kurt Hackel466d1a42006-05-01 11:11:13 -07002549 if (fr->flags & DLM_FINALIZE_STAGE2)
2550 stage = 2;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002551
Kurt Hackel466d1a42006-05-01 11:11:13 -07002552 mlog(0, "%s: node %u finalizing recovery stage%d of "
2553 "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2554 fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2555
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002556 spin_lock(&dlm->spinlock);
2557
2558 if (dlm->reco.new_master != fr->node_idx) {
2559 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2560 "%u is supposed to be the new master, dead=%u\n",
2561 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2562 BUG();
2563 }
2564 if (dlm->reco.dead_node != fr->dead_node) {
2565 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2566 "node %u, but node %u is supposed to be dead\n",
2567 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2568 BUG();
2569 }
2570
Kurt Hackel466d1a42006-05-01 11:11:13 -07002571 switch (stage) {
2572 case 1:
2573 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2574 if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2575 mlog(ML_ERROR, "%s: received finalize1 from "
2576 "new master %u for dead node %u, but "
2577 "this node has already received it!\n",
2578 dlm->name, fr->node_idx, fr->dead_node);
2579 dlm_print_reco_node_status(dlm);
2580 BUG();
2581 }
2582 dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2583 spin_unlock(&dlm->spinlock);
2584 break;
2585 case 2:
2586 if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2587 mlog(ML_ERROR, "%s: received finalize2 from "
2588 "new master %u for dead node %u, but "
2589 "this node did not have finalize1!\n",
2590 dlm->name, fr->node_idx, fr->dead_node);
2591 dlm_print_reco_node_status(dlm);
2592 BUG();
2593 }
2594 dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2595 spin_unlock(&dlm->spinlock);
2596 dlm_reset_recovery(dlm);
2597 dlm_kick_recovery_thread(dlm);
2598 break;
2599 default:
2600 BUG();
2601 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002602
Kurt Hackeld6dea6e2006-04-27 18:08:51 -07002603 mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2604 dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2605
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002606 dlm_put(dlm);
2607 return 0;
2608}