xref: /linux/fs/ocfs2/dlm/dlmdomain.c (revision 564eb714f5f09ac733c26860d5f0831f213fbdf1)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmdomain.c
5  *
6  * defines domain join / leave apis
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 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/debugfs.h>
36 
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
40 
41 #include "dlmapi.h"
42 #include "dlmcommon.h"
43 #include "dlmdomain.h"
44 #include "dlmdebug.h"
45 
46 #include "dlmver.h"
47 
48 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
49 #include "cluster/masklog.h"
50 
51 /*
52  * ocfs2 node maps are array of long int, which limits to send them freely
53  * across the wire due to endianness issues. To workaround this, we convert
54  * long ints to byte arrays. Following 3 routines are helper functions to
55  * set/test/copy bits within those array of bytes
56  */
57 static inline void byte_set_bit(u8 nr, u8 map[])
58 {
59 	map[nr >> 3] |= (1UL << (nr & 7));
60 }
61 
62 static inline int byte_test_bit(u8 nr, u8 map[])
63 {
64 	return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
65 }
66 
67 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
68 			unsigned int sz)
69 {
70 	unsigned int nn;
71 
72 	if (!sz)
73 		return;
74 
75 	memset(dmap, 0, ((sz + 7) >> 3));
76 	for (nn = 0 ; nn < sz; nn++)
77 		if (test_bit(nn, smap))
78 			byte_set_bit(nn, dmap);
79 }
80 
81 static void dlm_free_pagevec(void **vec, int pages)
82 {
83 	while (pages--)
84 		free_page((unsigned long)vec[pages]);
85 	kfree(vec);
86 }
87 
88 static void **dlm_alloc_pagevec(int pages)
89 {
90 	void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
91 	int i;
92 
93 	if (!vec)
94 		return NULL;
95 
96 	for (i = 0; i < pages; i++)
97 		if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
98 			goto out_free;
99 
100 	mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
101 	     pages, (unsigned long)DLM_HASH_PAGES,
102 	     (unsigned long)DLM_BUCKETS_PER_PAGE);
103 	return vec;
104 out_free:
105 	dlm_free_pagevec(vec, i);
106 	return NULL;
107 }
108 
109 /*
110  *
111  * spinlock lock ordering: if multiple locks are needed, obey this ordering:
112  *    dlm_domain_lock
113  *    struct dlm_ctxt->spinlock
114  *    struct dlm_lock_resource->spinlock
115  *    struct dlm_ctxt->master_lock
116  *    struct dlm_ctxt->ast_lock
117  *    dlm_master_list_entry->spinlock
118  *    dlm_lock->spinlock
119  *
120  */
121 
122 DEFINE_SPINLOCK(dlm_domain_lock);
123 LIST_HEAD(dlm_domains);
124 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
125 
126 /*
127  * The supported protocol version for DLM communication.  Running domains
128  * will have a negotiated version with the same major number and a minor
129  * number equal or smaller.  The dlm_ctxt->dlm_locking_proto field should
130  * be used to determine what a running domain is actually using.
131  *
132  * New in version 1.1:
133  *	- Message DLM_QUERY_REGION added to support global heartbeat
134  *	- Message DLM_QUERY_NODEINFO added to allow online node removes
135  * New in version 1.2:
136  * 	- Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
137  */
138 static const struct dlm_protocol_version dlm_protocol = {
139 	.pv_major = 1,
140 	.pv_minor = 2,
141 };
142 
143 #define DLM_DOMAIN_BACKOFF_MS 200
144 
145 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
146 				  void **ret_data);
147 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
148 				     void **ret_data);
149 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
150 				   void **ret_data);
151 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
152 				    void *data, void **ret_data);
153 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
154 				   void **ret_data);
155 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
156 				struct dlm_protocol_version *request);
157 
158 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
159 
160 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
161 {
162 	if (hlist_unhashed(&res->hash_node))
163 		return;
164 
165 	mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
166 	     res->lockname.name);
167 	hlist_del_init(&res->hash_node);
168 	dlm_lockres_put(res);
169 }
170 
171 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
172 {
173 	struct hlist_head *bucket;
174 	struct qstr *q;
175 
176 	assert_spin_locked(&dlm->spinlock);
177 
178 	q = &res->lockname;
179 	bucket = dlm_lockres_hash(dlm, q->hash);
180 
181 	/* get a reference for our hashtable */
182 	dlm_lockres_get(res);
183 
184 	hlist_add_head(&res->hash_node, bucket);
185 
186 	mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
187 	     res->lockname.name);
188 }
189 
190 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
191 						     const char *name,
192 						     unsigned int len,
193 						     unsigned int hash)
194 {
195 	struct hlist_head *bucket;
196 	struct dlm_lock_resource *res;
197 
198 	mlog(0, "%.*s\n", len, name);
199 
200 	assert_spin_locked(&dlm->spinlock);
201 
202 	bucket = dlm_lockres_hash(dlm, hash);
203 
204 	hlist_for_each_entry(res, bucket, hash_node) {
205 		if (res->lockname.name[0] != name[0])
206 			continue;
207 		if (unlikely(res->lockname.len != len))
208 			continue;
209 		if (memcmp(res->lockname.name + 1, name + 1, len - 1))
210 			continue;
211 		dlm_lockres_get(res);
212 		return res;
213 	}
214 	return NULL;
215 }
216 
217 /* intended to be called by functions which do not care about lock
218  * resources which are being purged (most net _handler functions).
219  * this will return NULL for any lock resource which is found but
220  * currently in the process of dropping its mastery reference.
221  * use __dlm_lookup_lockres_full when you need the lock resource
222  * regardless (e.g. dlm_get_lock_resource) */
223 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
224 						const char *name,
225 						unsigned int len,
226 						unsigned int hash)
227 {
228 	struct dlm_lock_resource *res = NULL;
229 
230 	mlog(0, "%.*s\n", len, name);
231 
232 	assert_spin_locked(&dlm->spinlock);
233 
234 	res = __dlm_lookup_lockres_full(dlm, name, len, hash);
235 	if (res) {
236 		spin_lock(&res->spinlock);
237 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
238 			spin_unlock(&res->spinlock);
239 			dlm_lockres_put(res);
240 			return NULL;
241 		}
242 		spin_unlock(&res->spinlock);
243 	}
244 
245 	return res;
246 }
247 
248 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
249 				    const char *name,
250 				    unsigned int len)
251 {
252 	struct dlm_lock_resource *res;
253 	unsigned int hash = dlm_lockid_hash(name, len);
254 
255 	spin_lock(&dlm->spinlock);
256 	res = __dlm_lookup_lockres(dlm, name, len, hash);
257 	spin_unlock(&dlm->spinlock);
258 	return res;
259 }
260 
261 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
262 {
263 	struct dlm_ctxt *tmp;
264 
265 	assert_spin_locked(&dlm_domain_lock);
266 
267 	/* tmp->name here is always NULL terminated,
268 	 * but domain may not be! */
269 	list_for_each_entry(tmp, &dlm_domains, list) {
270 		if (strlen(tmp->name) == len &&
271 		    memcmp(tmp->name, domain, len)==0)
272 			return tmp;
273 	}
274 
275 	return NULL;
276 }
277 
278 /* For null terminated domain strings ONLY */
279 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
280 {
281 	assert_spin_locked(&dlm_domain_lock);
282 
283 	return __dlm_lookup_domain_full(domain, strlen(domain));
284 }
285 
286 
287 /* returns true on one of two conditions:
288  * 1) the domain does not exist
289  * 2) the domain exists and it's state is "joined" */
290 static int dlm_wait_on_domain_helper(const char *domain)
291 {
292 	int ret = 0;
293 	struct dlm_ctxt *tmp = NULL;
294 
295 	spin_lock(&dlm_domain_lock);
296 
297 	tmp = __dlm_lookup_domain(domain);
298 	if (!tmp)
299 		ret = 1;
300 	else if (tmp->dlm_state == DLM_CTXT_JOINED)
301 		ret = 1;
302 
303 	spin_unlock(&dlm_domain_lock);
304 	return ret;
305 }
306 
307 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
308 {
309 	dlm_destroy_debugfs_subroot(dlm);
310 
311 	if (dlm->lockres_hash)
312 		dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
313 
314 	if (dlm->master_hash)
315 		dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
316 
317 	kfree(dlm->name);
318 	kfree(dlm);
319 }
320 
321 /* A little strange - this function will be called while holding
322  * dlm_domain_lock and is expected to be holding it on the way out. We
323  * will however drop and reacquire it multiple times */
324 static void dlm_ctxt_release(struct kref *kref)
325 {
326 	struct dlm_ctxt *dlm;
327 
328 	dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
329 
330 	BUG_ON(dlm->num_joins);
331 	BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
332 
333 	/* we may still be in the list if we hit an error during join. */
334 	list_del_init(&dlm->list);
335 
336 	spin_unlock(&dlm_domain_lock);
337 
338 	mlog(0, "freeing memory from domain %s\n", dlm->name);
339 
340 	wake_up(&dlm_domain_events);
341 
342 	dlm_free_ctxt_mem(dlm);
343 
344 	spin_lock(&dlm_domain_lock);
345 }
346 
347 void dlm_put(struct dlm_ctxt *dlm)
348 {
349 	spin_lock(&dlm_domain_lock);
350 	kref_put(&dlm->dlm_refs, dlm_ctxt_release);
351 	spin_unlock(&dlm_domain_lock);
352 }
353 
354 static void __dlm_get(struct dlm_ctxt *dlm)
355 {
356 	kref_get(&dlm->dlm_refs);
357 }
358 
359 /* given a questionable reference to a dlm object, gets a reference if
360  * it can find it in the list, otherwise returns NULL in which case
361  * you shouldn't trust your pointer. */
362 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
363 {
364 	struct dlm_ctxt *target;
365 	struct dlm_ctxt *ret = NULL;
366 
367 	spin_lock(&dlm_domain_lock);
368 
369 	list_for_each_entry(target, &dlm_domains, list) {
370 		if (target == dlm) {
371 			__dlm_get(target);
372 			ret = target;
373 			break;
374 		}
375 	}
376 
377 	spin_unlock(&dlm_domain_lock);
378 
379 	return ret;
380 }
381 
382 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
383 {
384 	int ret;
385 
386 	spin_lock(&dlm_domain_lock);
387 	ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
388 		(dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
389 	spin_unlock(&dlm_domain_lock);
390 
391 	return ret;
392 }
393 
394 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
395 {
396 	if (dlm->dlm_worker) {
397 		flush_workqueue(dlm->dlm_worker);
398 		destroy_workqueue(dlm->dlm_worker);
399 		dlm->dlm_worker = NULL;
400 	}
401 }
402 
403 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
404 {
405 	dlm_unregister_domain_handlers(dlm);
406 	dlm_debug_shutdown(dlm);
407 	dlm_complete_thread(dlm);
408 	dlm_complete_recovery_thread(dlm);
409 	dlm_destroy_dlm_worker(dlm);
410 
411 	/* We've left the domain. Now we can take ourselves out of the
412 	 * list and allow the kref stuff to help us free the
413 	 * memory. */
414 	spin_lock(&dlm_domain_lock);
415 	list_del_init(&dlm->list);
416 	spin_unlock(&dlm_domain_lock);
417 
418 	/* Wake up anyone waiting for us to remove this domain */
419 	wake_up(&dlm_domain_events);
420 }
421 
422 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
423 {
424 	int i, num, n, ret = 0;
425 	struct dlm_lock_resource *res;
426 	struct hlist_node *iter;
427 	struct hlist_head *bucket;
428 	int dropped;
429 
430 	mlog(0, "Migrating locks from domain %s\n", dlm->name);
431 
432 	num = 0;
433 	spin_lock(&dlm->spinlock);
434 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
435 redo_bucket:
436 		n = 0;
437 		bucket = dlm_lockres_hash(dlm, i);
438 		iter = bucket->first;
439 		while (iter) {
440 			n++;
441 			res = hlist_entry(iter, struct dlm_lock_resource,
442 					  hash_node);
443 			dlm_lockres_get(res);
444 			/* migrate, if necessary.  this will drop the dlm
445 			 * spinlock and retake it if it does migration. */
446 			dropped = dlm_empty_lockres(dlm, res);
447 
448 			spin_lock(&res->spinlock);
449 			if (dropped)
450 				__dlm_lockres_calc_usage(dlm, res);
451 			else
452 				iter = res->hash_node.next;
453 			spin_unlock(&res->spinlock);
454 
455 			dlm_lockres_put(res);
456 
457 			if (dropped) {
458 				cond_resched_lock(&dlm->spinlock);
459 				goto redo_bucket;
460 			}
461 		}
462 		cond_resched_lock(&dlm->spinlock);
463 		num += n;
464 	}
465 	spin_unlock(&dlm->spinlock);
466 	wake_up(&dlm->dlm_thread_wq);
467 
468 	/* let the dlm thread take care of purging, keep scanning until
469 	 * nothing remains in the hash */
470 	if (num) {
471 		mlog(0, "%s: %d lock resources in hash last pass\n",
472 		     dlm->name, num);
473 		ret = -EAGAIN;
474 	}
475 	mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
476 	return ret;
477 }
478 
479 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
480 {
481 	int ret;
482 
483 	spin_lock(&dlm->spinlock);
484 	ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
485 	spin_unlock(&dlm->spinlock);
486 
487 	return ret;
488 }
489 
490 static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
491 					 void *data, void **ret_data)
492 {
493 	struct dlm_ctxt *dlm = data;
494 	unsigned int node;
495 	struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
496 
497 	if (!dlm_grab(dlm))
498 		return 0;
499 
500 	node = exit_msg->node_idx;
501 	mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
502 
503 	spin_lock(&dlm->spinlock);
504 	set_bit(node, dlm->exit_domain_map);
505 	spin_unlock(&dlm->spinlock);
506 
507 	dlm_put(dlm);
508 
509 	return 0;
510 }
511 
512 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
513 {
514 	/* Yikes, a double spinlock! I need domain_lock for the dlm
515 	 * state and the dlm spinlock for join state... Sorry! */
516 again:
517 	spin_lock(&dlm_domain_lock);
518 	spin_lock(&dlm->spinlock);
519 
520 	if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
521 		mlog(0, "Node %d is joining, we wait on it.\n",
522 			  dlm->joining_node);
523 		spin_unlock(&dlm->spinlock);
524 		spin_unlock(&dlm_domain_lock);
525 
526 		wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
527 		goto again;
528 	}
529 
530 	dlm->dlm_state = DLM_CTXT_LEAVING;
531 	spin_unlock(&dlm->spinlock);
532 	spin_unlock(&dlm_domain_lock);
533 }
534 
535 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
536 {
537 	int node = -1, num = 0;
538 
539 	assert_spin_locked(&dlm->spinlock);
540 
541 	printk("( ");
542 	while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
543 				     node + 1)) < O2NM_MAX_NODES) {
544 		printk("%d ", node);
545 		++num;
546 	}
547 	printk(") %u nodes\n", num);
548 }
549 
550 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
551 				   void **ret_data)
552 {
553 	struct dlm_ctxt *dlm = data;
554 	unsigned int node;
555 	struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
556 
557 	mlog(0, "%p %u %p", msg, len, data);
558 
559 	if (!dlm_grab(dlm))
560 		return 0;
561 
562 	node = exit_msg->node_idx;
563 
564 	spin_lock(&dlm->spinlock);
565 	clear_bit(node, dlm->domain_map);
566 	clear_bit(node, dlm->exit_domain_map);
567 	printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
568 	__dlm_print_nodes(dlm);
569 
570 	/* notify anything attached to the heartbeat events */
571 	dlm_hb_event_notify_attached(dlm, node, 0);
572 
573 	spin_unlock(&dlm->spinlock);
574 
575 	dlm_put(dlm);
576 
577 	return 0;
578 }
579 
580 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
581 				    unsigned int node)
582 {
583 	int status;
584 	struct dlm_exit_domain leave_msg;
585 
586 	mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
587 	     msg_type, node);
588 
589 	memset(&leave_msg, 0, sizeof(leave_msg));
590 	leave_msg.node_idx = dlm->node_num;
591 
592 	status = o2net_send_message(msg_type, dlm->key, &leave_msg,
593 				    sizeof(leave_msg), node, NULL);
594 	if (status < 0)
595 		mlog(ML_ERROR, "Error %d sending domain exit message %u "
596 		     "to node %u on domain %s\n", status, msg_type, node,
597 		     dlm->name);
598 
599 	return status;
600 }
601 
602 static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
603 {
604 	int node = -1;
605 
606 	/* Support for begin exit domain was added in 1.2 */
607 	if (dlm->dlm_locking_proto.pv_major == 1 &&
608 	    dlm->dlm_locking_proto.pv_minor < 2)
609 		return;
610 
611 	/*
612 	 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
613 	 * informational. Meaning if a node does not receive the message,
614 	 * so be it.
615 	 */
616 	spin_lock(&dlm->spinlock);
617 	while (1) {
618 		node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
619 		if (node >= O2NM_MAX_NODES)
620 			break;
621 		if (node == dlm->node_num)
622 			continue;
623 
624 		spin_unlock(&dlm->spinlock);
625 		dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
626 		spin_lock(&dlm->spinlock);
627 	}
628 	spin_unlock(&dlm->spinlock);
629 }
630 
631 static void dlm_leave_domain(struct dlm_ctxt *dlm)
632 {
633 	int node, clear_node, status;
634 
635 	/* At this point we've migrated away all our locks and won't
636 	 * accept mastership of new ones. The dlm is responsible for
637 	 * almost nothing now. We make sure not to confuse any joining
638 	 * nodes and then commence shutdown procedure. */
639 
640 	spin_lock(&dlm->spinlock);
641 	/* Clear ourselves from the domain map */
642 	clear_bit(dlm->node_num, dlm->domain_map);
643 	while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
644 				     0)) < O2NM_MAX_NODES) {
645 		/* Drop the dlm spinlock. This is safe wrt the domain_map.
646 		 * -nodes cannot be added now as the
647 		 *   query_join_handlers knows to respond with OK_NO_MAP
648 		 * -we catch the right network errors if a node is
649 		 *   removed from the map while we're sending him the
650 		 *   exit message. */
651 		spin_unlock(&dlm->spinlock);
652 
653 		clear_node = 1;
654 
655 		status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
656 						  node);
657 		if (status < 0 &&
658 		    status != -ENOPROTOOPT &&
659 		    status != -ENOTCONN) {
660 			mlog(ML_NOTICE, "Error %d sending domain exit message "
661 			     "to node %d\n", status, node);
662 
663 			/* Not sure what to do here but lets sleep for
664 			 * a bit in case this was a transient
665 			 * error... */
666 			msleep(DLM_DOMAIN_BACKOFF_MS);
667 			clear_node = 0;
668 		}
669 
670 		spin_lock(&dlm->spinlock);
671 		/* If we're not clearing the node bit then we intend
672 		 * to loop back around to try again. */
673 		if (clear_node)
674 			clear_bit(node, dlm->domain_map);
675 	}
676 	spin_unlock(&dlm->spinlock);
677 }
678 
679 int dlm_joined(struct dlm_ctxt *dlm)
680 {
681 	int ret = 0;
682 
683 	spin_lock(&dlm_domain_lock);
684 
685 	if (dlm->dlm_state == DLM_CTXT_JOINED)
686 		ret = 1;
687 
688 	spin_unlock(&dlm_domain_lock);
689 
690 	return ret;
691 }
692 
693 int dlm_shutting_down(struct dlm_ctxt *dlm)
694 {
695 	int ret = 0;
696 
697 	spin_lock(&dlm_domain_lock);
698 
699 	if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
700 		ret = 1;
701 
702 	spin_unlock(&dlm_domain_lock);
703 
704 	return ret;
705 }
706 
707 void dlm_unregister_domain(struct dlm_ctxt *dlm)
708 {
709 	int leave = 0;
710 	struct dlm_lock_resource *res;
711 
712 	spin_lock(&dlm_domain_lock);
713 	BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
714 	BUG_ON(!dlm->num_joins);
715 
716 	dlm->num_joins--;
717 	if (!dlm->num_joins) {
718 		/* We mark it "in shutdown" now so new register
719 		 * requests wait until we've completely left the
720 		 * domain. Don't use DLM_CTXT_LEAVING yet as we still
721 		 * want new domain joins to communicate with us at
722 		 * least until we've completed migration of our
723 		 * resources. */
724 		dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
725 		leave = 1;
726 	}
727 	spin_unlock(&dlm_domain_lock);
728 
729 	if (leave) {
730 		mlog(0, "shutting down domain %s\n", dlm->name);
731 		dlm_begin_exit_domain(dlm);
732 
733 		/* We changed dlm state, notify the thread */
734 		dlm_kick_thread(dlm, NULL);
735 
736 		while (dlm_migrate_all_locks(dlm)) {
737 			/* Give dlm_thread time to purge the lockres' */
738 			msleep(500);
739 			mlog(0, "%s: more migration to do\n", dlm->name);
740 		}
741 
742 		/* This list should be empty. If not, print remaining lockres */
743 		if (!list_empty(&dlm->tracking_list)) {
744 			mlog(ML_ERROR, "Following lockres' are still on the "
745 			     "tracking list:\n");
746 			list_for_each_entry(res, &dlm->tracking_list, tracking)
747 				dlm_print_one_lock_resource(res);
748 		}
749 
750 		dlm_mark_domain_leaving(dlm);
751 		dlm_leave_domain(dlm);
752 		printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
753 		dlm_force_free_mles(dlm);
754 		dlm_complete_dlm_shutdown(dlm);
755 	}
756 	dlm_put(dlm);
757 }
758 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
759 
760 static int dlm_query_join_proto_check(char *proto_type, int node,
761 				      struct dlm_protocol_version *ours,
762 				      struct dlm_protocol_version *request)
763 {
764 	int rc;
765 	struct dlm_protocol_version proto = *request;
766 
767 	if (!dlm_protocol_compare(ours, &proto)) {
768 		mlog(0,
769 		     "node %u wanted to join with %s locking protocol "
770 		     "%u.%u, we respond with %u.%u\n",
771 		     node, proto_type,
772 		     request->pv_major,
773 		     request->pv_minor,
774 		     proto.pv_major, proto.pv_minor);
775 		request->pv_minor = proto.pv_minor;
776 		rc = 0;
777 	} else {
778 		mlog(ML_NOTICE,
779 		     "Node %u wanted to join with %s locking "
780 		     "protocol %u.%u, but we have %u.%u, disallowing\n",
781 		     node, proto_type,
782 		     request->pv_major,
783 		     request->pv_minor,
784 		     ours->pv_major,
785 		     ours->pv_minor);
786 		rc = 1;
787 	}
788 
789 	return rc;
790 }
791 
792 /*
793  * struct dlm_query_join_packet is made up of four one-byte fields.  They
794  * are effectively in big-endian order already.  However, little-endian
795  * machines swap them before putting the packet on the wire (because
796  * query_join's response is a status, and that status is treated as a u32
797  * on the wire).  Thus, a big-endian and little-endian machines will treat
798  * this structure differently.
799  *
800  * The solution is to have little-endian machines swap the structure when
801  * converting from the structure to the u32 representation.  This will
802  * result in the structure having the correct format on the wire no matter
803  * the host endian format.
804  */
805 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
806 					  u32 *wire)
807 {
808 	union dlm_query_join_response response;
809 
810 	response.packet = *packet;
811 	*wire = be32_to_cpu(response.intval);
812 }
813 
814 static void dlm_query_join_wire_to_packet(u32 wire,
815 					  struct dlm_query_join_packet *packet)
816 {
817 	union dlm_query_join_response response;
818 
819 	response.intval = cpu_to_be32(wire);
820 	*packet = response.packet;
821 }
822 
823 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
824 				  void **ret_data)
825 {
826 	struct dlm_query_join_request *query;
827 	struct dlm_query_join_packet packet = {
828 		.code = JOIN_DISALLOW,
829 	};
830 	struct dlm_ctxt *dlm = NULL;
831 	u32 response;
832 	u8 nodenum;
833 
834 	query = (struct dlm_query_join_request *) msg->buf;
835 
836 	mlog(0, "node %u wants to join domain %s\n", query->node_idx,
837 		  query->domain);
838 
839 	/*
840 	 * If heartbeat doesn't consider the node live, tell it
841 	 * to back off and try again.  This gives heartbeat a chance
842 	 * to catch up.
843 	 */
844 	if (!o2hb_check_node_heartbeating(query->node_idx)) {
845 		mlog(0, "node %u is not in our live map yet\n",
846 		     query->node_idx);
847 
848 		packet.code = JOIN_DISALLOW;
849 		goto respond;
850 	}
851 
852 	packet.code = JOIN_OK_NO_MAP;
853 
854 	spin_lock(&dlm_domain_lock);
855 	dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
856 	if (!dlm)
857 		goto unlock_respond;
858 
859 	/*
860 	 * There is a small window where the joining node may not see the
861 	 * node(s) that just left but still part of the cluster. DISALLOW
862 	 * join request if joining node has different node map.
863 	 */
864 	nodenum=0;
865 	while (nodenum < O2NM_MAX_NODES) {
866 		if (test_bit(nodenum, dlm->domain_map)) {
867 			if (!byte_test_bit(nodenum, query->node_map)) {
868 				mlog(0, "disallow join as node %u does not "
869 				     "have node %u in its nodemap\n",
870 				     query->node_idx, nodenum);
871 				packet.code = JOIN_DISALLOW;
872 				goto unlock_respond;
873 			}
874 		}
875 		nodenum++;
876 	}
877 
878 	/* Once the dlm ctxt is marked as leaving then we don't want
879 	 * to be put in someone's domain map.
880 	 * Also, explicitly disallow joining at certain troublesome
881 	 * times (ie. during recovery). */
882 	if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
883 		int bit = query->node_idx;
884 		spin_lock(&dlm->spinlock);
885 
886 		if (dlm->dlm_state == DLM_CTXT_NEW &&
887 		    dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
888 			/*If this is a brand new context and we
889 			 * haven't started our join process yet, then
890 			 * the other node won the race. */
891 			packet.code = JOIN_OK_NO_MAP;
892 		} else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
893 			/* Disallow parallel joins. */
894 			packet.code = JOIN_DISALLOW;
895 		} else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
896 			mlog(0, "node %u trying to join, but recovery "
897 			     "is ongoing.\n", bit);
898 			packet.code = JOIN_DISALLOW;
899 		} else if (test_bit(bit, dlm->recovery_map)) {
900 			mlog(0, "node %u trying to join, but it "
901 			     "still needs recovery.\n", bit);
902 			packet.code = JOIN_DISALLOW;
903 		} else if (test_bit(bit, dlm->domain_map)) {
904 			mlog(0, "node %u trying to join, but it "
905 			     "is still in the domain! needs recovery?\n",
906 			     bit);
907 			packet.code = JOIN_DISALLOW;
908 		} else {
909 			/* Alright we're fully a part of this domain
910 			 * so we keep some state as to who's joining
911 			 * and indicate to him that needs to be fixed
912 			 * up. */
913 
914 			/* Make sure we speak compatible locking protocols.  */
915 			if (dlm_query_join_proto_check("DLM", bit,
916 						       &dlm->dlm_locking_proto,
917 						       &query->dlm_proto)) {
918 				packet.code = JOIN_PROTOCOL_MISMATCH;
919 			} else if (dlm_query_join_proto_check("fs", bit,
920 							      &dlm->fs_locking_proto,
921 							      &query->fs_proto)) {
922 				packet.code = JOIN_PROTOCOL_MISMATCH;
923 			} else {
924 				packet.dlm_minor = query->dlm_proto.pv_minor;
925 				packet.fs_minor = query->fs_proto.pv_minor;
926 				packet.code = JOIN_OK;
927 				__dlm_set_joining_node(dlm, query->node_idx);
928 			}
929 		}
930 
931 		spin_unlock(&dlm->spinlock);
932 	}
933 unlock_respond:
934 	spin_unlock(&dlm_domain_lock);
935 
936 respond:
937 	mlog(0, "We respond with %u\n", packet.code);
938 
939 	dlm_query_join_packet_to_wire(&packet, &response);
940 	return response;
941 }
942 
943 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
944 				     void **ret_data)
945 {
946 	struct dlm_assert_joined *assert;
947 	struct dlm_ctxt *dlm = NULL;
948 
949 	assert = (struct dlm_assert_joined *) msg->buf;
950 
951 	mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
952 		  assert->domain);
953 
954 	spin_lock(&dlm_domain_lock);
955 	dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
956 	/* XXX should we consider no dlm ctxt an error? */
957 	if (dlm) {
958 		spin_lock(&dlm->spinlock);
959 
960 		/* Alright, this node has officially joined our
961 		 * domain. Set him in the map and clean up our
962 		 * leftover join state. */
963 		BUG_ON(dlm->joining_node != assert->node_idx);
964 		set_bit(assert->node_idx, dlm->domain_map);
965 		clear_bit(assert->node_idx, dlm->exit_domain_map);
966 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
967 
968 		printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
969 		       assert->node_idx, dlm->name);
970 		__dlm_print_nodes(dlm);
971 
972 		/* notify anything attached to the heartbeat events */
973 		dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
974 
975 		spin_unlock(&dlm->spinlock);
976 	}
977 	spin_unlock(&dlm_domain_lock);
978 
979 	return 0;
980 }
981 
982 static int dlm_match_regions(struct dlm_ctxt *dlm,
983 			     struct dlm_query_region *qr,
984 			     char *local, int locallen)
985 {
986 	char *remote = qr->qr_regions;
987 	char *l, *r;
988 	int localnr, i, j, foundit;
989 	int status = 0;
990 
991 	if (!o2hb_global_heartbeat_active()) {
992 		if (qr->qr_numregions) {
993 			mlog(ML_ERROR, "Domain %s: Joining node %d has global "
994 			     "heartbeat enabled but local node %d does not\n",
995 			     qr->qr_domain, qr->qr_node, dlm->node_num);
996 			status = -EINVAL;
997 		}
998 		goto bail;
999 	}
1000 
1001 	if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
1002 		mlog(ML_ERROR, "Domain %s: Local node %d has global "
1003 		     "heartbeat enabled but joining node %d does not\n",
1004 		     qr->qr_domain, dlm->node_num, qr->qr_node);
1005 		status = -EINVAL;
1006 		goto bail;
1007 	}
1008 
1009 	r = remote;
1010 	for (i = 0; i < qr->qr_numregions; ++i) {
1011 		mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
1012 		r += O2HB_MAX_REGION_NAME_LEN;
1013 	}
1014 
1015 	localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
1016 	localnr = o2hb_get_all_regions(local, (u8)localnr);
1017 
1018 	/* compare local regions with remote */
1019 	l = local;
1020 	for (i = 0; i < localnr; ++i) {
1021 		foundit = 0;
1022 		r = remote;
1023 		for (j = 0; j <= qr->qr_numregions; ++j) {
1024 			if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1025 				foundit = 1;
1026 				break;
1027 			}
1028 			r += O2HB_MAX_REGION_NAME_LEN;
1029 		}
1030 		if (!foundit) {
1031 			status = -EINVAL;
1032 			mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1033 			     "in local node %d but not in joining node %d\n",
1034 			     qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1035 			     dlm->node_num, qr->qr_node);
1036 			goto bail;
1037 		}
1038 		l += O2HB_MAX_REGION_NAME_LEN;
1039 	}
1040 
1041 	/* compare remote with local regions */
1042 	r = remote;
1043 	for (i = 0; i < qr->qr_numregions; ++i) {
1044 		foundit = 0;
1045 		l = local;
1046 		for (j = 0; j < localnr; ++j) {
1047 			if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1048 				foundit = 1;
1049 				break;
1050 			}
1051 			l += O2HB_MAX_REGION_NAME_LEN;
1052 		}
1053 		if (!foundit) {
1054 			status = -EINVAL;
1055 			mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1056 			     "in joining node %d but not in local node %d\n",
1057 			     qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1058 			     qr->qr_node, dlm->node_num);
1059 			goto bail;
1060 		}
1061 		r += O2HB_MAX_REGION_NAME_LEN;
1062 	}
1063 
1064 bail:
1065 	return status;
1066 }
1067 
1068 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1069 {
1070 	struct dlm_query_region *qr = NULL;
1071 	int status, ret = 0, i;
1072 	char *p;
1073 
1074 	if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1075 		goto bail;
1076 
1077 	qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1078 	if (!qr) {
1079 		ret = -ENOMEM;
1080 		mlog_errno(ret);
1081 		goto bail;
1082 	}
1083 
1084 	qr->qr_node = dlm->node_num;
1085 	qr->qr_namelen = strlen(dlm->name);
1086 	memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1087 	/* if local hb, the numregions will be zero */
1088 	if (o2hb_global_heartbeat_active())
1089 		qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1090 							 O2NM_MAX_REGIONS);
1091 
1092 	p = qr->qr_regions;
1093 	for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1094 		mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1095 
1096 	i = -1;
1097 	while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1098 				  i + 1)) < O2NM_MAX_NODES) {
1099 		if (i == dlm->node_num)
1100 			continue;
1101 
1102 		mlog(0, "Sending regions to node %d\n", i);
1103 
1104 		ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1105 					 sizeof(struct dlm_query_region),
1106 					 i, &status);
1107 		if (ret >= 0)
1108 			ret = status;
1109 		if (ret) {
1110 			mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1111 			     ret, i);
1112 			break;
1113 		}
1114 	}
1115 
1116 bail:
1117 	kfree(qr);
1118 	return ret;
1119 }
1120 
1121 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1122 				    void *data, void **ret_data)
1123 {
1124 	struct dlm_query_region *qr;
1125 	struct dlm_ctxt *dlm = NULL;
1126 	char *local = NULL;
1127 	int status = 0;
1128 	int locked = 0;
1129 
1130 	qr = (struct dlm_query_region *) msg->buf;
1131 
1132 	mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1133 	     qr->qr_domain);
1134 
1135 	/* buffer used in dlm_mast_regions() */
1136 	local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1137 	if (!local) {
1138 		status = -ENOMEM;
1139 		goto bail;
1140 	}
1141 
1142 	status = -EINVAL;
1143 
1144 	spin_lock(&dlm_domain_lock);
1145 	dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1146 	if (!dlm) {
1147 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1148 		     "before join domain\n", qr->qr_node, qr->qr_domain);
1149 		goto bail;
1150 	}
1151 
1152 	spin_lock(&dlm->spinlock);
1153 	locked = 1;
1154 	if (dlm->joining_node != qr->qr_node) {
1155 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1156 		     "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1157 		     dlm->joining_node);
1158 		goto bail;
1159 	}
1160 
1161 	/* Support for global heartbeat was added in 1.1 */
1162 	if (dlm->dlm_locking_proto.pv_major == 1 &&
1163 	    dlm->dlm_locking_proto.pv_minor == 0) {
1164 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1165 		     "but active dlm protocol is %d.%d\n", qr->qr_node,
1166 		     qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1167 		     dlm->dlm_locking_proto.pv_minor);
1168 		goto bail;
1169 	}
1170 
1171 	status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1172 
1173 bail:
1174 	if (locked)
1175 		spin_unlock(&dlm->spinlock);
1176 	spin_unlock(&dlm_domain_lock);
1177 
1178 	kfree(local);
1179 
1180 	return status;
1181 }
1182 
1183 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1184 {
1185 	struct o2nm_node *local;
1186 	struct dlm_node_info *remote;
1187 	int i, j;
1188 	int status = 0;
1189 
1190 	for (j = 0; j < qn->qn_numnodes; ++j)
1191 		mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1192 		     &(qn->qn_nodes[j].ni_ipv4_address),
1193 		     ntohs(qn->qn_nodes[j].ni_ipv4_port));
1194 
1195 	for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1196 		local = o2nm_get_node_by_num(i);
1197 		remote = NULL;
1198 		for (j = 0; j < qn->qn_numnodes; ++j) {
1199 			if (qn->qn_nodes[j].ni_nodenum == i) {
1200 				remote = &(qn->qn_nodes[j]);
1201 				break;
1202 			}
1203 		}
1204 
1205 		if (!local && !remote)
1206 			continue;
1207 
1208 		if ((local && !remote) || (!local && remote))
1209 			status = -EINVAL;
1210 
1211 		if (!status &&
1212 		    ((remote->ni_nodenum != local->nd_num) ||
1213 		     (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1214 		     (remote->ni_ipv4_address != local->nd_ipv4_address)))
1215 			status = -EINVAL;
1216 
1217 		if (status) {
1218 			if (remote && !local)
1219 				mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1220 				     "registered in joining node %d but not in "
1221 				     "local node %d\n", qn->qn_domain,
1222 				     remote->ni_nodenum,
1223 				     &(remote->ni_ipv4_address),
1224 				     ntohs(remote->ni_ipv4_port),
1225 				     qn->qn_nodenum, dlm->node_num);
1226 			if (local && !remote)
1227 				mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1228 				     "registered in local node %d but not in "
1229 				     "joining node %d\n", qn->qn_domain,
1230 				     local->nd_num, &(local->nd_ipv4_address),
1231 				     ntohs(local->nd_ipv4_port),
1232 				     dlm->node_num, qn->qn_nodenum);
1233 			BUG_ON((!local && !remote));
1234 		}
1235 
1236 		if (local)
1237 			o2nm_node_put(local);
1238 	}
1239 
1240 	return status;
1241 }
1242 
1243 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1244 {
1245 	struct dlm_query_nodeinfo *qn = NULL;
1246 	struct o2nm_node *node;
1247 	int ret = 0, status, count, i;
1248 
1249 	if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1250 		goto bail;
1251 
1252 	qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1253 	if (!qn) {
1254 		ret = -ENOMEM;
1255 		mlog_errno(ret);
1256 		goto bail;
1257 	}
1258 
1259 	for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1260 		node = o2nm_get_node_by_num(i);
1261 		if (!node)
1262 			continue;
1263 		qn->qn_nodes[count].ni_nodenum = node->nd_num;
1264 		qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1265 		qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1266 		mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1267 		     &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1268 		++count;
1269 		o2nm_node_put(node);
1270 	}
1271 
1272 	qn->qn_nodenum = dlm->node_num;
1273 	qn->qn_numnodes = count;
1274 	qn->qn_namelen = strlen(dlm->name);
1275 	memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1276 
1277 	i = -1;
1278 	while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1279 				  i + 1)) < O2NM_MAX_NODES) {
1280 		if (i == dlm->node_num)
1281 			continue;
1282 
1283 		mlog(0, "Sending nodeinfo to node %d\n", i);
1284 
1285 		ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1286 					 qn, sizeof(struct dlm_query_nodeinfo),
1287 					 i, &status);
1288 		if (ret >= 0)
1289 			ret = status;
1290 		if (ret) {
1291 			mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1292 			break;
1293 		}
1294 	}
1295 
1296 bail:
1297 	kfree(qn);
1298 	return ret;
1299 }
1300 
1301 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1302 				      void *data, void **ret_data)
1303 {
1304 	struct dlm_query_nodeinfo *qn;
1305 	struct dlm_ctxt *dlm = NULL;
1306 	int locked = 0, status = -EINVAL;
1307 
1308 	qn = (struct dlm_query_nodeinfo *) msg->buf;
1309 
1310 	mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1311 	     qn->qn_domain);
1312 
1313 	spin_lock(&dlm_domain_lock);
1314 	dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1315 	if (!dlm) {
1316 		mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1317 		     "join domain\n", qn->qn_nodenum, qn->qn_domain);
1318 		goto bail;
1319 	}
1320 
1321 	spin_lock(&dlm->spinlock);
1322 	locked = 1;
1323 	if (dlm->joining_node != qn->qn_nodenum) {
1324 		mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1325 		     "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1326 		     dlm->joining_node);
1327 		goto bail;
1328 	}
1329 
1330 	/* Support for node query was added in 1.1 */
1331 	if (dlm->dlm_locking_proto.pv_major == 1 &&
1332 	    dlm->dlm_locking_proto.pv_minor == 0) {
1333 		mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1334 		     "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1335 		     qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1336 		     dlm->dlm_locking_proto.pv_minor);
1337 		goto bail;
1338 	}
1339 
1340 	status = dlm_match_nodes(dlm, qn);
1341 
1342 bail:
1343 	if (locked)
1344 		spin_unlock(&dlm->spinlock);
1345 	spin_unlock(&dlm_domain_lock);
1346 
1347 	return status;
1348 }
1349 
1350 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1351 				   void **ret_data)
1352 {
1353 	struct dlm_cancel_join *cancel;
1354 	struct dlm_ctxt *dlm = NULL;
1355 
1356 	cancel = (struct dlm_cancel_join *) msg->buf;
1357 
1358 	mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1359 		  cancel->domain);
1360 
1361 	spin_lock(&dlm_domain_lock);
1362 	dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1363 
1364 	if (dlm) {
1365 		spin_lock(&dlm->spinlock);
1366 
1367 		/* Yikes, this guy wants to cancel his join. No
1368 		 * problem, we simply cleanup our join state. */
1369 		BUG_ON(dlm->joining_node != cancel->node_idx);
1370 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1371 
1372 		spin_unlock(&dlm->spinlock);
1373 	}
1374 	spin_unlock(&dlm_domain_lock);
1375 
1376 	return 0;
1377 }
1378 
1379 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1380 				    unsigned int node)
1381 {
1382 	int status;
1383 	struct dlm_cancel_join cancel_msg;
1384 
1385 	memset(&cancel_msg, 0, sizeof(cancel_msg));
1386 	cancel_msg.node_idx = dlm->node_num;
1387 	cancel_msg.name_len = strlen(dlm->name);
1388 	memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1389 
1390 	status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1391 				    &cancel_msg, sizeof(cancel_msg), node,
1392 				    NULL);
1393 	if (status < 0) {
1394 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1395 		     "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1396 		     node);
1397 		goto bail;
1398 	}
1399 
1400 bail:
1401 	return status;
1402 }
1403 
1404 /* map_size should be in bytes. */
1405 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1406 				 unsigned long *node_map,
1407 				 unsigned int map_size)
1408 {
1409 	int status, tmpstat;
1410 	unsigned int node;
1411 
1412 	if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1413 			 sizeof(unsigned long))) {
1414 		mlog(ML_ERROR,
1415 		     "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1416 		     map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1417 		return -EINVAL;
1418 	}
1419 
1420 	status = 0;
1421 	node = -1;
1422 	while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1423 				     node + 1)) < O2NM_MAX_NODES) {
1424 		if (node == dlm->node_num)
1425 			continue;
1426 
1427 		tmpstat = dlm_send_one_join_cancel(dlm, node);
1428 		if (tmpstat) {
1429 			mlog(ML_ERROR, "Error return %d cancelling join on "
1430 			     "node %d\n", tmpstat, node);
1431 			if (!status)
1432 				status = tmpstat;
1433 		}
1434 	}
1435 
1436 	if (status)
1437 		mlog_errno(status);
1438 	return status;
1439 }
1440 
1441 static int dlm_request_join(struct dlm_ctxt *dlm,
1442 			    int node,
1443 			    enum dlm_query_join_response_code *response)
1444 {
1445 	int status;
1446 	struct dlm_query_join_request join_msg;
1447 	struct dlm_query_join_packet packet;
1448 	u32 join_resp;
1449 
1450 	mlog(0, "querying node %d\n", node);
1451 
1452 	memset(&join_msg, 0, sizeof(join_msg));
1453 	join_msg.node_idx = dlm->node_num;
1454 	join_msg.name_len = strlen(dlm->name);
1455 	memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1456 	join_msg.dlm_proto = dlm->dlm_locking_proto;
1457 	join_msg.fs_proto = dlm->fs_locking_proto;
1458 
1459 	/* copy live node map to join message */
1460 	byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1461 
1462 	status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1463 				    sizeof(join_msg), node, &join_resp);
1464 	if (status < 0 && status != -ENOPROTOOPT) {
1465 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1466 		     "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1467 		     node);
1468 		goto bail;
1469 	}
1470 	dlm_query_join_wire_to_packet(join_resp, &packet);
1471 
1472 	/* -ENOPROTOOPT from the net code means the other side isn't
1473 	    listening for our message type -- that's fine, it means
1474 	    his dlm isn't up, so we can consider him a 'yes' but not
1475 	    joined into the domain.  */
1476 	if (status == -ENOPROTOOPT) {
1477 		status = 0;
1478 		*response = JOIN_OK_NO_MAP;
1479 	} else if (packet.code == JOIN_DISALLOW ||
1480 		   packet.code == JOIN_OK_NO_MAP) {
1481 		*response = packet.code;
1482 	} else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
1483 		mlog(ML_NOTICE,
1484 		     "This node requested DLM locking protocol %u.%u and "
1485 		     "filesystem locking protocol %u.%u.  At least one of "
1486 		     "the protocol versions on node %d is not compatible, "
1487 		     "disconnecting\n",
1488 		     dlm->dlm_locking_proto.pv_major,
1489 		     dlm->dlm_locking_proto.pv_minor,
1490 		     dlm->fs_locking_proto.pv_major,
1491 		     dlm->fs_locking_proto.pv_minor,
1492 		     node);
1493 		status = -EPROTO;
1494 		*response = packet.code;
1495 	} else if (packet.code == JOIN_OK) {
1496 		*response = packet.code;
1497 		/* Use the same locking protocol as the remote node */
1498 		dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1499 		dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1500 		mlog(0,
1501 		     "Node %d responds JOIN_OK with DLM locking protocol "
1502 		     "%u.%u and fs locking protocol %u.%u\n",
1503 		     node,
1504 		     dlm->dlm_locking_proto.pv_major,
1505 		     dlm->dlm_locking_proto.pv_minor,
1506 		     dlm->fs_locking_proto.pv_major,
1507 		     dlm->fs_locking_proto.pv_minor);
1508 	} else {
1509 		status = -EINVAL;
1510 		mlog(ML_ERROR, "invalid response %d from node %u\n",
1511 		     packet.code, node);
1512 	}
1513 
1514 	mlog(0, "status %d, node %d response is %d\n", status, node,
1515 	     *response);
1516 
1517 bail:
1518 	return status;
1519 }
1520 
1521 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1522 				    unsigned int node)
1523 {
1524 	int status;
1525 	struct dlm_assert_joined assert_msg;
1526 
1527 	mlog(0, "Sending join assert to node %u\n", node);
1528 
1529 	memset(&assert_msg, 0, sizeof(assert_msg));
1530 	assert_msg.node_idx = dlm->node_num;
1531 	assert_msg.name_len = strlen(dlm->name);
1532 	memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1533 
1534 	status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1535 				    &assert_msg, sizeof(assert_msg), node,
1536 				    NULL);
1537 	if (status < 0)
1538 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1539 		     "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1540 		     node);
1541 
1542 	return status;
1543 }
1544 
1545 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1546 				  unsigned long *node_map)
1547 {
1548 	int status, node, live;
1549 
1550 	status = 0;
1551 	node = -1;
1552 	while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1553 				     node + 1)) < O2NM_MAX_NODES) {
1554 		if (node == dlm->node_num)
1555 			continue;
1556 
1557 		do {
1558 			/* It is very important that this message be
1559 			 * received so we spin until either the node
1560 			 * has died or it gets the message. */
1561 			status = dlm_send_one_join_assert(dlm, node);
1562 
1563 			spin_lock(&dlm->spinlock);
1564 			live = test_bit(node, dlm->live_nodes_map);
1565 			spin_unlock(&dlm->spinlock);
1566 
1567 			if (status) {
1568 				mlog(ML_ERROR, "Error return %d asserting "
1569 				     "join on node %d\n", status, node);
1570 
1571 				/* give us some time between errors... */
1572 				if (live)
1573 					msleep(DLM_DOMAIN_BACKOFF_MS);
1574 			}
1575 		} while (status && live);
1576 	}
1577 }
1578 
1579 struct domain_join_ctxt {
1580 	unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1581 	unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1582 };
1583 
1584 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1585 				   struct domain_join_ctxt *ctxt,
1586 				   enum dlm_query_join_response_code response)
1587 {
1588 	int ret;
1589 
1590 	if (response == JOIN_DISALLOW) {
1591 		mlog(0, "Latest response of disallow -- should restart\n");
1592 		return 1;
1593 	}
1594 
1595 	spin_lock(&dlm->spinlock);
1596 	/* For now, we restart the process if the node maps have
1597 	 * changed at all */
1598 	ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1599 		     sizeof(dlm->live_nodes_map));
1600 	spin_unlock(&dlm->spinlock);
1601 
1602 	if (ret)
1603 		mlog(0, "Node maps changed -- should restart\n");
1604 
1605 	return ret;
1606 }
1607 
1608 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1609 {
1610 	int status = 0, tmpstat, node;
1611 	struct domain_join_ctxt *ctxt;
1612 	enum dlm_query_join_response_code response = JOIN_DISALLOW;
1613 
1614 	mlog(0, "%p", dlm);
1615 
1616 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1617 	if (!ctxt) {
1618 		status = -ENOMEM;
1619 		mlog_errno(status);
1620 		goto bail;
1621 	}
1622 
1623 	/* group sem locking should work for us here -- we're already
1624 	 * registered for heartbeat events so filling this should be
1625 	 * atomic wrt getting those handlers called. */
1626 	o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1627 
1628 	spin_lock(&dlm->spinlock);
1629 	memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1630 
1631 	__dlm_set_joining_node(dlm, dlm->node_num);
1632 
1633 	spin_unlock(&dlm->spinlock);
1634 
1635 	node = -1;
1636 	while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1637 				     node + 1)) < O2NM_MAX_NODES) {
1638 		if (node == dlm->node_num)
1639 			continue;
1640 
1641 		status = dlm_request_join(dlm, node, &response);
1642 		if (status < 0) {
1643 			mlog_errno(status);
1644 			goto bail;
1645 		}
1646 
1647 		/* Ok, either we got a response or the node doesn't have a
1648 		 * dlm up. */
1649 		if (response == JOIN_OK)
1650 			set_bit(node, ctxt->yes_resp_map);
1651 
1652 		if (dlm_should_restart_join(dlm, ctxt, response)) {
1653 			status = -EAGAIN;
1654 			goto bail;
1655 		}
1656 	}
1657 
1658 	mlog(0, "Yay, done querying nodes!\n");
1659 
1660 	/* Yay, everyone agree's we can join the domain. My domain is
1661 	 * comprised of all nodes who were put in the
1662 	 * yes_resp_map. Copy that into our domain map and send a join
1663 	 * assert message to clean up everyone elses state. */
1664 	spin_lock(&dlm->spinlock);
1665 	memcpy(dlm->domain_map, ctxt->yes_resp_map,
1666 	       sizeof(ctxt->yes_resp_map));
1667 	set_bit(dlm->node_num, dlm->domain_map);
1668 	spin_unlock(&dlm->spinlock);
1669 
1670 	/* Support for global heartbeat and node info was added in 1.1 */
1671 	if (dlm->dlm_locking_proto.pv_major > 1 ||
1672 	    dlm->dlm_locking_proto.pv_minor > 0) {
1673 		status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1674 		if (status) {
1675 			mlog_errno(status);
1676 			goto bail;
1677 		}
1678 		status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1679 		if (status) {
1680 			mlog_errno(status);
1681 			goto bail;
1682 		}
1683 	}
1684 
1685 	dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1686 
1687 	/* Joined state *must* be set before the joining node
1688 	 * information, otherwise the query_join handler may read no
1689 	 * current joiner but a state of NEW and tell joining nodes
1690 	 * we're not in the domain. */
1691 	spin_lock(&dlm_domain_lock);
1692 	dlm->dlm_state = DLM_CTXT_JOINED;
1693 	dlm->num_joins++;
1694 	spin_unlock(&dlm_domain_lock);
1695 
1696 bail:
1697 	spin_lock(&dlm->spinlock);
1698 	__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1699 	if (!status) {
1700 		printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1701 		__dlm_print_nodes(dlm);
1702 	}
1703 	spin_unlock(&dlm->spinlock);
1704 
1705 	if (ctxt) {
1706 		/* Do we need to send a cancel message to any nodes? */
1707 		if (status < 0) {
1708 			tmpstat = dlm_send_join_cancels(dlm,
1709 							ctxt->yes_resp_map,
1710 							sizeof(ctxt->yes_resp_map));
1711 			if (tmpstat < 0)
1712 				mlog_errno(tmpstat);
1713 		}
1714 		kfree(ctxt);
1715 	}
1716 
1717 	mlog(0, "returning %d\n", status);
1718 	return status;
1719 }
1720 
1721 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1722 {
1723 	o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1724 	o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1725 	o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1726 }
1727 
1728 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1729 {
1730 	int status;
1731 
1732 	mlog(0, "registering handlers.\n");
1733 
1734 	o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1735 			    dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1736 	status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1737 	if (status)
1738 		goto bail;
1739 
1740 	o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1741 			    dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1742 	status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1743 	if (status)
1744 		goto bail;
1745 
1746 	status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1747 					sizeof(struct dlm_master_request),
1748 					dlm_master_request_handler,
1749 					dlm, NULL, &dlm->dlm_domain_handlers);
1750 	if (status)
1751 		goto bail;
1752 
1753 	status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1754 					sizeof(struct dlm_assert_master),
1755 					dlm_assert_master_handler,
1756 					dlm, dlm_assert_master_post_handler,
1757 					&dlm->dlm_domain_handlers);
1758 	if (status)
1759 		goto bail;
1760 
1761 	status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1762 					sizeof(struct dlm_create_lock),
1763 					dlm_create_lock_handler,
1764 					dlm, NULL, &dlm->dlm_domain_handlers);
1765 	if (status)
1766 		goto bail;
1767 
1768 	status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1769 					DLM_CONVERT_LOCK_MAX_LEN,
1770 					dlm_convert_lock_handler,
1771 					dlm, NULL, &dlm->dlm_domain_handlers);
1772 	if (status)
1773 		goto bail;
1774 
1775 	status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1776 					DLM_UNLOCK_LOCK_MAX_LEN,
1777 					dlm_unlock_lock_handler,
1778 					dlm, NULL, &dlm->dlm_domain_handlers);
1779 	if (status)
1780 		goto bail;
1781 
1782 	status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1783 					DLM_PROXY_AST_MAX_LEN,
1784 					dlm_proxy_ast_handler,
1785 					dlm, NULL, &dlm->dlm_domain_handlers);
1786 	if (status)
1787 		goto bail;
1788 
1789 	status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1790 					sizeof(struct dlm_exit_domain),
1791 					dlm_exit_domain_handler,
1792 					dlm, NULL, &dlm->dlm_domain_handlers);
1793 	if (status)
1794 		goto bail;
1795 
1796 	status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1797 					sizeof(struct dlm_deref_lockres),
1798 					dlm_deref_lockres_handler,
1799 					dlm, NULL, &dlm->dlm_domain_handlers);
1800 	if (status)
1801 		goto bail;
1802 
1803 	status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1804 					sizeof(struct dlm_migrate_request),
1805 					dlm_migrate_request_handler,
1806 					dlm, NULL, &dlm->dlm_domain_handlers);
1807 	if (status)
1808 		goto bail;
1809 
1810 	status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1811 					DLM_MIG_LOCKRES_MAX_LEN,
1812 					dlm_mig_lockres_handler,
1813 					dlm, NULL, &dlm->dlm_domain_handlers);
1814 	if (status)
1815 		goto bail;
1816 
1817 	status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1818 					sizeof(struct dlm_master_requery),
1819 					dlm_master_requery_handler,
1820 					dlm, NULL, &dlm->dlm_domain_handlers);
1821 	if (status)
1822 		goto bail;
1823 
1824 	status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1825 					sizeof(struct dlm_lock_request),
1826 					dlm_request_all_locks_handler,
1827 					dlm, NULL, &dlm->dlm_domain_handlers);
1828 	if (status)
1829 		goto bail;
1830 
1831 	status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1832 					sizeof(struct dlm_reco_data_done),
1833 					dlm_reco_data_done_handler,
1834 					dlm, NULL, &dlm->dlm_domain_handlers);
1835 	if (status)
1836 		goto bail;
1837 
1838 	status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1839 					sizeof(struct dlm_begin_reco),
1840 					dlm_begin_reco_handler,
1841 					dlm, NULL, &dlm->dlm_domain_handlers);
1842 	if (status)
1843 		goto bail;
1844 
1845 	status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1846 					sizeof(struct dlm_finalize_reco),
1847 					dlm_finalize_reco_handler,
1848 					dlm, NULL, &dlm->dlm_domain_handlers);
1849 	if (status)
1850 		goto bail;
1851 
1852 	status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1853 					sizeof(struct dlm_exit_domain),
1854 					dlm_begin_exit_domain_handler,
1855 					dlm, NULL, &dlm->dlm_domain_handlers);
1856 	if (status)
1857 		goto bail;
1858 
1859 bail:
1860 	if (status)
1861 		dlm_unregister_domain_handlers(dlm);
1862 
1863 	return status;
1864 }
1865 
1866 static int dlm_join_domain(struct dlm_ctxt *dlm)
1867 {
1868 	int status;
1869 	unsigned int backoff;
1870 	unsigned int total_backoff = 0;
1871 
1872 	BUG_ON(!dlm);
1873 
1874 	mlog(0, "Join domain %s\n", dlm->name);
1875 
1876 	status = dlm_register_domain_handlers(dlm);
1877 	if (status) {
1878 		mlog_errno(status);
1879 		goto bail;
1880 	}
1881 
1882 	status = dlm_debug_init(dlm);
1883 	if (status < 0) {
1884 		mlog_errno(status);
1885 		goto bail;
1886 	}
1887 
1888 	status = dlm_launch_thread(dlm);
1889 	if (status < 0) {
1890 		mlog_errno(status);
1891 		goto bail;
1892 	}
1893 
1894 	status = dlm_launch_recovery_thread(dlm);
1895 	if (status < 0) {
1896 		mlog_errno(status);
1897 		goto bail;
1898 	}
1899 
1900 	dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1901 	if (!dlm->dlm_worker) {
1902 		status = -ENOMEM;
1903 		mlog_errno(status);
1904 		goto bail;
1905 	}
1906 
1907 	do {
1908 		status = dlm_try_to_join_domain(dlm);
1909 
1910 		/* If we're racing another node to the join, then we
1911 		 * need to back off temporarily and let them
1912 		 * complete. */
1913 #define	DLM_JOIN_TIMEOUT_MSECS	90000
1914 		if (status == -EAGAIN) {
1915 			if (signal_pending(current)) {
1916 				status = -ERESTARTSYS;
1917 				goto bail;
1918 			}
1919 
1920 			if (total_backoff >
1921 			    msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1922 				status = -ERESTARTSYS;
1923 				mlog(ML_NOTICE, "Timed out joining dlm domain "
1924 				     "%s after %u msecs\n", dlm->name,
1925 				     jiffies_to_msecs(total_backoff));
1926 				goto bail;
1927 			}
1928 
1929 			/*
1930 			 * <chip> After you!
1931 			 * <dale> No, after you!
1932 			 * <chip> I insist!
1933 			 * <dale> But you first!
1934 			 * ...
1935 			 */
1936 			backoff = (unsigned int)(jiffies & 0x3);
1937 			backoff *= DLM_DOMAIN_BACKOFF_MS;
1938 			total_backoff += backoff;
1939 			mlog(0, "backoff %d\n", backoff);
1940 			msleep(backoff);
1941 		}
1942 	} while (status == -EAGAIN);
1943 
1944 	if (status < 0) {
1945 		mlog_errno(status);
1946 		goto bail;
1947 	}
1948 
1949 	status = 0;
1950 bail:
1951 	wake_up(&dlm_domain_events);
1952 
1953 	if (status) {
1954 		dlm_unregister_domain_handlers(dlm);
1955 		dlm_debug_shutdown(dlm);
1956 		dlm_complete_thread(dlm);
1957 		dlm_complete_recovery_thread(dlm);
1958 		dlm_destroy_dlm_worker(dlm);
1959 	}
1960 
1961 	return status;
1962 }
1963 
1964 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1965 				u32 key)
1966 {
1967 	int i;
1968 	int ret;
1969 	struct dlm_ctxt *dlm = NULL;
1970 
1971 	dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1972 	if (!dlm) {
1973 		mlog_errno(-ENOMEM);
1974 		goto leave;
1975 	}
1976 
1977 	dlm->name = kstrdup(domain, GFP_KERNEL);
1978 	if (dlm->name == NULL) {
1979 		mlog_errno(-ENOMEM);
1980 		kfree(dlm);
1981 		dlm = NULL;
1982 		goto leave;
1983 	}
1984 
1985 	dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1986 	if (!dlm->lockres_hash) {
1987 		mlog_errno(-ENOMEM);
1988 		kfree(dlm->name);
1989 		kfree(dlm);
1990 		dlm = NULL;
1991 		goto leave;
1992 	}
1993 
1994 	for (i = 0; i < DLM_HASH_BUCKETS; i++)
1995 		INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1996 
1997 	dlm->master_hash = (struct hlist_head **)
1998 				dlm_alloc_pagevec(DLM_HASH_PAGES);
1999 	if (!dlm->master_hash) {
2000 		mlog_errno(-ENOMEM);
2001 		dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2002 		kfree(dlm->name);
2003 		kfree(dlm);
2004 		dlm = NULL;
2005 		goto leave;
2006 	}
2007 
2008 	for (i = 0; i < DLM_HASH_BUCKETS; i++)
2009 		INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
2010 
2011 	dlm->key = key;
2012 	dlm->node_num = o2nm_this_node();
2013 
2014 	ret = dlm_create_debugfs_subroot(dlm);
2015 	if (ret < 0) {
2016 		dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
2017 		dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2018 		kfree(dlm->name);
2019 		kfree(dlm);
2020 		dlm = NULL;
2021 		goto leave;
2022 	}
2023 
2024 	spin_lock_init(&dlm->spinlock);
2025 	spin_lock_init(&dlm->master_lock);
2026 	spin_lock_init(&dlm->ast_lock);
2027 	spin_lock_init(&dlm->track_lock);
2028 	INIT_LIST_HEAD(&dlm->list);
2029 	INIT_LIST_HEAD(&dlm->dirty_list);
2030 	INIT_LIST_HEAD(&dlm->reco.resources);
2031 	INIT_LIST_HEAD(&dlm->reco.received);
2032 	INIT_LIST_HEAD(&dlm->reco.node_data);
2033 	INIT_LIST_HEAD(&dlm->purge_list);
2034 	INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2035 	INIT_LIST_HEAD(&dlm->tracking_list);
2036 	dlm->reco.state = 0;
2037 
2038 	INIT_LIST_HEAD(&dlm->pending_asts);
2039 	INIT_LIST_HEAD(&dlm->pending_basts);
2040 
2041 	mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2042 		  dlm->recovery_map, &(dlm->recovery_map[0]));
2043 
2044 	memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2045 	memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2046 	memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2047 
2048 	dlm->dlm_thread_task = NULL;
2049 	dlm->dlm_reco_thread_task = NULL;
2050 	dlm->dlm_worker = NULL;
2051 	init_waitqueue_head(&dlm->dlm_thread_wq);
2052 	init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2053 	init_waitqueue_head(&dlm->reco.event);
2054 	init_waitqueue_head(&dlm->ast_wq);
2055 	init_waitqueue_head(&dlm->migration_wq);
2056 	INIT_LIST_HEAD(&dlm->mle_hb_events);
2057 
2058 	dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2059 	init_waitqueue_head(&dlm->dlm_join_events);
2060 
2061 	dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2062 	dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2063 
2064 	atomic_set(&dlm->res_tot_count, 0);
2065 	atomic_set(&dlm->res_cur_count, 0);
2066 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2067 		atomic_set(&dlm->mle_tot_count[i], 0);
2068 		atomic_set(&dlm->mle_cur_count[i], 0);
2069 	}
2070 
2071 	spin_lock_init(&dlm->work_lock);
2072 	INIT_LIST_HEAD(&dlm->work_list);
2073 	INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2074 
2075 	kref_init(&dlm->dlm_refs);
2076 	dlm->dlm_state = DLM_CTXT_NEW;
2077 
2078 	INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2079 
2080 	mlog(0, "context init: refcount %u\n",
2081 		  atomic_read(&dlm->dlm_refs.refcount));
2082 
2083 leave:
2084 	return dlm;
2085 }
2086 
2087 /*
2088  * Compare a requested locking protocol version against the current one.
2089  *
2090  * If the major numbers are different, they are incompatible.
2091  * If the current minor is greater than the request, they are incompatible.
2092  * If the current minor is less than or equal to the request, they are
2093  * compatible, and the requester should run at the current minor version.
2094  */
2095 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2096 				struct dlm_protocol_version *request)
2097 {
2098 	if (existing->pv_major != request->pv_major)
2099 		return 1;
2100 
2101 	if (existing->pv_minor > request->pv_minor)
2102 		return 1;
2103 
2104 	if (existing->pv_minor < request->pv_minor)
2105 		request->pv_minor = existing->pv_minor;
2106 
2107 	return 0;
2108 }
2109 
2110 /*
2111  * dlm_register_domain: one-time setup per "domain".
2112  *
2113  * The filesystem passes in the requested locking version via proto.
2114  * If registration was successful, proto will contain the negotiated
2115  * locking protocol.
2116  */
2117 struct dlm_ctxt * dlm_register_domain(const char *domain,
2118 			       u32 key,
2119 			       struct dlm_protocol_version *fs_proto)
2120 {
2121 	int ret;
2122 	struct dlm_ctxt *dlm = NULL;
2123 	struct dlm_ctxt *new_ctxt = NULL;
2124 
2125 	if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2126 		ret = -ENAMETOOLONG;
2127 		mlog(ML_ERROR, "domain name length too long\n");
2128 		goto leave;
2129 	}
2130 
2131 	mlog(0, "register called for domain \"%s\"\n", domain);
2132 
2133 retry:
2134 	dlm = NULL;
2135 	if (signal_pending(current)) {
2136 		ret = -ERESTARTSYS;
2137 		mlog_errno(ret);
2138 		goto leave;
2139 	}
2140 
2141 	spin_lock(&dlm_domain_lock);
2142 
2143 	dlm = __dlm_lookup_domain(domain);
2144 	if (dlm) {
2145 		if (dlm->dlm_state != DLM_CTXT_JOINED) {
2146 			spin_unlock(&dlm_domain_lock);
2147 
2148 			mlog(0, "This ctxt is not joined yet!\n");
2149 			wait_event_interruptible(dlm_domain_events,
2150 						 dlm_wait_on_domain_helper(
2151 							 domain));
2152 			goto retry;
2153 		}
2154 
2155 		if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2156 			spin_unlock(&dlm_domain_lock);
2157 			mlog(ML_ERROR,
2158 			     "Requested locking protocol version is not "
2159 			     "compatible with already registered domain "
2160 			     "\"%s\"\n", domain);
2161 			ret = -EPROTO;
2162 			goto leave;
2163 		}
2164 
2165 		__dlm_get(dlm);
2166 		dlm->num_joins++;
2167 
2168 		spin_unlock(&dlm_domain_lock);
2169 
2170 		ret = 0;
2171 		goto leave;
2172 	}
2173 
2174 	/* doesn't exist */
2175 	if (!new_ctxt) {
2176 		spin_unlock(&dlm_domain_lock);
2177 
2178 		new_ctxt = dlm_alloc_ctxt(domain, key);
2179 		if (new_ctxt)
2180 			goto retry;
2181 
2182 		ret = -ENOMEM;
2183 		mlog_errno(ret);
2184 		goto leave;
2185 	}
2186 
2187 	/* a little variable switch-a-roo here... */
2188 	dlm = new_ctxt;
2189 	new_ctxt = NULL;
2190 
2191 	/* add the new domain */
2192 	list_add_tail(&dlm->list, &dlm_domains);
2193 	spin_unlock(&dlm_domain_lock);
2194 
2195 	/*
2196 	 * Pass the locking protocol version into the join.  If the join
2197 	 * succeeds, it will have the negotiated protocol set.
2198 	 */
2199 	dlm->dlm_locking_proto = dlm_protocol;
2200 	dlm->fs_locking_proto = *fs_proto;
2201 
2202 	ret = dlm_join_domain(dlm);
2203 	if (ret) {
2204 		mlog_errno(ret);
2205 		dlm_put(dlm);
2206 		goto leave;
2207 	}
2208 
2209 	/* Tell the caller what locking protocol we negotiated */
2210 	*fs_proto = dlm->fs_locking_proto;
2211 
2212 	ret = 0;
2213 leave:
2214 	if (new_ctxt)
2215 		dlm_free_ctxt_mem(new_ctxt);
2216 
2217 	if (ret < 0)
2218 		dlm = ERR_PTR(ret);
2219 
2220 	return dlm;
2221 }
2222 EXPORT_SYMBOL_GPL(dlm_register_domain);
2223 
2224 static LIST_HEAD(dlm_join_handlers);
2225 
2226 static void dlm_unregister_net_handlers(void)
2227 {
2228 	o2net_unregister_handler_list(&dlm_join_handlers);
2229 }
2230 
2231 static int dlm_register_net_handlers(void)
2232 {
2233 	int status = 0;
2234 
2235 	status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2236 					sizeof(struct dlm_query_join_request),
2237 					dlm_query_join_handler,
2238 					NULL, NULL, &dlm_join_handlers);
2239 	if (status)
2240 		goto bail;
2241 
2242 	status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2243 					sizeof(struct dlm_assert_joined),
2244 					dlm_assert_joined_handler,
2245 					NULL, NULL, &dlm_join_handlers);
2246 	if (status)
2247 		goto bail;
2248 
2249 	status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2250 					sizeof(struct dlm_cancel_join),
2251 					dlm_cancel_join_handler,
2252 					NULL, NULL, &dlm_join_handlers);
2253 	if (status)
2254 		goto bail;
2255 
2256 	status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2257 					sizeof(struct dlm_query_region),
2258 					dlm_query_region_handler,
2259 					NULL, NULL, &dlm_join_handlers);
2260 
2261 	if (status)
2262 		goto bail;
2263 
2264 	status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2265 					sizeof(struct dlm_query_nodeinfo),
2266 					dlm_query_nodeinfo_handler,
2267 					NULL, NULL, &dlm_join_handlers);
2268 bail:
2269 	if (status < 0)
2270 		dlm_unregister_net_handlers();
2271 
2272 	return status;
2273 }
2274 
2275 /* Domain eviction callback handling.
2276  *
2277  * The file system requires notification of node death *before* the
2278  * dlm completes it's recovery work, otherwise it may be able to
2279  * acquire locks on resources requiring recovery. Since the dlm can
2280  * evict a node from it's domain *before* heartbeat fires, a similar
2281  * mechanism is required. */
2282 
2283 /* Eviction is not expected to happen often, so a per-domain lock is
2284  * not necessary. Eviction callbacks are allowed to sleep for short
2285  * periods of time. */
2286 static DECLARE_RWSEM(dlm_callback_sem);
2287 
2288 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2289 					int node_num)
2290 {
2291 	struct dlm_eviction_cb *cb;
2292 
2293 	down_read(&dlm_callback_sem);
2294 	list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2295 		cb->ec_func(node_num, cb->ec_data);
2296 	}
2297 	up_read(&dlm_callback_sem);
2298 }
2299 
2300 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2301 			   dlm_eviction_func *f,
2302 			   void *data)
2303 {
2304 	INIT_LIST_HEAD(&cb->ec_item);
2305 	cb->ec_func = f;
2306 	cb->ec_data = data;
2307 }
2308 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2309 
2310 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2311 			      struct dlm_eviction_cb *cb)
2312 {
2313 	down_write(&dlm_callback_sem);
2314 	list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2315 	up_write(&dlm_callback_sem);
2316 }
2317 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2318 
2319 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2320 {
2321 	down_write(&dlm_callback_sem);
2322 	list_del_init(&cb->ec_item);
2323 	up_write(&dlm_callback_sem);
2324 }
2325 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2326 
2327 static int __init dlm_init(void)
2328 {
2329 	int status;
2330 
2331 	dlm_print_version();
2332 
2333 	status = dlm_init_mle_cache();
2334 	if (status) {
2335 		mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2336 		goto error;
2337 	}
2338 
2339 	status = dlm_init_master_caches();
2340 	if (status) {
2341 		mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2342 		     "o2dlm_lockname slabcaches\n");
2343 		goto error;
2344 	}
2345 
2346 	status = dlm_init_lock_cache();
2347 	if (status) {
2348 		mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2349 		goto error;
2350 	}
2351 
2352 	status = dlm_register_net_handlers();
2353 	if (status) {
2354 		mlog(ML_ERROR, "Unable to register network handlers\n");
2355 		goto error;
2356 	}
2357 
2358 	status = dlm_create_debugfs_root();
2359 	if (status)
2360 		goto error;
2361 
2362 	return 0;
2363 error:
2364 	dlm_unregister_net_handlers();
2365 	dlm_destroy_lock_cache();
2366 	dlm_destroy_master_caches();
2367 	dlm_destroy_mle_cache();
2368 	return -1;
2369 }
2370 
2371 static void __exit dlm_exit (void)
2372 {
2373 	dlm_destroy_debugfs_root();
2374 	dlm_unregister_net_handlers();
2375 	dlm_destroy_lock_cache();
2376 	dlm_destroy_master_caches();
2377 	dlm_destroy_mle_cache();
2378 }
2379 
2380 MODULE_AUTHOR("Oracle");
2381 MODULE_LICENSE("GPL");
2382 
2383 module_init(dlm_init);
2384 module_exit(dlm_exit);
2385