xref: /openbmc/linux/fs/ocfs2/dlm/dlmdomain.c (revision 7aacf86b)
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 #include <linux/sched/signal.h>
37 
38 #include "cluster/heartbeat.h"
39 #include "cluster/nodemanager.h"
40 #include "cluster/tcp.h"
41 
42 #include "dlmapi.h"
43 #include "dlmcommon.h"
44 #include "dlmdomain.h"
45 #include "dlmdebug.h"
46 
47 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
48 #include "cluster/masklog.h"
49 
50 /*
51  * ocfs2 node maps are array of long int, which limits to send them freely
52  * across the wire due to endianness issues. To workaround this, we convert
53  * long ints to byte arrays. Following 3 routines are helper functions to
54  * set/test/copy bits within those array of bytes
55  */
56 static inline void byte_set_bit(u8 nr, u8 map[])
57 {
58 	map[nr >> 3] |= (1UL << (nr & 7));
59 }
60 
61 static inline int byte_test_bit(u8 nr, u8 map[])
62 {
63 	return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
64 }
65 
66 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
67 			unsigned int sz)
68 {
69 	unsigned int nn;
70 
71 	if (!sz)
72 		return;
73 
74 	memset(dmap, 0, ((sz + 7) >> 3));
75 	for (nn = 0 ; nn < sz; nn++)
76 		if (test_bit(nn, smap))
77 			byte_set_bit(nn, dmap);
78 }
79 
80 static void dlm_free_pagevec(void **vec, int pages)
81 {
82 	while (pages--)
83 		free_page((unsigned long)vec[pages]);
84 	kfree(vec);
85 }
86 
87 static void **dlm_alloc_pagevec(int pages)
88 {
89 	void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
90 	int i;
91 
92 	if (!vec)
93 		return NULL;
94 
95 	for (i = 0; i < pages; i++)
96 		if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
97 			goto out_free;
98 
99 	mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
100 	     pages, (unsigned long)DLM_HASH_PAGES,
101 	     (unsigned long)DLM_BUCKETS_PER_PAGE);
102 	return vec;
103 out_free:
104 	dlm_free_pagevec(vec, i);
105 	return NULL;
106 }
107 
108 /*
109  *
110  * spinlock lock ordering: if multiple locks are needed, obey this ordering:
111  *    dlm_domain_lock
112  *    struct dlm_ctxt->spinlock
113  *    struct dlm_lock_resource->spinlock
114  *    struct dlm_ctxt->master_lock
115  *    struct dlm_ctxt->ast_lock
116  *    dlm_master_list_entry->spinlock
117  *    dlm_lock->spinlock
118  *
119  */
120 
121 DEFINE_SPINLOCK(dlm_domain_lock);
122 LIST_HEAD(dlm_domains);
123 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
124 
125 /*
126  * The supported protocol version for DLM communication.  Running domains
127  * will have a negotiated version with the same major number and a minor
128  * number equal or smaller.  The dlm_ctxt->dlm_locking_proto field should
129  * be used to determine what a running domain is actually using.
130  *
131  * New in version 1.1:
132  *	- Message DLM_QUERY_REGION added to support global heartbeat
133  *	- Message DLM_QUERY_NODEINFO added to allow online node removes
134  * New in version 1.2:
135  * 	- Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
136  * New in version 1.3:
137  *	- Message DLM_DEREF_LOCKRES_DONE added to inform non-master that the
138  *	  refmap is cleared
139  */
140 static const struct dlm_protocol_version dlm_protocol = {
141 	.pv_major = 1,
142 	.pv_minor = 3,
143 };
144 
145 #define DLM_DOMAIN_BACKOFF_MS 200
146 
147 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
148 				  void **ret_data);
149 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
150 				     void **ret_data);
151 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
152 				   void **ret_data);
153 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
154 				    void *data, void **ret_data);
155 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
156 				   void **ret_data);
157 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
158 				struct dlm_protocol_version *request);
159 
160 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
161 
162 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
163 {
164 	if (hlist_unhashed(&res->hash_node))
165 		return;
166 
167 	mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
168 	     res->lockname.name);
169 	hlist_del_init(&res->hash_node);
170 	dlm_lockres_put(res);
171 }
172 
173 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
174 {
175 	struct hlist_head *bucket;
176 
177 	assert_spin_locked(&dlm->spinlock);
178 
179 	bucket = dlm_lockres_hash(dlm, res->lockname.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_shutting_down(struct dlm_ctxt *dlm)
680 {
681 	int ret = 0;
682 
683 	spin_lock(&dlm_domain_lock);
684 
685 	if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
686 		ret = 1;
687 
688 	spin_unlock(&dlm_domain_lock);
689 
690 	return ret;
691 }
692 
693 void dlm_unregister_domain(struct dlm_ctxt *dlm)
694 {
695 	int leave = 0;
696 	struct dlm_lock_resource *res;
697 
698 	spin_lock(&dlm_domain_lock);
699 	BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
700 	BUG_ON(!dlm->num_joins);
701 
702 	dlm->num_joins--;
703 	if (!dlm->num_joins) {
704 		/* We mark it "in shutdown" now so new register
705 		 * requests wait until we've completely left the
706 		 * domain. Don't use DLM_CTXT_LEAVING yet as we still
707 		 * want new domain joins to communicate with us at
708 		 * least until we've completed migration of our
709 		 * resources. */
710 		dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
711 		leave = 1;
712 	}
713 	spin_unlock(&dlm_domain_lock);
714 
715 	if (leave) {
716 		mlog(0, "shutting down domain %s\n", dlm->name);
717 		dlm_begin_exit_domain(dlm);
718 
719 		/* We changed dlm state, notify the thread */
720 		dlm_kick_thread(dlm, NULL);
721 
722 		while (dlm_migrate_all_locks(dlm)) {
723 			/* Give dlm_thread time to purge the lockres' */
724 			msleep(500);
725 			mlog(0, "%s: more migration to do\n", dlm->name);
726 		}
727 
728 		/* This list should be empty. If not, print remaining lockres */
729 		if (!list_empty(&dlm->tracking_list)) {
730 			mlog(ML_ERROR, "Following lockres' are still on the "
731 			     "tracking list:\n");
732 			list_for_each_entry(res, &dlm->tracking_list, tracking)
733 				dlm_print_one_lock_resource(res);
734 		}
735 
736 		dlm_mark_domain_leaving(dlm);
737 		dlm_leave_domain(dlm);
738 		printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
739 		dlm_force_free_mles(dlm);
740 		dlm_complete_dlm_shutdown(dlm);
741 	}
742 	dlm_put(dlm);
743 }
744 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
745 
746 static int dlm_query_join_proto_check(char *proto_type, int node,
747 				      struct dlm_protocol_version *ours,
748 				      struct dlm_protocol_version *request)
749 {
750 	int rc;
751 	struct dlm_protocol_version proto = *request;
752 
753 	if (!dlm_protocol_compare(ours, &proto)) {
754 		mlog(0,
755 		     "node %u wanted to join with %s locking protocol "
756 		     "%u.%u, we respond with %u.%u\n",
757 		     node, proto_type,
758 		     request->pv_major,
759 		     request->pv_minor,
760 		     proto.pv_major, proto.pv_minor);
761 		request->pv_minor = proto.pv_minor;
762 		rc = 0;
763 	} else {
764 		mlog(ML_NOTICE,
765 		     "Node %u wanted to join with %s locking "
766 		     "protocol %u.%u, but we have %u.%u, disallowing\n",
767 		     node, proto_type,
768 		     request->pv_major,
769 		     request->pv_minor,
770 		     ours->pv_major,
771 		     ours->pv_minor);
772 		rc = 1;
773 	}
774 
775 	return rc;
776 }
777 
778 /*
779  * struct dlm_query_join_packet is made up of four one-byte fields.  They
780  * are effectively in big-endian order already.  However, little-endian
781  * machines swap them before putting the packet on the wire (because
782  * query_join's response is a status, and that status is treated as a u32
783  * on the wire).  Thus, a big-endian and little-endian machines will treat
784  * this structure differently.
785  *
786  * The solution is to have little-endian machines swap the structure when
787  * converting from the structure to the u32 representation.  This will
788  * result in the structure having the correct format on the wire no matter
789  * the host endian format.
790  */
791 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
792 					  u32 *wire)
793 {
794 	union dlm_query_join_response response;
795 
796 	response.packet = *packet;
797 	*wire = be32_to_cpu(response.intval);
798 }
799 
800 static void dlm_query_join_wire_to_packet(u32 wire,
801 					  struct dlm_query_join_packet *packet)
802 {
803 	union dlm_query_join_response response;
804 
805 	response.intval = cpu_to_be32(wire);
806 	*packet = response.packet;
807 }
808 
809 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
810 				  void **ret_data)
811 {
812 	struct dlm_query_join_request *query;
813 	struct dlm_query_join_packet packet = {
814 		.code = JOIN_DISALLOW,
815 	};
816 	struct dlm_ctxt *dlm = NULL;
817 	u32 response;
818 	u8 nodenum;
819 
820 	query = (struct dlm_query_join_request *) msg->buf;
821 
822 	mlog(0, "node %u wants to join domain %s\n", query->node_idx,
823 		  query->domain);
824 
825 	/*
826 	 * If heartbeat doesn't consider the node live, tell it
827 	 * to back off and try again.  This gives heartbeat a chance
828 	 * to catch up.
829 	 */
830 	if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
831 		mlog(0, "node %u is not in our live map yet\n",
832 		     query->node_idx);
833 
834 		packet.code = JOIN_DISALLOW;
835 		goto respond;
836 	}
837 
838 	packet.code = JOIN_OK_NO_MAP;
839 
840 	spin_lock(&dlm_domain_lock);
841 	dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
842 	if (!dlm)
843 		goto unlock_respond;
844 
845 	/*
846 	 * There is a small window where the joining node may not see the
847 	 * node(s) that just left but still part of the cluster. DISALLOW
848 	 * join request if joining node has different node map.
849 	 */
850 	nodenum=0;
851 	while (nodenum < O2NM_MAX_NODES) {
852 		if (test_bit(nodenum, dlm->domain_map)) {
853 			if (!byte_test_bit(nodenum, query->node_map)) {
854 				mlog(0, "disallow join as node %u does not "
855 				     "have node %u in its nodemap\n",
856 				     query->node_idx, nodenum);
857 				packet.code = JOIN_DISALLOW;
858 				goto unlock_respond;
859 			}
860 		}
861 		nodenum++;
862 	}
863 
864 	/* Once the dlm ctxt is marked as leaving then we don't want
865 	 * to be put in someone's domain map.
866 	 * Also, explicitly disallow joining at certain troublesome
867 	 * times (ie. during recovery). */
868 	if (dlm->dlm_state != DLM_CTXT_LEAVING) {
869 		int bit = query->node_idx;
870 		spin_lock(&dlm->spinlock);
871 
872 		if (dlm->dlm_state == DLM_CTXT_NEW &&
873 		    dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
874 			/*If this is a brand new context and we
875 			 * haven't started our join process yet, then
876 			 * the other node won the race. */
877 			packet.code = JOIN_OK_NO_MAP;
878 		} else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
879 			/* Disallow parallel joins. */
880 			packet.code = JOIN_DISALLOW;
881 		} else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
882 			mlog(0, "node %u trying to join, but recovery "
883 			     "is ongoing.\n", bit);
884 			packet.code = JOIN_DISALLOW;
885 		} else if (test_bit(bit, dlm->recovery_map)) {
886 			mlog(0, "node %u trying to join, but it "
887 			     "still needs recovery.\n", bit);
888 			packet.code = JOIN_DISALLOW;
889 		} else if (test_bit(bit, dlm->domain_map)) {
890 			mlog(0, "node %u trying to join, but it "
891 			     "is still in the domain! needs recovery?\n",
892 			     bit);
893 			packet.code = JOIN_DISALLOW;
894 		} else {
895 			/* Alright we're fully a part of this domain
896 			 * so we keep some state as to who's joining
897 			 * and indicate to him that needs to be fixed
898 			 * up. */
899 
900 			/* Make sure we speak compatible locking protocols.  */
901 			if (dlm_query_join_proto_check("DLM", bit,
902 						       &dlm->dlm_locking_proto,
903 						       &query->dlm_proto)) {
904 				packet.code = JOIN_PROTOCOL_MISMATCH;
905 			} else if (dlm_query_join_proto_check("fs", bit,
906 							      &dlm->fs_locking_proto,
907 							      &query->fs_proto)) {
908 				packet.code = JOIN_PROTOCOL_MISMATCH;
909 			} else {
910 				packet.dlm_minor = query->dlm_proto.pv_minor;
911 				packet.fs_minor = query->fs_proto.pv_minor;
912 				packet.code = JOIN_OK;
913 				__dlm_set_joining_node(dlm, query->node_idx);
914 			}
915 		}
916 
917 		spin_unlock(&dlm->spinlock);
918 	}
919 unlock_respond:
920 	spin_unlock(&dlm_domain_lock);
921 
922 respond:
923 	mlog(0, "We respond with %u\n", packet.code);
924 
925 	dlm_query_join_packet_to_wire(&packet, &response);
926 	return response;
927 }
928 
929 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
930 				     void **ret_data)
931 {
932 	struct dlm_assert_joined *assert;
933 	struct dlm_ctxt *dlm = NULL;
934 
935 	assert = (struct dlm_assert_joined *) msg->buf;
936 
937 	mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
938 		  assert->domain);
939 
940 	spin_lock(&dlm_domain_lock);
941 	dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
942 	/* XXX should we consider no dlm ctxt an error? */
943 	if (dlm) {
944 		spin_lock(&dlm->spinlock);
945 
946 		/* Alright, this node has officially joined our
947 		 * domain. Set him in the map and clean up our
948 		 * leftover join state. */
949 		BUG_ON(dlm->joining_node != assert->node_idx);
950 
951 		if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
952 			mlog(0, "dlm recovery is ongoing, disallow join\n");
953 			spin_unlock(&dlm->spinlock);
954 			spin_unlock(&dlm_domain_lock);
955 			return -EAGAIN;
956 		}
957 
958 		set_bit(assert->node_idx, dlm->domain_map);
959 		clear_bit(assert->node_idx, dlm->exit_domain_map);
960 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
961 
962 		printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
963 		       assert->node_idx, dlm->name);
964 		__dlm_print_nodes(dlm);
965 
966 		/* notify anything attached to the heartbeat events */
967 		dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
968 
969 		spin_unlock(&dlm->spinlock);
970 	}
971 	spin_unlock(&dlm_domain_lock);
972 
973 	return 0;
974 }
975 
976 static int dlm_match_regions(struct dlm_ctxt *dlm,
977 			     struct dlm_query_region *qr,
978 			     char *local, int locallen)
979 {
980 	char *remote = qr->qr_regions;
981 	char *l, *r;
982 	int localnr, i, j, foundit;
983 	int status = 0;
984 
985 	if (!o2hb_global_heartbeat_active()) {
986 		if (qr->qr_numregions) {
987 			mlog(ML_ERROR, "Domain %s: Joining node %d has global "
988 			     "heartbeat enabled but local node %d does not\n",
989 			     qr->qr_domain, qr->qr_node, dlm->node_num);
990 			status = -EINVAL;
991 		}
992 		goto bail;
993 	}
994 
995 	if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
996 		mlog(ML_ERROR, "Domain %s: Local node %d has global "
997 		     "heartbeat enabled but joining node %d does not\n",
998 		     qr->qr_domain, dlm->node_num, qr->qr_node);
999 		status = -EINVAL;
1000 		goto bail;
1001 	}
1002 
1003 	r = remote;
1004 	for (i = 0; i < qr->qr_numregions; ++i) {
1005 		mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
1006 		r += O2HB_MAX_REGION_NAME_LEN;
1007 	}
1008 
1009 	localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
1010 	localnr = o2hb_get_all_regions(local, (u8)localnr);
1011 
1012 	/* compare local regions with remote */
1013 	l = local;
1014 	for (i = 0; i < localnr; ++i) {
1015 		foundit = 0;
1016 		r = remote;
1017 		for (j = 0; j <= qr->qr_numregions; ++j) {
1018 			if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1019 				foundit = 1;
1020 				break;
1021 			}
1022 			r += O2HB_MAX_REGION_NAME_LEN;
1023 		}
1024 		if (!foundit) {
1025 			status = -EINVAL;
1026 			mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1027 			     "in local node %d but not in joining node %d\n",
1028 			     qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1029 			     dlm->node_num, qr->qr_node);
1030 			goto bail;
1031 		}
1032 		l += O2HB_MAX_REGION_NAME_LEN;
1033 	}
1034 
1035 	/* compare remote with local regions */
1036 	r = remote;
1037 	for (i = 0; i < qr->qr_numregions; ++i) {
1038 		foundit = 0;
1039 		l = local;
1040 		for (j = 0; j < localnr; ++j) {
1041 			if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1042 				foundit = 1;
1043 				break;
1044 			}
1045 			l += O2HB_MAX_REGION_NAME_LEN;
1046 		}
1047 		if (!foundit) {
1048 			status = -EINVAL;
1049 			mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1050 			     "in joining node %d but not in local node %d\n",
1051 			     qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1052 			     qr->qr_node, dlm->node_num);
1053 			goto bail;
1054 		}
1055 		r += O2HB_MAX_REGION_NAME_LEN;
1056 	}
1057 
1058 bail:
1059 	return status;
1060 }
1061 
1062 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1063 {
1064 	struct dlm_query_region *qr = NULL;
1065 	int status, ret = 0, i;
1066 	char *p;
1067 
1068 	if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1069 		goto bail;
1070 
1071 	qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1072 	if (!qr) {
1073 		ret = -ENOMEM;
1074 		mlog_errno(ret);
1075 		goto bail;
1076 	}
1077 
1078 	qr->qr_node = dlm->node_num;
1079 	qr->qr_namelen = strlen(dlm->name);
1080 	memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1081 	/* if local hb, the numregions will be zero */
1082 	if (o2hb_global_heartbeat_active())
1083 		qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1084 							 O2NM_MAX_REGIONS);
1085 
1086 	p = qr->qr_regions;
1087 	for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1088 		mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1089 
1090 	i = -1;
1091 	while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1092 				  i + 1)) < O2NM_MAX_NODES) {
1093 		if (i == dlm->node_num)
1094 			continue;
1095 
1096 		mlog(0, "Sending regions to node %d\n", i);
1097 
1098 		ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1099 					 sizeof(struct dlm_query_region),
1100 					 i, &status);
1101 		if (ret >= 0)
1102 			ret = status;
1103 		if (ret) {
1104 			mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1105 			     ret, i);
1106 			break;
1107 		}
1108 	}
1109 
1110 bail:
1111 	kfree(qr);
1112 	return ret;
1113 }
1114 
1115 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1116 				    void *data, void **ret_data)
1117 {
1118 	struct dlm_query_region *qr;
1119 	struct dlm_ctxt *dlm = NULL;
1120 	char *local = NULL;
1121 	int status = 0;
1122 
1123 	qr = (struct dlm_query_region *) msg->buf;
1124 
1125 	mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1126 	     qr->qr_domain);
1127 
1128 	/* buffer used in dlm_mast_regions() */
1129 	local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1130 	if (!local)
1131 		return -ENOMEM;
1132 
1133 	status = -EINVAL;
1134 
1135 	spin_lock(&dlm_domain_lock);
1136 	dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1137 	if (!dlm) {
1138 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1139 		     "before join domain\n", qr->qr_node, qr->qr_domain);
1140 		goto out_domain_lock;
1141 	}
1142 
1143 	spin_lock(&dlm->spinlock);
1144 	if (dlm->joining_node != qr->qr_node) {
1145 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1146 		     "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1147 		     dlm->joining_node);
1148 		goto out_dlm_lock;
1149 	}
1150 
1151 	/* Support for global heartbeat was added in 1.1 */
1152 	if (dlm->dlm_locking_proto.pv_major == 1 &&
1153 	    dlm->dlm_locking_proto.pv_minor == 0) {
1154 		mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1155 		     "but active dlm protocol is %d.%d\n", qr->qr_node,
1156 		     qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1157 		     dlm->dlm_locking_proto.pv_minor);
1158 		goto out_dlm_lock;
1159 	}
1160 
1161 	status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1162 
1163 out_dlm_lock:
1164 	spin_unlock(&dlm->spinlock);
1165 
1166 out_domain_lock:
1167 	spin_unlock(&dlm_domain_lock);
1168 
1169 	kfree(local);
1170 
1171 	return status;
1172 }
1173 
1174 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1175 {
1176 	struct o2nm_node *local;
1177 	struct dlm_node_info *remote;
1178 	int i, j;
1179 	int status = 0;
1180 
1181 	for (j = 0; j < qn->qn_numnodes; ++j)
1182 		mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1183 		     &(qn->qn_nodes[j].ni_ipv4_address),
1184 		     ntohs(qn->qn_nodes[j].ni_ipv4_port));
1185 
1186 	for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1187 		local = o2nm_get_node_by_num(i);
1188 		remote = NULL;
1189 		for (j = 0; j < qn->qn_numnodes; ++j) {
1190 			if (qn->qn_nodes[j].ni_nodenum == i) {
1191 				remote = &(qn->qn_nodes[j]);
1192 				break;
1193 			}
1194 		}
1195 
1196 		if (!local && !remote)
1197 			continue;
1198 
1199 		if ((local && !remote) || (!local && remote))
1200 			status = -EINVAL;
1201 
1202 		if (!status &&
1203 		    ((remote->ni_nodenum != local->nd_num) ||
1204 		     (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1205 		     (remote->ni_ipv4_address != local->nd_ipv4_address)))
1206 			status = -EINVAL;
1207 
1208 		if (status) {
1209 			if (remote && !local)
1210 				mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1211 				     "registered in joining node %d but not in "
1212 				     "local node %d\n", qn->qn_domain,
1213 				     remote->ni_nodenum,
1214 				     &(remote->ni_ipv4_address),
1215 				     ntohs(remote->ni_ipv4_port),
1216 				     qn->qn_nodenum, dlm->node_num);
1217 			if (local && !remote)
1218 				mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1219 				     "registered in local node %d but not in "
1220 				     "joining node %d\n", qn->qn_domain,
1221 				     local->nd_num, &(local->nd_ipv4_address),
1222 				     ntohs(local->nd_ipv4_port),
1223 				     dlm->node_num, qn->qn_nodenum);
1224 			BUG_ON((!local && !remote));
1225 		}
1226 
1227 		if (local)
1228 			o2nm_node_put(local);
1229 	}
1230 
1231 	return status;
1232 }
1233 
1234 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1235 {
1236 	struct dlm_query_nodeinfo *qn = NULL;
1237 	struct o2nm_node *node;
1238 	int ret = 0, status, count, i;
1239 
1240 	if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1241 		goto bail;
1242 
1243 	qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1244 	if (!qn) {
1245 		ret = -ENOMEM;
1246 		mlog_errno(ret);
1247 		goto bail;
1248 	}
1249 
1250 	for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1251 		node = o2nm_get_node_by_num(i);
1252 		if (!node)
1253 			continue;
1254 		qn->qn_nodes[count].ni_nodenum = node->nd_num;
1255 		qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1256 		qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1257 		mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1258 		     &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1259 		++count;
1260 		o2nm_node_put(node);
1261 	}
1262 
1263 	qn->qn_nodenum = dlm->node_num;
1264 	qn->qn_numnodes = count;
1265 	qn->qn_namelen = strlen(dlm->name);
1266 	memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1267 
1268 	i = -1;
1269 	while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1270 				  i + 1)) < O2NM_MAX_NODES) {
1271 		if (i == dlm->node_num)
1272 			continue;
1273 
1274 		mlog(0, "Sending nodeinfo to node %d\n", i);
1275 
1276 		ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1277 					 qn, sizeof(struct dlm_query_nodeinfo),
1278 					 i, &status);
1279 		if (ret >= 0)
1280 			ret = status;
1281 		if (ret) {
1282 			mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1283 			break;
1284 		}
1285 	}
1286 
1287 bail:
1288 	kfree(qn);
1289 	return ret;
1290 }
1291 
1292 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1293 				      void *data, void **ret_data)
1294 {
1295 	struct dlm_query_nodeinfo *qn;
1296 	struct dlm_ctxt *dlm = NULL;
1297 	int locked = 0, status = -EINVAL;
1298 
1299 	qn = (struct dlm_query_nodeinfo *) msg->buf;
1300 
1301 	mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1302 	     qn->qn_domain);
1303 
1304 	spin_lock(&dlm_domain_lock);
1305 	dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1306 	if (!dlm) {
1307 		mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1308 		     "join domain\n", qn->qn_nodenum, qn->qn_domain);
1309 		goto bail;
1310 	}
1311 
1312 	spin_lock(&dlm->spinlock);
1313 	locked = 1;
1314 	if (dlm->joining_node != qn->qn_nodenum) {
1315 		mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1316 		     "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1317 		     dlm->joining_node);
1318 		goto bail;
1319 	}
1320 
1321 	/* Support for node query was added in 1.1 */
1322 	if (dlm->dlm_locking_proto.pv_major == 1 &&
1323 	    dlm->dlm_locking_proto.pv_minor == 0) {
1324 		mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1325 		     "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1326 		     qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1327 		     dlm->dlm_locking_proto.pv_minor);
1328 		goto bail;
1329 	}
1330 
1331 	status = dlm_match_nodes(dlm, qn);
1332 
1333 bail:
1334 	if (locked)
1335 		spin_unlock(&dlm->spinlock);
1336 	spin_unlock(&dlm_domain_lock);
1337 
1338 	return status;
1339 }
1340 
1341 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1342 				   void **ret_data)
1343 {
1344 	struct dlm_cancel_join *cancel;
1345 	struct dlm_ctxt *dlm = NULL;
1346 
1347 	cancel = (struct dlm_cancel_join *) msg->buf;
1348 
1349 	mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1350 		  cancel->domain);
1351 
1352 	spin_lock(&dlm_domain_lock);
1353 	dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1354 
1355 	if (dlm) {
1356 		spin_lock(&dlm->spinlock);
1357 
1358 		/* Yikes, this guy wants to cancel his join. No
1359 		 * problem, we simply cleanup our join state. */
1360 		BUG_ON(dlm->joining_node != cancel->node_idx);
1361 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1362 
1363 		spin_unlock(&dlm->spinlock);
1364 	}
1365 	spin_unlock(&dlm_domain_lock);
1366 
1367 	return 0;
1368 }
1369 
1370 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1371 				    unsigned int node)
1372 {
1373 	int status;
1374 	struct dlm_cancel_join cancel_msg;
1375 
1376 	memset(&cancel_msg, 0, sizeof(cancel_msg));
1377 	cancel_msg.node_idx = dlm->node_num;
1378 	cancel_msg.name_len = strlen(dlm->name);
1379 	memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1380 
1381 	status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1382 				    &cancel_msg, sizeof(cancel_msg), node,
1383 				    NULL);
1384 	if (status < 0) {
1385 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1386 		     "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1387 		     node);
1388 		goto bail;
1389 	}
1390 
1391 bail:
1392 	return status;
1393 }
1394 
1395 /* map_size should be in bytes. */
1396 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1397 				 unsigned long *node_map,
1398 				 unsigned int map_size)
1399 {
1400 	int status, tmpstat;
1401 	int node;
1402 
1403 	if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1404 			 sizeof(unsigned long))) {
1405 		mlog(ML_ERROR,
1406 		     "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1407 		     map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1408 		return -EINVAL;
1409 	}
1410 
1411 	status = 0;
1412 	node = -1;
1413 	while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1414 				     node + 1)) < O2NM_MAX_NODES) {
1415 		if (node == dlm->node_num)
1416 			continue;
1417 
1418 		tmpstat = dlm_send_one_join_cancel(dlm, node);
1419 		if (tmpstat) {
1420 			mlog(ML_ERROR, "Error return %d cancelling join on "
1421 			     "node %d\n", tmpstat, node);
1422 			if (!status)
1423 				status = tmpstat;
1424 		}
1425 	}
1426 
1427 	if (status)
1428 		mlog_errno(status);
1429 	return status;
1430 }
1431 
1432 static int dlm_request_join(struct dlm_ctxt *dlm,
1433 			    int node,
1434 			    enum dlm_query_join_response_code *response)
1435 {
1436 	int status;
1437 	struct dlm_query_join_request join_msg;
1438 	struct dlm_query_join_packet packet;
1439 	u32 join_resp;
1440 
1441 	mlog(0, "querying node %d\n", node);
1442 
1443 	memset(&join_msg, 0, sizeof(join_msg));
1444 	join_msg.node_idx = dlm->node_num;
1445 	join_msg.name_len = strlen(dlm->name);
1446 	memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1447 	join_msg.dlm_proto = dlm->dlm_locking_proto;
1448 	join_msg.fs_proto = dlm->fs_locking_proto;
1449 
1450 	/* copy live node map to join message */
1451 	byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1452 
1453 	status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1454 				    sizeof(join_msg), node, &join_resp);
1455 	if (status < 0 && status != -ENOPROTOOPT) {
1456 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1457 		     "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1458 		     node);
1459 		goto bail;
1460 	}
1461 	dlm_query_join_wire_to_packet(join_resp, &packet);
1462 
1463 	/* -ENOPROTOOPT from the net code means the other side isn't
1464 	    listening for our message type -- that's fine, it means
1465 	    his dlm isn't up, so we can consider him a 'yes' but not
1466 	    joined into the domain.  */
1467 	if (status == -ENOPROTOOPT) {
1468 		status = 0;
1469 		*response = JOIN_OK_NO_MAP;
1470 	} else {
1471 		*response = packet.code;
1472 		switch (packet.code) {
1473 		case JOIN_DISALLOW:
1474 		case JOIN_OK_NO_MAP:
1475 			break;
1476 		case JOIN_PROTOCOL_MISMATCH:
1477 			mlog(ML_NOTICE,
1478 			     "This node requested DLM locking protocol %u.%u and "
1479 			     "filesystem locking protocol %u.%u.  At least one of "
1480 			     "the protocol versions on node %d is not compatible, "
1481 			     "disconnecting\n",
1482 			     dlm->dlm_locking_proto.pv_major,
1483 			     dlm->dlm_locking_proto.pv_minor,
1484 			     dlm->fs_locking_proto.pv_major,
1485 			     dlm->fs_locking_proto.pv_minor,
1486 			     node);
1487 			status = -EPROTO;
1488 			break;
1489 		case JOIN_OK:
1490 			/* Use the same locking protocol as the remote node */
1491 			dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1492 			dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1493 			mlog(0,
1494 			     "Node %d responds JOIN_OK with DLM locking protocol "
1495 			     "%u.%u and fs locking protocol %u.%u\n",
1496 			     node,
1497 			     dlm->dlm_locking_proto.pv_major,
1498 			     dlm->dlm_locking_proto.pv_minor,
1499 			     dlm->fs_locking_proto.pv_major,
1500 			     dlm->fs_locking_proto.pv_minor);
1501 			break;
1502 		default:
1503 			status = -EINVAL;
1504 			mlog(ML_ERROR, "invalid response %d from node %u\n",
1505 			     packet.code, node);
1506 			/* Reset response to JOIN_DISALLOW */
1507 			*response = JOIN_DISALLOW;
1508 			break;
1509 		}
1510 	}
1511 
1512 	mlog(0, "status %d, node %d response is %d\n", status, node,
1513 	     *response);
1514 
1515 bail:
1516 	return status;
1517 }
1518 
1519 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1520 				    unsigned int node)
1521 {
1522 	int status;
1523 	int ret;
1524 	struct dlm_assert_joined assert_msg;
1525 
1526 	mlog(0, "Sending join assert to node %u\n", node);
1527 
1528 	memset(&assert_msg, 0, sizeof(assert_msg));
1529 	assert_msg.node_idx = dlm->node_num;
1530 	assert_msg.name_len = strlen(dlm->name);
1531 	memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1532 
1533 	status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1534 				    &assert_msg, sizeof(assert_msg), node,
1535 				    &ret);
1536 	if (status < 0)
1537 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1538 		     "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1539 		     node);
1540 	else
1541 		status = ret;
1542 
1543 	return status;
1544 }
1545 
1546 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1547 				  unsigned long *node_map)
1548 {
1549 	int status, node, live;
1550 
1551 	status = 0;
1552 	node = -1;
1553 	while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1554 				     node + 1)) < O2NM_MAX_NODES) {
1555 		if (node == dlm->node_num)
1556 			continue;
1557 
1558 		do {
1559 			/* It is very important that this message be
1560 			 * received so we spin until either the node
1561 			 * has died or it gets the message. */
1562 			status = dlm_send_one_join_assert(dlm, node);
1563 
1564 			spin_lock(&dlm->spinlock);
1565 			live = test_bit(node, dlm->live_nodes_map);
1566 			spin_unlock(&dlm->spinlock);
1567 
1568 			if (status) {
1569 				mlog(ML_ERROR, "Error return %d asserting "
1570 				     "join on node %d\n", status, node);
1571 
1572 				/* give us some time between errors... */
1573 				if (live)
1574 					msleep(DLM_DOMAIN_BACKOFF_MS);
1575 			}
1576 		} while (status && live);
1577 	}
1578 }
1579 
1580 struct domain_join_ctxt {
1581 	unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1582 	unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1583 };
1584 
1585 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1586 				   struct domain_join_ctxt *ctxt,
1587 				   enum dlm_query_join_response_code response)
1588 {
1589 	int ret;
1590 
1591 	if (response == JOIN_DISALLOW) {
1592 		mlog(0, "Latest response of disallow -- should restart\n");
1593 		return 1;
1594 	}
1595 
1596 	spin_lock(&dlm->spinlock);
1597 	/* For now, we restart the process if the node maps have
1598 	 * changed at all */
1599 	ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1600 		     sizeof(dlm->live_nodes_map));
1601 	spin_unlock(&dlm->spinlock);
1602 
1603 	if (ret)
1604 		mlog(0, "Node maps changed -- should restart\n");
1605 
1606 	return ret;
1607 }
1608 
1609 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1610 {
1611 	int status = 0, tmpstat, node;
1612 	struct domain_join_ctxt *ctxt;
1613 	enum dlm_query_join_response_code response = JOIN_DISALLOW;
1614 
1615 	mlog(0, "%p", dlm);
1616 
1617 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1618 	if (!ctxt) {
1619 		status = -ENOMEM;
1620 		mlog_errno(status);
1621 		goto bail;
1622 	}
1623 
1624 	/* group sem locking should work for us here -- we're already
1625 	 * registered for heartbeat events so filling this should be
1626 	 * atomic wrt getting those handlers called. */
1627 	o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1628 
1629 	spin_lock(&dlm->spinlock);
1630 	memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1631 
1632 	__dlm_set_joining_node(dlm, dlm->node_num);
1633 
1634 	spin_unlock(&dlm->spinlock);
1635 
1636 	node = -1;
1637 	while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1638 				     node + 1)) < O2NM_MAX_NODES) {
1639 		if (node == dlm->node_num)
1640 			continue;
1641 
1642 		status = dlm_request_join(dlm, node, &response);
1643 		if (status < 0) {
1644 			mlog_errno(status);
1645 			goto bail;
1646 		}
1647 
1648 		/* Ok, either we got a response or the node doesn't have a
1649 		 * dlm up. */
1650 		if (response == JOIN_OK)
1651 			set_bit(node, ctxt->yes_resp_map);
1652 
1653 		if (dlm_should_restart_join(dlm, ctxt, response)) {
1654 			status = -EAGAIN;
1655 			goto bail;
1656 		}
1657 	}
1658 
1659 	mlog(0, "Yay, done querying nodes!\n");
1660 
1661 	/* Yay, everyone agree's we can join the domain. My domain is
1662 	 * comprised of all nodes who were put in the
1663 	 * yes_resp_map. Copy that into our domain map and send a join
1664 	 * assert message to clean up everyone elses state. */
1665 	spin_lock(&dlm->spinlock);
1666 	memcpy(dlm->domain_map, ctxt->yes_resp_map,
1667 	       sizeof(ctxt->yes_resp_map));
1668 	set_bit(dlm->node_num, dlm->domain_map);
1669 	spin_unlock(&dlm->spinlock);
1670 
1671 	/* Support for global heartbeat and node info was added in 1.1 */
1672 	if (dlm->dlm_locking_proto.pv_major > 1 ||
1673 	    dlm->dlm_locking_proto.pv_minor > 0) {
1674 		status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1675 		if (status) {
1676 			mlog_errno(status);
1677 			goto bail;
1678 		}
1679 		status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1680 		if (status) {
1681 			mlog_errno(status);
1682 			goto bail;
1683 		}
1684 	}
1685 
1686 	dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1687 
1688 	/* Joined state *must* be set before the joining node
1689 	 * information, otherwise the query_join handler may read no
1690 	 * current joiner but a state of NEW and tell joining nodes
1691 	 * we're not in the domain. */
1692 	spin_lock(&dlm_domain_lock);
1693 	dlm->dlm_state = DLM_CTXT_JOINED;
1694 	dlm->num_joins++;
1695 	spin_unlock(&dlm_domain_lock);
1696 
1697 bail:
1698 	spin_lock(&dlm->spinlock);
1699 	__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1700 	if (!status) {
1701 		printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1702 		__dlm_print_nodes(dlm);
1703 	}
1704 	spin_unlock(&dlm->spinlock);
1705 
1706 	if (ctxt) {
1707 		/* Do we need to send a cancel message to any nodes? */
1708 		if (status < 0) {
1709 			tmpstat = dlm_send_join_cancels(dlm,
1710 							ctxt->yes_resp_map,
1711 							sizeof(ctxt->yes_resp_map));
1712 			if (tmpstat < 0)
1713 				mlog_errno(tmpstat);
1714 		}
1715 		kfree(ctxt);
1716 	}
1717 
1718 	mlog(0, "returning %d\n", status);
1719 	return status;
1720 }
1721 
1722 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1723 {
1724 	o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1725 	o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1726 	o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1727 }
1728 
1729 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1730 {
1731 	int status;
1732 
1733 	mlog(0, "registering handlers.\n");
1734 
1735 	o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1736 			    dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1737 	o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1738 			    dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1739 
1740 	status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1741 	if (status)
1742 		goto bail;
1743 
1744 	status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1745 	if (status)
1746 		goto bail;
1747 
1748 	status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1749 					sizeof(struct dlm_master_request),
1750 					dlm_master_request_handler,
1751 					dlm, NULL, &dlm->dlm_domain_handlers);
1752 	if (status)
1753 		goto bail;
1754 
1755 	status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1756 					sizeof(struct dlm_assert_master),
1757 					dlm_assert_master_handler,
1758 					dlm, dlm_assert_master_post_handler,
1759 					&dlm->dlm_domain_handlers);
1760 	if (status)
1761 		goto bail;
1762 
1763 	status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1764 					sizeof(struct dlm_create_lock),
1765 					dlm_create_lock_handler,
1766 					dlm, NULL, &dlm->dlm_domain_handlers);
1767 	if (status)
1768 		goto bail;
1769 
1770 	status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1771 					DLM_CONVERT_LOCK_MAX_LEN,
1772 					dlm_convert_lock_handler,
1773 					dlm, NULL, &dlm->dlm_domain_handlers);
1774 	if (status)
1775 		goto bail;
1776 
1777 	status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1778 					DLM_UNLOCK_LOCK_MAX_LEN,
1779 					dlm_unlock_lock_handler,
1780 					dlm, NULL, &dlm->dlm_domain_handlers);
1781 	if (status)
1782 		goto bail;
1783 
1784 	status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1785 					DLM_PROXY_AST_MAX_LEN,
1786 					dlm_proxy_ast_handler,
1787 					dlm, NULL, &dlm->dlm_domain_handlers);
1788 	if (status)
1789 		goto bail;
1790 
1791 	status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1792 					sizeof(struct dlm_exit_domain),
1793 					dlm_exit_domain_handler,
1794 					dlm, NULL, &dlm->dlm_domain_handlers);
1795 	if (status)
1796 		goto bail;
1797 
1798 	status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1799 					sizeof(struct dlm_deref_lockres),
1800 					dlm_deref_lockres_handler,
1801 					dlm, NULL, &dlm->dlm_domain_handlers);
1802 	if (status)
1803 		goto bail;
1804 
1805 	status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1806 					sizeof(struct dlm_migrate_request),
1807 					dlm_migrate_request_handler,
1808 					dlm, NULL, &dlm->dlm_domain_handlers);
1809 	if (status)
1810 		goto bail;
1811 
1812 	status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1813 					DLM_MIG_LOCKRES_MAX_LEN,
1814 					dlm_mig_lockres_handler,
1815 					dlm, NULL, &dlm->dlm_domain_handlers);
1816 	if (status)
1817 		goto bail;
1818 
1819 	status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1820 					sizeof(struct dlm_master_requery),
1821 					dlm_master_requery_handler,
1822 					dlm, NULL, &dlm->dlm_domain_handlers);
1823 	if (status)
1824 		goto bail;
1825 
1826 	status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1827 					sizeof(struct dlm_lock_request),
1828 					dlm_request_all_locks_handler,
1829 					dlm, NULL, &dlm->dlm_domain_handlers);
1830 	if (status)
1831 		goto bail;
1832 
1833 	status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1834 					sizeof(struct dlm_reco_data_done),
1835 					dlm_reco_data_done_handler,
1836 					dlm, NULL, &dlm->dlm_domain_handlers);
1837 	if (status)
1838 		goto bail;
1839 
1840 	status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1841 					sizeof(struct dlm_begin_reco),
1842 					dlm_begin_reco_handler,
1843 					dlm, NULL, &dlm->dlm_domain_handlers);
1844 	if (status)
1845 		goto bail;
1846 
1847 	status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1848 					sizeof(struct dlm_finalize_reco),
1849 					dlm_finalize_reco_handler,
1850 					dlm, NULL, &dlm->dlm_domain_handlers);
1851 	if (status)
1852 		goto bail;
1853 
1854 	status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1855 					sizeof(struct dlm_exit_domain),
1856 					dlm_begin_exit_domain_handler,
1857 					dlm, NULL, &dlm->dlm_domain_handlers);
1858 	if (status)
1859 		goto bail;
1860 
1861 	status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
1862 					sizeof(struct dlm_deref_lockres_done),
1863 					dlm_deref_lockres_done_handler,
1864 					dlm, NULL, &dlm->dlm_domain_handlers);
1865 bail:
1866 	if (status)
1867 		dlm_unregister_domain_handlers(dlm);
1868 
1869 	return status;
1870 }
1871 
1872 static int dlm_join_domain(struct dlm_ctxt *dlm)
1873 {
1874 	int status;
1875 	unsigned int backoff;
1876 	unsigned int total_backoff = 0;
1877 	char wq_name[O2NM_MAX_NAME_LEN];
1878 
1879 	BUG_ON(!dlm);
1880 
1881 	mlog(0, "Join domain %s\n", dlm->name);
1882 
1883 	status = dlm_register_domain_handlers(dlm);
1884 	if (status) {
1885 		mlog_errno(status);
1886 		goto bail;
1887 	}
1888 
1889 	status = dlm_launch_thread(dlm);
1890 	if (status < 0) {
1891 		mlog_errno(status);
1892 		goto bail;
1893 	}
1894 
1895 	status = dlm_launch_recovery_thread(dlm);
1896 	if (status < 0) {
1897 		mlog_errno(status);
1898 		goto bail;
1899 	}
1900 
1901 	status = dlm_debug_init(dlm);
1902 	if (status < 0) {
1903 		mlog_errno(status);
1904 		goto bail;
1905 	}
1906 
1907 	snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
1908 	dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
1909 	if (!dlm->dlm_worker) {
1910 		status = -ENOMEM;
1911 		mlog_errno(status);
1912 		goto bail;
1913 	}
1914 
1915 	do {
1916 		status = dlm_try_to_join_domain(dlm);
1917 
1918 		/* If we're racing another node to the join, then we
1919 		 * need to back off temporarily and let them
1920 		 * complete. */
1921 #define	DLM_JOIN_TIMEOUT_MSECS	90000
1922 		if (status == -EAGAIN) {
1923 			if (signal_pending(current)) {
1924 				status = -ERESTARTSYS;
1925 				goto bail;
1926 			}
1927 
1928 			if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
1929 				status = -ERESTARTSYS;
1930 				mlog(ML_NOTICE, "Timed out joining dlm domain "
1931 				     "%s after %u msecs\n", dlm->name,
1932 				     total_backoff);
1933 				goto bail;
1934 			}
1935 
1936 			/*
1937 			 * <chip> After you!
1938 			 * <dale> No, after you!
1939 			 * <chip> I insist!
1940 			 * <dale> But you first!
1941 			 * ...
1942 			 */
1943 			backoff = (unsigned int)(jiffies & 0x3);
1944 			backoff *= DLM_DOMAIN_BACKOFF_MS;
1945 			total_backoff += backoff;
1946 			mlog(0, "backoff %d\n", backoff);
1947 			msleep(backoff);
1948 		}
1949 	} while (status == -EAGAIN);
1950 
1951 	if (status < 0) {
1952 		mlog_errno(status);
1953 		goto bail;
1954 	}
1955 
1956 	status = 0;
1957 bail:
1958 	wake_up(&dlm_domain_events);
1959 
1960 	if (status) {
1961 		dlm_unregister_domain_handlers(dlm);
1962 		dlm_debug_shutdown(dlm);
1963 		dlm_complete_thread(dlm);
1964 		dlm_complete_recovery_thread(dlm);
1965 		dlm_destroy_dlm_worker(dlm);
1966 	}
1967 
1968 	return status;
1969 }
1970 
1971 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1972 				u32 key)
1973 {
1974 	int i;
1975 	int ret;
1976 	struct dlm_ctxt *dlm = NULL;
1977 
1978 	dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1979 	if (!dlm) {
1980 		ret = -ENOMEM;
1981 		mlog_errno(ret);
1982 		goto leave;
1983 	}
1984 
1985 	dlm->name = kstrdup(domain, GFP_KERNEL);
1986 	if (dlm->name == NULL) {
1987 		ret = -ENOMEM;
1988 		mlog_errno(ret);
1989 		goto leave;
1990 	}
1991 
1992 	dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1993 	if (!dlm->lockres_hash) {
1994 		ret = -ENOMEM;
1995 		mlog_errno(ret);
1996 		goto leave;
1997 	}
1998 
1999 	for (i = 0; i < DLM_HASH_BUCKETS; i++)
2000 		INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
2001 
2002 	dlm->master_hash = (struct hlist_head **)
2003 				dlm_alloc_pagevec(DLM_HASH_PAGES);
2004 	if (!dlm->master_hash) {
2005 		ret = -ENOMEM;
2006 		mlog_errno(ret);
2007 		goto leave;
2008 	}
2009 
2010 	for (i = 0; i < DLM_HASH_BUCKETS; i++)
2011 		INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
2012 
2013 	dlm->key = key;
2014 	dlm->node_num = o2nm_this_node();
2015 
2016 	ret = dlm_create_debugfs_subroot(dlm);
2017 	if (ret < 0)
2018 		goto leave;
2019 
2020 	spin_lock_init(&dlm->spinlock);
2021 	spin_lock_init(&dlm->master_lock);
2022 	spin_lock_init(&dlm->ast_lock);
2023 	spin_lock_init(&dlm->track_lock);
2024 	INIT_LIST_HEAD(&dlm->list);
2025 	INIT_LIST_HEAD(&dlm->dirty_list);
2026 	INIT_LIST_HEAD(&dlm->reco.resources);
2027 	INIT_LIST_HEAD(&dlm->reco.node_data);
2028 	INIT_LIST_HEAD(&dlm->purge_list);
2029 	INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2030 	INIT_LIST_HEAD(&dlm->tracking_list);
2031 	dlm->reco.state = 0;
2032 
2033 	INIT_LIST_HEAD(&dlm->pending_asts);
2034 	INIT_LIST_HEAD(&dlm->pending_basts);
2035 
2036 	mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2037 		  dlm->recovery_map, &(dlm->recovery_map[0]));
2038 
2039 	memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2040 	memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2041 	memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2042 
2043 	dlm->dlm_thread_task = NULL;
2044 	dlm->dlm_reco_thread_task = NULL;
2045 	dlm->dlm_worker = NULL;
2046 	init_waitqueue_head(&dlm->dlm_thread_wq);
2047 	init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2048 	init_waitqueue_head(&dlm->reco.event);
2049 	init_waitqueue_head(&dlm->ast_wq);
2050 	init_waitqueue_head(&dlm->migration_wq);
2051 	INIT_LIST_HEAD(&dlm->mle_hb_events);
2052 
2053 	dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2054 	init_waitqueue_head(&dlm->dlm_join_events);
2055 
2056 	dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2057 	dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2058 
2059 	atomic_set(&dlm->res_tot_count, 0);
2060 	atomic_set(&dlm->res_cur_count, 0);
2061 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2062 		atomic_set(&dlm->mle_tot_count[i], 0);
2063 		atomic_set(&dlm->mle_cur_count[i], 0);
2064 	}
2065 
2066 	spin_lock_init(&dlm->work_lock);
2067 	INIT_LIST_HEAD(&dlm->work_list);
2068 	INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2069 
2070 	kref_init(&dlm->dlm_refs);
2071 	dlm->dlm_state = DLM_CTXT_NEW;
2072 
2073 	INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2074 
2075 	mlog(0, "context init: refcount %u\n",
2076 		  kref_read(&dlm->dlm_refs));
2077 
2078 leave:
2079 	if (ret < 0 && dlm) {
2080 		if (dlm->master_hash)
2081 			dlm_free_pagevec((void **)dlm->master_hash,
2082 					DLM_HASH_PAGES);
2083 
2084 		if (dlm->lockres_hash)
2085 			dlm_free_pagevec((void **)dlm->lockres_hash,
2086 					DLM_HASH_PAGES);
2087 
2088 		kfree(dlm->name);
2089 		kfree(dlm);
2090 		dlm = NULL;
2091 	}
2092 	return dlm;
2093 }
2094 
2095 /*
2096  * Compare a requested locking protocol version against the current one.
2097  *
2098  * If the major numbers are different, they are incompatible.
2099  * If the current minor is greater than the request, they are incompatible.
2100  * If the current minor is less than or equal to the request, they are
2101  * compatible, and the requester should run at the current minor version.
2102  */
2103 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2104 				struct dlm_protocol_version *request)
2105 {
2106 	if (existing->pv_major != request->pv_major)
2107 		return 1;
2108 
2109 	if (existing->pv_minor > request->pv_minor)
2110 		return 1;
2111 
2112 	if (existing->pv_minor < request->pv_minor)
2113 		request->pv_minor = existing->pv_minor;
2114 
2115 	return 0;
2116 }
2117 
2118 /*
2119  * dlm_register_domain: one-time setup per "domain".
2120  *
2121  * The filesystem passes in the requested locking version via proto.
2122  * If registration was successful, proto will contain the negotiated
2123  * locking protocol.
2124  */
2125 struct dlm_ctxt * dlm_register_domain(const char *domain,
2126 			       u32 key,
2127 			       struct dlm_protocol_version *fs_proto)
2128 {
2129 	int ret;
2130 	struct dlm_ctxt *dlm = NULL;
2131 	struct dlm_ctxt *new_ctxt = NULL;
2132 
2133 	if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2134 		ret = -ENAMETOOLONG;
2135 		mlog(ML_ERROR, "domain name length too long\n");
2136 		goto leave;
2137 	}
2138 
2139 	mlog(0, "register called for domain \"%s\"\n", domain);
2140 
2141 retry:
2142 	dlm = NULL;
2143 	if (signal_pending(current)) {
2144 		ret = -ERESTARTSYS;
2145 		mlog_errno(ret);
2146 		goto leave;
2147 	}
2148 
2149 	spin_lock(&dlm_domain_lock);
2150 
2151 	dlm = __dlm_lookup_domain(domain);
2152 	if (dlm) {
2153 		if (dlm->dlm_state != DLM_CTXT_JOINED) {
2154 			spin_unlock(&dlm_domain_lock);
2155 
2156 			mlog(0, "This ctxt is not joined yet!\n");
2157 			wait_event_interruptible(dlm_domain_events,
2158 						 dlm_wait_on_domain_helper(
2159 							 domain));
2160 			goto retry;
2161 		}
2162 
2163 		if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2164 			spin_unlock(&dlm_domain_lock);
2165 			mlog(ML_ERROR,
2166 			     "Requested locking protocol version is not "
2167 			     "compatible with already registered domain "
2168 			     "\"%s\"\n", domain);
2169 			ret = -EPROTO;
2170 			goto leave;
2171 		}
2172 
2173 		__dlm_get(dlm);
2174 		dlm->num_joins++;
2175 
2176 		spin_unlock(&dlm_domain_lock);
2177 
2178 		ret = 0;
2179 		goto leave;
2180 	}
2181 
2182 	/* doesn't exist */
2183 	if (!new_ctxt) {
2184 		spin_unlock(&dlm_domain_lock);
2185 
2186 		new_ctxt = dlm_alloc_ctxt(domain, key);
2187 		if (new_ctxt)
2188 			goto retry;
2189 
2190 		ret = -ENOMEM;
2191 		mlog_errno(ret);
2192 		goto leave;
2193 	}
2194 
2195 	/* a little variable switch-a-roo here... */
2196 	dlm = new_ctxt;
2197 	new_ctxt = NULL;
2198 
2199 	/* add the new domain */
2200 	list_add_tail(&dlm->list, &dlm_domains);
2201 	spin_unlock(&dlm_domain_lock);
2202 
2203 	/*
2204 	 * Pass the locking protocol version into the join.  If the join
2205 	 * succeeds, it will have the negotiated protocol set.
2206 	 */
2207 	dlm->dlm_locking_proto = dlm_protocol;
2208 	dlm->fs_locking_proto = *fs_proto;
2209 
2210 	ret = dlm_join_domain(dlm);
2211 	if (ret) {
2212 		mlog_errno(ret);
2213 		dlm_put(dlm);
2214 		goto leave;
2215 	}
2216 
2217 	/* Tell the caller what locking protocol we negotiated */
2218 	*fs_proto = dlm->fs_locking_proto;
2219 
2220 	ret = 0;
2221 leave:
2222 	if (new_ctxt)
2223 		dlm_free_ctxt_mem(new_ctxt);
2224 
2225 	if (ret < 0)
2226 		dlm = ERR_PTR(ret);
2227 
2228 	return dlm;
2229 }
2230 EXPORT_SYMBOL_GPL(dlm_register_domain);
2231 
2232 static LIST_HEAD(dlm_join_handlers);
2233 
2234 static void dlm_unregister_net_handlers(void)
2235 {
2236 	o2net_unregister_handler_list(&dlm_join_handlers);
2237 }
2238 
2239 static int dlm_register_net_handlers(void)
2240 {
2241 	int status = 0;
2242 
2243 	status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2244 					sizeof(struct dlm_query_join_request),
2245 					dlm_query_join_handler,
2246 					NULL, NULL, &dlm_join_handlers);
2247 	if (status)
2248 		goto bail;
2249 
2250 	status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2251 					sizeof(struct dlm_assert_joined),
2252 					dlm_assert_joined_handler,
2253 					NULL, NULL, &dlm_join_handlers);
2254 	if (status)
2255 		goto bail;
2256 
2257 	status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2258 					sizeof(struct dlm_cancel_join),
2259 					dlm_cancel_join_handler,
2260 					NULL, NULL, &dlm_join_handlers);
2261 	if (status)
2262 		goto bail;
2263 
2264 	status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2265 					sizeof(struct dlm_query_region),
2266 					dlm_query_region_handler,
2267 					NULL, NULL, &dlm_join_handlers);
2268 
2269 	if (status)
2270 		goto bail;
2271 
2272 	status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2273 					sizeof(struct dlm_query_nodeinfo),
2274 					dlm_query_nodeinfo_handler,
2275 					NULL, NULL, &dlm_join_handlers);
2276 bail:
2277 	if (status < 0)
2278 		dlm_unregister_net_handlers();
2279 
2280 	return status;
2281 }
2282 
2283 /* Domain eviction callback handling.
2284  *
2285  * The file system requires notification of node death *before* the
2286  * dlm completes it's recovery work, otherwise it may be able to
2287  * acquire locks on resources requiring recovery. Since the dlm can
2288  * evict a node from it's domain *before* heartbeat fires, a similar
2289  * mechanism is required. */
2290 
2291 /* Eviction is not expected to happen often, so a per-domain lock is
2292  * not necessary. Eviction callbacks are allowed to sleep for short
2293  * periods of time. */
2294 static DECLARE_RWSEM(dlm_callback_sem);
2295 
2296 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2297 					int node_num)
2298 {
2299 	struct dlm_eviction_cb *cb;
2300 
2301 	down_read(&dlm_callback_sem);
2302 	list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2303 		cb->ec_func(node_num, cb->ec_data);
2304 	}
2305 	up_read(&dlm_callback_sem);
2306 }
2307 
2308 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2309 			   dlm_eviction_func *f,
2310 			   void *data)
2311 {
2312 	INIT_LIST_HEAD(&cb->ec_item);
2313 	cb->ec_func = f;
2314 	cb->ec_data = data;
2315 }
2316 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2317 
2318 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2319 			      struct dlm_eviction_cb *cb)
2320 {
2321 	down_write(&dlm_callback_sem);
2322 	list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2323 	up_write(&dlm_callback_sem);
2324 }
2325 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2326 
2327 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2328 {
2329 	down_write(&dlm_callback_sem);
2330 	list_del_init(&cb->ec_item);
2331 	up_write(&dlm_callback_sem);
2332 }
2333 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2334 
2335 static int __init dlm_init(void)
2336 {
2337 	int status;
2338 
2339 	status = dlm_init_mle_cache();
2340 	if (status) {
2341 		mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2342 		goto error;
2343 	}
2344 
2345 	status = dlm_init_master_caches();
2346 	if (status) {
2347 		mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2348 		     "o2dlm_lockname slabcaches\n");
2349 		goto error;
2350 	}
2351 
2352 	status = dlm_init_lock_cache();
2353 	if (status) {
2354 		mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2355 		goto error;
2356 	}
2357 
2358 	status = dlm_register_net_handlers();
2359 	if (status) {
2360 		mlog(ML_ERROR, "Unable to register network handlers\n");
2361 		goto error;
2362 	}
2363 
2364 	status = dlm_create_debugfs_root();
2365 	if (status)
2366 		goto error;
2367 
2368 	return 0;
2369 error:
2370 	dlm_unregister_net_handlers();
2371 	dlm_destroy_lock_cache();
2372 	dlm_destroy_master_caches();
2373 	dlm_destroy_mle_cache();
2374 	return -1;
2375 }
2376 
2377 static void __exit dlm_exit (void)
2378 {
2379 	dlm_destroy_debugfs_root();
2380 	dlm_unregister_net_handlers();
2381 	dlm_destroy_lock_cache();
2382 	dlm_destroy_master_caches();
2383 	dlm_destroy_mle_cache();
2384 }
2385 
2386 MODULE_AUTHOR("Oracle");
2387 MODULE_LICENSE("GPL");
2388 MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2389 
2390 module_init(dlm_init);
2391 module_exit(dlm_exit);
2392