Linux 4.19.133
[linux/fpc-iii.git] / fs / ocfs2 / dlm / dlmdomain.c
blob2acd58ba9b7b27dbaf2a2834c8ee0aea7c609f94
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmdomain.c
6 * defines domain join / leave apis
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
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>
38 #include "cluster/heartbeat.h"
39 #include "cluster/nodemanager.h"
40 #include "cluster/tcp.h"
42 #include "dlmapi.h"
43 #include "dlmcommon.h"
44 #include "dlmdomain.h"
45 #include "dlmdebug.h"
47 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
48 #include "cluster/masklog.h"
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
56 static inline void byte_set_bit(u8 nr, u8 map[])
58 map[nr >> 3] |= (1UL << (nr & 7));
61 static inline int byte_test_bit(u8 nr, u8 map[])
63 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
66 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
67 unsigned int sz)
69 unsigned int nn;
71 if (!sz)
72 return;
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);
80 static void dlm_free_pagevec(void **vec, int pages)
82 while (pages--)
83 free_page((unsigned long)vec[pages]);
84 kfree(vec);
87 static void **dlm_alloc_pagevec(int pages)
89 void **vec = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
90 int i;
92 if (!vec)
93 return NULL;
95 for (i = 0; i < pages; i++)
96 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
97 goto out_free;
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;
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
121 DEFINE_SPINLOCK(dlm_domain_lock);
122 LIST_HEAD(dlm_domains);
123 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
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.
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
140 static const struct dlm_protocol_version dlm_protocol = {
141 .pv_major = 1,
142 .pv_minor = 3,
145 #define DLM_DOMAIN_BACKOFF_MS 200
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);
160 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
162 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
164 if (hlist_unhashed(&res->hash_node))
165 return;
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);
173 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
175 struct hlist_head *bucket;
177 assert_spin_locked(&dlm->spinlock);
179 bucket = dlm_lockres_hash(dlm, res->lockname.hash);
181 /* get a reference for our hashtable */
182 dlm_lockres_get(res);
184 hlist_add_head(&res->hash_node, bucket);
186 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
187 res->lockname.name);
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)
195 struct hlist_head *bucket;
196 struct dlm_lock_resource *res;
198 mlog(0, "%.*s\n", len, name);
200 assert_spin_locked(&dlm->spinlock);
202 bucket = dlm_lockres_hash(dlm, hash);
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;
214 return NULL;
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)
228 struct dlm_lock_resource *res = NULL;
230 mlog(0, "%.*s\n", len, name);
232 assert_spin_locked(&dlm->spinlock);
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;
242 spin_unlock(&res->spinlock);
245 return res;
248 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
249 const char *name,
250 unsigned int len)
252 struct dlm_lock_resource *res;
253 unsigned int hash = dlm_lockid_hash(name, len);
255 spin_lock(&dlm->spinlock);
256 res = __dlm_lookup_lockres(dlm, name, len, hash);
257 spin_unlock(&dlm->spinlock);
258 return res;
261 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
263 struct dlm_ctxt *tmp;
265 assert_spin_locked(&dlm_domain_lock);
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;
275 return NULL;
278 /* For null terminated domain strings ONLY */
279 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
281 assert_spin_locked(&dlm_domain_lock);
283 return __dlm_lookup_domain_full(domain, strlen(domain));
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)
292 int ret = 0;
293 struct dlm_ctxt *tmp = NULL;
295 spin_lock(&dlm_domain_lock);
297 tmp = __dlm_lookup_domain(domain);
298 if (!tmp)
299 ret = 1;
300 else if (tmp->dlm_state == DLM_CTXT_JOINED)
301 ret = 1;
303 spin_unlock(&dlm_domain_lock);
304 return ret;
307 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
309 dlm_destroy_debugfs_subroot(dlm);
311 if (dlm->lockres_hash)
312 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
314 if (dlm->master_hash)
315 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
317 kfree(dlm->name);
318 kfree(dlm);
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)
326 struct dlm_ctxt *dlm;
328 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
330 BUG_ON(dlm->num_joins);
331 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
333 /* we may still be in the list if we hit an error during join. */
334 list_del_init(&dlm->list);
336 spin_unlock(&dlm_domain_lock);
338 mlog(0, "freeing memory from domain %s\n", dlm->name);
340 wake_up(&dlm_domain_events);
342 dlm_free_ctxt_mem(dlm);
344 spin_lock(&dlm_domain_lock);
347 void dlm_put(struct dlm_ctxt *dlm)
349 spin_lock(&dlm_domain_lock);
350 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
351 spin_unlock(&dlm_domain_lock);
354 static void __dlm_get(struct dlm_ctxt *dlm)
356 kref_get(&dlm->dlm_refs);
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)
364 struct dlm_ctxt *target;
365 struct dlm_ctxt *ret = NULL;
367 spin_lock(&dlm_domain_lock);
369 list_for_each_entry(target, &dlm_domains, list) {
370 if (target == dlm) {
371 __dlm_get(target);
372 ret = target;
373 break;
377 spin_unlock(&dlm_domain_lock);
379 return ret;
382 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
384 int ret;
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);
391 return ret;
394 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
396 if (dlm->dlm_worker) {
397 destroy_workqueue(dlm->dlm_worker);
398 dlm->dlm_worker = NULL;
402 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
404 dlm_unregister_domain_handlers(dlm);
405 dlm_debug_shutdown(dlm);
406 dlm_complete_thread(dlm);
407 dlm_complete_recovery_thread(dlm);
408 dlm_destroy_dlm_worker(dlm);
410 /* We've left the domain. Now we can take ourselves out of the
411 * list and allow the kref stuff to help us free the
412 * memory. */
413 spin_lock(&dlm_domain_lock);
414 list_del_init(&dlm->list);
415 spin_unlock(&dlm_domain_lock);
417 /* Wake up anyone waiting for us to remove this domain */
418 wake_up(&dlm_domain_events);
421 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
423 int i, num, n, ret = 0;
424 struct dlm_lock_resource *res;
425 struct hlist_node *iter;
426 struct hlist_head *bucket;
427 int dropped;
429 mlog(0, "Migrating locks from domain %s\n", dlm->name);
431 num = 0;
432 spin_lock(&dlm->spinlock);
433 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
434 redo_bucket:
435 n = 0;
436 bucket = dlm_lockres_hash(dlm, i);
437 iter = bucket->first;
438 while (iter) {
439 n++;
440 res = hlist_entry(iter, struct dlm_lock_resource,
441 hash_node);
442 dlm_lockres_get(res);
443 /* migrate, if necessary. this will drop the dlm
444 * spinlock and retake it if it does migration. */
445 dropped = dlm_empty_lockres(dlm, res);
447 spin_lock(&res->spinlock);
448 if (dropped)
449 __dlm_lockres_calc_usage(dlm, res);
450 else
451 iter = res->hash_node.next;
452 spin_unlock(&res->spinlock);
454 dlm_lockres_put(res);
456 if (dropped) {
457 cond_resched_lock(&dlm->spinlock);
458 goto redo_bucket;
461 cond_resched_lock(&dlm->spinlock);
462 num += n;
465 if (!num) {
466 if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
467 mlog(0, "%s: perhaps there are more lock resources "
468 "need to be migrated after dlm recovery\n", dlm->name);
469 ret = -EAGAIN;
470 } else {
471 mlog(0, "%s: we won't do dlm recovery after migrating "
472 "all lock resources\n", dlm->name);
473 dlm->migrate_done = 1;
477 spin_unlock(&dlm->spinlock);
478 wake_up(&dlm->dlm_thread_wq);
480 /* let the dlm thread take care of purging, keep scanning until
481 * nothing remains in the hash */
482 if (num) {
483 mlog(0, "%s: %d lock resources in hash last pass\n",
484 dlm->name, num);
485 ret = -EAGAIN;
487 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
488 return ret;
491 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
493 int ret;
495 spin_lock(&dlm->spinlock);
496 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
497 spin_unlock(&dlm->spinlock);
499 return ret;
502 static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
503 void *data, void **ret_data)
505 struct dlm_ctxt *dlm = data;
506 unsigned int node;
507 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
509 if (!dlm_grab(dlm))
510 return 0;
512 node = exit_msg->node_idx;
513 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
515 spin_lock(&dlm->spinlock);
516 set_bit(node, dlm->exit_domain_map);
517 spin_unlock(&dlm->spinlock);
519 dlm_put(dlm);
521 return 0;
524 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
526 /* Yikes, a double spinlock! I need domain_lock for the dlm
527 * state and the dlm spinlock for join state... Sorry! */
528 again:
529 spin_lock(&dlm_domain_lock);
530 spin_lock(&dlm->spinlock);
532 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
533 mlog(0, "Node %d is joining, we wait on it.\n",
534 dlm->joining_node);
535 spin_unlock(&dlm->spinlock);
536 spin_unlock(&dlm_domain_lock);
538 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
539 goto again;
542 dlm->dlm_state = DLM_CTXT_LEAVING;
543 spin_unlock(&dlm->spinlock);
544 spin_unlock(&dlm_domain_lock);
547 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
549 int node = -1, num = 0;
551 assert_spin_locked(&dlm->spinlock);
553 printk("( ");
554 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
555 node + 1)) < O2NM_MAX_NODES) {
556 printk("%d ", node);
557 ++num;
559 printk(") %u nodes\n", num);
562 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
563 void **ret_data)
565 struct dlm_ctxt *dlm = data;
566 unsigned int node;
567 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
569 mlog(0, "%p %u %p", msg, len, data);
571 if (!dlm_grab(dlm))
572 return 0;
574 node = exit_msg->node_idx;
576 spin_lock(&dlm->spinlock);
577 clear_bit(node, dlm->domain_map);
578 clear_bit(node, dlm->exit_domain_map);
579 printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
580 __dlm_print_nodes(dlm);
582 /* notify anything attached to the heartbeat events */
583 dlm_hb_event_notify_attached(dlm, node, 0);
585 spin_unlock(&dlm->spinlock);
587 dlm_put(dlm);
589 return 0;
592 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
593 unsigned int node)
595 int status;
596 struct dlm_exit_domain leave_msg;
598 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
599 msg_type, node);
601 memset(&leave_msg, 0, sizeof(leave_msg));
602 leave_msg.node_idx = dlm->node_num;
604 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
605 sizeof(leave_msg), node, NULL);
606 if (status < 0)
607 mlog(ML_ERROR, "Error %d sending domain exit message %u "
608 "to node %u on domain %s\n", status, msg_type, node,
609 dlm->name);
611 return status;
614 static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
616 int node = -1;
618 /* Support for begin exit domain was added in 1.2 */
619 if (dlm->dlm_locking_proto.pv_major == 1 &&
620 dlm->dlm_locking_proto.pv_minor < 2)
621 return;
624 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
625 * informational. Meaning if a node does not receive the message,
626 * so be it.
628 spin_lock(&dlm->spinlock);
629 while (1) {
630 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
631 if (node >= O2NM_MAX_NODES)
632 break;
633 if (node == dlm->node_num)
634 continue;
636 spin_unlock(&dlm->spinlock);
637 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
638 spin_lock(&dlm->spinlock);
640 spin_unlock(&dlm->spinlock);
643 static void dlm_leave_domain(struct dlm_ctxt *dlm)
645 int node, clear_node, status;
647 /* At this point we've migrated away all our locks and won't
648 * accept mastership of new ones. The dlm is responsible for
649 * almost nothing now. We make sure not to confuse any joining
650 * nodes and then commence shutdown procedure. */
652 spin_lock(&dlm->spinlock);
653 /* Clear ourselves from the domain map */
654 clear_bit(dlm->node_num, dlm->domain_map);
655 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
656 0)) < O2NM_MAX_NODES) {
657 /* Drop the dlm spinlock. This is safe wrt the domain_map.
658 * -nodes cannot be added now as the
659 * query_join_handlers knows to respond with OK_NO_MAP
660 * -we catch the right network errors if a node is
661 * removed from the map while we're sending him the
662 * exit message. */
663 spin_unlock(&dlm->spinlock);
665 clear_node = 1;
667 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
668 node);
669 if (status < 0 &&
670 status != -ENOPROTOOPT &&
671 status != -ENOTCONN) {
672 mlog(ML_NOTICE, "Error %d sending domain exit message "
673 "to node %d\n", status, node);
675 /* Not sure what to do here but lets sleep for
676 * a bit in case this was a transient
677 * error... */
678 msleep(DLM_DOMAIN_BACKOFF_MS);
679 clear_node = 0;
682 spin_lock(&dlm->spinlock);
683 /* If we're not clearing the node bit then we intend
684 * to loop back around to try again. */
685 if (clear_node)
686 clear_bit(node, dlm->domain_map);
688 spin_unlock(&dlm->spinlock);
691 void dlm_unregister_domain(struct dlm_ctxt *dlm)
693 int leave = 0;
694 struct dlm_lock_resource *res;
696 spin_lock(&dlm_domain_lock);
697 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
698 BUG_ON(!dlm->num_joins);
700 dlm->num_joins--;
701 if (!dlm->num_joins) {
702 /* We mark it "in shutdown" now so new register
703 * requests wait until we've completely left the
704 * domain. Don't use DLM_CTXT_LEAVING yet as we still
705 * want new domain joins to communicate with us at
706 * least until we've completed migration of our
707 * resources. */
708 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
709 leave = 1;
711 spin_unlock(&dlm_domain_lock);
713 if (leave) {
714 mlog(0, "shutting down domain %s\n", dlm->name);
715 dlm_begin_exit_domain(dlm);
717 /* We changed dlm state, notify the thread */
718 dlm_kick_thread(dlm, NULL);
720 while (dlm_migrate_all_locks(dlm)) {
721 /* Give dlm_thread time to purge the lockres' */
722 msleep(500);
723 mlog(0, "%s: more migration to do\n", dlm->name);
726 /* This list should be empty. If not, print remaining lockres */
727 if (!list_empty(&dlm->tracking_list)) {
728 mlog(ML_ERROR, "Following lockres' are still on the "
729 "tracking list:\n");
730 list_for_each_entry(res, &dlm->tracking_list, tracking)
731 dlm_print_one_lock_resource(res);
734 dlm_mark_domain_leaving(dlm);
735 dlm_leave_domain(dlm);
736 printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
737 dlm_force_free_mles(dlm);
738 dlm_complete_dlm_shutdown(dlm);
740 dlm_put(dlm);
742 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
744 static int dlm_query_join_proto_check(char *proto_type, int node,
745 struct dlm_protocol_version *ours,
746 struct dlm_protocol_version *request)
748 int rc;
749 struct dlm_protocol_version proto = *request;
751 if (!dlm_protocol_compare(ours, &proto)) {
752 mlog(0,
753 "node %u wanted to join with %s locking protocol "
754 "%u.%u, we respond with %u.%u\n",
755 node, proto_type,
756 request->pv_major,
757 request->pv_minor,
758 proto.pv_major, proto.pv_minor);
759 request->pv_minor = proto.pv_minor;
760 rc = 0;
761 } else {
762 mlog(ML_NOTICE,
763 "Node %u wanted to join with %s locking "
764 "protocol %u.%u, but we have %u.%u, disallowing\n",
765 node, proto_type,
766 request->pv_major,
767 request->pv_minor,
768 ours->pv_major,
769 ours->pv_minor);
770 rc = 1;
773 return rc;
777 * struct dlm_query_join_packet is made up of four one-byte fields. They
778 * are effectively in big-endian order already. However, little-endian
779 * machines swap them before putting the packet on the wire (because
780 * query_join's response is a status, and that status is treated as a u32
781 * on the wire). Thus, a big-endian and little-endian machines will treat
782 * this structure differently.
784 * The solution is to have little-endian machines swap the structure when
785 * converting from the structure to the u32 representation. This will
786 * result in the structure having the correct format on the wire no matter
787 * the host endian format.
789 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
790 u32 *wire)
792 union dlm_query_join_response response;
794 response.packet = *packet;
795 *wire = be32_to_cpu(response.intval);
798 static void dlm_query_join_wire_to_packet(u32 wire,
799 struct dlm_query_join_packet *packet)
801 union dlm_query_join_response response;
803 response.intval = cpu_to_be32(wire);
804 *packet = response.packet;
807 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
808 void **ret_data)
810 struct dlm_query_join_request *query;
811 struct dlm_query_join_packet packet = {
812 .code = JOIN_DISALLOW,
814 struct dlm_ctxt *dlm = NULL;
815 u32 response;
816 u8 nodenum;
818 query = (struct dlm_query_join_request *) msg->buf;
820 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
821 query->domain);
824 * If heartbeat doesn't consider the node live, tell it
825 * to back off and try again. This gives heartbeat a chance
826 * to catch up.
828 if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
829 mlog(0, "node %u is not in our live map yet\n",
830 query->node_idx);
832 packet.code = JOIN_DISALLOW;
833 goto respond;
836 packet.code = JOIN_OK_NO_MAP;
838 spin_lock(&dlm_domain_lock);
839 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
840 if (!dlm)
841 goto unlock_respond;
844 * There is a small window where the joining node may not see the
845 * node(s) that just left but still part of the cluster. DISALLOW
846 * join request if joining node has different node map.
848 nodenum=0;
849 while (nodenum < O2NM_MAX_NODES) {
850 if (test_bit(nodenum, dlm->domain_map)) {
851 if (!byte_test_bit(nodenum, query->node_map)) {
852 mlog(0, "disallow join as node %u does not "
853 "have node %u in its nodemap\n",
854 query->node_idx, nodenum);
855 packet.code = JOIN_DISALLOW;
856 goto unlock_respond;
859 nodenum++;
862 /* Once the dlm ctxt is marked as leaving then we don't want
863 * to be put in someone's domain map.
864 * Also, explicitly disallow joining at certain troublesome
865 * times (ie. during recovery). */
866 if (dlm->dlm_state != DLM_CTXT_LEAVING) {
867 int bit = query->node_idx;
868 spin_lock(&dlm->spinlock);
870 if (dlm->dlm_state == DLM_CTXT_NEW &&
871 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
872 /*If this is a brand new context and we
873 * haven't started our join process yet, then
874 * the other node won the race. */
875 packet.code = JOIN_OK_NO_MAP;
876 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
877 /* Disallow parallel joins. */
878 packet.code = JOIN_DISALLOW;
879 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
880 mlog(0, "node %u trying to join, but recovery "
881 "is ongoing.\n", bit);
882 packet.code = JOIN_DISALLOW;
883 } else if (test_bit(bit, dlm->recovery_map)) {
884 mlog(0, "node %u trying to join, but it "
885 "still needs recovery.\n", bit);
886 packet.code = JOIN_DISALLOW;
887 } else if (test_bit(bit, dlm->domain_map)) {
888 mlog(0, "node %u trying to join, but it "
889 "is still in the domain! needs recovery?\n",
890 bit);
891 packet.code = JOIN_DISALLOW;
892 } else {
893 /* Alright we're fully a part of this domain
894 * so we keep some state as to who's joining
895 * and indicate to him that needs to be fixed
896 * up. */
898 /* Make sure we speak compatible locking protocols. */
899 if (dlm_query_join_proto_check("DLM", bit,
900 &dlm->dlm_locking_proto,
901 &query->dlm_proto)) {
902 packet.code = JOIN_PROTOCOL_MISMATCH;
903 } else if (dlm_query_join_proto_check("fs", bit,
904 &dlm->fs_locking_proto,
905 &query->fs_proto)) {
906 packet.code = JOIN_PROTOCOL_MISMATCH;
907 } else {
908 packet.dlm_minor = query->dlm_proto.pv_minor;
909 packet.fs_minor = query->fs_proto.pv_minor;
910 packet.code = JOIN_OK;
911 __dlm_set_joining_node(dlm, query->node_idx);
915 spin_unlock(&dlm->spinlock);
917 unlock_respond:
918 spin_unlock(&dlm_domain_lock);
920 respond:
921 mlog(0, "We respond with %u\n", packet.code);
923 dlm_query_join_packet_to_wire(&packet, &response);
924 return response;
927 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
928 void **ret_data)
930 struct dlm_assert_joined *assert;
931 struct dlm_ctxt *dlm = NULL;
933 assert = (struct dlm_assert_joined *) msg->buf;
935 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
936 assert->domain);
938 spin_lock(&dlm_domain_lock);
939 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
940 /* XXX should we consider no dlm ctxt an error? */
941 if (dlm) {
942 spin_lock(&dlm->spinlock);
944 /* Alright, this node has officially joined our
945 * domain. Set him in the map and clean up our
946 * leftover join state. */
947 BUG_ON(dlm->joining_node != assert->node_idx);
949 if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
950 mlog(0, "dlm recovery is ongoing, disallow join\n");
951 spin_unlock(&dlm->spinlock);
952 spin_unlock(&dlm_domain_lock);
953 return -EAGAIN;
956 set_bit(assert->node_idx, dlm->domain_map);
957 clear_bit(assert->node_idx, dlm->exit_domain_map);
958 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
960 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
961 assert->node_idx, dlm->name);
962 __dlm_print_nodes(dlm);
964 /* notify anything attached to the heartbeat events */
965 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
967 spin_unlock(&dlm->spinlock);
969 spin_unlock(&dlm_domain_lock);
971 return 0;
974 static int dlm_match_regions(struct dlm_ctxt *dlm,
975 struct dlm_query_region *qr,
976 char *local, int locallen)
978 char *remote = qr->qr_regions;
979 char *l, *r;
980 int localnr, i, j, foundit;
981 int status = 0;
983 if (!o2hb_global_heartbeat_active()) {
984 if (qr->qr_numregions) {
985 mlog(ML_ERROR, "Domain %s: Joining node %d has global "
986 "heartbeat enabled but local node %d does not\n",
987 qr->qr_domain, qr->qr_node, dlm->node_num);
988 status = -EINVAL;
990 goto bail;
993 if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
994 mlog(ML_ERROR, "Domain %s: Local node %d has global "
995 "heartbeat enabled but joining node %d does not\n",
996 qr->qr_domain, dlm->node_num, qr->qr_node);
997 status = -EINVAL;
998 goto bail;
1001 r = remote;
1002 for (i = 0; i < qr->qr_numregions; ++i) {
1003 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
1004 r += O2HB_MAX_REGION_NAME_LEN;
1007 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
1008 localnr = o2hb_get_all_regions(local, (u8)localnr);
1010 /* compare local regions with remote */
1011 l = local;
1012 for (i = 0; i < localnr; ++i) {
1013 foundit = 0;
1014 r = remote;
1015 for (j = 0; j <= qr->qr_numregions; ++j) {
1016 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1017 foundit = 1;
1018 break;
1020 r += O2HB_MAX_REGION_NAME_LEN;
1022 if (!foundit) {
1023 status = -EINVAL;
1024 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1025 "in local node %d but not in joining node %d\n",
1026 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1027 dlm->node_num, qr->qr_node);
1028 goto bail;
1030 l += O2HB_MAX_REGION_NAME_LEN;
1033 /* compare remote with local regions */
1034 r = remote;
1035 for (i = 0; i < qr->qr_numregions; ++i) {
1036 foundit = 0;
1037 l = local;
1038 for (j = 0; j < localnr; ++j) {
1039 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1040 foundit = 1;
1041 break;
1043 l += O2HB_MAX_REGION_NAME_LEN;
1045 if (!foundit) {
1046 status = -EINVAL;
1047 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1048 "in joining node %d but not in local node %d\n",
1049 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1050 qr->qr_node, dlm->node_num);
1051 goto bail;
1053 r += O2HB_MAX_REGION_NAME_LEN;
1056 bail:
1057 return status;
1060 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1062 struct dlm_query_region *qr = NULL;
1063 int status, ret = 0, i;
1064 char *p;
1066 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1067 goto bail;
1069 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1070 if (!qr) {
1071 ret = -ENOMEM;
1072 mlog_errno(ret);
1073 goto bail;
1076 qr->qr_node = dlm->node_num;
1077 qr->qr_namelen = strlen(dlm->name);
1078 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1079 /* if local hb, the numregions will be zero */
1080 if (o2hb_global_heartbeat_active())
1081 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1082 O2NM_MAX_REGIONS);
1084 p = qr->qr_regions;
1085 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1086 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1088 i = -1;
1089 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1090 i + 1)) < O2NM_MAX_NODES) {
1091 if (i == dlm->node_num)
1092 continue;
1094 mlog(0, "Sending regions to node %d\n", i);
1096 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1097 sizeof(struct dlm_query_region),
1098 i, &status);
1099 if (ret >= 0)
1100 ret = status;
1101 if (ret) {
1102 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1103 ret, i);
1104 break;
1108 bail:
1109 kfree(qr);
1110 return ret;
1113 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1114 void *data, void **ret_data)
1116 struct dlm_query_region *qr;
1117 struct dlm_ctxt *dlm = NULL;
1118 char *local = NULL;
1119 int status = 0;
1121 qr = (struct dlm_query_region *) msg->buf;
1123 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1124 qr->qr_domain);
1126 /* buffer used in dlm_mast_regions() */
1127 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1128 if (!local)
1129 return -ENOMEM;
1131 status = -EINVAL;
1133 spin_lock(&dlm_domain_lock);
1134 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1135 if (!dlm) {
1136 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1137 "before join domain\n", qr->qr_node, qr->qr_domain);
1138 goto out_domain_lock;
1141 spin_lock(&dlm->spinlock);
1142 if (dlm->joining_node != qr->qr_node) {
1143 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1144 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1145 dlm->joining_node);
1146 goto out_dlm_lock;
1149 /* Support for global heartbeat was added in 1.1 */
1150 if (dlm->dlm_locking_proto.pv_major == 1 &&
1151 dlm->dlm_locking_proto.pv_minor == 0) {
1152 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1153 "but active dlm protocol is %d.%d\n", qr->qr_node,
1154 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1155 dlm->dlm_locking_proto.pv_minor);
1156 goto out_dlm_lock;
1159 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1161 out_dlm_lock:
1162 spin_unlock(&dlm->spinlock);
1164 out_domain_lock:
1165 spin_unlock(&dlm_domain_lock);
1167 kfree(local);
1169 return status;
1172 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1174 struct o2nm_node *local;
1175 struct dlm_node_info *remote;
1176 int i, j;
1177 int status = 0;
1179 for (j = 0; j < qn->qn_numnodes; ++j)
1180 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1181 &(qn->qn_nodes[j].ni_ipv4_address),
1182 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1184 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1185 local = o2nm_get_node_by_num(i);
1186 remote = NULL;
1187 for (j = 0; j < qn->qn_numnodes; ++j) {
1188 if (qn->qn_nodes[j].ni_nodenum == i) {
1189 remote = &(qn->qn_nodes[j]);
1190 break;
1194 if (!local && !remote)
1195 continue;
1197 if ((local && !remote) || (!local && remote))
1198 status = -EINVAL;
1200 if (!status &&
1201 ((remote->ni_nodenum != local->nd_num) ||
1202 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1203 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1204 status = -EINVAL;
1206 if (status) {
1207 if (remote && !local)
1208 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1209 "registered in joining node %d but not in "
1210 "local node %d\n", qn->qn_domain,
1211 remote->ni_nodenum,
1212 &(remote->ni_ipv4_address),
1213 ntohs(remote->ni_ipv4_port),
1214 qn->qn_nodenum, dlm->node_num);
1215 if (local && !remote)
1216 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1217 "registered in local node %d but not in "
1218 "joining node %d\n", qn->qn_domain,
1219 local->nd_num, &(local->nd_ipv4_address),
1220 ntohs(local->nd_ipv4_port),
1221 dlm->node_num, qn->qn_nodenum);
1222 BUG_ON((!local && !remote));
1225 if (local)
1226 o2nm_node_put(local);
1229 return status;
1232 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1234 struct dlm_query_nodeinfo *qn = NULL;
1235 struct o2nm_node *node;
1236 int ret = 0, status, count, i;
1238 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1239 goto bail;
1241 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1242 if (!qn) {
1243 ret = -ENOMEM;
1244 mlog_errno(ret);
1245 goto bail;
1248 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1249 node = o2nm_get_node_by_num(i);
1250 if (!node)
1251 continue;
1252 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1253 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1254 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1255 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1256 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1257 ++count;
1258 o2nm_node_put(node);
1261 qn->qn_nodenum = dlm->node_num;
1262 qn->qn_numnodes = count;
1263 qn->qn_namelen = strlen(dlm->name);
1264 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1266 i = -1;
1267 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1268 i + 1)) < O2NM_MAX_NODES) {
1269 if (i == dlm->node_num)
1270 continue;
1272 mlog(0, "Sending nodeinfo to node %d\n", i);
1274 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1275 qn, sizeof(struct dlm_query_nodeinfo),
1276 i, &status);
1277 if (ret >= 0)
1278 ret = status;
1279 if (ret) {
1280 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1281 break;
1285 bail:
1286 kfree(qn);
1287 return ret;
1290 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1291 void *data, void **ret_data)
1293 struct dlm_query_nodeinfo *qn;
1294 struct dlm_ctxt *dlm = NULL;
1295 int locked = 0, status = -EINVAL;
1297 qn = (struct dlm_query_nodeinfo *) msg->buf;
1299 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1300 qn->qn_domain);
1302 spin_lock(&dlm_domain_lock);
1303 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1304 if (!dlm) {
1305 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1306 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1307 goto bail;
1310 spin_lock(&dlm->spinlock);
1311 locked = 1;
1312 if (dlm->joining_node != qn->qn_nodenum) {
1313 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1314 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1315 dlm->joining_node);
1316 goto bail;
1319 /* Support for node query was added in 1.1 */
1320 if (dlm->dlm_locking_proto.pv_major == 1 &&
1321 dlm->dlm_locking_proto.pv_minor == 0) {
1322 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1323 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1324 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1325 dlm->dlm_locking_proto.pv_minor);
1326 goto bail;
1329 status = dlm_match_nodes(dlm, qn);
1331 bail:
1332 if (locked)
1333 spin_unlock(&dlm->spinlock);
1334 spin_unlock(&dlm_domain_lock);
1336 return status;
1339 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1340 void **ret_data)
1342 struct dlm_cancel_join *cancel;
1343 struct dlm_ctxt *dlm = NULL;
1345 cancel = (struct dlm_cancel_join *) msg->buf;
1347 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1348 cancel->domain);
1350 spin_lock(&dlm_domain_lock);
1351 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1353 if (dlm) {
1354 spin_lock(&dlm->spinlock);
1356 /* Yikes, this guy wants to cancel his join. No
1357 * problem, we simply cleanup our join state. */
1358 BUG_ON(dlm->joining_node != cancel->node_idx);
1359 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1361 spin_unlock(&dlm->spinlock);
1363 spin_unlock(&dlm_domain_lock);
1365 return 0;
1368 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1369 unsigned int node)
1371 int status;
1372 struct dlm_cancel_join cancel_msg;
1374 memset(&cancel_msg, 0, sizeof(cancel_msg));
1375 cancel_msg.node_idx = dlm->node_num;
1376 cancel_msg.name_len = strlen(dlm->name);
1377 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1379 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1380 &cancel_msg, sizeof(cancel_msg), node,
1381 NULL);
1382 if (status < 0) {
1383 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1384 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1385 node);
1386 goto bail;
1389 bail:
1390 return status;
1393 /* map_size should be in bytes. */
1394 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1395 unsigned long *node_map,
1396 unsigned int map_size)
1398 int status, tmpstat;
1399 int node;
1401 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1402 sizeof(unsigned long))) {
1403 mlog(ML_ERROR,
1404 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1405 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1406 return -EINVAL;
1409 status = 0;
1410 node = -1;
1411 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1412 node + 1)) < O2NM_MAX_NODES) {
1413 if (node == dlm->node_num)
1414 continue;
1416 tmpstat = dlm_send_one_join_cancel(dlm, node);
1417 if (tmpstat) {
1418 mlog(ML_ERROR, "Error return %d cancelling join on "
1419 "node %d\n", tmpstat, node);
1420 if (!status)
1421 status = tmpstat;
1425 if (status)
1426 mlog_errno(status);
1427 return status;
1430 static int dlm_request_join(struct dlm_ctxt *dlm,
1431 int node,
1432 enum dlm_query_join_response_code *response)
1434 int status;
1435 struct dlm_query_join_request join_msg;
1436 struct dlm_query_join_packet packet;
1437 u32 join_resp;
1439 mlog(0, "querying node %d\n", node);
1441 memset(&join_msg, 0, sizeof(join_msg));
1442 join_msg.node_idx = dlm->node_num;
1443 join_msg.name_len = strlen(dlm->name);
1444 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1445 join_msg.dlm_proto = dlm->dlm_locking_proto;
1446 join_msg.fs_proto = dlm->fs_locking_proto;
1448 /* copy live node map to join message */
1449 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1451 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1452 sizeof(join_msg), node, &join_resp);
1453 if (status < 0 && status != -ENOPROTOOPT) {
1454 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1455 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1456 node);
1457 goto bail;
1459 dlm_query_join_wire_to_packet(join_resp, &packet);
1461 /* -ENOPROTOOPT from the net code means the other side isn't
1462 listening for our message type -- that's fine, it means
1463 his dlm isn't up, so we can consider him a 'yes' but not
1464 joined into the domain. */
1465 if (status == -ENOPROTOOPT) {
1466 status = 0;
1467 *response = JOIN_OK_NO_MAP;
1468 } else {
1469 *response = packet.code;
1470 switch (packet.code) {
1471 case JOIN_DISALLOW:
1472 case JOIN_OK_NO_MAP:
1473 break;
1474 case JOIN_PROTOCOL_MISMATCH:
1475 mlog(ML_NOTICE,
1476 "This node requested DLM locking protocol %u.%u and "
1477 "filesystem locking protocol %u.%u. At least one of "
1478 "the protocol versions on node %d is not compatible, "
1479 "disconnecting\n",
1480 dlm->dlm_locking_proto.pv_major,
1481 dlm->dlm_locking_proto.pv_minor,
1482 dlm->fs_locking_proto.pv_major,
1483 dlm->fs_locking_proto.pv_minor,
1484 node);
1485 status = -EPROTO;
1486 break;
1487 case JOIN_OK:
1488 /* Use the same locking protocol as the remote node */
1489 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1490 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1491 mlog(0,
1492 "Node %d responds JOIN_OK with DLM locking protocol "
1493 "%u.%u and fs locking protocol %u.%u\n",
1494 node,
1495 dlm->dlm_locking_proto.pv_major,
1496 dlm->dlm_locking_proto.pv_minor,
1497 dlm->fs_locking_proto.pv_major,
1498 dlm->fs_locking_proto.pv_minor);
1499 break;
1500 default:
1501 status = -EINVAL;
1502 mlog(ML_ERROR, "invalid response %d from node %u\n",
1503 packet.code, node);
1504 /* Reset response to JOIN_DISALLOW */
1505 *response = JOIN_DISALLOW;
1506 break;
1510 mlog(0, "status %d, node %d response is %d\n", status, node,
1511 *response);
1513 bail:
1514 return status;
1517 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1518 unsigned int node)
1520 int status;
1521 int ret;
1522 struct dlm_assert_joined assert_msg;
1524 mlog(0, "Sending join assert to node %u\n", node);
1526 memset(&assert_msg, 0, sizeof(assert_msg));
1527 assert_msg.node_idx = dlm->node_num;
1528 assert_msg.name_len = strlen(dlm->name);
1529 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1531 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1532 &assert_msg, sizeof(assert_msg), node,
1533 &ret);
1534 if (status < 0)
1535 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1536 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1537 node);
1538 else
1539 status = ret;
1541 return status;
1544 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1545 unsigned long *node_map)
1547 int status, node, live;
1549 status = 0;
1550 node = -1;
1551 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1552 node + 1)) < O2NM_MAX_NODES) {
1553 if (node == dlm->node_num)
1554 continue;
1556 do {
1557 /* It is very important that this message be
1558 * received so we spin until either the node
1559 * has died or it gets the message. */
1560 status = dlm_send_one_join_assert(dlm, node);
1562 spin_lock(&dlm->spinlock);
1563 live = test_bit(node, dlm->live_nodes_map);
1564 spin_unlock(&dlm->spinlock);
1566 if (status) {
1567 mlog(ML_ERROR, "Error return %d asserting "
1568 "join on node %d\n", status, node);
1570 /* give us some time between errors... */
1571 if (live)
1572 msleep(DLM_DOMAIN_BACKOFF_MS);
1574 } while (status && live);
1578 struct domain_join_ctxt {
1579 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1580 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1583 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1584 struct domain_join_ctxt *ctxt,
1585 enum dlm_query_join_response_code response)
1587 int ret;
1589 if (response == JOIN_DISALLOW) {
1590 mlog(0, "Latest response of disallow -- should restart\n");
1591 return 1;
1594 spin_lock(&dlm->spinlock);
1595 /* For now, we restart the process if the node maps have
1596 * changed at all */
1597 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1598 sizeof(dlm->live_nodes_map));
1599 spin_unlock(&dlm->spinlock);
1601 if (ret)
1602 mlog(0, "Node maps changed -- should restart\n");
1604 return ret;
1607 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1609 int status = 0, tmpstat, node;
1610 struct domain_join_ctxt *ctxt;
1611 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1613 mlog(0, "%p", dlm);
1615 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1616 if (!ctxt) {
1617 status = -ENOMEM;
1618 mlog_errno(status);
1619 goto bail;
1622 /* group sem locking should work for us here -- we're already
1623 * registered for heartbeat events so filling this should be
1624 * atomic wrt getting those handlers called. */
1625 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1627 spin_lock(&dlm->spinlock);
1628 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1630 __dlm_set_joining_node(dlm, dlm->node_num);
1632 spin_unlock(&dlm->spinlock);
1634 node = -1;
1635 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1636 node + 1)) < O2NM_MAX_NODES) {
1637 if (node == dlm->node_num)
1638 continue;
1640 status = dlm_request_join(dlm, node, &response);
1641 if (status < 0) {
1642 mlog_errno(status);
1643 goto bail;
1646 /* Ok, either we got a response or the node doesn't have a
1647 * dlm up. */
1648 if (response == JOIN_OK)
1649 set_bit(node, ctxt->yes_resp_map);
1651 if (dlm_should_restart_join(dlm, ctxt, response)) {
1652 status = -EAGAIN;
1653 goto bail;
1657 mlog(0, "Yay, done querying nodes!\n");
1659 /* Yay, everyone agree's we can join the domain. My domain is
1660 * comprised of all nodes who were put in the
1661 * yes_resp_map. Copy that into our domain map and send a join
1662 * assert message to clean up everyone elses state. */
1663 spin_lock(&dlm->spinlock);
1664 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1665 sizeof(ctxt->yes_resp_map));
1666 set_bit(dlm->node_num, dlm->domain_map);
1667 spin_unlock(&dlm->spinlock);
1669 /* Support for global heartbeat and node info was added in 1.1 */
1670 if (dlm->dlm_locking_proto.pv_major > 1 ||
1671 dlm->dlm_locking_proto.pv_minor > 0) {
1672 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1673 if (status) {
1674 mlog_errno(status);
1675 goto bail;
1677 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1678 if (status) {
1679 mlog_errno(status);
1680 goto bail;
1684 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1686 /* Joined state *must* be set before the joining node
1687 * information, otherwise the query_join handler may read no
1688 * current joiner but a state of NEW and tell joining nodes
1689 * we're not in the domain. */
1690 spin_lock(&dlm_domain_lock);
1691 dlm->dlm_state = DLM_CTXT_JOINED;
1692 dlm->num_joins++;
1693 spin_unlock(&dlm_domain_lock);
1695 bail:
1696 spin_lock(&dlm->spinlock);
1697 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1698 if (!status) {
1699 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1700 __dlm_print_nodes(dlm);
1702 spin_unlock(&dlm->spinlock);
1704 if (ctxt) {
1705 /* Do we need to send a cancel message to any nodes? */
1706 if (status < 0) {
1707 tmpstat = dlm_send_join_cancels(dlm,
1708 ctxt->yes_resp_map,
1709 sizeof(ctxt->yes_resp_map));
1710 if (tmpstat < 0)
1711 mlog_errno(tmpstat);
1713 kfree(ctxt);
1716 mlog(0, "returning %d\n", status);
1717 return status;
1720 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1722 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1723 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1724 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1727 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1729 int status;
1731 mlog(0, "registering handlers.\n");
1733 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1734 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1735 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1736 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1738 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1739 if (status)
1740 goto bail;
1742 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1743 if (status)
1744 goto bail;
1746 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1747 sizeof(struct dlm_master_request),
1748 dlm_master_request_handler,
1749 dlm, NULL, &dlm->dlm_domain_handlers);
1750 if (status)
1751 goto bail;
1753 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1754 sizeof(struct dlm_assert_master),
1755 dlm_assert_master_handler,
1756 dlm, dlm_assert_master_post_handler,
1757 &dlm->dlm_domain_handlers);
1758 if (status)
1759 goto bail;
1761 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1762 sizeof(struct dlm_create_lock),
1763 dlm_create_lock_handler,
1764 dlm, NULL, &dlm->dlm_domain_handlers);
1765 if (status)
1766 goto bail;
1768 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1769 DLM_CONVERT_LOCK_MAX_LEN,
1770 dlm_convert_lock_handler,
1771 dlm, NULL, &dlm->dlm_domain_handlers);
1772 if (status)
1773 goto bail;
1775 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1776 DLM_UNLOCK_LOCK_MAX_LEN,
1777 dlm_unlock_lock_handler,
1778 dlm, NULL, &dlm->dlm_domain_handlers);
1779 if (status)
1780 goto bail;
1782 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1783 DLM_PROXY_AST_MAX_LEN,
1784 dlm_proxy_ast_handler,
1785 dlm, NULL, &dlm->dlm_domain_handlers);
1786 if (status)
1787 goto bail;
1789 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1790 sizeof(struct dlm_exit_domain),
1791 dlm_exit_domain_handler,
1792 dlm, NULL, &dlm->dlm_domain_handlers);
1793 if (status)
1794 goto bail;
1796 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1797 sizeof(struct dlm_deref_lockres),
1798 dlm_deref_lockres_handler,
1799 dlm, NULL, &dlm->dlm_domain_handlers);
1800 if (status)
1801 goto bail;
1803 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1804 sizeof(struct dlm_migrate_request),
1805 dlm_migrate_request_handler,
1806 dlm, NULL, &dlm->dlm_domain_handlers);
1807 if (status)
1808 goto bail;
1810 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1811 DLM_MIG_LOCKRES_MAX_LEN,
1812 dlm_mig_lockres_handler,
1813 dlm, NULL, &dlm->dlm_domain_handlers);
1814 if (status)
1815 goto bail;
1817 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1818 sizeof(struct dlm_master_requery),
1819 dlm_master_requery_handler,
1820 dlm, NULL, &dlm->dlm_domain_handlers);
1821 if (status)
1822 goto bail;
1824 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1825 sizeof(struct dlm_lock_request),
1826 dlm_request_all_locks_handler,
1827 dlm, NULL, &dlm->dlm_domain_handlers);
1828 if (status)
1829 goto bail;
1831 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1832 sizeof(struct dlm_reco_data_done),
1833 dlm_reco_data_done_handler,
1834 dlm, NULL, &dlm->dlm_domain_handlers);
1835 if (status)
1836 goto bail;
1838 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1839 sizeof(struct dlm_begin_reco),
1840 dlm_begin_reco_handler,
1841 dlm, NULL, &dlm->dlm_domain_handlers);
1842 if (status)
1843 goto bail;
1845 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1846 sizeof(struct dlm_finalize_reco),
1847 dlm_finalize_reco_handler,
1848 dlm, NULL, &dlm->dlm_domain_handlers);
1849 if (status)
1850 goto bail;
1852 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1853 sizeof(struct dlm_exit_domain),
1854 dlm_begin_exit_domain_handler,
1855 dlm, NULL, &dlm->dlm_domain_handlers);
1856 if (status)
1857 goto bail;
1859 status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
1860 sizeof(struct dlm_deref_lockres_done),
1861 dlm_deref_lockres_done_handler,
1862 dlm, NULL, &dlm->dlm_domain_handlers);
1863 bail:
1864 if (status)
1865 dlm_unregister_domain_handlers(dlm);
1867 return status;
1870 static int dlm_join_domain(struct dlm_ctxt *dlm)
1872 int status;
1873 unsigned int backoff;
1874 unsigned int total_backoff = 0;
1875 char wq_name[O2NM_MAX_NAME_LEN];
1877 BUG_ON(!dlm);
1879 mlog(0, "Join domain %s\n", dlm->name);
1881 status = dlm_register_domain_handlers(dlm);
1882 if (status) {
1883 mlog_errno(status);
1884 goto bail;
1887 status = dlm_launch_thread(dlm);
1888 if (status < 0) {
1889 mlog_errno(status);
1890 goto bail;
1893 status = dlm_launch_recovery_thread(dlm);
1894 if (status < 0) {
1895 mlog_errno(status);
1896 goto bail;
1899 status = dlm_debug_init(dlm);
1900 if (status < 0) {
1901 mlog_errno(status);
1902 goto bail;
1905 snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
1906 dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
1907 if (!dlm->dlm_worker) {
1908 status = -ENOMEM;
1909 mlog_errno(status);
1910 goto bail;
1913 do {
1914 status = dlm_try_to_join_domain(dlm);
1916 /* If we're racing another node to the join, then we
1917 * need to back off temporarily and let them
1918 * complete. */
1919 #define DLM_JOIN_TIMEOUT_MSECS 90000
1920 if (status == -EAGAIN) {
1921 if (signal_pending(current)) {
1922 status = -ERESTARTSYS;
1923 goto bail;
1926 if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
1927 status = -ERESTARTSYS;
1928 mlog(ML_NOTICE, "Timed out joining dlm domain "
1929 "%s after %u msecs\n", dlm->name,
1930 total_backoff);
1931 goto bail;
1935 * <chip> After you!
1936 * <dale> No, after you!
1937 * <chip> I insist!
1938 * <dale> But you first!
1939 * ...
1941 backoff = (unsigned int)(jiffies & 0x3);
1942 backoff *= DLM_DOMAIN_BACKOFF_MS;
1943 total_backoff += backoff;
1944 mlog(0, "backoff %d\n", backoff);
1945 msleep(backoff);
1947 } while (status == -EAGAIN);
1949 if (status < 0) {
1950 mlog_errno(status);
1951 goto bail;
1954 status = 0;
1955 bail:
1956 wake_up(&dlm_domain_events);
1958 if (status) {
1959 dlm_unregister_domain_handlers(dlm);
1960 dlm_debug_shutdown(dlm);
1961 dlm_complete_thread(dlm);
1962 dlm_complete_recovery_thread(dlm);
1963 dlm_destroy_dlm_worker(dlm);
1966 return status;
1969 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1970 u32 key)
1972 int i;
1973 int ret;
1974 struct dlm_ctxt *dlm = NULL;
1976 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1977 if (!dlm) {
1978 ret = -ENOMEM;
1979 mlog_errno(ret);
1980 goto leave;
1983 dlm->name = kstrdup(domain, GFP_KERNEL);
1984 if (dlm->name == NULL) {
1985 ret = -ENOMEM;
1986 mlog_errno(ret);
1987 goto leave;
1990 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1991 if (!dlm->lockres_hash) {
1992 ret = -ENOMEM;
1993 mlog_errno(ret);
1994 goto leave;
1997 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1998 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
2000 dlm->master_hash = (struct hlist_head **)
2001 dlm_alloc_pagevec(DLM_HASH_PAGES);
2002 if (!dlm->master_hash) {
2003 ret = -ENOMEM;
2004 mlog_errno(ret);
2005 goto leave;
2008 for (i = 0; i < DLM_HASH_BUCKETS; i++)
2009 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
2011 dlm->key = key;
2012 dlm->node_num = o2nm_this_node();
2014 ret = dlm_create_debugfs_subroot(dlm);
2015 if (ret < 0)
2016 goto leave;
2018 spin_lock_init(&dlm->spinlock);
2019 spin_lock_init(&dlm->master_lock);
2020 spin_lock_init(&dlm->ast_lock);
2021 spin_lock_init(&dlm->track_lock);
2022 INIT_LIST_HEAD(&dlm->list);
2023 INIT_LIST_HEAD(&dlm->dirty_list);
2024 INIT_LIST_HEAD(&dlm->reco.resources);
2025 INIT_LIST_HEAD(&dlm->reco.node_data);
2026 INIT_LIST_HEAD(&dlm->purge_list);
2027 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2028 INIT_LIST_HEAD(&dlm->tracking_list);
2029 dlm->reco.state = 0;
2031 INIT_LIST_HEAD(&dlm->pending_asts);
2032 INIT_LIST_HEAD(&dlm->pending_basts);
2034 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2035 dlm->recovery_map, &(dlm->recovery_map[0]));
2037 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2038 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2039 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2041 dlm->dlm_thread_task = NULL;
2042 dlm->dlm_reco_thread_task = NULL;
2043 dlm->dlm_worker = NULL;
2044 init_waitqueue_head(&dlm->dlm_thread_wq);
2045 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2046 init_waitqueue_head(&dlm->reco.event);
2047 init_waitqueue_head(&dlm->ast_wq);
2048 init_waitqueue_head(&dlm->migration_wq);
2049 INIT_LIST_HEAD(&dlm->mle_hb_events);
2051 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2052 init_waitqueue_head(&dlm->dlm_join_events);
2054 dlm->migrate_done = 0;
2056 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2057 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
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);
2066 spin_lock_init(&dlm->work_lock);
2067 INIT_LIST_HEAD(&dlm->work_list);
2068 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2070 kref_init(&dlm->dlm_refs);
2071 dlm->dlm_state = DLM_CTXT_NEW;
2073 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2075 mlog(0, "context init: refcount %u\n",
2076 kref_read(&dlm->dlm_refs));
2078 leave:
2079 if (ret < 0 && dlm) {
2080 if (dlm->master_hash)
2081 dlm_free_pagevec((void **)dlm->master_hash,
2082 DLM_HASH_PAGES);
2084 if (dlm->lockres_hash)
2085 dlm_free_pagevec((void **)dlm->lockres_hash,
2086 DLM_HASH_PAGES);
2088 kfree(dlm->name);
2089 kfree(dlm);
2090 dlm = NULL;
2092 return dlm;
2096 * Compare a requested locking protocol version against the current one.
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.
2103 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2104 struct dlm_protocol_version *request)
2106 if (existing->pv_major != request->pv_major)
2107 return 1;
2109 if (existing->pv_minor > request->pv_minor)
2110 return 1;
2112 if (existing->pv_minor < request->pv_minor)
2113 request->pv_minor = existing->pv_minor;
2115 return 0;
2119 * dlm_register_domain: one-time setup per "domain".
2121 * The filesystem passes in the requested locking version via proto.
2122 * If registration was successful, proto will contain the negotiated
2123 * locking protocol.
2125 struct dlm_ctxt * dlm_register_domain(const char *domain,
2126 u32 key,
2127 struct dlm_protocol_version *fs_proto)
2129 int ret;
2130 struct dlm_ctxt *dlm = NULL;
2131 struct dlm_ctxt *new_ctxt = NULL;
2133 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2134 ret = -ENAMETOOLONG;
2135 mlog(ML_ERROR, "domain name length too long\n");
2136 goto leave;
2139 mlog(0, "register called for domain \"%s\"\n", domain);
2141 retry:
2142 dlm = NULL;
2143 if (signal_pending(current)) {
2144 ret = -ERESTARTSYS;
2145 mlog_errno(ret);
2146 goto leave;
2149 spin_lock(&dlm_domain_lock);
2151 dlm = __dlm_lookup_domain(domain);
2152 if (dlm) {
2153 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2154 spin_unlock(&dlm_domain_lock);
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;
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;
2173 __dlm_get(dlm);
2174 dlm->num_joins++;
2176 spin_unlock(&dlm_domain_lock);
2178 ret = 0;
2179 goto leave;
2182 /* doesn't exist */
2183 if (!new_ctxt) {
2184 spin_unlock(&dlm_domain_lock);
2186 new_ctxt = dlm_alloc_ctxt(domain, key);
2187 if (new_ctxt)
2188 goto retry;
2190 ret = -ENOMEM;
2191 mlog_errno(ret);
2192 goto leave;
2195 /* a little variable switch-a-roo here... */
2196 dlm = new_ctxt;
2197 new_ctxt = NULL;
2199 /* add the new domain */
2200 list_add_tail(&dlm->list, &dlm_domains);
2201 spin_unlock(&dlm_domain_lock);
2204 * Pass the locking protocol version into the join. If the join
2205 * succeeds, it will have the negotiated protocol set.
2207 dlm->dlm_locking_proto = dlm_protocol;
2208 dlm->fs_locking_proto = *fs_proto;
2210 ret = dlm_join_domain(dlm);
2211 if (ret) {
2212 mlog_errno(ret);
2213 dlm_put(dlm);
2214 goto leave;
2217 /* Tell the caller what locking protocol we negotiated */
2218 *fs_proto = dlm->fs_locking_proto;
2220 ret = 0;
2221 leave:
2222 if (new_ctxt)
2223 dlm_free_ctxt_mem(new_ctxt);
2225 if (ret < 0)
2226 dlm = ERR_PTR(ret);
2228 return dlm;
2230 EXPORT_SYMBOL_GPL(dlm_register_domain);
2232 static LIST_HEAD(dlm_join_handlers);
2234 static void dlm_unregister_net_handlers(void)
2236 o2net_unregister_handler_list(&dlm_join_handlers);
2239 static int dlm_register_net_handlers(void)
2241 int status = 0;
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;
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;
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;
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);
2269 if (status)
2270 goto bail;
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();
2280 return status;
2283 /* Domain eviction callback handling.
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. */
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);
2296 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2297 int node_num)
2299 struct dlm_eviction_cb *cb;
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);
2305 up_read(&dlm_callback_sem);
2308 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2309 dlm_eviction_func *f,
2310 void *data)
2312 INIT_LIST_HEAD(&cb->ec_item);
2313 cb->ec_func = f;
2314 cb->ec_data = data;
2316 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2318 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2319 struct dlm_eviction_cb *cb)
2321 down_write(&dlm_callback_sem);
2322 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2323 up_write(&dlm_callback_sem);
2325 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2327 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2329 down_write(&dlm_callback_sem);
2330 list_del_init(&cb->ec_item);
2331 up_write(&dlm_callback_sem);
2333 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2335 static int __init dlm_init(void)
2337 int status;
2339 status = dlm_init_mle_cache();
2340 if (status) {
2341 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2342 goto error;
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;
2352 status = dlm_init_lock_cache();
2353 if (status) {
2354 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2355 goto error;
2358 status = dlm_register_net_handlers();
2359 if (status) {
2360 mlog(ML_ERROR, "Unable to register network handlers\n");
2361 goto error;
2364 status = dlm_create_debugfs_root();
2365 if (status)
2366 goto error;
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;
2377 static void __exit dlm_exit (void)
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();
2386 MODULE_AUTHOR("Oracle");
2387 MODULE_LICENSE("GPL");
2388 MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2390 module_init(dlm_init);
2391 module_exit(dlm_exit);