1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * defines domain join / leave apis
7 * Copyright (C) 2004 Oracle. All rights reserved.
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>
21 #include "../cluster/heartbeat.h"
22 #include "../cluster/nodemanager.h"
23 #include "../cluster/tcp.h"
26 #include "dlmcommon.h"
27 #include "dlmdomain.h"
30 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
31 #include "../cluster/masklog.h"
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
39 static inline void byte_set_bit(u8 nr, u8 map[])
41 map[nr >> 3] |= (1UL << (nr & 7));
44 static inline int byte_test_bit(u8 nr, u8 map[])
46 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
49 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
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);
63 static void dlm_free_pagevec(void **vec, int pages)
66 free_page((unsigned long)vec[pages]);
70 static void **dlm_alloc_pagevec(int pages)
72 void **vec = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
78 for (i = 0; i < pages; i++)
79 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
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);
87 dlm_free_pagevec(vec, i);
93 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
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
104 DEFINE_SPINLOCK(dlm_domain_lock);
105 LIST_HEAD(dlm_domains);
106 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
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.
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
123 static const struct dlm_protocol_version dlm_protocol = {
128 #define DLM_DOMAIN_BACKOFF_MS 200
130 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
132 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
134 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *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,
140 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
141 struct dlm_protocol_version *request);
143 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
145 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
147 if (hlist_unhashed(&res->hash_node))
150 mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
152 hlist_del_init(&res->hash_node);
153 dlm_lockres_put(res);
156 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
158 struct hlist_head *bucket;
160 assert_spin_locked(&dlm->spinlock);
162 bucket = dlm_lockres_hash(dlm, res->lockname.hash);
164 /* get a reference for our hashtable */
165 dlm_lockres_get(res);
167 hlist_add_head(&res->hash_node, bucket);
169 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
173 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
178 struct hlist_head *bucket;
179 struct dlm_lock_resource *res;
181 mlog(0, "%.*s\n", len, name);
183 assert_spin_locked(&dlm->spinlock);
185 bucket = dlm_lockres_hash(dlm, hash);
187 hlist_for_each_entry(res, bucket, hash_node) {
188 if (res->lockname.name[0] != name[0])
190 if (unlikely(res->lockname.len != len))
192 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
194 dlm_lockres_get(res);
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,
211 struct dlm_lock_resource *res = NULL;
213 mlog(0, "%.*s\n", len, name);
215 assert_spin_locked(&dlm->spinlock);
217 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
219 spin_lock(&res->spinlock);
220 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
221 spin_unlock(&res->spinlock);
222 dlm_lockres_put(res);
225 spin_unlock(&res->spinlock);
231 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
235 struct dlm_lock_resource *res;
236 unsigned int hash = dlm_lockid_hash(name, len);
238 spin_lock(&dlm->spinlock);
239 res = __dlm_lookup_lockres(dlm, name, len, hash);
240 spin_unlock(&dlm->spinlock);
244 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
246 struct dlm_ctxt *tmp;
248 assert_spin_locked(&dlm_domain_lock);
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)
261 /* For null terminated domain strings ONLY */
262 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
264 assert_spin_locked(&dlm_domain_lock);
266 return __dlm_lookup_domain_full(domain, strlen(domain));
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)
276 struct dlm_ctxt *tmp = NULL;
278 spin_lock(&dlm_domain_lock);
280 tmp = __dlm_lookup_domain(domain);
283 else if (tmp->dlm_state == DLM_CTXT_JOINED)
286 spin_unlock(&dlm_domain_lock);
290 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
292 dlm_destroy_debugfs_subroot(dlm);
294 if (dlm->lockres_hash)
295 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
297 if (dlm->master_hash)
298 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
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)
309 struct dlm_ctxt *dlm;
311 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
313 BUG_ON(dlm->num_joins);
314 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
316 /* we may still be in the list if we hit an error during join. */
317 list_del_init(&dlm->list);
319 spin_unlock(&dlm_domain_lock);
321 mlog(0, "freeing memory from domain %s\n", dlm->name);
323 wake_up(&dlm_domain_events);
325 dlm_free_ctxt_mem(dlm);
327 spin_lock(&dlm_domain_lock);
330 void dlm_put(struct dlm_ctxt *dlm)
332 spin_lock(&dlm_domain_lock);
333 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
334 spin_unlock(&dlm_domain_lock);
337 static void __dlm_get(struct dlm_ctxt *dlm)
339 kref_get(&dlm->dlm_refs);
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)
347 struct dlm_ctxt *target;
348 struct dlm_ctxt *ret = NULL;
350 spin_lock(&dlm_domain_lock);
352 list_for_each_entry(target, &dlm_domains, list) {
360 spin_unlock(&dlm_domain_lock);
365 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
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);
377 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
379 if (dlm->dlm_worker) {
380 destroy_workqueue(dlm->dlm_worker);
381 dlm->dlm_worker = NULL;
385 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
387 dlm_unregister_domain_handlers(dlm);
388 dlm_complete_thread(dlm);
389 dlm_complete_recovery_thread(dlm);
390 dlm_destroy_dlm_worker(dlm);
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
395 spin_lock(&dlm_domain_lock);
396 list_del_init(&dlm->list);
397 spin_unlock(&dlm_domain_lock);
399 /* Wake up anyone waiting for us to remove this domain */
400 wake_up(&dlm_domain_events);
403 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
405 int i, num, n, ret = 0;
406 struct dlm_lock_resource *res;
407 struct hlist_node *iter;
408 struct hlist_head *bucket;
411 mlog(0, "Migrating locks from domain %s\n", dlm->name);
414 spin_lock(&dlm->spinlock);
415 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
418 bucket = dlm_lockres_hash(dlm, i);
419 iter = bucket->first;
422 res = hlist_entry(iter, struct dlm_lock_resource,
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);
429 spin_lock(&res->spinlock);
431 __dlm_lockres_calc_usage(dlm, res);
433 iter = res->hash_node.next;
434 spin_unlock(&res->spinlock);
436 dlm_lockres_put(res);
439 cond_resched_lock(&dlm->spinlock);
443 cond_resched_lock(&dlm->spinlock);
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);
453 mlog(0, "%s: we won't do dlm recovery after migrating "
454 "all lock resources\n", dlm->name);
455 dlm->migrate_done = 1;
459 spin_unlock(&dlm->spinlock);
460 wake_up(&dlm->dlm_thread_wq);
462 /* let the dlm thread take care of purging, keep scanning until
463 * nothing remains in the hash */
465 mlog(0, "%s: %d lock resources in hash last pass\n",
469 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
473 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
477 spin_lock(&dlm->spinlock);
478 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
479 spin_unlock(&dlm->spinlock);
484 static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
485 void *data, void **ret_data)
487 struct dlm_ctxt *dlm = data;
489 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
494 node = exit_msg->node_idx;
495 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
497 spin_lock(&dlm->spinlock);
498 set_bit(node, dlm->exit_domain_map);
499 spin_unlock(&dlm->spinlock);
506 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
508 /* Yikes, a double spinlock! I need domain_lock for the dlm
509 * state and the dlm spinlock for join state... Sorry! */
511 spin_lock(&dlm_domain_lock);
512 spin_lock(&dlm->spinlock);
514 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
515 mlog(0, "Node %d is joining, we wait on it.\n",
517 spin_unlock(&dlm->spinlock);
518 spin_unlock(&dlm_domain_lock);
520 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
524 dlm->dlm_state = DLM_CTXT_LEAVING;
525 spin_unlock(&dlm->spinlock);
526 spin_unlock(&dlm_domain_lock);
529 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
531 int node = -1, num = 0;
533 assert_spin_locked(&dlm->spinlock);
536 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
537 node + 1)) < O2NM_MAX_NODES) {
541 printk(") %u nodes\n", num);
544 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
547 struct dlm_ctxt *dlm = data;
549 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
551 mlog(0, "%p %u %p", msg, len, data);
556 node = exit_msg->node_idx;
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);
564 /* notify anything attached to the heartbeat events */
565 dlm_hb_event_notify_attached(dlm, node, 0);
567 spin_unlock(&dlm->spinlock);
574 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
578 struct dlm_exit_domain leave_msg;
580 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
583 memset(&leave_msg, 0, sizeof(leave_msg));
584 leave_msg.node_idx = dlm->node_num;
586 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
587 sizeof(leave_msg), node, NULL);
589 mlog(ML_ERROR, "Error %d sending domain exit message %u "
590 "to node %u on domain %s\n", status, msg_type, node,
596 static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
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)
606 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
607 * informational. Meaning if a node does not receive the message,
610 spin_lock(&dlm->spinlock);
612 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
613 if (node >= O2NM_MAX_NODES)
615 if (node == dlm->node_num)
618 spin_unlock(&dlm->spinlock);
619 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
620 spin_lock(&dlm->spinlock);
622 spin_unlock(&dlm->spinlock);
625 static void dlm_leave_domain(struct dlm_ctxt *dlm)
627 int node, clear_node, status;
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. */
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
645 spin_unlock(&dlm->spinlock);
649 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
652 status != -ENOPROTOOPT &&
653 status != -ENOTCONN) {
654 mlog(ML_NOTICE, "Error %d sending domain exit message "
655 "to node %d\n", status, node);
657 /* Not sure what to do here but lets sleep for
658 * a bit in case this was a transient
660 msleep(DLM_DOMAIN_BACKOFF_MS);
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. */
668 clear_bit(node, dlm->domain_map);
670 spin_unlock(&dlm->spinlock);
673 void dlm_unregister_domain(struct dlm_ctxt *dlm)
676 struct dlm_lock_resource *res;
678 spin_lock(&dlm_domain_lock);
679 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
680 BUG_ON(!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
690 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
693 spin_unlock(&dlm_domain_lock);
696 mlog(0, "shutting down domain %s\n", dlm->name);
697 dlm_begin_exit_domain(dlm);
699 /* We changed dlm state, notify the thread */
700 dlm_kick_thread(dlm, NULL);
702 while (dlm_migrate_all_locks(dlm)) {
703 /* Give dlm_thread time to purge the lockres' */
705 mlog(0, "%s: more migration to do\n", dlm->name);
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 "
712 list_for_each_entry(res, &dlm->tracking_list, tracking)
713 dlm_print_one_lock_resource(res);
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);
724 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
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)
731 struct dlm_protocol_version proto = *request;
733 if (!dlm_protocol_compare(ours, &proto)) {
735 "node %u wanted to join with %s locking protocol "
736 "%u.%u, we respond with %u.%u\n",
740 proto.pv_major, proto.pv_minor);
741 request->pv_minor = proto.pv_minor;
745 "Node %u wanted to join with %s locking "
746 "protocol %u.%u, but we have %u.%u, disallowing\n",
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.
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.
771 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
774 union dlm_query_join_response response;
776 response.packet = *packet;
777 *wire = be32_to_cpu(response.intval);
780 static void dlm_query_join_wire_to_packet(u32 wire,
781 struct dlm_query_join_packet *packet)
783 union dlm_query_join_response response;
785 response.intval = cpu_to_be32(wire);
786 *packet = response.packet;
789 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
792 struct dlm_query_join_request *query;
793 struct dlm_query_join_packet packet = {
794 .code = JOIN_DISALLOW,
796 struct dlm_ctxt *dlm = NULL;
800 query = (struct dlm_query_join_request *) msg->buf;
802 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
806 * If heartbeat doesn't consider the node live, tell it
807 * to back off and try again. This gives heartbeat a chance
810 if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
811 mlog(0, "node %u is not in our live map yet\n",
814 packet.code = JOIN_DISALLOW;
818 packet.code = JOIN_OK_NO_MAP;
820 spin_lock(&dlm_domain_lock);
821 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
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.
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;
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);
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",
873 packet.code = JOIN_DISALLOW;
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
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,
888 packet.code = JOIN_PROTOCOL_MISMATCH;
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);
897 spin_unlock(&dlm->spinlock);
900 spin_unlock(&dlm_domain_lock);
903 mlog(0, "We respond with %u\n", packet.code);
905 dlm_query_join_packet_to_wire(&packet, &response);
909 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
912 struct dlm_assert_joined *assert;
913 struct dlm_ctxt *dlm = NULL;
915 assert = (struct dlm_assert_joined *) msg->buf;
917 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
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? */
924 spin_lock(&dlm->spinlock);
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);
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);
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);
942 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
943 assert->node_idx, dlm->name);
944 __dlm_print_nodes(dlm);
946 /* notify anything attached to the heartbeat events */
947 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
949 spin_unlock(&dlm->spinlock);
951 spin_unlock(&dlm_domain_lock);
956 static int dlm_match_regions(struct dlm_ctxt *dlm,
957 struct dlm_query_region *qr,
958 char *local, int locallen)
960 char *remote = qr->qr_regions;
962 int localnr, i, j, foundit;
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);
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);
984 for (i = 0; i < qr->qr_numregions; ++i) {
985 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
986 r += O2HB_MAX_REGION_NAME_LEN;
989 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
990 localnr = o2hb_get_all_regions(local, (u8)localnr);
992 /* compare local regions with remote */
994 for (i = 0; i < localnr; ++i) {
997 for (j = 0; j <= qr->qr_numregions; ++j) {
998 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1002 r += O2HB_MAX_REGION_NAME_LEN;
1006 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1007 "in local node %d but not in joining node %d\n",
1008 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1009 dlm->node_num, qr->qr_node);
1012 l += O2HB_MAX_REGION_NAME_LEN;
1015 /* compare remote with local regions */
1017 for (i = 0; i < qr->qr_numregions; ++i) {
1020 for (j = 0; j < localnr; ++j) {
1021 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1025 l += O2HB_MAX_REGION_NAME_LEN;
1029 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1030 "in joining node %d but not in local node %d\n",
1031 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1032 qr->qr_node, dlm->node_num);
1035 r += O2HB_MAX_REGION_NAME_LEN;
1042 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1044 struct dlm_query_region *qr = NULL;
1045 int status, ret = 0, i;
1048 if (find_first_bit(node_map, O2NM_MAX_NODES) >= O2NM_MAX_NODES)
1051 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1058 qr->qr_node = dlm->node_num;
1059 qr->qr_namelen = strlen(dlm->name);
1060 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1061 /* if local hb, the numregions will be zero */
1062 if (o2hb_global_heartbeat_active())
1063 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1067 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1068 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1071 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1072 i + 1)) < O2NM_MAX_NODES) {
1073 if (i == dlm->node_num)
1076 mlog(0, "Sending regions to node %d\n", i);
1078 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1079 sizeof(struct dlm_query_region),
1084 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1095 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1096 void *data, void **ret_data)
1098 struct dlm_query_region *qr;
1099 struct dlm_ctxt *dlm = NULL;
1103 qr = (struct dlm_query_region *) msg->buf;
1105 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1108 /* buffer used in dlm_mast_regions() */
1109 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1115 spin_lock(&dlm_domain_lock);
1116 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1118 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1119 "before join domain\n", qr->qr_node, qr->qr_domain);
1120 goto out_domain_lock;
1123 spin_lock(&dlm->spinlock);
1124 if (dlm->joining_node != qr->qr_node) {
1125 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1126 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1131 /* Support for global heartbeat was added in 1.1 */
1132 if (dlm->dlm_locking_proto.pv_major == 1 &&
1133 dlm->dlm_locking_proto.pv_minor == 0) {
1134 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1135 "but active dlm protocol is %d.%d\n", qr->qr_node,
1136 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1137 dlm->dlm_locking_proto.pv_minor);
1141 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1144 spin_unlock(&dlm->spinlock);
1147 spin_unlock(&dlm_domain_lock);
1154 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1156 struct o2nm_node *local;
1157 struct dlm_node_info *remote;
1161 for (j = 0; j < qn->qn_numnodes; ++j)
1162 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1163 &(qn->qn_nodes[j].ni_ipv4_address),
1164 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1166 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1167 local = o2nm_get_node_by_num(i);
1169 for (j = 0; j < qn->qn_numnodes; ++j) {
1170 if (qn->qn_nodes[j].ni_nodenum == i) {
1171 remote = &(qn->qn_nodes[j]);
1176 if (!local && !remote)
1179 if ((local && !remote) || (!local && remote))
1183 ((remote->ni_nodenum != local->nd_num) ||
1184 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1185 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1189 if (remote && !local)
1190 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1191 "registered in joining node %d but not in "
1192 "local node %d\n", qn->qn_domain,
1194 &(remote->ni_ipv4_address),
1195 ntohs(remote->ni_ipv4_port),
1196 qn->qn_nodenum, dlm->node_num);
1197 if (local && !remote)
1198 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1199 "registered in local node %d but not in "
1200 "joining node %d\n", qn->qn_domain,
1201 local->nd_num, &(local->nd_ipv4_address),
1202 ntohs(local->nd_ipv4_port),
1203 dlm->node_num, qn->qn_nodenum);
1204 BUG_ON((!local && !remote));
1208 o2nm_node_put(local);
1214 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1216 struct dlm_query_nodeinfo *qn = NULL;
1217 struct o2nm_node *node;
1218 int ret = 0, status, count, i;
1220 if (find_first_bit(node_map, O2NM_MAX_NODES) >= O2NM_MAX_NODES)
1223 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1230 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1231 node = o2nm_get_node_by_num(i);
1234 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1235 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1236 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1237 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1238 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1240 o2nm_node_put(node);
1243 qn->qn_nodenum = dlm->node_num;
1244 qn->qn_numnodes = count;
1245 qn->qn_namelen = strlen(dlm->name);
1246 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1249 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1250 i + 1)) < O2NM_MAX_NODES) {
1251 if (i == dlm->node_num)
1254 mlog(0, "Sending nodeinfo to node %d\n", i);
1256 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1257 qn, sizeof(struct dlm_query_nodeinfo),
1262 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1272 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1273 void *data, void **ret_data)
1275 struct dlm_query_nodeinfo *qn;
1276 struct dlm_ctxt *dlm = NULL;
1277 int locked = 0, status = -EINVAL;
1279 qn = (struct dlm_query_nodeinfo *) msg->buf;
1281 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1284 spin_lock(&dlm_domain_lock);
1285 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1287 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1288 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1292 spin_lock(&dlm->spinlock);
1294 if (dlm->joining_node != qn->qn_nodenum) {
1295 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1296 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1301 /* Support for node query was added in 1.1 */
1302 if (dlm->dlm_locking_proto.pv_major == 1 &&
1303 dlm->dlm_locking_proto.pv_minor == 0) {
1304 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1305 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1306 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1307 dlm->dlm_locking_proto.pv_minor);
1311 status = dlm_match_nodes(dlm, qn);
1315 spin_unlock(&dlm->spinlock);
1316 spin_unlock(&dlm_domain_lock);
1321 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1324 struct dlm_cancel_join *cancel;
1325 struct dlm_ctxt *dlm = NULL;
1327 cancel = (struct dlm_cancel_join *) msg->buf;
1329 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1332 spin_lock(&dlm_domain_lock);
1333 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1336 spin_lock(&dlm->spinlock);
1338 /* Yikes, this guy wants to cancel his join. No
1339 * problem, we simply cleanup our join state. */
1340 BUG_ON(dlm->joining_node != cancel->node_idx);
1341 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1343 spin_unlock(&dlm->spinlock);
1345 spin_unlock(&dlm_domain_lock);
1350 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1354 struct dlm_cancel_join cancel_msg;
1356 memset(&cancel_msg, 0, sizeof(cancel_msg));
1357 cancel_msg.node_idx = dlm->node_num;
1358 cancel_msg.name_len = strlen(dlm->name);
1359 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1361 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1362 &cancel_msg, sizeof(cancel_msg), node,
1365 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1366 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1375 /* map_size should be in bytes. */
1376 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1377 unsigned long *node_map,
1378 unsigned int map_size)
1380 int status, tmpstat;
1383 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1384 sizeof(unsigned long))) {
1386 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1387 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1393 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1394 node + 1)) < O2NM_MAX_NODES) {
1395 if (node == dlm->node_num)
1398 tmpstat = dlm_send_one_join_cancel(dlm, node);
1400 mlog(ML_ERROR, "Error return %d cancelling join on "
1401 "node %d\n", tmpstat, node);
1412 static int dlm_request_join(struct dlm_ctxt *dlm,
1414 enum dlm_query_join_response_code *response)
1417 struct dlm_query_join_request join_msg;
1418 struct dlm_query_join_packet packet;
1421 mlog(0, "querying node %d\n", node);
1423 memset(&join_msg, 0, sizeof(join_msg));
1424 join_msg.node_idx = dlm->node_num;
1425 join_msg.name_len = strlen(dlm->name);
1426 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1427 join_msg.dlm_proto = dlm->dlm_locking_proto;
1428 join_msg.fs_proto = dlm->fs_locking_proto;
1430 /* copy live node map to join message */
1431 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1433 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1434 sizeof(join_msg), node, &join_resp);
1435 if (status < 0 && status != -ENOPROTOOPT) {
1436 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1437 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1441 dlm_query_join_wire_to_packet(join_resp, &packet);
1443 /* -ENOPROTOOPT from the net code means the other side isn't
1444 listening for our message type -- that's fine, it means
1445 his dlm isn't up, so we can consider him a 'yes' but not
1446 joined into the domain. */
1447 if (status == -ENOPROTOOPT) {
1449 *response = JOIN_OK_NO_MAP;
1451 *response = packet.code;
1452 switch (packet.code) {
1454 case JOIN_OK_NO_MAP:
1456 case JOIN_PROTOCOL_MISMATCH:
1458 "This node requested DLM locking protocol %u.%u and "
1459 "filesystem locking protocol %u.%u. At least one of "
1460 "the protocol versions on node %d is not compatible, "
1462 dlm->dlm_locking_proto.pv_major,
1463 dlm->dlm_locking_proto.pv_minor,
1464 dlm->fs_locking_proto.pv_major,
1465 dlm->fs_locking_proto.pv_minor,
1470 /* Use the same locking protocol as the remote node */
1471 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1472 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1474 "Node %d responds JOIN_OK with DLM locking protocol "
1475 "%u.%u and fs locking protocol %u.%u\n",
1477 dlm->dlm_locking_proto.pv_major,
1478 dlm->dlm_locking_proto.pv_minor,
1479 dlm->fs_locking_proto.pv_major,
1480 dlm->fs_locking_proto.pv_minor);
1484 mlog(ML_ERROR, "invalid response %d from node %u\n",
1486 /* Reset response to JOIN_DISALLOW */
1487 *response = JOIN_DISALLOW;
1492 mlog(0, "status %d, node %d response is %d\n", status, node,
1499 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1504 struct dlm_assert_joined assert_msg;
1506 mlog(0, "Sending join assert to node %u\n", node);
1508 memset(&assert_msg, 0, sizeof(assert_msg));
1509 assert_msg.node_idx = dlm->node_num;
1510 assert_msg.name_len = strlen(dlm->name);
1511 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1513 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1514 &assert_msg, sizeof(assert_msg), node,
1517 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1518 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1526 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1527 unsigned long *node_map)
1529 int status, node, live;
1533 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1534 node + 1)) < O2NM_MAX_NODES) {
1535 if (node == dlm->node_num)
1539 /* It is very important that this message be
1540 * received so we spin until either the node
1541 * has died or it gets the message. */
1542 status = dlm_send_one_join_assert(dlm, node);
1544 spin_lock(&dlm->spinlock);
1545 live = test_bit(node, dlm->live_nodes_map);
1546 spin_unlock(&dlm->spinlock);
1549 mlog(ML_ERROR, "Error return %d asserting "
1550 "join on node %d\n", status, node);
1552 /* give us some time between errors... */
1554 msleep(DLM_DOMAIN_BACKOFF_MS);
1556 } while (status && live);
1560 struct domain_join_ctxt {
1561 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1562 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1565 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1566 struct domain_join_ctxt *ctxt,
1567 enum dlm_query_join_response_code response)
1571 if (response == JOIN_DISALLOW) {
1572 mlog(0, "Latest response of disallow -- should restart\n");
1576 spin_lock(&dlm->spinlock);
1577 /* For now, we restart the process if the node maps have
1579 ret = !bitmap_equal(ctxt->live_map, dlm->live_nodes_map,
1581 spin_unlock(&dlm->spinlock);
1584 mlog(0, "Node maps changed -- should restart\n");
1589 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1591 int status = 0, tmpstat, node;
1592 struct domain_join_ctxt *ctxt;
1593 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1597 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1604 /* group sem locking should work for us here -- we're already
1605 * registered for heartbeat events so filling this should be
1606 * atomic wrt getting those handlers called. */
1607 o2hb_fill_node_map(dlm->live_nodes_map, O2NM_MAX_NODES);
1609 spin_lock(&dlm->spinlock);
1610 bitmap_copy(ctxt->live_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1611 __dlm_set_joining_node(dlm, dlm->node_num);
1612 spin_unlock(&dlm->spinlock);
1615 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1616 node + 1)) < O2NM_MAX_NODES) {
1617 if (node == dlm->node_num)
1620 status = dlm_request_join(dlm, node, &response);
1626 /* Ok, either we got a response or the node doesn't have a
1628 if (response == JOIN_OK)
1629 set_bit(node, ctxt->yes_resp_map);
1631 if (dlm_should_restart_join(dlm, ctxt, response)) {
1637 mlog(0, "Yay, done querying nodes!\n");
1639 /* Yay, everyone agree's we can join the domain. My domain is
1640 * comprised of all nodes who were put in the
1641 * yes_resp_map. Copy that into our domain map and send a join
1642 * assert message to clean up everyone elses state. */
1643 spin_lock(&dlm->spinlock);
1644 bitmap_copy(dlm->domain_map, ctxt->yes_resp_map, O2NM_MAX_NODES);
1645 set_bit(dlm->node_num, dlm->domain_map);
1646 spin_unlock(&dlm->spinlock);
1648 /* Support for global heartbeat and node info was added in 1.1 */
1649 if (dlm->dlm_locking_proto.pv_major > 1 ||
1650 dlm->dlm_locking_proto.pv_minor > 0) {
1651 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1656 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1663 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1665 /* Joined state *must* be set before the joining node
1666 * information, otherwise the query_join handler may read no
1667 * current joiner but a state of NEW and tell joining nodes
1668 * we're not in the domain. */
1669 spin_lock(&dlm_domain_lock);
1670 dlm->dlm_state = DLM_CTXT_JOINED;
1672 spin_unlock(&dlm_domain_lock);
1675 spin_lock(&dlm->spinlock);
1676 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1678 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1679 __dlm_print_nodes(dlm);
1681 spin_unlock(&dlm->spinlock);
1684 /* Do we need to send a cancel message to any nodes? */
1686 tmpstat = dlm_send_join_cancels(dlm,
1688 sizeof(ctxt->yes_resp_map));
1690 mlog_errno(tmpstat);
1695 mlog(0, "returning %d\n", status);
1699 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1701 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1702 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1703 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1706 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1710 mlog(0, "registering handlers.\n");
1712 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1713 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1714 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1715 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1717 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1721 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1725 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1726 sizeof(struct dlm_master_request),
1727 dlm_master_request_handler,
1728 dlm, NULL, &dlm->dlm_domain_handlers);
1732 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1733 sizeof(struct dlm_assert_master),
1734 dlm_assert_master_handler,
1735 dlm, dlm_assert_master_post_handler,
1736 &dlm->dlm_domain_handlers);
1740 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1741 sizeof(struct dlm_create_lock),
1742 dlm_create_lock_handler,
1743 dlm, NULL, &dlm->dlm_domain_handlers);
1747 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1748 DLM_CONVERT_LOCK_MAX_LEN,
1749 dlm_convert_lock_handler,
1750 dlm, NULL, &dlm->dlm_domain_handlers);
1754 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1755 DLM_UNLOCK_LOCK_MAX_LEN,
1756 dlm_unlock_lock_handler,
1757 dlm, NULL, &dlm->dlm_domain_handlers);
1761 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1762 DLM_PROXY_AST_MAX_LEN,
1763 dlm_proxy_ast_handler,
1764 dlm, NULL, &dlm->dlm_domain_handlers);
1768 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1769 sizeof(struct dlm_exit_domain),
1770 dlm_exit_domain_handler,
1771 dlm, NULL, &dlm->dlm_domain_handlers);
1775 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1776 sizeof(struct dlm_deref_lockres),
1777 dlm_deref_lockres_handler,
1778 dlm, NULL, &dlm->dlm_domain_handlers);
1782 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1783 sizeof(struct dlm_migrate_request),
1784 dlm_migrate_request_handler,
1785 dlm, NULL, &dlm->dlm_domain_handlers);
1789 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1790 DLM_MIG_LOCKRES_MAX_LEN,
1791 dlm_mig_lockres_handler,
1792 dlm, NULL, &dlm->dlm_domain_handlers);
1796 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1797 sizeof(struct dlm_master_requery),
1798 dlm_master_requery_handler,
1799 dlm, NULL, &dlm->dlm_domain_handlers);
1803 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1804 sizeof(struct dlm_lock_request),
1805 dlm_request_all_locks_handler,
1806 dlm, NULL, &dlm->dlm_domain_handlers);
1810 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1811 sizeof(struct dlm_reco_data_done),
1812 dlm_reco_data_done_handler,
1813 dlm, NULL, &dlm->dlm_domain_handlers);
1817 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1818 sizeof(struct dlm_begin_reco),
1819 dlm_begin_reco_handler,
1820 dlm, NULL, &dlm->dlm_domain_handlers);
1824 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1825 sizeof(struct dlm_finalize_reco),
1826 dlm_finalize_reco_handler,
1827 dlm, NULL, &dlm->dlm_domain_handlers);
1831 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1832 sizeof(struct dlm_exit_domain),
1833 dlm_begin_exit_domain_handler,
1834 dlm, NULL, &dlm->dlm_domain_handlers);
1838 status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
1839 sizeof(struct dlm_deref_lockres_done),
1840 dlm_deref_lockres_done_handler,
1841 dlm, NULL, &dlm->dlm_domain_handlers);
1844 dlm_unregister_domain_handlers(dlm);
1849 static int dlm_join_domain(struct dlm_ctxt *dlm)
1852 unsigned int backoff;
1853 unsigned int total_backoff = 0;
1854 char wq_name[O2NM_MAX_NAME_LEN];
1858 mlog(0, "Join domain %s\n", dlm->name);
1860 status = dlm_register_domain_handlers(dlm);
1866 status = dlm_launch_thread(dlm);
1872 status = dlm_launch_recovery_thread(dlm);
1878 dlm_debug_init(dlm);
1880 snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
1881 dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
1882 if (!dlm->dlm_worker) {
1889 status = dlm_try_to_join_domain(dlm);
1891 /* If we're racing another node to the join, then we
1892 * need to back off temporarily and let them
1894 #define DLM_JOIN_TIMEOUT_MSECS 90000
1895 if (status == -EAGAIN) {
1896 if (signal_pending(current)) {
1897 status = -ERESTARTSYS;
1901 if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
1902 status = -ERESTARTSYS;
1903 mlog(ML_NOTICE, "Timed out joining dlm domain "
1904 "%s after %u msecs\n", dlm->name,
1911 * <dale> No, after you!
1913 * <dale> But you first!
1916 backoff = (unsigned int)(jiffies & 0x3);
1917 backoff *= DLM_DOMAIN_BACKOFF_MS;
1918 total_backoff += backoff;
1919 mlog(0, "backoff %d\n", backoff);
1922 } while (status == -EAGAIN);
1931 wake_up(&dlm_domain_events);
1934 dlm_unregister_domain_handlers(dlm);
1935 dlm_complete_thread(dlm);
1936 dlm_complete_recovery_thread(dlm);
1937 dlm_destroy_dlm_worker(dlm);
1943 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1948 struct dlm_ctxt *dlm = NULL;
1950 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1957 dlm->name = kstrdup(domain, GFP_KERNEL);
1958 if (dlm->name == NULL) {
1964 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1965 if (!dlm->lockres_hash) {
1971 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1972 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1974 dlm->master_hash = (struct hlist_head **)
1975 dlm_alloc_pagevec(DLM_HASH_PAGES);
1976 if (!dlm->master_hash) {
1982 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1983 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1986 dlm->node_num = o2nm_this_node();
1988 dlm_create_debugfs_subroot(dlm);
1990 spin_lock_init(&dlm->spinlock);
1991 spin_lock_init(&dlm->master_lock);
1992 spin_lock_init(&dlm->ast_lock);
1993 spin_lock_init(&dlm->track_lock);
1994 INIT_LIST_HEAD(&dlm->list);
1995 INIT_LIST_HEAD(&dlm->dirty_list);
1996 INIT_LIST_HEAD(&dlm->reco.resources);
1997 INIT_LIST_HEAD(&dlm->reco.node_data);
1998 INIT_LIST_HEAD(&dlm->purge_list);
1999 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2000 INIT_LIST_HEAD(&dlm->tracking_list);
2001 dlm->reco.state = 0;
2003 INIT_LIST_HEAD(&dlm->pending_asts);
2004 INIT_LIST_HEAD(&dlm->pending_basts);
2006 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2007 dlm->recovery_map, &(dlm->recovery_map[0]));
2009 bitmap_zero(dlm->recovery_map, O2NM_MAX_NODES);
2010 bitmap_zero(dlm->live_nodes_map, O2NM_MAX_NODES);
2011 bitmap_zero(dlm->domain_map, O2NM_MAX_NODES);
2013 dlm->dlm_thread_task = NULL;
2014 dlm->dlm_reco_thread_task = NULL;
2015 dlm->dlm_worker = NULL;
2016 init_waitqueue_head(&dlm->dlm_thread_wq);
2017 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2018 init_waitqueue_head(&dlm->reco.event);
2019 init_waitqueue_head(&dlm->ast_wq);
2020 init_waitqueue_head(&dlm->migration_wq);
2021 INIT_LIST_HEAD(&dlm->mle_hb_events);
2023 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2024 init_waitqueue_head(&dlm->dlm_join_events);
2026 dlm->migrate_done = 0;
2028 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2029 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2031 atomic_set(&dlm->res_tot_count, 0);
2032 atomic_set(&dlm->res_cur_count, 0);
2033 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2034 atomic_set(&dlm->mle_tot_count[i], 0);
2035 atomic_set(&dlm->mle_cur_count[i], 0);
2038 spin_lock_init(&dlm->work_lock);
2039 INIT_LIST_HEAD(&dlm->work_list);
2040 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2042 kref_init(&dlm->dlm_refs);
2043 dlm->dlm_state = DLM_CTXT_NEW;
2045 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2047 mlog(0, "context init: refcount %u\n",
2048 kref_read(&dlm->dlm_refs));
2052 if (ret < 0 && dlm) {
2053 if (dlm->master_hash)
2054 dlm_free_pagevec((void **)dlm->master_hash,
2057 if (dlm->lockres_hash)
2058 dlm_free_pagevec((void **)dlm->lockres_hash,
2069 * Compare a requested locking protocol version against the current one.
2071 * If the major numbers are different, they are incompatible.
2072 * If the current minor is greater than the request, they are incompatible.
2073 * If the current minor is less than or equal to the request, they are
2074 * compatible, and the requester should run at the current minor version.
2076 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2077 struct dlm_protocol_version *request)
2079 if (existing->pv_major != request->pv_major)
2082 if (existing->pv_minor > request->pv_minor)
2085 if (existing->pv_minor < request->pv_minor)
2086 request->pv_minor = existing->pv_minor;
2092 * dlm_register_domain: one-time setup per "domain".
2094 * The filesystem passes in the requested locking version via proto.
2095 * If registration was successful, proto will contain the negotiated
2098 struct dlm_ctxt * dlm_register_domain(const char *domain,
2100 struct dlm_protocol_version *fs_proto)
2103 struct dlm_ctxt *dlm = NULL;
2104 struct dlm_ctxt *new_ctxt = NULL;
2106 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2107 ret = -ENAMETOOLONG;
2108 mlog(ML_ERROR, "domain name length too long\n");
2112 mlog(0, "register called for domain \"%s\"\n", domain);
2116 if (signal_pending(current)) {
2122 spin_lock(&dlm_domain_lock);
2124 dlm = __dlm_lookup_domain(domain);
2126 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2127 spin_unlock(&dlm_domain_lock);
2129 mlog(0, "This ctxt is not joined yet!\n");
2130 wait_event_interruptible(dlm_domain_events,
2131 dlm_wait_on_domain_helper(
2136 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2137 spin_unlock(&dlm_domain_lock);
2139 "Requested locking protocol version is not "
2140 "compatible with already registered domain "
2141 "\"%s\"\n", domain);
2149 spin_unlock(&dlm_domain_lock);
2157 spin_unlock(&dlm_domain_lock);
2159 new_ctxt = dlm_alloc_ctxt(domain, key);
2168 /* a little variable switch-a-roo here... */
2172 /* add the new domain */
2173 list_add_tail(&dlm->list, &dlm_domains);
2174 spin_unlock(&dlm_domain_lock);
2177 * Pass the locking protocol version into the join. If the join
2178 * succeeds, it will have the negotiated protocol set.
2180 dlm->dlm_locking_proto = dlm_protocol;
2181 dlm->fs_locking_proto = *fs_proto;
2183 ret = dlm_join_domain(dlm);
2190 /* Tell the caller what locking protocol we negotiated */
2191 *fs_proto = dlm->fs_locking_proto;
2196 dlm_free_ctxt_mem(new_ctxt);
2203 EXPORT_SYMBOL_GPL(dlm_register_domain);
2205 static LIST_HEAD(dlm_join_handlers);
2207 static void dlm_unregister_net_handlers(void)
2209 o2net_unregister_handler_list(&dlm_join_handlers);
2212 static int dlm_register_net_handlers(void)
2216 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2217 sizeof(struct dlm_query_join_request),
2218 dlm_query_join_handler,
2219 NULL, NULL, &dlm_join_handlers);
2223 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2224 sizeof(struct dlm_assert_joined),
2225 dlm_assert_joined_handler,
2226 NULL, NULL, &dlm_join_handlers);
2230 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2231 sizeof(struct dlm_cancel_join),
2232 dlm_cancel_join_handler,
2233 NULL, NULL, &dlm_join_handlers);
2237 status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2238 sizeof(struct dlm_query_region),
2239 dlm_query_region_handler,
2240 NULL, NULL, &dlm_join_handlers);
2245 status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2246 sizeof(struct dlm_query_nodeinfo),
2247 dlm_query_nodeinfo_handler,
2248 NULL, NULL, &dlm_join_handlers);
2251 dlm_unregister_net_handlers();
2256 /* Domain eviction callback handling.
2258 * The file system requires notification of node death *before* the
2259 * dlm completes it's recovery work, otherwise it may be able to
2260 * acquire locks on resources requiring recovery. Since the dlm can
2261 * evict a node from it's domain *before* heartbeat fires, a similar
2262 * mechanism is required. */
2264 /* Eviction is not expected to happen often, so a per-domain lock is
2265 * not necessary. Eviction callbacks are allowed to sleep for short
2266 * periods of time. */
2267 static DECLARE_RWSEM(dlm_callback_sem);
2269 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2272 struct dlm_eviction_cb *cb;
2274 down_read(&dlm_callback_sem);
2275 list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2276 cb->ec_func(node_num, cb->ec_data);
2278 up_read(&dlm_callback_sem);
2281 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2282 dlm_eviction_func *f,
2285 INIT_LIST_HEAD(&cb->ec_item);
2289 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2291 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2292 struct dlm_eviction_cb *cb)
2294 down_write(&dlm_callback_sem);
2295 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2296 up_write(&dlm_callback_sem);
2298 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2300 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2302 down_write(&dlm_callback_sem);
2303 list_del_init(&cb->ec_item);
2304 up_write(&dlm_callback_sem);
2306 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2308 static int __init dlm_init(void)
2312 status = dlm_init_mle_cache();
2314 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2318 status = dlm_init_master_caches();
2320 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2321 "o2dlm_lockname slabcaches\n");
2325 status = dlm_init_lock_cache();
2327 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2331 status = dlm_register_net_handlers();
2333 mlog(ML_ERROR, "Unable to register network handlers\n");
2337 dlm_create_debugfs_root();
2341 dlm_unregister_net_handlers();
2342 dlm_destroy_lock_cache();
2343 dlm_destroy_master_caches();
2344 dlm_destroy_mle_cache();
2348 static void __exit dlm_exit (void)
2350 dlm_destroy_debugfs_root();
2351 dlm_unregister_net_handlers();
2352 dlm_destroy_lock_cache();
2353 dlm_destroy_master_caches();
2354 dlm_destroy_mle_cache();
2357 MODULE_AUTHOR("Oracle");
2358 MODULE_LICENSE("GPL");
2359 MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2361 module_init(dlm_init);
2362 module_exit(dlm_exit);