1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
7 * defines domain join / leave apis
9 * Copyright (C) 2004 Oracle. All rights reserved.
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/highmem.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/debugfs.h>
21 #include <linux/sched/signal.h>
23 #include "../cluster/heartbeat.h"
24 #include "../cluster/nodemanager.h"
25 #include "../cluster/tcp.h"
28 #include "dlmcommon.h"
29 #include "dlmdomain.h"
32 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
33 #include "../cluster/masklog.h"
36 * ocfs2 node maps are array of long int, which limits to send them freely
37 * across the wire due to endianness issues. To workaround this, we convert
38 * long ints to byte arrays. Following 3 routines are helper functions to
39 * set/test/copy bits within those array of bytes
41 static inline void byte_set_bit(u8 nr, u8 map[])
43 map[nr >> 3] |= (1UL << (nr & 7));
46 static inline int byte_test_bit(u8 nr, u8 map[])
48 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
51 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
59 memset(dmap, 0, ((sz + 7) >> 3));
60 for (nn = 0 ; nn < sz; nn++)
61 if (test_bit(nn, smap))
62 byte_set_bit(nn, dmap);
65 static void dlm_free_pagevec(void **vec, int pages)
68 free_page((unsigned long)vec[pages]);
72 static void **dlm_alloc_pagevec(int pages)
74 void **vec = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
80 for (i = 0; i < pages; i++)
81 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
84 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
85 pages, (unsigned long)DLM_HASH_PAGES,
86 (unsigned long)DLM_BUCKETS_PER_PAGE);
89 dlm_free_pagevec(vec, i);
95 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
97 * struct dlm_ctxt->spinlock
98 * struct dlm_lock_resource->spinlock
99 * struct dlm_ctxt->master_lock
100 * struct dlm_ctxt->ast_lock
101 * dlm_master_list_entry->spinlock
106 DEFINE_SPINLOCK(dlm_domain_lock);
107 LIST_HEAD(dlm_domains);
108 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
111 * The supported protocol version for DLM communication. Running domains
112 * will have a negotiated version with the same major number and a minor
113 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
114 * be used to determine what a running domain is actually using.
116 * New in version 1.1:
117 * - Message DLM_QUERY_REGION added to support global heartbeat
118 * - Message DLM_QUERY_NODEINFO added to allow online node removes
119 * New in version 1.2:
120 * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
121 * New in version 1.3:
122 * - Message DLM_DEREF_LOCKRES_DONE added to inform non-master that the
125 static const struct dlm_protocol_version dlm_protocol = {
130 #define DLM_DOMAIN_BACKOFF_MS 200
132 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
134 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
136 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
138 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
139 void *data, void **ret_data);
140 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
142 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
143 struct dlm_protocol_version *request);
145 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
147 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
149 if (hlist_unhashed(&res->hash_node))
152 mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
154 hlist_del_init(&res->hash_node);
155 dlm_lockres_put(res);
158 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
160 struct hlist_head *bucket;
162 assert_spin_locked(&dlm->spinlock);
164 bucket = dlm_lockres_hash(dlm, res->lockname.hash);
166 /* get a reference for our hashtable */
167 dlm_lockres_get(res);
169 hlist_add_head(&res->hash_node, bucket);
171 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
175 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
180 struct hlist_head *bucket;
181 struct dlm_lock_resource *res;
183 mlog(0, "%.*s\n", len, name);
185 assert_spin_locked(&dlm->spinlock);
187 bucket = dlm_lockres_hash(dlm, hash);
189 hlist_for_each_entry(res, bucket, hash_node) {
190 if (res->lockname.name[0] != name[0])
192 if (unlikely(res->lockname.len != len))
194 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
196 dlm_lockres_get(res);
202 /* intended to be called by functions which do not care about lock
203 * resources which are being purged (most net _handler functions).
204 * this will return NULL for any lock resource which is found but
205 * currently in the process of dropping its mastery reference.
206 * use __dlm_lookup_lockres_full when you need the lock resource
207 * regardless (e.g. dlm_get_lock_resource) */
208 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
213 struct dlm_lock_resource *res = NULL;
215 mlog(0, "%.*s\n", len, name);
217 assert_spin_locked(&dlm->spinlock);
219 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
221 spin_lock(&res->spinlock);
222 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
223 spin_unlock(&res->spinlock);
224 dlm_lockres_put(res);
227 spin_unlock(&res->spinlock);
233 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
237 struct dlm_lock_resource *res;
238 unsigned int hash = dlm_lockid_hash(name, len);
240 spin_lock(&dlm->spinlock);
241 res = __dlm_lookup_lockres(dlm, name, len, hash);
242 spin_unlock(&dlm->spinlock);
246 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
248 struct dlm_ctxt *tmp;
250 assert_spin_locked(&dlm_domain_lock);
252 /* tmp->name here is always NULL terminated,
253 * but domain may not be! */
254 list_for_each_entry(tmp, &dlm_domains, list) {
255 if (strlen(tmp->name) == len &&
256 memcmp(tmp->name, domain, len)==0)
263 /* For null terminated domain strings ONLY */
264 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
266 assert_spin_locked(&dlm_domain_lock);
268 return __dlm_lookup_domain_full(domain, strlen(domain));
272 /* returns true on one of two conditions:
273 * 1) the domain does not exist
274 * 2) the domain exists and it's state is "joined" */
275 static int dlm_wait_on_domain_helper(const char *domain)
278 struct dlm_ctxt *tmp = NULL;
280 spin_lock(&dlm_domain_lock);
282 tmp = __dlm_lookup_domain(domain);
285 else if (tmp->dlm_state == DLM_CTXT_JOINED)
288 spin_unlock(&dlm_domain_lock);
292 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
294 dlm_destroy_debugfs_subroot(dlm);
296 if (dlm->lockres_hash)
297 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
299 if (dlm->master_hash)
300 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
306 /* A little strange - this function will be called while holding
307 * dlm_domain_lock and is expected to be holding it on the way out. We
308 * will however drop and reacquire it multiple times */
309 static void dlm_ctxt_release(struct kref *kref)
311 struct dlm_ctxt *dlm;
313 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
315 BUG_ON(dlm->num_joins);
316 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
318 /* we may still be in the list if we hit an error during join. */
319 list_del_init(&dlm->list);
321 spin_unlock(&dlm_domain_lock);
323 mlog(0, "freeing memory from domain %s\n", dlm->name);
325 wake_up(&dlm_domain_events);
327 dlm_free_ctxt_mem(dlm);
329 spin_lock(&dlm_domain_lock);
332 void dlm_put(struct dlm_ctxt *dlm)
334 spin_lock(&dlm_domain_lock);
335 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
336 spin_unlock(&dlm_domain_lock);
339 static void __dlm_get(struct dlm_ctxt *dlm)
341 kref_get(&dlm->dlm_refs);
344 /* given a questionable reference to a dlm object, gets a reference if
345 * it can find it in the list, otherwise returns NULL in which case
346 * you shouldn't trust your pointer. */
347 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
349 struct dlm_ctxt *target;
350 struct dlm_ctxt *ret = NULL;
352 spin_lock(&dlm_domain_lock);
354 list_for_each_entry(target, &dlm_domains, list) {
362 spin_unlock(&dlm_domain_lock);
367 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
371 spin_lock(&dlm_domain_lock);
372 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
373 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
374 spin_unlock(&dlm_domain_lock);
379 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
381 if (dlm->dlm_worker) {
382 destroy_workqueue(dlm->dlm_worker);
383 dlm->dlm_worker = NULL;
387 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
389 dlm_unregister_domain_handlers(dlm);
390 dlm_complete_thread(dlm);
391 dlm_complete_recovery_thread(dlm);
392 dlm_destroy_dlm_worker(dlm);
394 /* We've left the domain. Now we can take ourselves out of the
395 * list and allow the kref stuff to help us free the
397 spin_lock(&dlm_domain_lock);
398 list_del_init(&dlm->list);
399 spin_unlock(&dlm_domain_lock);
401 /* Wake up anyone waiting for us to remove this domain */
402 wake_up(&dlm_domain_events);
405 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
407 int i, num, n, ret = 0;
408 struct dlm_lock_resource *res;
409 struct hlist_node *iter;
410 struct hlist_head *bucket;
413 mlog(0, "Migrating locks from domain %s\n", dlm->name);
416 spin_lock(&dlm->spinlock);
417 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
420 bucket = dlm_lockres_hash(dlm, i);
421 iter = bucket->first;
424 res = hlist_entry(iter, struct dlm_lock_resource,
426 dlm_lockres_get(res);
427 /* migrate, if necessary. this will drop the dlm
428 * spinlock and retake it if it does migration. */
429 dropped = dlm_empty_lockres(dlm, res);
431 spin_lock(&res->spinlock);
433 __dlm_lockres_calc_usage(dlm, res);
435 iter = res->hash_node.next;
436 spin_unlock(&res->spinlock);
438 dlm_lockres_put(res);
441 cond_resched_lock(&dlm->spinlock);
445 cond_resched_lock(&dlm->spinlock);
450 if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
451 mlog(0, "%s: perhaps there are more lock resources "
452 "need to be migrated after dlm recovery\n", dlm->name);
455 mlog(0, "%s: we won't do dlm recovery after migrating "
456 "all lock resources\n", dlm->name);
457 dlm->migrate_done = 1;
461 spin_unlock(&dlm->spinlock);
462 wake_up(&dlm->dlm_thread_wq);
464 /* let the dlm thread take care of purging, keep scanning until
465 * nothing remains in the hash */
467 mlog(0, "%s: %d lock resources in hash last pass\n",
471 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
475 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
479 spin_lock(&dlm->spinlock);
480 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
481 spin_unlock(&dlm->spinlock);
486 static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
487 void *data, void **ret_data)
489 struct dlm_ctxt *dlm = data;
491 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
496 node = exit_msg->node_idx;
497 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
499 spin_lock(&dlm->spinlock);
500 set_bit(node, dlm->exit_domain_map);
501 spin_unlock(&dlm->spinlock);
508 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
510 /* Yikes, a double spinlock! I need domain_lock for the dlm
511 * state and the dlm spinlock for join state... Sorry! */
513 spin_lock(&dlm_domain_lock);
514 spin_lock(&dlm->spinlock);
516 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
517 mlog(0, "Node %d is joining, we wait on it.\n",
519 spin_unlock(&dlm->spinlock);
520 spin_unlock(&dlm_domain_lock);
522 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
526 dlm->dlm_state = DLM_CTXT_LEAVING;
527 spin_unlock(&dlm->spinlock);
528 spin_unlock(&dlm_domain_lock);
531 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
533 int node = -1, num = 0;
535 assert_spin_locked(&dlm->spinlock);
538 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
539 node + 1)) < O2NM_MAX_NODES) {
543 printk(") %u nodes\n", num);
546 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
549 struct dlm_ctxt *dlm = data;
551 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
553 mlog(0, "%p %u %p", msg, len, data);
558 node = exit_msg->node_idx;
560 spin_lock(&dlm->spinlock);
561 clear_bit(node, dlm->domain_map);
562 clear_bit(node, dlm->exit_domain_map);
563 printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
564 __dlm_print_nodes(dlm);
566 /* notify anything attached to the heartbeat events */
567 dlm_hb_event_notify_attached(dlm, node, 0);
569 spin_unlock(&dlm->spinlock);
576 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
580 struct dlm_exit_domain leave_msg;
582 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
585 memset(&leave_msg, 0, sizeof(leave_msg));
586 leave_msg.node_idx = dlm->node_num;
588 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
589 sizeof(leave_msg), node, NULL);
591 mlog(ML_ERROR, "Error %d sending domain exit message %u "
592 "to node %u on domain %s\n", status, msg_type, node,
598 static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
602 /* Support for begin exit domain was added in 1.2 */
603 if (dlm->dlm_locking_proto.pv_major == 1 &&
604 dlm->dlm_locking_proto.pv_minor < 2)
608 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
609 * informational. Meaning if a node does not receive the message,
612 spin_lock(&dlm->spinlock);
614 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
615 if (node >= O2NM_MAX_NODES)
617 if (node == dlm->node_num)
620 spin_unlock(&dlm->spinlock);
621 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
622 spin_lock(&dlm->spinlock);
624 spin_unlock(&dlm->spinlock);
627 static void dlm_leave_domain(struct dlm_ctxt *dlm)
629 int node, clear_node, status;
631 /* At this point we've migrated away all our locks and won't
632 * accept mastership of new ones. The dlm is responsible for
633 * almost nothing now. We make sure not to confuse any joining
634 * nodes and then commence shutdown procedure. */
636 spin_lock(&dlm->spinlock);
637 /* Clear ourselves from the domain map */
638 clear_bit(dlm->node_num, dlm->domain_map);
639 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
640 0)) < O2NM_MAX_NODES) {
641 /* Drop the dlm spinlock. This is safe wrt the domain_map.
642 * -nodes cannot be added now as the
643 * query_join_handlers knows to respond with OK_NO_MAP
644 * -we catch the right network errors if a node is
645 * removed from the map while we're sending him the
647 spin_unlock(&dlm->spinlock);
651 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
654 status != -ENOPROTOOPT &&
655 status != -ENOTCONN) {
656 mlog(ML_NOTICE, "Error %d sending domain exit message "
657 "to node %d\n", status, node);
659 /* Not sure what to do here but lets sleep for
660 * a bit in case this was a transient
662 msleep(DLM_DOMAIN_BACKOFF_MS);
666 spin_lock(&dlm->spinlock);
667 /* If we're not clearing the node bit then we intend
668 * to loop back around to try again. */
670 clear_bit(node, dlm->domain_map);
672 spin_unlock(&dlm->spinlock);
675 void dlm_unregister_domain(struct dlm_ctxt *dlm)
678 struct dlm_lock_resource *res;
680 spin_lock(&dlm_domain_lock);
681 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
682 BUG_ON(!dlm->num_joins);
685 if (!dlm->num_joins) {
686 /* We mark it "in shutdown" now so new register
687 * requests wait until we've completely left the
688 * domain. Don't use DLM_CTXT_LEAVING yet as we still
689 * want new domain joins to communicate with us at
690 * least until we've completed migration of our
692 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
695 spin_unlock(&dlm_domain_lock);
698 mlog(0, "shutting down domain %s\n", dlm->name);
699 dlm_begin_exit_domain(dlm);
701 /* We changed dlm state, notify the thread */
702 dlm_kick_thread(dlm, NULL);
704 while (dlm_migrate_all_locks(dlm)) {
705 /* Give dlm_thread time to purge the lockres' */
707 mlog(0, "%s: more migration to do\n", dlm->name);
710 /* This list should be empty. If not, print remaining lockres */
711 if (!list_empty(&dlm->tracking_list)) {
712 mlog(ML_ERROR, "Following lockres' are still on the "
714 list_for_each_entry(res, &dlm->tracking_list, tracking)
715 dlm_print_one_lock_resource(res);
718 dlm_mark_domain_leaving(dlm);
719 dlm_leave_domain(dlm);
720 printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
721 dlm_force_free_mles(dlm);
722 dlm_complete_dlm_shutdown(dlm);
726 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
728 static int dlm_query_join_proto_check(char *proto_type, int node,
729 struct dlm_protocol_version *ours,
730 struct dlm_protocol_version *request)
733 struct dlm_protocol_version proto = *request;
735 if (!dlm_protocol_compare(ours, &proto)) {
737 "node %u wanted to join with %s locking protocol "
738 "%u.%u, we respond with %u.%u\n",
742 proto.pv_major, proto.pv_minor);
743 request->pv_minor = proto.pv_minor;
747 "Node %u wanted to join with %s locking "
748 "protocol %u.%u, but we have %u.%u, disallowing\n",
761 * struct dlm_query_join_packet is made up of four one-byte fields. They
762 * are effectively in big-endian order already. However, little-endian
763 * machines swap them before putting the packet on the wire (because
764 * query_join's response is a status, and that status is treated as a u32
765 * on the wire). Thus, a big-endian and little-endian machines will treat
766 * this structure differently.
768 * The solution is to have little-endian machines swap the structure when
769 * converting from the structure to the u32 representation. This will
770 * result in the structure having the correct format on the wire no matter
771 * the host endian format.
773 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
776 union dlm_query_join_response response;
778 response.packet = *packet;
779 *wire = be32_to_cpu(response.intval);
782 static void dlm_query_join_wire_to_packet(u32 wire,
783 struct dlm_query_join_packet *packet)
785 union dlm_query_join_response response;
787 response.intval = cpu_to_be32(wire);
788 *packet = response.packet;
791 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
794 struct dlm_query_join_request *query;
795 struct dlm_query_join_packet packet = {
796 .code = JOIN_DISALLOW,
798 struct dlm_ctxt *dlm = NULL;
802 query = (struct dlm_query_join_request *) msg->buf;
804 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
808 * If heartbeat doesn't consider the node live, tell it
809 * to back off and try again. This gives heartbeat a chance
812 if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
813 mlog(0, "node %u is not in our live map yet\n",
816 packet.code = JOIN_DISALLOW;
820 packet.code = JOIN_OK_NO_MAP;
822 spin_lock(&dlm_domain_lock);
823 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
828 * There is a small window where the joining node may not see the
829 * node(s) that just left but still part of the cluster. DISALLOW
830 * join request if joining node has different node map.
833 while (nodenum < O2NM_MAX_NODES) {
834 if (test_bit(nodenum, dlm->domain_map)) {
835 if (!byte_test_bit(nodenum, query->node_map)) {
836 mlog(0, "disallow join as node %u does not "
837 "have node %u in its nodemap\n",
838 query->node_idx, nodenum);
839 packet.code = JOIN_DISALLOW;
846 /* Once the dlm ctxt is marked as leaving then we don't want
847 * to be put in someone's domain map.
848 * Also, explicitly disallow joining at certain troublesome
849 * times (ie. during recovery). */
850 if (dlm->dlm_state != DLM_CTXT_LEAVING) {
851 int bit = query->node_idx;
852 spin_lock(&dlm->spinlock);
854 if (dlm->dlm_state == DLM_CTXT_NEW &&
855 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
856 /*If this is a brand new context and we
857 * haven't started our join process yet, then
858 * the other node won the race. */
859 packet.code = JOIN_OK_NO_MAP;
860 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
861 /* Disallow parallel joins. */
862 packet.code = JOIN_DISALLOW;
863 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
864 mlog(0, "node %u trying to join, but recovery "
865 "is ongoing.\n", bit);
866 packet.code = JOIN_DISALLOW;
867 } else if (test_bit(bit, dlm->recovery_map)) {
868 mlog(0, "node %u trying to join, but it "
869 "still needs recovery.\n", bit);
870 packet.code = JOIN_DISALLOW;
871 } else if (test_bit(bit, dlm->domain_map)) {
872 mlog(0, "node %u trying to join, but it "
873 "is still in the domain! needs recovery?\n",
875 packet.code = JOIN_DISALLOW;
877 /* Alright we're fully a part of this domain
878 * so we keep some state as to who's joining
879 * and indicate to him that needs to be fixed
882 /* Make sure we speak compatible locking protocols. */
883 if (dlm_query_join_proto_check("DLM", bit,
884 &dlm->dlm_locking_proto,
885 &query->dlm_proto)) {
886 packet.code = JOIN_PROTOCOL_MISMATCH;
887 } else if (dlm_query_join_proto_check("fs", bit,
888 &dlm->fs_locking_proto,
890 packet.code = JOIN_PROTOCOL_MISMATCH;
892 packet.dlm_minor = query->dlm_proto.pv_minor;
893 packet.fs_minor = query->fs_proto.pv_minor;
894 packet.code = JOIN_OK;
895 __dlm_set_joining_node(dlm, query->node_idx);
899 spin_unlock(&dlm->spinlock);
902 spin_unlock(&dlm_domain_lock);
905 mlog(0, "We respond with %u\n", packet.code);
907 dlm_query_join_packet_to_wire(&packet, &response);
911 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
914 struct dlm_assert_joined *assert;
915 struct dlm_ctxt *dlm = NULL;
917 assert = (struct dlm_assert_joined *) msg->buf;
919 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
922 spin_lock(&dlm_domain_lock);
923 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
924 /* XXX should we consider no dlm ctxt an error? */
926 spin_lock(&dlm->spinlock);
928 /* Alright, this node has officially joined our
929 * domain. Set him in the map and clean up our
930 * leftover join state. */
931 BUG_ON(dlm->joining_node != assert->node_idx);
933 if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
934 mlog(0, "dlm recovery is ongoing, disallow join\n");
935 spin_unlock(&dlm->spinlock);
936 spin_unlock(&dlm_domain_lock);
940 set_bit(assert->node_idx, dlm->domain_map);
941 clear_bit(assert->node_idx, dlm->exit_domain_map);
942 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
944 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
945 assert->node_idx, dlm->name);
946 __dlm_print_nodes(dlm);
948 /* notify anything attached to the heartbeat events */
949 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
951 spin_unlock(&dlm->spinlock);
953 spin_unlock(&dlm_domain_lock);
958 static int dlm_match_regions(struct dlm_ctxt *dlm,
959 struct dlm_query_region *qr,
960 char *local, int locallen)
962 char *remote = qr->qr_regions;
964 int localnr, i, j, foundit;
967 if (!o2hb_global_heartbeat_active()) {
968 if (qr->qr_numregions) {
969 mlog(ML_ERROR, "Domain %s: Joining node %d has global "
970 "heartbeat enabled but local node %d does not\n",
971 qr->qr_domain, qr->qr_node, dlm->node_num);
977 if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
978 mlog(ML_ERROR, "Domain %s: Local node %d has global "
979 "heartbeat enabled but joining node %d does not\n",
980 qr->qr_domain, dlm->node_num, qr->qr_node);
986 for (i = 0; i < qr->qr_numregions; ++i) {
987 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
988 r += O2HB_MAX_REGION_NAME_LEN;
991 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
992 localnr = o2hb_get_all_regions(local, (u8)localnr);
994 /* compare local regions with remote */
996 for (i = 0; i < localnr; ++i) {
999 for (j = 0; j <= qr->qr_numregions; ++j) {
1000 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1004 r += O2HB_MAX_REGION_NAME_LEN;
1008 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1009 "in local node %d but not in joining node %d\n",
1010 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1011 dlm->node_num, qr->qr_node);
1014 l += O2HB_MAX_REGION_NAME_LEN;
1017 /* compare remote with local regions */
1019 for (i = 0; i < qr->qr_numregions; ++i) {
1022 for (j = 0; j < localnr; ++j) {
1023 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1027 l += O2HB_MAX_REGION_NAME_LEN;
1031 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1032 "in joining node %d but not in local node %d\n",
1033 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1034 qr->qr_node, dlm->node_num);
1037 r += O2HB_MAX_REGION_NAME_LEN;
1044 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1046 struct dlm_query_region *qr = NULL;
1047 int status, ret = 0, i;
1050 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1053 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1060 qr->qr_node = dlm->node_num;
1061 qr->qr_namelen = strlen(dlm->name);
1062 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1063 /* if local hb, the numregions will be zero */
1064 if (o2hb_global_heartbeat_active())
1065 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1069 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1070 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1073 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1074 i + 1)) < O2NM_MAX_NODES) {
1075 if (i == dlm->node_num)
1078 mlog(0, "Sending regions to node %d\n", i);
1080 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1081 sizeof(struct dlm_query_region),
1086 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1097 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1098 void *data, void **ret_data)
1100 struct dlm_query_region *qr;
1101 struct dlm_ctxt *dlm = NULL;
1105 qr = (struct dlm_query_region *) msg->buf;
1107 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1110 /* buffer used in dlm_mast_regions() */
1111 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1117 spin_lock(&dlm_domain_lock);
1118 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1120 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1121 "before join domain\n", qr->qr_node, qr->qr_domain);
1122 goto out_domain_lock;
1125 spin_lock(&dlm->spinlock);
1126 if (dlm->joining_node != qr->qr_node) {
1127 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1128 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1133 /* Support for global heartbeat was added in 1.1 */
1134 if (dlm->dlm_locking_proto.pv_major == 1 &&
1135 dlm->dlm_locking_proto.pv_minor == 0) {
1136 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1137 "but active dlm protocol is %d.%d\n", qr->qr_node,
1138 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1139 dlm->dlm_locking_proto.pv_minor);
1143 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1146 spin_unlock(&dlm->spinlock);
1149 spin_unlock(&dlm_domain_lock);
1156 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1158 struct o2nm_node *local;
1159 struct dlm_node_info *remote;
1163 for (j = 0; j < qn->qn_numnodes; ++j)
1164 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1165 &(qn->qn_nodes[j].ni_ipv4_address),
1166 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1168 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1169 local = o2nm_get_node_by_num(i);
1171 for (j = 0; j < qn->qn_numnodes; ++j) {
1172 if (qn->qn_nodes[j].ni_nodenum == i) {
1173 remote = &(qn->qn_nodes[j]);
1178 if (!local && !remote)
1181 if ((local && !remote) || (!local && remote))
1185 ((remote->ni_nodenum != local->nd_num) ||
1186 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1187 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1191 if (remote && !local)
1192 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1193 "registered in joining node %d but not in "
1194 "local node %d\n", qn->qn_domain,
1196 &(remote->ni_ipv4_address),
1197 ntohs(remote->ni_ipv4_port),
1198 qn->qn_nodenum, dlm->node_num);
1199 if (local && !remote)
1200 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1201 "registered in local node %d but not in "
1202 "joining node %d\n", qn->qn_domain,
1203 local->nd_num, &(local->nd_ipv4_address),
1204 ntohs(local->nd_ipv4_port),
1205 dlm->node_num, qn->qn_nodenum);
1206 BUG_ON((!local && !remote));
1210 o2nm_node_put(local);
1216 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1218 struct dlm_query_nodeinfo *qn = NULL;
1219 struct o2nm_node *node;
1220 int ret = 0, status, count, i;
1222 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1225 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1232 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1233 node = o2nm_get_node_by_num(i);
1236 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1237 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1238 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1239 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1240 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1242 o2nm_node_put(node);
1245 qn->qn_nodenum = dlm->node_num;
1246 qn->qn_numnodes = count;
1247 qn->qn_namelen = strlen(dlm->name);
1248 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1251 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1252 i + 1)) < O2NM_MAX_NODES) {
1253 if (i == dlm->node_num)
1256 mlog(0, "Sending nodeinfo to node %d\n", i);
1258 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1259 qn, sizeof(struct dlm_query_nodeinfo),
1264 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1274 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1275 void *data, void **ret_data)
1277 struct dlm_query_nodeinfo *qn;
1278 struct dlm_ctxt *dlm = NULL;
1279 int locked = 0, status = -EINVAL;
1281 qn = (struct dlm_query_nodeinfo *) msg->buf;
1283 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1286 spin_lock(&dlm_domain_lock);
1287 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1289 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1290 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1294 spin_lock(&dlm->spinlock);
1296 if (dlm->joining_node != qn->qn_nodenum) {
1297 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1298 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1303 /* Support for node query was added in 1.1 */
1304 if (dlm->dlm_locking_proto.pv_major == 1 &&
1305 dlm->dlm_locking_proto.pv_minor == 0) {
1306 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1307 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1308 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1309 dlm->dlm_locking_proto.pv_minor);
1313 status = dlm_match_nodes(dlm, qn);
1317 spin_unlock(&dlm->spinlock);
1318 spin_unlock(&dlm_domain_lock);
1323 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1326 struct dlm_cancel_join *cancel;
1327 struct dlm_ctxt *dlm = NULL;
1329 cancel = (struct dlm_cancel_join *) msg->buf;
1331 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1334 spin_lock(&dlm_domain_lock);
1335 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1338 spin_lock(&dlm->spinlock);
1340 /* Yikes, this guy wants to cancel his join. No
1341 * problem, we simply cleanup our join state. */
1342 BUG_ON(dlm->joining_node != cancel->node_idx);
1343 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1345 spin_unlock(&dlm->spinlock);
1347 spin_unlock(&dlm_domain_lock);
1352 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1356 struct dlm_cancel_join cancel_msg;
1358 memset(&cancel_msg, 0, sizeof(cancel_msg));
1359 cancel_msg.node_idx = dlm->node_num;
1360 cancel_msg.name_len = strlen(dlm->name);
1361 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1363 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1364 &cancel_msg, sizeof(cancel_msg), node,
1367 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1368 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1377 /* map_size should be in bytes. */
1378 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1379 unsigned long *node_map,
1380 unsigned int map_size)
1382 int status, tmpstat;
1385 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1386 sizeof(unsigned long))) {
1388 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1389 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1395 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1396 node + 1)) < O2NM_MAX_NODES) {
1397 if (node == dlm->node_num)
1400 tmpstat = dlm_send_one_join_cancel(dlm, node);
1402 mlog(ML_ERROR, "Error return %d cancelling join on "
1403 "node %d\n", tmpstat, node);
1414 static int dlm_request_join(struct dlm_ctxt *dlm,
1416 enum dlm_query_join_response_code *response)
1419 struct dlm_query_join_request join_msg;
1420 struct dlm_query_join_packet packet;
1423 mlog(0, "querying node %d\n", node);
1425 memset(&join_msg, 0, sizeof(join_msg));
1426 join_msg.node_idx = dlm->node_num;
1427 join_msg.name_len = strlen(dlm->name);
1428 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1429 join_msg.dlm_proto = dlm->dlm_locking_proto;
1430 join_msg.fs_proto = dlm->fs_locking_proto;
1432 /* copy live node map to join message */
1433 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1435 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1436 sizeof(join_msg), node, &join_resp);
1437 if (status < 0 && status != -ENOPROTOOPT) {
1438 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1439 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1443 dlm_query_join_wire_to_packet(join_resp, &packet);
1445 /* -ENOPROTOOPT from the net code means the other side isn't
1446 listening for our message type -- that's fine, it means
1447 his dlm isn't up, so we can consider him a 'yes' but not
1448 joined into the domain. */
1449 if (status == -ENOPROTOOPT) {
1451 *response = JOIN_OK_NO_MAP;
1453 *response = packet.code;
1454 switch (packet.code) {
1456 case JOIN_OK_NO_MAP:
1458 case JOIN_PROTOCOL_MISMATCH:
1460 "This node requested DLM locking protocol %u.%u and "
1461 "filesystem locking protocol %u.%u. At least one of "
1462 "the protocol versions on node %d is not compatible, "
1464 dlm->dlm_locking_proto.pv_major,
1465 dlm->dlm_locking_proto.pv_minor,
1466 dlm->fs_locking_proto.pv_major,
1467 dlm->fs_locking_proto.pv_minor,
1472 /* Use the same locking protocol as the remote node */
1473 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1474 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1476 "Node %d responds JOIN_OK with DLM locking protocol "
1477 "%u.%u and fs locking protocol %u.%u\n",
1479 dlm->dlm_locking_proto.pv_major,
1480 dlm->dlm_locking_proto.pv_minor,
1481 dlm->fs_locking_proto.pv_major,
1482 dlm->fs_locking_proto.pv_minor);
1486 mlog(ML_ERROR, "invalid response %d from node %u\n",
1488 /* Reset response to JOIN_DISALLOW */
1489 *response = JOIN_DISALLOW;
1494 mlog(0, "status %d, node %d response is %d\n", status, node,
1501 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1506 struct dlm_assert_joined assert_msg;
1508 mlog(0, "Sending join assert to node %u\n", node);
1510 memset(&assert_msg, 0, sizeof(assert_msg));
1511 assert_msg.node_idx = dlm->node_num;
1512 assert_msg.name_len = strlen(dlm->name);
1513 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1515 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1516 &assert_msg, sizeof(assert_msg), node,
1519 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1520 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1528 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1529 unsigned long *node_map)
1531 int status, node, live;
1535 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1536 node + 1)) < O2NM_MAX_NODES) {
1537 if (node == dlm->node_num)
1541 /* It is very important that this message be
1542 * received so we spin until either the node
1543 * has died or it gets the message. */
1544 status = dlm_send_one_join_assert(dlm, node);
1546 spin_lock(&dlm->spinlock);
1547 live = test_bit(node, dlm->live_nodes_map);
1548 spin_unlock(&dlm->spinlock);
1551 mlog(ML_ERROR, "Error return %d asserting "
1552 "join on node %d\n", status, node);
1554 /* give us some time between errors... */
1556 msleep(DLM_DOMAIN_BACKOFF_MS);
1558 } while (status && live);
1562 struct domain_join_ctxt {
1563 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1564 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1567 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1568 struct domain_join_ctxt *ctxt,
1569 enum dlm_query_join_response_code response)
1573 if (response == JOIN_DISALLOW) {
1574 mlog(0, "Latest response of disallow -- should restart\n");
1578 spin_lock(&dlm->spinlock);
1579 /* For now, we restart the process if the node maps have
1581 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1582 sizeof(dlm->live_nodes_map));
1583 spin_unlock(&dlm->spinlock);
1586 mlog(0, "Node maps changed -- should restart\n");
1591 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1593 int status = 0, tmpstat, node;
1594 struct domain_join_ctxt *ctxt;
1595 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1599 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1606 /* group sem locking should work for us here -- we're already
1607 * registered for heartbeat events so filling this should be
1608 * atomic wrt getting those handlers called. */
1609 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1611 spin_lock(&dlm->spinlock);
1612 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1614 __dlm_set_joining_node(dlm, dlm->node_num);
1616 spin_unlock(&dlm->spinlock);
1619 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1620 node + 1)) < O2NM_MAX_NODES) {
1621 if (node == dlm->node_num)
1624 status = dlm_request_join(dlm, node, &response);
1630 /* Ok, either we got a response or the node doesn't have a
1632 if (response == JOIN_OK)
1633 set_bit(node, ctxt->yes_resp_map);
1635 if (dlm_should_restart_join(dlm, ctxt, response)) {
1641 mlog(0, "Yay, done querying nodes!\n");
1643 /* Yay, everyone agree's we can join the domain. My domain is
1644 * comprised of all nodes who were put in the
1645 * yes_resp_map. Copy that into our domain map and send a join
1646 * assert message to clean up everyone elses state. */
1647 spin_lock(&dlm->spinlock);
1648 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1649 sizeof(ctxt->yes_resp_map));
1650 set_bit(dlm->node_num, dlm->domain_map);
1651 spin_unlock(&dlm->spinlock);
1653 /* Support for global heartbeat and node info was added in 1.1 */
1654 if (dlm->dlm_locking_proto.pv_major > 1 ||
1655 dlm->dlm_locking_proto.pv_minor > 0) {
1656 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1661 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1668 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1670 /* Joined state *must* be set before the joining node
1671 * information, otherwise the query_join handler may read no
1672 * current joiner but a state of NEW and tell joining nodes
1673 * we're not in the domain. */
1674 spin_lock(&dlm_domain_lock);
1675 dlm->dlm_state = DLM_CTXT_JOINED;
1677 spin_unlock(&dlm_domain_lock);
1680 spin_lock(&dlm->spinlock);
1681 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1683 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1684 __dlm_print_nodes(dlm);
1686 spin_unlock(&dlm->spinlock);
1689 /* Do we need to send a cancel message to any nodes? */
1691 tmpstat = dlm_send_join_cancels(dlm,
1693 sizeof(ctxt->yes_resp_map));
1695 mlog_errno(tmpstat);
1700 mlog(0, "returning %d\n", status);
1704 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1706 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1707 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1708 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1711 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1715 mlog(0, "registering handlers.\n");
1717 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1718 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1719 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1720 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1722 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1726 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1730 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1731 sizeof(struct dlm_master_request),
1732 dlm_master_request_handler,
1733 dlm, NULL, &dlm->dlm_domain_handlers);
1737 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1738 sizeof(struct dlm_assert_master),
1739 dlm_assert_master_handler,
1740 dlm, dlm_assert_master_post_handler,
1741 &dlm->dlm_domain_handlers);
1745 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1746 sizeof(struct dlm_create_lock),
1747 dlm_create_lock_handler,
1748 dlm, NULL, &dlm->dlm_domain_handlers);
1752 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1753 DLM_CONVERT_LOCK_MAX_LEN,
1754 dlm_convert_lock_handler,
1755 dlm, NULL, &dlm->dlm_domain_handlers);
1759 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1760 DLM_UNLOCK_LOCK_MAX_LEN,
1761 dlm_unlock_lock_handler,
1762 dlm, NULL, &dlm->dlm_domain_handlers);
1766 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1767 DLM_PROXY_AST_MAX_LEN,
1768 dlm_proxy_ast_handler,
1769 dlm, NULL, &dlm->dlm_domain_handlers);
1773 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1774 sizeof(struct dlm_exit_domain),
1775 dlm_exit_domain_handler,
1776 dlm, NULL, &dlm->dlm_domain_handlers);
1780 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1781 sizeof(struct dlm_deref_lockres),
1782 dlm_deref_lockres_handler,
1783 dlm, NULL, &dlm->dlm_domain_handlers);
1787 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1788 sizeof(struct dlm_migrate_request),
1789 dlm_migrate_request_handler,
1790 dlm, NULL, &dlm->dlm_domain_handlers);
1794 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1795 DLM_MIG_LOCKRES_MAX_LEN,
1796 dlm_mig_lockres_handler,
1797 dlm, NULL, &dlm->dlm_domain_handlers);
1801 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1802 sizeof(struct dlm_master_requery),
1803 dlm_master_requery_handler,
1804 dlm, NULL, &dlm->dlm_domain_handlers);
1808 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1809 sizeof(struct dlm_lock_request),
1810 dlm_request_all_locks_handler,
1811 dlm, NULL, &dlm->dlm_domain_handlers);
1815 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1816 sizeof(struct dlm_reco_data_done),
1817 dlm_reco_data_done_handler,
1818 dlm, NULL, &dlm->dlm_domain_handlers);
1822 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1823 sizeof(struct dlm_begin_reco),
1824 dlm_begin_reco_handler,
1825 dlm, NULL, &dlm->dlm_domain_handlers);
1829 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1830 sizeof(struct dlm_finalize_reco),
1831 dlm_finalize_reco_handler,
1832 dlm, NULL, &dlm->dlm_domain_handlers);
1836 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1837 sizeof(struct dlm_exit_domain),
1838 dlm_begin_exit_domain_handler,
1839 dlm, NULL, &dlm->dlm_domain_handlers);
1843 status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
1844 sizeof(struct dlm_deref_lockres_done),
1845 dlm_deref_lockres_done_handler,
1846 dlm, NULL, &dlm->dlm_domain_handlers);
1849 dlm_unregister_domain_handlers(dlm);
1854 static int dlm_join_domain(struct dlm_ctxt *dlm)
1857 unsigned int backoff;
1858 unsigned int total_backoff = 0;
1859 char wq_name[O2NM_MAX_NAME_LEN];
1863 mlog(0, "Join domain %s\n", dlm->name);
1865 status = dlm_register_domain_handlers(dlm);
1871 status = dlm_launch_thread(dlm);
1877 status = dlm_launch_recovery_thread(dlm);
1883 dlm_debug_init(dlm);
1885 snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
1886 dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
1887 if (!dlm->dlm_worker) {
1894 status = dlm_try_to_join_domain(dlm);
1896 /* If we're racing another node to the join, then we
1897 * need to back off temporarily and let them
1899 #define DLM_JOIN_TIMEOUT_MSECS 90000
1900 if (status == -EAGAIN) {
1901 if (signal_pending(current)) {
1902 status = -ERESTARTSYS;
1906 if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
1907 status = -ERESTARTSYS;
1908 mlog(ML_NOTICE, "Timed out joining dlm domain "
1909 "%s after %u msecs\n", dlm->name,
1916 * <dale> No, after you!
1918 * <dale> But you first!
1921 backoff = (unsigned int)(jiffies & 0x3);
1922 backoff *= DLM_DOMAIN_BACKOFF_MS;
1923 total_backoff += backoff;
1924 mlog(0, "backoff %d\n", backoff);
1927 } while (status == -EAGAIN);
1936 wake_up(&dlm_domain_events);
1939 dlm_unregister_domain_handlers(dlm);
1940 dlm_complete_thread(dlm);
1941 dlm_complete_recovery_thread(dlm);
1942 dlm_destroy_dlm_worker(dlm);
1948 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1953 struct dlm_ctxt *dlm = NULL;
1955 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1962 dlm->name = kstrdup(domain, GFP_KERNEL);
1963 if (dlm->name == NULL) {
1969 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1970 if (!dlm->lockres_hash) {
1976 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1977 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1979 dlm->master_hash = (struct hlist_head **)
1980 dlm_alloc_pagevec(DLM_HASH_PAGES);
1981 if (!dlm->master_hash) {
1987 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1988 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1991 dlm->node_num = o2nm_this_node();
1993 dlm_create_debugfs_subroot(dlm);
1995 spin_lock_init(&dlm->spinlock);
1996 spin_lock_init(&dlm->master_lock);
1997 spin_lock_init(&dlm->ast_lock);
1998 spin_lock_init(&dlm->track_lock);
1999 INIT_LIST_HEAD(&dlm->list);
2000 INIT_LIST_HEAD(&dlm->dirty_list);
2001 INIT_LIST_HEAD(&dlm->reco.resources);
2002 INIT_LIST_HEAD(&dlm->reco.node_data);
2003 INIT_LIST_HEAD(&dlm->purge_list);
2004 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2005 INIT_LIST_HEAD(&dlm->tracking_list);
2006 dlm->reco.state = 0;
2008 INIT_LIST_HEAD(&dlm->pending_asts);
2009 INIT_LIST_HEAD(&dlm->pending_basts);
2011 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2012 dlm->recovery_map, &(dlm->recovery_map[0]));
2014 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2015 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2016 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2018 dlm->dlm_thread_task = NULL;
2019 dlm->dlm_reco_thread_task = NULL;
2020 dlm->dlm_worker = NULL;
2021 init_waitqueue_head(&dlm->dlm_thread_wq);
2022 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2023 init_waitqueue_head(&dlm->reco.event);
2024 init_waitqueue_head(&dlm->ast_wq);
2025 init_waitqueue_head(&dlm->migration_wq);
2026 INIT_LIST_HEAD(&dlm->mle_hb_events);
2028 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2029 init_waitqueue_head(&dlm->dlm_join_events);
2031 dlm->migrate_done = 0;
2033 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2034 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2036 atomic_set(&dlm->res_tot_count, 0);
2037 atomic_set(&dlm->res_cur_count, 0);
2038 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2039 atomic_set(&dlm->mle_tot_count[i], 0);
2040 atomic_set(&dlm->mle_cur_count[i], 0);
2043 spin_lock_init(&dlm->work_lock);
2044 INIT_LIST_HEAD(&dlm->work_list);
2045 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2047 kref_init(&dlm->dlm_refs);
2048 dlm->dlm_state = DLM_CTXT_NEW;
2050 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2052 mlog(0, "context init: refcount %u\n",
2053 kref_read(&dlm->dlm_refs));
2057 if (ret < 0 && dlm) {
2058 if (dlm->master_hash)
2059 dlm_free_pagevec((void **)dlm->master_hash,
2062 if (dlm->lockres_hash)
2063 dlm_free_pagevec((void **)dlm->lockres_hash,
2074 * Compare a requested locking protocol version against the current one.
2076 * If the major numbers are different, they are incompatible.
2077 * If the current minor is greater than the request, they are incompatible.
2078 * If the current minor is less than or equal to the request, they are
2079 * compatible, and the requester should run at the current minor version.
2081 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2082 struct dlm_protocol_version *request)
2084 if (existing->pv_major != request->pv_major)
2087 if (existing->pv_minor > request->pv_minor)
2090 if (existing->pv_minor < request->pv_minor)
2091 request->pv_minor = existing->pv_minor;
2097 * dlm_register_domain: one-time setup per "domain".
2099 * The filesystem passes in the requested locking version via proto.
2100 * If registration was successful, proto will contain the negotiated
2103 struct dlm_ctxt * dlm_register_domain(const char *domain,
2105 struct dlm_protocol_version *fs_proto)
2108 struct dlm_ctxt *dlm = NULL;
2109 struct dlm_ctxt *new_ctxt = NULL;
2111 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2112 ret = -ENAMETOOLONG;
2113 mlog(ML_ERROR, "domain name length too long\n");
2117 mlog(0, "register called for domain \"%s\"\n", domain);
2121 if (signal_pending(current)) {
2127 spin_lock(&dlm_domain_lock);
2129 dlm = __dlm_lookup_domain(domain);
2131 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2132 spin_unlock(&dlm_domain_lock);
2134 mlog(0, "This ctxt is not joined yet!\n");
2135 wait_event_interruptible(dlm_domain_events,
2136 dlm_wait_on_domain_helper(
2141 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2142 spin_unlock(&dlm_domain_lock);
2144 "Requested locking protocol version is not "
2145 "compatible with already registered domain "
2146 "\"%s\"\n", domain);
2154 spin_unlock(&dlm_domain_lock);
2162 spin_unlock(&dlm_domain_lock);
2164 new_ctxt = dlm_alloc_ctxt(domain, key);
2173 /* a little variable switch-a-roo here... */
2177 /* add the new domain */
2178 list_add_tail(&dlm->list, &dlm_domains);
2179 spin_unlock(&dlm_domain_lock);
2182 * Pass the locking protocol version into the join. If the join
2183 * succeeds, it will have the negotiated protocol set.
2185 dlm->dlm_locking_proto = dlm_protocol;
2186 dlm->fs_locking_proto = *fs_proto;
2188 ret = dlm_join_domain(dlm);
2195 /* Tell the caller what locking protocol we negotiated */
2196 *fs_proto = dlm->fs_locking_proto;
2201 dlm_free_ctxt_mem(new_ctxt);
2208 EXPORT_SYMBOL_GPL(dlm_register_domain);
2210 static LIST_HEAD(dlm_join_handlers);
2212 static void dlm_unregister_net_handlers(void)
2214 o2net_unregister_handler_list(&dlm_join_handlers);
2217 static int dlm_register_net_handlers(void)
2221 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2222 sizeof(struct dlm_query_join_request),
2223 dlm_query_join_handler,
2224 NULL, NULL, &dlm_join_handlers);
2228 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2229 sizeof(struct dlm_assert_joined),
2230 dlm_assert_joined_handler,
2231 NULL, NULL, &dlm_join_handlers);
2235 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2236 sizeof(struct dlm_cancel_join),
2237 dlm_cancel_join_handler,
2238 NULL, NULL, &dlm_join_handlers);
2242 status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2243 sizeof(struct dlm_query_region),
2244 dlm_query_region_handler,
2245 NULL, NULL, &dlm_join_handlers);
2250 status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2251 sizeof(struct dlm_query_nodeinfo),
2252 dlm_query_nodeinfo_handler,
2253 NULL, NULL, &dlm_join_handlers);
2256 dlm_unregister_net_handlers();
2261 /* Domain eviction callback handling.
2263 * The file system requires notification of node death *before* the
2264 * dlm completes it's recovery work, otherwise it may be able to
2265 * acquire locks on resources requiring recovery. Since the dlm can
2266 * evict a node from it's domain *before* heartbeat fires, a similar
2267 * mechanism is required. */
2269 /* Eviction is not expected to happen often, so a per-domain lock is
2270 * not necessary. Eviction callbacks are allowed to sleep for short
2271 * periods of time. */
2272 static DECLARE_RWSEM(dlm_callback_sem);
2274 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2277 struct dlm_eviction_cb *cb;
2279 down_read(&dlm_callback_sem);
2280 list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2281 cb->ec_func(node_num, cb->ec_data);
2283 up_read(&dlm_callback_sem);
2286 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2287 dlm_eviction_func *f,
2290 INIT_LIST_HEAD(&cb->ec_item);
2294 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2296 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2297 struct dlm_eviction_cb *cb)
2299 down_write(&dlm_callback_sem);
2300 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2301 up_write(&dlm_callback_sem);
2303 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2305 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2307 down_write(&dlm_callback_sem);
2308 list_del_init(&cb->ec_item);
2309 up_write(&dlm_callback_sem);
2311 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2313 static int __init dlm_init(void)
2317 status = dlm_init_mle_cache();
2319 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2323 status = dlm_init_master_caches();
2325 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2326 "o2dlm_lockname slabcaches\n");
2330 status = dlm_init_lock_cache();
2332 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2336 status = dlm_register_net_handlers();
2338 mlog(ML_ERROR, "Unable to register network handlers\n");
2342 dlm_create_debugfs_root();
2346 dlm_unregister_net_handlers();
2347 dlm_destroy_lock_cache();
2348 dlm_destroy_master_caches();
2349 dlm_destroy_mle_cache();
2353 static void __exit dlm_exit (void)
2355 dlm_destroy_debugfs_root();
2356 dlm_unregister_net_handlers();
2357 dlm_destroy_lock_cache();
2358 dlm_destroy_master_caches();
2359 dlm_destroy_mle_cache();
2362 MODULE_AUTHOR("Oracle");
2363 MODULE_LICENSE("GPL");
2364 MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2366 module_init(dlm_init);
2367 module_exit(dlm_exit);