1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * defines domain join / leave apis
8 * Copyright (C) 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/debugfs.h>
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
42 #include "dlmcommon.h"
43 #include "dlmdomain.h"
46 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
47 #include "cluster/masklog.h"
50 * ocfs2 node maps are array of long int, which limits to send them freely
51 * across the wire due to endianness issues. To workaround this, we convert
52 * long ints to byte arrays. Following 3 routines are helper functions to
53 * set/test/copy bits within those array of bytes
55 static inline void byte_set_bit(u8 nr, u8 map[])
57 map[nr >> 3] |= (1UL << (nr & 7));
60 static inline int byte_test_bit(u8 nr, u8 map[])
62 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
65 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
73 memset(dmap, 0, ((sz + 7) >> 3));
74 for (nn = 0 ; nn < sz; nn++)
75 if (test_bit(nn, smap))
76 byte_set_bit(nn, dmap);
79 static void dlm_free_pagevec(void **vec, int pages)
82 free_page((unsigned long)vec[pages]);
86 static void **dlm_alloc_pagevec(int pages)
88 void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
94 for (i = 0; i < pages; i++)
95 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
98 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
99 pages, (unsigned long)DLM_HASH_PAGES,
100 (unsigned long)DLM_BUCKETS_PER_PAGE);
103 dlm_free_pagevec(vec, i);
109 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
111 * struct dlm_ctxt->spinlock
112 * struct dlm_lock_resource->spinlock
113 * struct dlm_ctxt->master_lock
114 * struct dlm_ctxt->ast_lock
115 * dlm_master_list_entry->spinlock
120 DEFINE_SPINLOCK(dlm_domain_lock);
121 LIST_HEAD(dlm_domains);
122 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
125 * The supported protocol version for DLM communication. Running domains
126 * will have a negotiated version with the same major number and a minor
127 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
128 * be used to determine what a running domain is actually using.
130 * New in version 1.1:
131 * - Message DLM_QUERY_REGION added to support global heartbeat
132 * - Message DLM_QUERY_NODEINFO added to allow online node removes
133 * New in version 1.2:
134 * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
136 static const struct dlm_protocol_version dlm_protocol = {
141 #define DLM_DOMAIN_BACKOFF_MS 200
143 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
145 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
147 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
149 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
150 void *data, void **ret_data);
151 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
153 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
154 struct dlm_protocol_version *request);
156 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
158 void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
160 if (hlist_unhashed(&res->hash_node))
163 mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
165 hlist_del_init(&res->hash_node);
166 dlm_lockres_put(res);
169 void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
171 struct hlist_head *bucket;
174 assert_spin_locked(&dlm->spinlock);
177 bucket = dlm_lockres_hash(dlm, q->hash);
179 /* get a reference for our hashtable */
180 dlm_lockres_get(res);
182 hlist_add_head(&res->hash_node, bucket);
184 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
188 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
193 struct hlist_head *bucket;
194 struct dlm_lock_resource *res;
196 mlog(0, "%.*s\n", len, name);
198 assert_spin_locked(&dlm->spinlock);
200 bucket = dlm_lockres_hash(dlm, hash);
202 hlist_for_each_entry(res, bucket, hash_node) {
203 if (res->lockname.name[0] != name[0])
205 if (unlikely(res->lockname.len != len))
207 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
209 dlm_lockres_get(res);
215 /* intended to be called by functions which do not care about lock
216 * resources which are being purged (most net _handler functions).
217 * this will return NULL for any lock resource which is found but
218 * currently in the process of dropping its mastery reference.
219 * use __dlm_lookup_lockres_full when you need the lock resource
220 * regardless (e.g. dlm_get_lock_resource) */
221 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
226 struct dlm_lock_resource *res = NULL;
228 mlog(0, "%.*s\n", len, name);
230 assert_spin_locked(&dlm->spinlock);
232 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
234 spin_lock(&res->spinlock);
235 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
236 spin_unlock(&res->spinlock);
237 dlm_lockres_put(res);
240 spin_unlock(&res->spinlock);
246 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
250 struct dlm_lock_resource *res;
251 unsigned int hash = dlm_lockid_hash(name, len);
253 spin_lock(&dlm->spinlock);
254 res = __dlm_lookup_lockres(dlm, name, len, hash);
255 spin_unlock(&dlm->spinlock);
259 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
261 struct dlm_ctxt *tmp;
263 assert_spin_locked(&dlm_domain_lock);
265 /* tmp->name here is always NULL terminated,
266 * but domain may not be! */
267 list_for_each_entry(tmp, &dlm_domains, list) {
268 if (strlen(tmp->name) == len &&
269 memcmp(tmp->name, domain, len)==0)
276 /* For null terminated domain strings ONLY */
277 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
279 assert_spin_locked(&dlm_domain_lock);
281 return __dlm_lookup_domain_full(domain, strlen(domain));
285 /* returns true on one of two conditions:
286 * 1) the domain does not exist
287 * 2) the domain exists and it's state is "joined" */
288 static int dlm_wait_on_domain_helper(const char *domain)
291 struct dlm_ctxt *tmp = NULL;
293 spin_lock(&dlm_domain_lock);
295 tmp = __dlm_lookup_domain(domain);
298 else if (tmp->dlm_state == DLM_CTXT_JOINED)
301 spin_unlock(&dlm_domain_lock);
305 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
307 dlm_destroy_debugfs_subroot(dlm);
309 if (dlm->lockres_hash)
310 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
312 if (dlm->master_hash)
313 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
319 /* A little strange - this function will be called while holding
320 * dlm_domain_lock and is expected to be holding it on the way out. We
321 * will however drop and reacquire it multiple times */
322 static void dlm_ctxt_release(struct kref *kref)
324 struct dlm_ctxt *dlm;
326 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
328 BUG_ON(dlm->num_joins);
329 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
331 /* we may still be in the list if we hit an error during join. */
332 list_del_init(&dlm->list);
334 spin_unlock(&dlm_domain_lock);
336 mlog(0, "freeing memory from domain %s\n", dlm->name);
338 wake_up(&dlm_domain_events);
340 dlm_free_ctxt_mem(dlm);
342 spin_lock(&dlm_domain_lock);
345 void dlm_put(struct dlm_ctxt *dlm)
347 spin_lock(&dlm_domain_lock);
348 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
349 spin_unlock(&dlm_domain_lock);
352 static void __dlm_get(struct dlm_ctxt *dlm)
354 kref_get(&dlm->dlm_refs);
357 /* given a questionable reference to a dlm object, gets a reference if
358 * it can find it in the list, otherwise returns NULL in which case
359 * you shouldn't trust your pointer. */
360 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
362 struct dlm_ctxt *target;
363 struct dlm_ctxt *ret = NULL;
365 spin_lock(&dlm_domain_lock);
367 list_for_each_entry(target, &dlm_domains, list) {
375 spin_unlock(&dlm_domain_lock);
380 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
384 spin_lock(&dlm_domain_lock);
385 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
386 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
387 spin_unlock(&dlm_domain_lock);
392 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
394 if (dlm->dlm_worker) {
395 flush_workqueue(dlm->dlm_worker);
396 destroy_workqueue(dlm->dlm_worker);
397 dlm->dlm_worker = NULL;
401 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
403 dlm_unregister_domain_handlers(dlm);
404 dlm_debug_shutdown(dlm);
405 dlm_complete_thread(dlm);
406 dlm_complete_recovery_thread(dlm);
407 dlm_destroy_dlm_worker(dlm);
409 /* We've left the domain. Now we can take ourselves out of the
410 * list and allow the kref stuff to help us free the
412 spin_lock(&dlm_domain_lock);
413 list_del_init(&dlm->list);
414 spin_unlock(&dlm_domain_lock);
416 /* Wake up anyone waiting for us to remove this domain */
417 wake_up(&dlm_domain_events);
420 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
422 int i, num, n, ret = 0;
423 struct dlm_lock_resource *res;
424 struct hlist_node *iter;
425 struct hlist_head *bucket;
428 mlog(0, "Migrating locks from domain %s\n", dlm->name);
431 spin_lock(&dlm->spinlock);
432 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
435 bucket = dlm_lockres_hash(dlm, i);
436 iter = bucket->first;
439 res = hlist_entry(iter, struct dlm_lock_resource,
441 dlm_lockres_get(res);
442 /* migrate, if necessary. this will drop the dlm
443 * spinlock and retake it if it does migration. */
444 dropped = dlm_empty_lockres(dlm, res);
446 spin_lock(&res->spinlock);
448 __dlm_lockres_calc_usage(dlm, res);
450 iter = res->hash_node.next;
451 spin_unlock(&res->spinlock);
453 dlm_lockres_put(res);
456 cond_resched_lock(&dlm->spinlock);
460 cond_resched_lock(&dlm->spinlock);
463 spin_unlock(&dlm->spinlock);
464 wake_up(&dlm->dlm_thread_wq);
466 /* let the dlm thread take care of purging, keep scanning until
467 * nothing remains in the hash */
469 mlog(0, "%s: %d lock resources in hash last pass\n",
473 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
477 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
481 spin_lock(&dlm->spinlock);
482 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
483 spin_unlock(&dlm->spinlock);
488 static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
489 void *data, void **ret_data)
491 struct dlm_ctxt *dlm = data;
493 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
498 node = exit_msg->node_idx;
499 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
501 spin_lock(&dlm->spinlock);
502 set_bit(node, dlm->exit_domain_map);
503 spin_unlock(&dlm->spinlock);
510 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
512 /* Yikes, a double spinlock! I need domain_lock for the dlm
513 * state and the dlm spinlock for join state... Sorry! */
515 spin_lock(&dlm_domain_lock);
516 spin_lock(&dlm->spinlock);
518 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
519 mlog(0, "Node %d is joining, we wait on it.\n",
521 spin_unlock(&dlm->spinlock);
522 spin_unlock(&dlm_domain_lock);
524 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
528 dlm->dlm_state = DLM_CTXT_LEAVING;
529 spin_unlock(&dlm->spinlock);
530 spin_unlock(&dlm_domain_lock);
533 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
535 int node = -1, num = 0;
537 assert_spin_locked(&dlm->spinlock);
540 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
541 node + 1)) < O2NM_MAX_NODES) {
545 printk(") %u nodes\n", num);
548 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
551 struct dlm_ctxt *dlm = data;
553 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
555 mlog(0, "%p %u %p", msg, len, data);
560 node = exit_msg->node_idx;
562 spin_lock(&dlm->spinlock);
563 clear_bit(node, dlm->domain_map);
564 clear_bit(node, dlm->exit_domain_map);
565 printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
566 __dlm_print_nodes(dlm);
568 /* notify anything attached to the heartbeat events */
569 dlm_hb_event_notify_attached(dlm, node, 0);
571 spin_unlock(&dlm->spinlock);
578 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
582 struct dlm_exit_domain leave_msg;
584 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
587 memset(&leave_msg, 0, sizeof(leave_msg));
588 leave_msg.node_idx = dlm->node_num;
590 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
591 sizeof(leave_msg), node, NULL);
593 mlog(ML_ERROR, "Error %d sending domain exit message %u "
594 "to node %u on domain %s\n", status, msg_type, node,
600 static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
604 /* Support for begin exit domain was added in 1.2 */
605 if (dlm->dlm_locking_proto.pv_major == 1 &&
606 dlm->dlm_locking_proto.pv_minor < 2)
610 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
611 * informational. Meaning if a node does not receive the message,
614 spin_lock(&dlm->spinlock);
616 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
617 if (node >= O2NM_MAX_NODES)
619 if (node == dlm->node_num)
622 spin_unlock(&dlm->spinlock);
623 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
624 spin_lock(&dlm->spinlock);
626 spin_unlock(&dlm->spinlock);
629 static void dlm_leave_domain(struct dlm_ctxt *dlm)
631 int node, clear_node, status;
633 /* At this point we've migrated away all our locks and won't
634 * accept mastership of new ones. The dlm is responsible for
635 * almost nothing now. We make sure not to confuse any joining
636 * nodes and then commence shutdown procedure. */
638 spin_lock(&dlm->spinlock);
639 /* Clear ourselves from the domain map */
640 clear_bit(dlm->node_num, dlm->domain_map);
641 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
642 0)) < O2NM_MAX_NODES) {
643 /* Drop the dlm spinlock. This is safe wrt the domain_map.
644 * -nodes cannot be added now as the
645 * query_join_handlers knows to respond with OK_NO_MAP
646 * -we catch the right network errors if a node is
647 * removed from the map while we're sending him the
649 spin_unlock(&dlm->spinlock);
653 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
656 status != -ENOPROTOOPT &&
657 status != -ENOTCONN) {
658 mlog(ML_NOTICE, "Error %d sending domain exit message "
659 "to node %d\n", status, node);
661 /* Not sure what to do here but lets sleep for
662 * a bit in case this was a transient
664 msleep(DLM_DOMAIN_BACKOFF_MS);
668 spin_lock(&dlm->spinlock);
669 /* If we're not clearing the node bit then we intend
670 * to loop back around to try again. */
672 clear_bit(node, dlm->domain_map);
674 spin_unlock(&dlm->spinlock);
677 int dlm_joined(struct dlm_ctxt *dlm)
681 spin_lock(&dlm_domain_lock);
683 if (dlm->dlm_state == DLM_CTXT_JOINED)
686 spin_unlock(&dlm_domain_lock);
691 int dlm_shutting_down(struct dlm_ctxt *dlm)
695 spin_lock(&dlm_domain_lock);
697 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
700 spin_unlock(&dlm_domain_lock);
705 void dlm_unregister_domain(struct dlm_ctxt *dlm)
708 struct dlm_lock_resource *res;
710 spin_lock(&dlm_domain_lock);
711 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
712 BUG_ON(!dlm->num_joins);
715 if (!dlm->num_joins) {
716 /* We mark it "in shutdown" now so new register
717 * requests wait until we've completely left the
718 * domain. Don't use DLM_CTXT_LEAVING yet as we still
719 * want new domain joins to communicate with us at
720 * least until we've completed migration of our
722 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
725 spin_unlock(&dlm_domain_lock);
728 mlog(0, "shutting down domain %s\n", dlm->name);
729 dlm_begin_exit_domain(dlm);
731 /* We changed dlm state, notify the thread */
732 dlm_kick_thread(dlm, NULL);
734 while (dlm_migrate_all_locks(dlm)) {
735 /* Give dlm_thread time to purge the lockres' */
737 mlog(0, "%s: more migration to do\n", dlm->name);
740 /* This list should be empty. If not, print remaining lockres */
741 if (!list_empty(&dlm->tracking_list)) {
742 mlog(ML_ERROR, "Following lockres' are still on the "
744 list_for_each_entry(res, &dlm->tracking_list, tracking)
745 dlm_print_one_lock_resource(res);
748 dlm_mark_domain_leaving(dlm);
749 dlm_leave_domain(dlm);
750 printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
751 dlm_force_free_mles(dlm);
752 dlm_complete_dlm_shutdown(dlm);
756 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
758 static int dlm_query_join_proto_check(char *proto_type, int node,
759 struct dlm_protocol_version *ours,
760 struct dlm_protocol_version *request)
763 struct dlm_protocol_version proto = *request;
765 if (!dlm_protocol_compare(ours, &proto)) {
767 "node %u wanted to join with %s locking protocol "
768 "%u.%u, we respond with %u.%u\n",
772 proto.pv_major, proto.pv_minor);
773 request->pv_minor = proto.pv_minor;
777 "Node %u wanted to join with %s locking "
778 "protocol %u.%u, but we have %u.%u, disallowing\n",
791 * struct dlm_query_join_packet is made up of four one-byte fields. They
792 * are effectively in big-endian order already. However, little-endian
793 * machines swap them before putting the packet on the wire (because
794 * query_join's response is a status, and that status is treated as a u32
795 * on the wire). Thus, a big-endian and little-endian machines will treat
796 * this structure differently.
798 * The solution is to have little-endian machines swap the structure when
799 * converting from the structure to the u32 representation. This will
800 * result in the structure having the correct format on the wire no matter
801 * the host endian format.
803 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
806 union dlm_query_join_response response;
808 response.packet = *packet;
809 *wire = be32_to_cpu(response.intval);
812 static void dlm_query_join_wire_to_packet(u32 wire,
813 struct dlm_query_join_packet *packet)
815 union dlm_query_join_response response;
817 response.intval = cpu_to_be32(wire);
818 *packet = response.packet;
821 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
824 struct dlm_query_join_request *query;
825 struct dlm_query_join_packet packet = {
826 .code = JOIN_DISALLOW,
828 struct dlm_ctxt *dlm = NULL;
832 query = (struct dlm_query_join_request *) msg->buf;
834 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
838 * If heartbeat doesn't consider the node live, tell it
839 * to back off and try again. This gives heartbeat a chance
842 if (!o2hb_check_node_heartbeating(query->node_idx)) {
843 mlog(0, "node %u is not in our live map yet\n",
846 packet.code = JOIN_DISALLOW;
850 packet.code = JOIN_OK_NO_MAP;
852 spin_lock(&dlm_domain_lock);
853 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
858 * There is a small window where the joining node may not see the
859 * node(s) that just left but still part of the cluster. DISALLOW
860 * join request if joining node has different node map.
863 while (nodenum < O2NM_MAX_NODES) {
864 if (test_bit(nodenum, dlm->domain_map)) {
865 if (!byte_test_bit(nodenum, query->node_map)) {
866 mlog(0, "disallow join as node %u does not "
867 "have node %u in its nodemap\n",
868 query->node_idx, nodenum);
869 packet.code = JOIN_DISALLOW;
876 /* Once the dlm ctxt is marked as leaving then we don't want
877 * to be put in someone's domain map.
878 * Also, explicitly disallow joining at certain troublesome
879 * times (ie. during recovery). */
880 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
881 int bit = query->node_idx;
882 spin_lock(&dlm->spinlock);
884 if (dlm->dlm_state == DLM_CTXT_NEW &&
885 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
886 /*If this is a brand new context and we
887 * haven't started our join process yet, then
888 * the other node won the race. */
889 packet.code = JOIN_OK_NO_MAP;
890 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
891 /* Disallow parallel joins. */
892 packet.code = JOIN_DISALLOW;
893 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
894 mlog(0, "node %u trying to join, but recovery "
895 "is ongoing.\n", bit);
896 packet.code = JOIN_DISALLOW;
897 } else if (test_bit(bit, dlm->recovery_map)) {
898 mlog(0, "node %u trying to join, but it "
899 "still needs recovery.\n", bit);
900 packet.code = JOIN_DISALLOW;
901 } else if (test_bit(bit, dlm->domain_map)) {
902 mlog(0, "node %u trying to join, but it "
903 "is still in the domain! needs recovery?\n",
905 packet.code = JOIN_DISALLOW;
907 /* Alright we're fully a part of this domain
908 * so we keep some state as to who's joining
909 * and indicate to him that needs to be fixed
912 /* Make sure we speak compatible locking protocols. */
913 if (dlm_query_join_proto_check("DLM", bit,
914 &dlm->dlm_locking_proto,
915 &query->dlm_proto)) {
916 packet.code = JOIN_PROTOCOL_MISMATCH;
917 } else if (dlm_query_join_proto_check("fs", bit,
918 &dlm->fs_locking_proto,
920 packet.code = JOIN_PROTOCOL_MISMATCH;
922 packet.dlm_minor = query->dlm_proto.pv_minor;
923 packet.fs_minor = query->fs_proto.pv_minor;
924 packet.code = JOIN_OK;
925 __dlm_set_joining_node(dlm, query->node_idx);
929 spin_unlock(&dlm->spinlock);
932 spin_unlock(&dlm_domain_lock);
935 mlog(0, "We respond with %u\n", packet.code);
937 dlm_query_join_packet_to_wire(&packet, &response);
941 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
944 struct dlm_assert_joined *assert;
945 struct dlm_ctxt *dlm = NULL;
947 assert = (struct dlm_assert_joined *) msg->buf;
949 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
952 spin_lock(&dlm_domain_lock);
953 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
954 /* XXX should we consider no dlm ctxt an error? */
956 spin_lock(&dlm->spinlock);
958 /* Alright, this node has officially joined our
959 * domain. Set him in the map and clean up our
960 * leftover join state. */
961 BUG_ON(dlm->joining_node != assert->node_idx);
962 set_bit(assert->node_idx, dlm->domain_map);
963 clear_bit(assert->node_idx, dlm->exit_domain_map);
964 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
966 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
967 assert->node_idx, dlm->name);
968 __dlm_print_nodes(dlm);
970 /* notify anything attached to the heartbeat events */
971 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
973 spin_unlock(&dlm->spinlock);
975 spin_unlock(&dlm_domain_lock);
980 static int dlm_match_regions(struct dlm_ctxt *dlm,
981 struct dlm_query_region *qr,
982 char *local, int locallen)
984 char *remote = qr->qr_regions;
986 int localnr, i, j, foundit;
989 if (!o2hb_global_heartbeat_active()) {
990 if (qr->qr_numregions) {
991 mlog(ML_ERROR, "Domain %s: Joining node %d has global "
992 "heartbeat enabled but local node %d does not\n",
993 qr->qr_domain, qr->qr_node, dlm->node_num);
999 if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
1000 mlog(ML_ERROR, "Domain %s: Local node %d has global "
1001 "heartbeat enabled but joining node %d does not\n",
1002 qr->qr_domain, dlm->node_num, qr->qr_node);
1008 for (i = 0; i < qr->qr_numregions; ++i) {
1009 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
1010 r += O2HB_MAX_REGION_NAME_LEN;
1013 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
1014 localnr = o2hb_get_all_regions(local, (u8)localnr);
1016 /* compare local regions with remote */
1018 for (i = 0; i < localnr; ++i) {
1021 for (j = 0; j <= qr->qr_numregions; ++j) {
1022 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1026 r += O2HB_MAX_REGION_NAME_LEN;
1030 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1031 "in local node %d but not in joining node %d\n",
1032 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1033 dlm->node_num, qr->qr_node);
1036 l += O2HB_MAX_REGION_NAME_LEN;
1039 /* compare remote with local regions */
1041 for (i = 0; i < qr->qr_numregions; ++i) {
1044 for (j = 0; j < localnr; ++j) {
1045 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1049 l += O2HB_MAX_REGION_NAME_LEN;
1053 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1054 "in joining node %d but not in local node %d\n",
1055 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1056 qr->qr_node, dlm->node_num);
1059 r += O2HB_MAX_REGION_NAME_LEN;
1066 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1068 struct dlm_query_region *qr = NULL;
1069 int status, ret = 0, i;
1072 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1075 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1082 qr->qr_node = dlm->node_num;
1083 qr->qr_namelen = strlen(dlm->name);
1084 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1085 /* if local hb, the numregions will be zero */
1086 if (o2hb_global_heartbeat_active())
1087 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1091 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1092 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1095 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1096 i + 1)) < O2NM_MAX_NODES) {
1097 if (i == dlm->node_num)
1100 mlog(0, "Sending regions to node %d\n", i);
1102 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1103 sizeof(struct dlm_query_region),
1108 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1119 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1120 void *data, void **ret_data)
1122 struct dlm_query_region *qr;
1123 struct dlm_ctxt *dlm = NULL;
1127 qr = (struct dlm_query_region *) msg->buf;
1129 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1132 /* buffer used in dlm_mast_regions() */
1133 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1139 spin_lock(&dlm_domain_lock);
1140 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1142 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1143 "before join domain\n", qr->qr_node, qr->qr_domain);
1144 goto out_domain_lock;
1147 spin_lock(&dlm->spinlock);
1148 if (dlm->joining_node != qr->qr_node) {
1149 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1150 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1155 /* Support for global heartbeat was added in 1.1 */
1156 if (dlm->dlm_locking_proto.pv_major == 1 &&
1157 dlm->dlm_locking_proto.pv_minor == 0) {
1158 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1159 "but active dlm protocol is %d.%d\n", qr->qr_node,
1160 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1161 dlm->dlm_locking_proto.pv_minor);
1165 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1168 spin_unlock(&dlm->spinlock);
1171 spin_unlock(&dlm_domain_lock);
1178 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1180 struct o2nm_node *local;
1181 struct dlm_node_info *remote;
1185 for (j = 0; j < qn->qn_numnodes; ++j)
1186 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1187 &(qn->qn_nodes[j].ni_ipv4_address),
1188 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1190 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1191 local = o2nm_get_node_by_num(i);
1193 for (j = 0; j < qn->qn_numnodes; ++j) {
1194 if (qn->qn_nodes[j].ni_nodenum == i) {
1195 remote = &(qn->qn_nodes[j]);
1200 if (!local && !remote)
1203 if ((local && !remote) || (!local && remote))
1207 ((remote->ni_nodenum != local->nd_num) ||
1208 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1209 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1213 if (remote && !local)
1214 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1215 "registered in joining node %d but not in "
1216 "local node %d\n", qn->qn_domain,
1218 &(remote->ni_ipv4_address),
1219 ntohs(remote->ni_ipv4_port),
1220 qn->qn_nodenum, dlm->node_num);
1221 if (local && !remote)
1222 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1223 "registered in local node %d but not in "
1224 "joining node %d\n", qn->qn_domain,
1225 local->nd_num, &(local->nd_ipv4_address),
1226 ntohs(local->nd_ipv4_port),
1227 dlm->node_num, qn->qn_nodenum);
1228 BUG_ON((!local && !remote));
1232 o2nm_node_put(local);
1238 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1240 struct dlm_query_nodeinfo *qn = NULL;
1241 struct o2nm_node *node;
1242 int ret = 0, status, count, i;
1244 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1247 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1254 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1255 node = o2nm_get_node_by_num(i);
1258 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1259 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1260 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1261 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1262 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1264 o2nm_node_put(node);
1267 qn->qn_nodenum = dlm->node_num;
1268 qn->qn_numnodes = count;
1269 qn->qn_namelen = strlen(dlm->name);
1270 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1273 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1274 i + 1)) < O2NM_MAX_NODES) {
1275 if (i == dlm->node_num)
1278 mlog(0, "Sending nodeinfo to node %d\n", i);
1280 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1281 qn, sizeof(struct dlm_query_nodeinfo),
1286 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1296 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1297 void *data, void **ret_data)
1299 struct dlm_query_nodeinfo *qn;
1300 struct dlm_ctxt *dlm = NULL;
1301 int locked = 0, status = -EINVAL;
1303 qn = (struct dlm_query_nodeinfo *) msg->buf;
1305 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1308 spin_lock(&dlm_domain_lock);
1309 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1311 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1312 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1316 spin_lock(&dlm->spinlock);
1318 if (dlm->joining_node != qn->qn_nodenum) {
1319 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1320 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1325 /* Support for node query was added in 1.1 */
1326 if (dlm->dlm_locking_proto.pv_major == 1 &&
1327 dlm->dlm_locking_proto.pv_minor == 0) {
1328 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1329 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1330 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1331 dlm->dlm_locking_proto.pv_minor);
1335 status = dlm_match_nodes(dlm, qn);
1339 spin_unlock(&dlm->spinlock);
1340 spin_unlock(&dlm_domain_lock);
1345 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1348 struct dlm_cancel_join *cancel;
1349 struct dlm_ctxt *dlm = NULL;
1351 cancel = (struct dlm_cancel_join *) msg->buf;
1353 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1356 spin_lock(&dlm_domain_lock);
1357 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1360 spin_lock(&dlm->spinlock);
1362 /* Yikes, this guy wants to cancel his join. No
1363 * problem, we simply cleanup our join state. */
1364 BUG_ON(dlm->joining_node != cancel->node_idx);
1365 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1367 spin_unlock(&dlm->spinlock);
1369 spin_unlock(&dlm_domain_lock);
1374 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1378 struct dlm_cancel_join cancel_msg;
1380 memset(&cancel_msg, 0, sizeof(cancel_msg));
1381 cancel_msg.node_idx = dlm->node_num;
1382 cancel_msg.name_len = strlen(dlm->name);
1383 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1385 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1386 &cancel_msg, sizeof(cancel_msg), node,
1389 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1390 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1399 /* map_size should be in bytes. */
1400 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1401 unsigned long *node_map,
1402 unsigned int map_size)
1404 int status, tmpstat;
1407 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1408 sizeof(unsigned long))) {
1410 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1411 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1417 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1418 node + 1)) < O2NM_MAX_NODES) {
1419 if (node == dlm->node_num)
1422 tmpstat = dlm_send_one_join_cancel(dlm, node);
1424 mlog(ML_ERROR, "Error return %d cancelling join on "
1425 "node %d\n", tmpstat, node);
1436 static int dlm_request_join(struct dlm_ctxt *dlm,
1438 enum dlm_query_join_response_code *response)
1441 struct dlm_query_join_request join_msg;
1442 struct dlm_query_join_packet packet;
1445 mlog(0, "querying node %d\n", node);
1447 memset(&join_msg, 0, sizeof(join_msg));
1448 join_msg.node_idx = dlm->node_num;
1449 join_msg.name_len = strlen(dlm->name);
1450 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1451 join_msg.dlm_proto = dlm->dlm_locking_proto;
1452 join_msg.fs_proto = dlm->fs_locking_proto;
1454 /* copy live node map to join message */
1455 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1457 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1458 sizeof(join_msg), node, &join_resp);
1459 if (status < 0 && status != -ENOPROTOOPT) {
1460 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1461 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1465 dlm_query_join_wire_to_packet(join_resp, &packet);
1467 /* -ENOPROTOOPT from the net code means the other side isn't
1468 listening for our message type -- that's fine, it means
1469 his dlm isn't up, so we can consider him a 'yes' but not
1470 joined into the domain. */
1471 if (status == -ENOPROTOOPT) {
1473 *response = JOIN_OK_NO_MAP;
1474 } else if (packet.code == JOIN_DISALLOW ||
1475 packet.code == JOIN_OK_NO_MAP) {
1476 *response = packet.code;
1477 } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
1479 "This node requested DLM locking protocol %u.%u and "
1480 "filesystem locking protocol %u.%u. At least one of "
1481 "the protocol versions on node %d is not compatible, "
1483 dlm->dlm_locking_proto.pv_major,
1484 dlm->dlm_locking_proto.pv_minor,
1485 dlm->fs_locking_proto.pv_major,
1486 dlm->fs_locking_proto.pv_minor,
1489 *response = packet.code;
1490 } else if (packet.code == JOIN_OK) {
1491 *response = packet.code;
1492 /* Use the same locking protocol as the remote node */
1493 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1494 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1496 "Node %d responds JOIN_OK with DLM locking protocol "
1497 "%u.%u and fs locking protocol %u.%u\n",
1499 dlm->dlm_locking_proto.pv_major,
1500 dlm->dlm_locking_proto.pv_minor,
1501 dlm->fs_locking_proto.pv_major,
1502 dlm->fs_locking_proto.pv_minor);
1505 mlog(ML_ERROR, "invalid response %d from node %u\n",
1509 mlog(0, "status %d, node %d response is %d\n", status, node,
1516 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1520 struct dlm_assert_joined assert_msg;
1522 mlog(0, "Sending join assert to node %u\n", node);
1524 memset(&assert_msg, 0, sizeof(assert_msg));
1525 assert_msg.node_idx = dlm->node_num;
1526 assert_msg.name_len = strlen(dlm->name);
1527 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1529 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1530 &assert_msg, sizeof(assert_msg), node,
1533 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1534 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1540 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1541 unsigned long *node_map)
1543 int status, node, live;
1547 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1548 node + 1)) < O2NM_MAX_NODES) {
1549 if (node == dlm->node_num)
1553 /* It is very important that this message be
1554 * received so we spin until either the node
1555 * has died or it gets the message. */
1556 status = dlm_send_one_join_assert(dlm, node);
1558 spin_lock(&dlm->spinlock);
1559 live = test_bit(node, dlm->live_nodes_map);
1560 spin_unlock(&dlm->spinlock);
1563 mlog(ML_ERROR, "Error return %d asserting "
1564 "join on node %d\n", status, node);
1566 /* give us some time between errors... */
1568 msleep(DLM_DOMAIN_BACKOFF_MS);
1570 } while (status && live);
1574 struct domain_join_ctxt {
1575 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1576 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1579 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1580 struct domain_join_ctxt *ctxt,
1581 enum dlm_query_join_response_code response)
1585 if (response == JOIN_DISALLOW) {
1586 mlog(0, "Latest response of disallow -- should restart\n");
1590 spin_lock(&dlm->spinlock);
1591 /* For now, we restart the process if the node maps have
1593 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1594 sizeof(dlm->live_nodes_map));
1595 spin_unlock(&dlm->spinlock);
1598 mlog(0, "Node maps changed -- should restart\n");
1603 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1605 int status = 0, tmpstat, node;
1606 struct domain_join_ctxt *ctxt;
1607 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1611 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1618 /* group sem locking should work for us here -- we're already
1619 * registered for heartbeat events so filling this should be
1620 * atomic wrt getting those handlers called. */
1621 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1623 spin_lock(&dlm->spinlock);
1624 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1626 __dlm_set_joining_node(dlm, dlm->node_num);
1628 spin_unlock(&dlm->spinlock);
1631 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1632 node + 1)) < O2NM_MAX_NODES) {
1633 if (node == dlm->node_num)
1636 status = dlm_request_join(dlm, node, &response);
1642 /* Ok, either we got a response or the node doesn't have a
1644 if (response == JOIN_OK)
1645 set_bit(node, ctxt->yes_resp_map);
1647 if (dlm_should_restart_join(dlm, ctxt, response)) {
1653 mlog(0, "Yay, done querying nodes!\n");
1655 /* Yay, everyone agree's we can join the domain. My domain is
1656 * comprised of all nodes who were put in the
1657 * yes_resp_map. Copy that into our domain map and send a join
1658 * assert message to clean up everyone elses state. */
1659 spin_lock(&dlm->spinlock);
1660 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1661 sizeof(ctxt->yes_resp_map));
1662 set_bit(dlm->node_num, dlm->domain_map);
1663 spin_unlock(&dlm->spinlock);
1665 /* Support for global heartbeat and node info was added in 1.1 */
1666 if (dlm->dlm_locking_proto.pv_major > 1 ||
1667 dlm->dlm_locking_proto.pv_minor > 0) {
1668 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1673 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1680 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1682 /* Joined state *must* be set before the joining node
1683 * information, otherwise the query_join handler may read no
1684 * current joiner but a state of NEW and tell joining nodes
1685 * we're not in the domain. */
1686 spin_lock(&dlm_domain_lock);
1687 dlm->dlm_state = DLM_CTXT_JOINED;
1689 spin_unlock(&dlm_domain_lock);
1692 spin_lock(&dlm->spinlock);
1693 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1695 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1696 __dlm_print_nodes(dlm);
1698 spin_unlock(&dlm->spinlock);
1701 /* Do we need to send a cancel message to any nodes? */
1703 tmpstat = dlm_send_join_cancels(dlm,
1705 sizeof(ctxt->yes_resp_map));
1707 mlog_errno(tmpstat);
1712 mlog(0, "returning %d\n", status);
1716 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1718 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1719 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1720 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1723 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1727 mlog(0, "registering handlers.\n");
1729 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1730 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1731 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1735 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1736 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1737 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1741 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1742 sizeof(struct dlm_master_request),
1743 dlm_master_request_handler,
1744 dlm, NULL, &dlm->dlm_domain_handlers);
1748 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1749 sizeof(struct dlm_assert_master),
1750 dlm_assert_master_handler,
1751 dlm, dlm_assert_master_post_handler,
1752 &dlm->dlm_domain_handlers);
1756 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1757 sizeof(struct dlm_create_lock),
1758 dlm_create_lock_handler,
1759 dlm, NULL, &dlm->dlm_domain_handlers);
1763 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1764 DLM_CONVERT_LOCK_MAX_LEN,
1765 dlm_convert_lock_handler,
1766 dlm, NULL, &dlm->dlm_domain_handlers);
1770 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1771 DLM_UNLOCK_LOCK_MAX_LEN,
1772 dlm_unlock_lock_handler,
1773 dlm, NULL, &dlm->dlm_domain_handlers);
1777 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1778 DLM_PROXY_AST_MAX_LEN,
1779 dlm_proxy_ast_handler,
1780 dlm, NULL, &dlm->dlm_domain_handlers);
1784 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1785 sizeof(struct dlm_exit_domain),
1786 dlm_exit_domain_handler,
1787 dlm, NULL, &dlm->dlm_domain_handlers);
1791 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1792 sizeof(struct dlm_deref_lockres),
1793 dlm_deref_lockres_handler,
1794 dlm, NULL, &dlm->dlm_domain_handlers);
1798 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1799 sizeof(struct dlm_migrate_request),
1800 dlm_migrate_request_handler,
1801 dlm, NULL, &dlm->dlm_domain_handlers);
1805 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1806 DLM_MIG_LOCKRES_MAX_LEN,
1807 dlm_mig_lockres_handler,
1808 dlm, NULL, &dlm->dlm_domain_handlers);
1812 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1813 sizeof(struct dlm_master_requery),
1814 dlm_master_requery_handler,
1815 dlm, NULL, &dlm->dlm_domain_handlers);
1819 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1820 sizeof(struct dlm_lock_request),
1821 dlm_request_all_locks_handler,
1822 dlm, NULL, &dlm->dlm_domain_handlers);
1826 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1827 sizeof(struct dlm_reco_data_done),
1828 dlm_reco_data_done_handler,
1829 dlm, NULL, &dlm->dlm_domain_handlers);
1833 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1834 sizeof(struct dlm_begin_reco),
1835 dlm_begin_reco_handler,
1836 dlm, NULL, &dlm->dlm_domain_handlers);
1840 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1841 sizeof(struct dlm_finalize_reco),
1842 dlm_finalize_reco_handler,
1843 dlm, NULL, &dlm->dlm_domain_handlers);
1847 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1848 sizeof(struct dlm_exit_domain),
1849 dlm_begin_exit_domain_handler,
1850 dlm, NULL, &dlm->dlm_domain_handlers);
1856 dlm_unregister_domain_handlers(dlm);
1861 static int dlm_join_domain(struct dlm_ctxt *dlm)
1864 unsigned int backoff;
1865 unsigned int total_backoff = 0;
1869 mlog(0, "Join domain %s\n", dlm->name);
1871 status = dlm_register_domain_handlers(dlm);
1877 status = dlm_launch_thread(dlm);
1883 status = dlm_launch_recovery_thread(dlm);
1889 status = dlm_debug_init(dlm);
1895 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1896 if (!dlm->dlm_worker) {
1903 status = dlm_try_to_join_domain(dlm);
1905 /* If we're racing another node to the join, then we
1906 * need to back off temporarily and let them
1908 #define DLM_JOIN_TIMEOUT_MSECS 90000
1909 if (status == -EAGAIN) {
1910 if (signal_pending(current)) {
1911 status = -ERESTARTSYS;
1916 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1917 status = -ERESTARTSYS;
1918 mlog(ML_NOTICE, "Timed out joining dlm domain "
1919 "%s after %u msecs\n", dlm->name,
1920 jiffies_to_msecs(total_backoff));
1926 * <dale> No, after you!
1928 * <dale> But you first!
1931 backoff = (unsigned int)(jiffies & 0x3);
1932 backoff *= DLM_DOMAIN_BACKOFF_MS;
1933 total_backoff += backoff;
1934 mlog(0, "backoff %d\n", backoff);
1937 } while (status == -EAGAIN);
1946 wake_up(&dlm_domain_events);
1949 dlm_unregister_domain_handlers(dlm);
1950 dlm_debug_shutdown(dlm);
1951 dlm_complete_thread(dlm);
1952 dlm_complete_recovery_thread(dlm);
1953 dlm_destroy_dlm_worker(dlm);
1959 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1964 struct dlm_ctxt *dlm = NULL;
1966 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1968 mlog_errno(-ENOMEM);
1972 dlm->name = kstrdup(domain, GFP_KERNEL);
1973 if (dlm->name == NULL) {
1974 mlog_errno(-ENOMEM);
1980 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1981 if (!dlm->lockres_hash) {
1982 mlog_errno(-ENOMEM);
1989 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1990 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1992 dlm->master_hash = (struct hlist_head **)
1993 dlm_alloc_pagevec(DLM_HASH_PAGES);
1994 if (!dlm->master_hash) {
1995 mlog_errno(-ENOMEM);
1996 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2003 for (i = 0; i < DLM_HASH_BUCKETS; i++)
2004 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
2007 dlm->node_num = o2nm_this_node();
2009 ret = dlm_create_debugfs_subroot(dlm);
2011 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
2012 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2019 spin_lock_init(&dlm->spinlock);
2020 spin_lock_init(&dlm->master_lock);
2021 spin_lock_init(&dlm->ast_lock);
2022 spin_lock_init(&dlm->track_lock);
2023 INIT_LIST_HEAD(&dlm->list);
2024 INIT_LIST_HEAD(&dlm->dirty_list);
2025 INIT_LIST_HEAD(&dlm->reco.resources);
2026 INIT_LIST_HEAD(&dlm->reco.received);
2027 INIT_LIST_HEAD(&dlm->reco.node_data);
2028 INIT_LIST_HEAD(&dlm->purge_list);
2029 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2030 INIT_LIST_HEAD(&dlm->tracking_list);
2031 dlm->reco.state = 0;
2033 INIT_LIST_HEAD(&dlm->pending_asts);
2034 INIT_LIST_HEAD(&dlm->pending_basts);
2036 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2037 dlm->recovery_map, &(dlm->recovery_map[0]));
2039 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2040 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2041 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2043 dlm->dlm_thread_task = NULL;
2044 dlm->dlm_reco_thread_task = NULL;
2045 dlm->dlm_worker = NULL;
2046 init_waitqueue_head(&dlm->dlm_thread_wq);
2047 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2048 init_waitqueue_head(&dlm->reco.event);
2049 init_waitqueue_head(&dlm->ast_wq);
2050 init_waitqueue_head(&dlm->migration_wq);
2051 INIT_LIST_HEAD(&dlm->mle_hb_events);
2053 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2054 init_waitqueue_head(&dlm->dlm_join_events);
2056 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2057 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2059 atomic_set(&dlm->res_tot_count, 0);
2060 atomic_set(&dlm->res_cur_count, 0);
2061 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2062 atomic_set(&dlm->mle_tot_count[i], 0);
2063 atomic_set(&dlm->mle_cur_count[i], 0);
2066 spin_lock_init(&dlm->work_lock);
2067 INIT_LIST_HEAD(&dlm->work_list);
2068 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2070 kref_init(&dlm->dlm_refs);
2071 dlm->dlm_state = DLM_CTXT_NEW;
2073 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2075 mlog(0, "context init: refcount %u\n",
2076 atomic_read(&dlm->dlm_refs.refcount));
2083 * Compare a requested locking protocol version against the current one.
2085 * If the major numbers are different, they are incompatible.
2086 * If the current minor is greater than the request, they are incompatible.
2087 * If the current minor is less than or equal to the request, they are
2088 * compatible, and the requester should run at the current minor version.
2090 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2091 struct dlm_protocol_version *request)
2093 if (existing->pv_major != request->pv_major)
2096 if (existing->pv_minor > request->pv_minor)
2099 if (existing->pv_minor < request->pv_minor)
2100 request->pv_minor = existing->pv_minor;
2106 * dlm_register_domain: one-time setup per "domain".
2108 * The filesystem passes in the requested locking version via proto.
2109 * If registration was successful, proto will contain the negotiated
2112 struct dlm_ctxt * dlm_register_domain(const char *domain,
2114 struct dlm_protocol_version *fs_proto)
2117 struct dlm_ctxt *dlm = NULL;
2118 struct dlm_ctxt *new_ctxt = NULL;
2120 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2121 ret = -ENAMETOOLONG;
2122 mlog(ML_ERROR, "domain name length too long\n");
2126 mlog(0, "register called for domain \"%s\"\n", domain);
2130 if (signal_pending(current)) {
2136 spin_lock(&dlm_domain_lock);
2138 dlm = __dlm_lookup_domain(domain);
2140 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2141 spin_unlock(&dlm_domain_lock);
2143 mlog(0, "This ctxt is not joined yet!\n");
2144 wait_event_interruptible(dlm_domain_events,
2145 dlm_wait_on_domain_helper(
2150 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2151 spin_unlock(&dlm_domain_lock);
2153 "Requested locking protocol version is not "
2154 "compatible with already registered domain "
2155 "\"%s\"\n", domain);
2163 spin_unlock(&dlm_domain_lock);
2171 spin_unlock(&dlm_domain_lock);
2173 new_ctxt = dlm_alloc_ctxt(domain, key);
2182 /* a little variable switch-a-roo here... */
2186 /* add the new domain */
2187 list_add_tail(&dlm->list, &dlm_domains);
2188 spin_unlock(&dlm_domain_lock);
2191 * Pass the locking protocol version into the join. If the join
2192 * succeeds, it will have the negotiated protocol set.
2194 dlm->dlm_locking_proto = dlm_protocol;
2195 dlm->fs_locking_proto = *fs_proto;
2197 ret = dlm_join_domain(dlm);
2204 /* Tell the caller what locking protocol we negotiated */
2205 *fs_proto = dlm->fs_locking_proto;
2210 dlm_free_ctxt_mem(new_ctxt);
2217 EXPORT_SYMBOL_GPL(dlm_register_domain);
2219 static LIST_HEAD(dlm_join_handlers);
2221 static void dlm_unregister_net_handlers(void)
2223 o2net_unregister_handler_list(&dlm_join_handlers);
2226 static int dlm_register_net_handlers(void)
2230 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2231 sizeof(struct dlm_query_join_request),
2232 dlm_query_join_handler,
2233 NULL, NULL, &dlm_join_handlers);
2237 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2238 sizeof(struct dlm_assert_joined),
2239 dlm_assert_joined_handler,
2240 NULL, NULL, &dlm_join_handlers);
2244 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2245 sizeof(struct dlm_cancel_join),
2246 dlm_cancel_join_handler,
2247 NULL, NULL, &dlm_join_handlers);
2251 status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2252 sizeof(struct dlm_query_region),
2253 dlm_query_region_handler,
2254 NULL, NULL, &dlm_join_handlers);
2259 status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2260 sizeof(struct dlm_query_nodeinfo),
2261 dlm_query_nodeinfo_handler,
2262 NULL, NULL, &dlm_join_handlers);
2265 dlm_unregister_net_handlers();
2270 /* Domain eviction callback handling.
2272 * The file system requires notification of node death *before* the
2273 * dlm completes it's recovery work, otherwise it may be able to
2274 * acquire locks on resources requiring recovery. Since the dlm can
2275 * evict a node from it's domain *before* heartbeat fires, a similar
2276 * mechanism is required. */
2278 /* Eviction is not expected to happen often, so a per-domain lock is
2279 * not necessary. Eviction callbacks are allowed to sleep for short
2280 * periods of time. */
2281 static DECLARE_RWSEM(dlm_callback_sem);
2283 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2286 struct dlm_eviction_cb *cb;
2288 down_read(&dlm_callback_sem);
2289 list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2290 cb->ec_func(node_num, cb->ec_data);
2292 up_read(&dlm_callback_sem);
2295 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2296 dlm_eviction_func *f,
2299 INIT_LIST_HEAD(&cb->ec_item);
2303 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2305 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2306 struct dlm_eviction_cb *cb)
2308 down_write(&dlm_callback_sem);
2309 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2310 up_write(&dlm_callback_sem);
2312 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2314 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2316 down_write(&dlm_callback_sem);
2317 list_del_init(&cb->ec_item);
2318 up_write(&dlm_callback_sem);
2320 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2322 static int __init dlm_init(void)
2326 status = dlm_init_mle_cache();
2328 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2332 status = dlm_init_master_caches();
2334 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2335 "o2dlm_lockname slabcaches\n");
2339 status = dlm_init_lock_cache();
2341 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2345 status = dlm_register_net_handlers();
2347 mlog(ML_ERROR, "Unable to register network handlers\n");
2351 status = dlm_create_debugfs_root();
2357 dlm_unregister_net_handlers();
2358 dlm_destroy_lock_cache();
2359 dlm_destroy_master_caches();
2360 dlm_destroy_mle_cache();
2364 static void __exit dlm_exit (void)
2366 dlm_destroy_debugfs_root();
2367 dlm_unregister_net_handlers();
2368 dlm_destroy_lock_cache();
2369 dlm_destroy_master_caches();
2370 dlm_destroy_mle_cache();
2373 MODULE_AUTHOR("Oracle");
2374 MODULE_LICENSE("GPL");
2375 MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2377 module_init(dlm_init);
2378 module_exit(dlm_exit);