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