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/utsname.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/delay.h>
35 #include <linux/err.h>
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
42 #include "dlmcommon.h"
44 #include "dlmdomain.h"
48 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
49 #include "cluster/masklog.h"
52 * ocfs2 node maps are array of long int, which limits to send them freely
53 * across the wire due to endianness issues. To workaround this, we convert
54 * long ints to byte arrays. Following 3 routines are helper functions to
55 * set/test/copy bits within those array of bytes
57 static inline void byte_set_bit(u8 nr, u8 map[])
59 map[nr >> 3] |= (1UL << (nr & 7));
62 static inline int byte_test_bit(u8 nr, u8 map[])
64 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
67 static inline void byte_copymap(u8 dmap[], unsigned long smap[],
75 memset(dmap, 0, ((sz + 7) >> 3));
76 for (nn = 0 ; nn < sz; nn++)
77 if (test_bit(nn, smap))
78 byte_set_bit(nn, dmap);
81 static void dlm_free_pagevec(void **vec, int pages)
84 free_page((unsigned long)vec[pages]);
88 static void **dlm_alloc_pagevec(int pages)
90 void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
96 for (i = 0; i < pages; i++)
97 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
100 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
101 pages, (unsigned long)DLM_HASH_PAGES,
102 (unsigned long)DLM_BUCKETS_PER_PAGE);
105 dlm_free_pagevec(vec, i);
111 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
113 * struct dlm_ctxt->spinlock
114 * struct dlm_lock_resource->spinlock
115 * struct dlm_ctxt->master_lock
116 * struct dlm_ctxt->ast_lock
117 * dlm_master_list_entry->spinlock
122 DEFINE_SPINLOCK(dlm_domain_lock);
123 LIST_HEAD(dlm_domains);
124 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
126 #define DLM_DOMAIN_BACKOFF_MS 200
128 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
130 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
132 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
134 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
137 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
139 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
141 hlist_del_init(&lockres->hash_node);
142 dlm_lockres_put(lockres);
145 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
146 struct dlm_lock_resource *res)
148 struct hlist_head *bucket;
151 assert_spin_locked(&dlm->spinlock);
154 bucket = dlm_lockres_hash(dlm, q->hash);
156 /* get a reference for our hashtable */
157 dlm_lockres_get(res);
159 hlist_add_head(&res->hash_node, bucket);
162 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
167 struct hlist_head *bucket;
168 struct hlist_node *list;
170 mlog_entry("%.*s\n", len, name);
172 assert_spin_locked(&dlm->spinlock);
174 bucket = dlm_lockres_hash(dlm, hash);
176 hlist_for_each(list, bucket) {
177 struct dlm_lock_resource *res = hlist_entry(list,
178 struct dlm_lock_resource, hash_node);
179 if (res->lockname.name[0] != name[0])
181 if (unlikely(res->lockname.len != len))
183 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
185 dlm_lockres_get(res);
191 /* intended to be called by functions which do not care about lock
192 * resources which are being purged (most net _handler functions).
193 * this will return NULL for any lock resource which is found but
194 * currently in the process of dropping its mastery reference.
195 * use __dlm_lookup_lockres_full when you need the lock resource
196 * regardless (e.g. dlm_get_lock_resource) */
197 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
202 struct dlm_lock_resource *res = NULL;
204 mlog_entry("%.*s\n", len, name);
206 assert_spin_locked(&dlm->spinlock);
208 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
210 spin_lock(&res->spinlock);
211 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
212 spin_unlock(&res->spinlock);
213 dlm_lockres_put(res);
216 spin_unlock(&res->spinlock);
222 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
226 struct dlm_lock_resource *res;
227 unsigned int hash = dlm_lockid_hash(name, len);
229 spin_lock(&dlm->spinlock);
230 res = __dlm_lookup_lockres(dlm, name, len, hash);
231 spin_unlock(&dlm->spinlock);
235 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
237 struct dlm_ctxt *tmp = NULL;
238 struct list_head *iter;
240 assert_spin_locked(&dlm_domain_lock);
242 /* tmp->name here is always NULL terminated,
243 * but domain may not be! */
244 list_for_each(iter, &dlm_domains) {
245 tmp = list_entry (iter, struct dlm_ctxt, list);
246 if (strlen(tmp->name) == len &&
247 memcmp(tmp->name, domain, len)==0)
255 /* For null terminated domain strings ONLY */
256 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
258 assert_spin_locked(&dlm_domain_lock);
260 return __dlm_lookup_domain_full(domain, strlen(domain));
264 /* returns true on one of two conditions:
265 * 1) the domain does not exist
266 * 2) the domain exists and it's state is "joined" */
267 static int dlm_wait_on_domain_helper(const char *domain)
270 struct dlm_ctxt *tmp = NULL;
272 spin_lock(&dlm_domain_lock);
274 tmp = __dlm_lookup_domain(domain);
277 else if (tmp->dlm_state == DLM_CTXT_JOINED)
280 spin_unlock(&dlm_domain_lock);
284 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
286 if (dlm->lockres_hash)
287 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
295 /* A little strange - this function will be called while holding
296 * dlm_domain_lock and is expected to be holding it on the way out. We
297 * will however drop and reacquire it multiple times */
298 static void dlm_ctxt_release(struct kref *kref)
300 struct dlm_ctxt *dlm;
302 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
304 BUG_ON(dlm->num_joins);
305 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
307 /* we may still be in the list if we hit an error during join. */
308 list_del_init(&dlm->list);
310 spin_unlock(&dlm_domain_lock);
312 mlog(0, "freeing memory from domain %s\n", dlm->name);
314 wake_up(&dlm_domain_events);
316 dlm_free_ctxt_mem(dlm);
318 spin_lock(&dlm_domain_lock);
321 void dlm_put(struct dlm_ctxt *dlm)
323 spin_lock(&dlm_domain_lock);
324 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
325 spin_unlock(&dlm_domain_lock);
328 static void __dlm_get(struct dlm_ctxt *dlm)
330 kref_get(&dlm->dlm_refs);
333 /* given a questionable reference to a dlm object, gets a reference if
334 * it can find it in the list, otherwise returns NULL in which case
335 * you shouldn't trust your pointer. */
336 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
338 struct list_head *iter;
339 struct dlm_ctxt *target = NULL;
341 spin_lock(&dlm_domain_lock);
343 list_for_each(iter, &dlm_domains) {
344 target = list_entry (iter, struct dlm_ctxt, list);
354 spin_unlock(&dlm_domain_lock);
359 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
363 spin_lock(&dlm_domain_lock);
364 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
365 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
366 spin_unlock(&dlm_domain_lock);
371 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
373 if (dlm->dlm_worker) {
374 flush_workqueue(dlm->dlm_worker);
375 destroy_workqueue(dlm->dlm_worker);
376 dlm->dlm_worker = NULL;
380 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
382 dlm_unregister_domain_handlers(dlm);
383 dlm_complete_thread(dlm);
384 dlm_complete_recovery_thread(dlm);
385 dlm_destroy_dlm_worker(dlm);
387 /* We've left the domain. Now we can take ourselves out of the
388 * list and allow the kref stuff to help us free the
390 spin_lock(&dlm_domain_lock);
391 list_del_init(&dlm->list);
392 spin_unlock(&dlm_domain_lock);
394 /* Wake up anyone waiting for us to remove this domain */
395 wake_up(&dlm_domain_events);
398 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
400 int i, num, n, ret = 0;
401 struct dlm_lock_resource *res;
402 struct hlist_node *iter;
403 struct hlist_head *bucket;
406 mlog(0, "Migrating locks from domain %s\n", dlm->name);
409 spin_lock(&dlm->spinlock);
410 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
413 bucket = dlm_lockres_hash(dlm, i);
414 iter = bucket->first;
417 res = hlist_entry(iter, struct dlm_lock_resource,
419 dlm_lockres_get(res);
420 /* migrate, if necessary. this will drop the dlm
421 * spinlock and retake it if it does migration. */
422 dropped = dlm_empty_lockres(dlm, res);
424 spin_lock(&res->spinlock);
425 __dlm_lockres_calc_usage(dlm, res);
426 iter = res->hash_node.next;
427 spin_unlock(&res->spinlock);
429 dlm_lockres_put(res);
431 cond_resched_lock(&dlm->spinlock);
437 mlog(0, "%s: touched %d lockreses in bucket %d "
438 "(tot=%d)\n", dlm->name, n, i, num);
440 spin_unlock(&dlm->spinlock);
441 wake_up(&dlm->dlm_thread_wq);
443 /* let the dlm thread take care of purging, keep scanning until
444 * nothing remains in the hash */
446 mlog(0, "%s: %d lock resources in hash last pass\n",
450 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
454 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
458 spin_lock(&dlm->spinlock);
459 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
460 spin_unlock(&dlm->spinlock);
465 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
467 /* Yikes, a double spinlock! I need domain_lock for the dlm
468 * state and the dlm spinlock for join state... Sorry! */
470 spin_lock(&dlm_domain_lock);
471 spin_lock(&dlm->spinlock);
473 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
474 mlog(0, "Node %d is joining, we wait on it.\n",
476 spin_unlock(&dlm->spinlock);
477 spin_unlock(&dlm_domain_lock);
479 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
483 dlm->dlm_state = DLM_CTXT_LEAVING;
484 spin_unlock(&dlm->spinlock);
485 spin_unlock(&dlm_domain_lock);
488 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
492 assert_spin_locked(&dlm->spinlock);
494 printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
496 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
497 node + 1)) < O2NM_MAX_NODES) {
503 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
506 struct dlm_ctxt *dlm = data;
508 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
510 mlog_entry("%p %u %p", msg, len, data);
515 node = exit_msg->node_idx;
517 printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
519 spin_lock(&dlm->spinlock);
520 clear_bit(node, dlm->domain_map);
521 __dlm_print_nodes(dlm);
523 /* notify anything attached to the heartbeat events */
524 dlm_hb_event_notify_attached(dlm, node, 0);
526 spin_unlock(&dlm->spinlock);
533 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
537 struct dlm_exit_domain leave_msg;
539 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
540 node, dlm->name, dlm->node_num);
542 memset(&leave_msg, 0, sizeof(leave_msg));
543 leave_msg.node_idx = dlm->node_num;
545 status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
546 &leave_msg, sizeof(leave_msg), node,
549 mlog(0, "status return %d from o2net_send_message\n", status);
555 static void dlm_leave_domain(struct dlm_ctxt *dlm)
557 int node, clear_node, status;
559 /* At this point we've migrated away all our locks and won't
560 * accept mastership of new ones. The dlm is responsible for
561 * almost nothing now. We make sure not to confuse any joining
562 * nodes and then commence shutdown procedure. */
564 spin_lock(&dlm->spinlock);
565 /* Clear ourselves from the domain map */
566 clear_bit(dlm->node_num, dlm->domain_map);
567 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
568 0)) < O2NM_MAX_NODES) {
569 /* Drop the dlm spinlock. This is safe wrt the domain_map.
570 * -nodes cannot be added now as the
571 * query_join_handlers knows to respond with OK_NO_MAP
572 * -we catch the right network errors if a node is
573 * removed from the map while we're sending him the
575 spin_unlock(&dlm->spinlock);
579 status = dlm_send_one_domain_exit(dlm, node);
581 status != -ENOPROTOOPT &&
582 status != -ENOTCONN) {
583 mlog(ML_NOTICE, "Error %d sending domain exit message "
584 "to node %d\n", status, node);
586 /* Not sure what to do here but lets sleep for
587 * a bit in case this was a transient
589 msleep(DLM_DOMAIN_BACKOFF_MS);
593 spin_lock(&dlm->spinlock);
594 /* If we're not clearing the node bit then we intend
595 * to loop back around to try again. */
597 clear_bit(node, dlm->domain_map);
599 spin_unlock(&dlm->spinlock);
602 int dlm_joined(struct dlm_ctxt *dlm)
606 spin_lock(&dlm_domain_lock);
608 if (dlm->dlm_state == DLM_CTXT_JOINED)
611 spin_unlock(&dlm_domain_lock);
616 int dlm_shutting_down(struct dlm_ctxt *dlm)
620 spin_lock(&dlm_domain_lock);
622 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
625 spin_unlock(&dlm_domain_lock);
630 void dlm_unregister_domain(struct dlm_ctxt *dlm)
634 spin_lock(&dlm_domain_lock);
635 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
636 BUG_ON(!dlm->num_joins);
639 if (!dlm->num_joins) {
640 /* We mark it "in shutdown" now so new register
641 * requests wait until we've completely left the
642 * domain. Don't use DLM_CTXT_LEAVING yet as we still
643 * want new domain joins to communicate with us at
644 * least until we've completed migration of our
646 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
649 spin_unlock(&dlm_domain_lock);
652 mlog(0, "shutting down domain %s\n", dlm->name);
654 /* We changed dlm state, notify the thread */
655 dlm_kick_thread(dlm, NULL);
657 while (dlm_migrate_all_locks(dlm)) {
658 mlog(0, "%s: more migration to do\n", dlm->name);
660 dlm_mark_domain_leaving(dlm);
661 dlm_leave_domain(dlm);
662 dlm_complete_dlm_shutdown(dlm);
666 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
668 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
671 struct dlm_query_join_request *query;
672 enum dlm_query_join_response response;
673 struct dlm_ctxt *dlm = NULL;
676 query = (struct dlm_query_join_request *) msg->buf;
678 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
682 * If heartbeat doesn't consider the node live, tell it
683 * to back off and try again. This gives heartbeat a chance
686 if (!o2hb_check_node_heartbeating(query->node_idx)) {
687 mlog(0, "node %u is not in our live map yet\n",
690 response = JOIN_DISALLOW;
694 response = JOIN_OK_NO_MAP;
696 spin_lock(&dlm_domain_lock);
697 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
702 * There is a small window where the joining node may not see the
703 * node(s) that just left but still part of the cluster. DISALLOW
704 * join request if joining node has different node map.
707 while (nodenum < O2NM_MAX_NODES) {
708 if (test_bit(nodenum, dlm->domain_map)) {
709 if (!byte_test_bit(nodenum, query->node_map)) {
710 response = JOIN_DISALLOW;
717 /* Once the dlm ctxt is marked as leaving then we don't want
718 * to be put in someone's domain map.
719 * Also, explicitly disallow joining at certain troublesome
720 * times (ie. during recovery). */
721 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
722 int bit = query->node_idx;
723 spin_lock(&dlm->spinlock);
725 if (dlm->dlm_state == DLM_CTXT_NEW &&
726 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
727 /*If this is a brand new context and we
728 * haven't started our join process yet, then
729 * the other node won the race. */
730 response = JOIN_OK_NO_MAP;
731 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
732 /* Disallow parallel joins. */
733 response = JOIN_DISALLOW;
734 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
735 mlog(ML_NOTICE, "node %u trying to join, but recovery "
736 "is ongoing.\n", bit);
737 response = JOIN_DISALLOW;
738 } else if (test_bit(bit, dlm->recovery_map)) {
739 mlog(ML_NOTICE, "node %u trying to join, but it "
740 "still needs recovery.\n", bit);
741 response = JOIN_DISALLOW;
742 } else if (test_bit(bit, dlm->domain_map)) {
743 mlog(ML_NOTICE, "node %u trying to join, but it "
744 "is still in the domain! needs recovery?\n",
746 response = JOIN_DISALLOW;
748 /* Alright we're fully a part of this domain
749 * so we keep some state as to who's joining
750 * and indicate to him that needs to be fixed
753 __dlm_set_joining_node(dlm, query->node_idx);
756 spin_unlock(&dlm->spinlock);
759 spin_unlock(&dlm_domain_lock);
762 mlog(0, "We respond with %u\n", response);
767 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
770 struct dlm_assert_joined *assert;
771 struct dlm_ctxt *dlm = NULL;
773 assert = (struct dlm_assert_joined *) msg->buf;
775 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
778 spin_lock(&dlm_domain_lock);
779 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
780 /* XXX should we consider no dlm ctxt an error? */
782 spin_lock(&dlm->spinlock);
784 /* Alright, this node has officially joined our
785 * domain. Set him in the map and clean up our
786 * leftover join state. */
787 BUG_ON(dlm->joining_node != assert->node_idx);
788 set_bit(assert->node_idx, dlm->domain_map);
789 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
791 printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
792 assert->node_idx, dlm->name);
793 __dlm_print_nodes(dlm);
795 /* notify anything attached to the heartbeat events */
796 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
798 spin_unlock(&dlm->spinlock);
800 spin_unlock(&dlm_domain_lock);
805 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
808 struct dlm_cancel_join *cancel;
809 struct dlm_ctxt *dlm = NULL;
811 cancel = (struct dlm_cancel_join *) msg->buf;
813 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
816 spin_lock(&dlm_domain_lock);
817 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
820 spin_lock(&dlm->spinlock);
822 /* Yikes, this guy wants to cancel his join. No
823 * problem, we simply cleanup our join state. */
824 BUG_ON(dlm->joining_node != cancel->node_idx);
825 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
827 spin_unlock(&dlm->spinlock);
829 spin_unlock(&dlm_domain_lock);
834 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
838 struct dlm_cancel_join cancel_msg;
840 memset(&cancel_msg, 0, sizeof(cancel_msg));
841 cancel_msg.node_idx = dlm->node_num;
842 cancel_msg.name_len = strlen(dlm->name);
843 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
845 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
846 &cancel_msg, sizeof(cancel_msg), node,
857 /* map_size should be in bytes. */
858 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
859 unsigned long *node_map,
860 unsigned int map_size)
865 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
866 sizeof(unsigned long))) {
868 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
869 map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
875 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
876 node + 1)) < O2NM_MAX_NODES) {
877 if (node == dlm->node_num)
880 tmpstat = dlm_send_one_join_cancel(dlm, node);
882 mlog(ML_ERROR, "Error return %d cancelling join on "
883 "node %d\n", tmpstat, node);
894 static int dlm_request_join(struct dlm_ctxt *dlm,
896 enum dlm_query_join_response *response)
899 struct dlm_query_join_request join_msg;
901 mlog(0, "querying node %d\n", node);
903 memset(&join_msg, 0, sizeof(join_msg));
904 join_msg.node_idx = dlm->node_num;
905 join_msg.name_len = strlen(dlm->name);
906 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
908 /* copy live node map to join message */
909 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
911 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
912 sizeof(join_msg), node, &retval);
913 if (status < 0 && status != -ENOPROTOOPT) {
918 /* -ENOPROTOOPT from the net code means the other side isn't
919 listening for our message type -- that's fine, it means
920 his dlm isn't up, so we can consider him a 'yes' but not
921 joined into the domain. */
922 if (status == -ENOPROTOOPT) {
924 *response = JOIN_OK_NO_MAP;
925 } else if (retval == JOIN_DISALLOW ||
927 retval == JOIN_OK_NO_MAP) {
931 mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
935 mlog(0, "status %d, node %d response is %d\n", status, node,
942 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
946 struct dlm_assert_joined assert_msg;
948 mlog(0, "Sending join assert to node %u\n", node);
950 memset(&assert_msg, 0, sizeof(assert_msg));
951 assert_msg.node_idx = dlm->node_num;
952 assert_msg.name_len = strlen(dlm->name);
953 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
955 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
956 &assert_msg, sizeof(assert_msg), node,
964 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
965 unsigned long *node_map)
967 int status, node, live;
971 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
972 node + 1)) < O2NM_MAX_NODES) {
973 if (node == dlm->node_num)
977 /* It is very important that this message be
978 * received so we spin until either the node
979 * has died or it gets the message. */
980 status = dlm_send_one_join_assert(dlm, node);
982 spin_lock(&dlm->spinlock);
983 live = test_bit(node, dlm->live_nodes_map);
984 spin_unlock(&dlm->spinlock);
987 mlog(ML_ERROR, "Error return %d asserting "
988 "join on node %d\n", status, node);
990 /* give us some time between errors... */
992 msleep(DLM_DOMAIN_BACKOFF_MS);
994 } while (status && live);
998 struct domain_join_ctxt {
999 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1000 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1003 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1004 struct domain_join_ctxt *ctxt,
1005 enum dlm_query_join_response response)
1009 if (response == JOIN_DISALLOW) {
1010 mlog(0, "Latest response of disallow -- should restart\n");
1014 spin_lock(&dlm->spinlock);
1015 /* For now, we restart the process if the node maps have
1017 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1018 sizeof(dlm->live_nodes_map));
1019 spin_unlock(&dlm->spinlock);
1022 mlog(0, "Node maps changed -- should restart\n");
1027 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1029 int status = 0, tmpstat, node;
1030 struct domain_join_ctxt *ctxt;
1031 enum dlm_query_join_response response;
1033 mlog_entry("%p", dlm);
1035 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1042 /* group sem locking should work for us here -- we're already
1043 * registered for heartbeat events so filling this should be
1044 * atomic wrt getting those handlers called. */
1045 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1047 spin_lock(&dlm->spinlock);
1048 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1050 __dlm_set_joining_node(dlm, dlm->node_num);
1052 spin_unlock(&dlm->spinlock);
1055 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1056 node + 1)) < O2NM_MAX_NODES) {
1057 if (node == dlm->node_num)
1060 status = dlm_request_join(dlm, node, &response);
1066 /* Ok, either we got a response or the node doesn't have a
1068 if (response == JOIN_OK)
1069 set_bit(node, ctxt->yes_resp_map);
1071 if (dlm_should_restart_join(dlm, ctxt, response)) {
1077 mlog(0, "Yay, done querying nodes!\n");
1079 /* Yay, everyone agree's we can join the domain. My domain is
1080 * comprised of all nodes who were put in the
1081 * yes_resp_map. Copy that into our domain map and send a join
1082 * assert message to clean up everyone elses state. */
1083 spin_lock(&dlm->spinlock);
1084 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1085 sizeof(ctxt->yes_resp_map));
1086 set_bit(dlm->node_num, dlm->domain_map);
1087 spin_unlock(&dlm->spinlock);
1089 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1091 /* Joined state *must* be set before the joining node
1092 * information, otherwise the query_join handler may read no
1093 * current joiner but a state of NEW and tell joining nodes
1094 * we're not in the domain. */
1095 spin_lock(&dlm_domain_lock);
1096 dlm->dlm_state = DLM_CTXT_JOINED;
1098 spin_unlock(&dlm_domain_lock);
1101 spin_lock(&dlm->spinlock);
1102 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1104 __dlm_print_nodes(dlm);
1105 spin_unlock(&dlm->spinlock);
1108 /* Do we need to send a cancel message to any nodes? */
1110 tmpstat = dlm_send_join_cancels(dlm,
1112 sizeof(ctxt->yes_resp_map));
1114 mlog_errno(tmpstat);
1119 mlog(0, "returning %d\n", status);
1123 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1125 o2hb_unregister_callback(&dlm->dlm_hb_up);
1126 o2hb_unregister_callback(&dlm->dlm_hb_down);
1127 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1130 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1134 mlog(0, "registering handlers.\n");
1136 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1137 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1138 status = o2hb_register_callback(&dlm->dlm_hb_down);
1142 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1143 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1144 status = o2hb_register_callback(&dlm->dlm_hb_up);
1148 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1149 sizeof(struct dlm_master_request),
1150 dlm_master_request_handler,
1151 dlm, NULL, &dlm->dlm_domain_handlers);
1155 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1156 sizeof(struct dlm_assert_master),
1157 dlm_assert_master_handler,
1158 dlm, dlm_assert_master_post_handler,
1159 &dlm->dlm_domain_handlers);
1163 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1164 sizeof(struct dlm_create_lock),
1165 dlm_create_lock_handler,
1166 dlm, NULL, &dlm->dlm_domain_handlers);
1170 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1171 DLM_CONVERT_LOCK_MAX_LEN,
1172 dlm_convert_lock_handler,
1173 dlm, NULL, &dlm->dlm_domain_handlers);
1177 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1178 DLM_UNLOCK_LOCK_MAX_LEN,
1179 dlm_unlock_lock_handler,
1180 dlm, NULL, &dlm->dlm_domain_handlers);
1184 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1185 DLM_PROXY_AST_MAX_LEN,
1186 dlm_proxy_ast_handler,
1187 dlm, NULL, &dlm->dlm_domain_handlers);
1191 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1192 sizeof(struct dlm_exit_domain),
1193 dlm_exit_domain_handler,
1194 dlm, NULL, &dlm->dlm_domain_handlers);
1198 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1199 sizeof(struct dlm_deref_lockres),
1200 dlm_deref_lockres_handler,
1201 dlm, NULL, &dlm->dlm_domain_handlers);
1205 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1206 sizeof(struct dlm_migrate_request),
1207 dlm_migrate_request_handler,
1208 dlm, NULL, &dlm->dlm_domain_handlers);
1212 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1213 DLM_MIG_LOCKRES_MAX_LEN,
1214 dlm_mig_lockres_handler,
1215 dlm, NULL, &dlm->dlm_domain_handlers);
1219 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1220 sizeof(struct dlm_master_requery),
1221 dlm_master_requery_handler,
1222 dlm, NULL, &dlm->dlm_domain_handlers);
1226 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1227 sizeof(struct dlm_lock_request),
1228 dlm_request_all_locks_handler,
1229 dlm, NULL, &dlm->dlm_domain_handlers);
1233 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1234 sizeof(struct dlm_reco_data_done),
1235 dlm_reco_data_done_handler,
1236 dlm, NULL, &dlm->dlm_domain_handlers);
1240 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1241 sizeof(struct dlm_begin_reco),
1242 dlm_begin_reco_handler,
1243 dlm, NULL, &dlm->dlm_domain_handlers);
1247 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1248 sizeof(struct dlm_finalize_reco),
1249 dlm_finalize_reco_handler,
1250 dlm, NULL, &dlm->dlm_domain_handlers);
1256 dlm_unregister_domain_handlers(dlm);
1261 static int dlm_join_domain(struct dlm_ctxt *dlm)
1267 mlog(0, "Join domain %s\n", dlm->name);
1269 status = dlm_register_domain_handlers(dlm);
1275 status = dlm_launch_thread(dlm);
1281 status = dlm_launch_recovery_thread(dlm);
1287 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1288 if (!dlm->dlm_worker) {
1295 unsigned int backoff;
1296 status = dlm_try_to_join_domain(dlm);
1298 /* If we're racing another node to the join, then we
1299 * need to back off temporarily and let them
1301 if (status == -EAGAIN) {
1302 if (signal_pending(current)) {
1303 status = -ERESTARTSYS;
1309 * <dale> No, after you!
1311 * <dale> But you first!
1314 backoff = (unsigned int)(jiffies & 0x3);
1315 backoff *= DLM_DOMAIN_BACKOFF_MS;
1316 mlog(0, "backoff %d\n", backoff);
1319 } while (status == -EAGAIN);
1328 wake_up(&dlm_domain_events);
1331 dlm_unregister_domain_handlers(dlm);
1332 dlm_complete_thread(dlm);
1333 dlm_complete_recovery_thread(dlm);
1334 dlm_destroy_dlm_worker(dlm);
1340 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1344 struct dlm_ctxt *dlm = NULL;
1346 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1348 mlog_errno(-ENOMEM);
1352 dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1353 if (dlm->name == NULL) {
1354 mlog_errno(-ENOMEM);
1360 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1361 if (!dlm->lockres_hash) {
1362 mlog_errno(-ENOMEM);
1369 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1370 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1372 strcpy(dlm->name, domain);
1374 dlm->node_num = o2nm_this_node();
1376 spin_lock_init(&dlm->spinlock);
1377 spin_lock_init(&dlm->master_lock);
1378 spin_lock_init(&dlm->ast_lock);
1379 INIT_LIST_HEAD(&dlm->list);
1380 INIT_LIST_HEAD(&dlm->dirty_list);
1381 INIT_LIST_HEAD(&dlm->reco.resources);
1382 INIT_LIST_HEAD(&dlm->reco.received);
1383 INIT_LIST_HEAD(&dlm->reco.node_data);
1384 INIT_LIST_HEAD(&dlm->purge_list);
1385 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1386 dlm->reco.state = 0;
1388 INIT_LIST_HEAD(&dlm->pending_asts);
1389 INIT_LIST_HEAD(&dlm->pending_basts);
1391 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1392 dlm->recovery_map, &(dlm->recovery_map[0]));
1394 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1395 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1396 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1398 dlm->dlm_thread_task = NULL;
1399 dlm->dlm_reco_thread_task = NULL;
1400 dlm->dlm_worker = NULL;
1401 init_waitqueue_head(&dlm->dlm_thread_wq);
1402 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1403 init_waitqueue_head(&dlm->reco.event);
1404 init_waitqueue_head(&dlm->ast_wq);
1405 init_waitqueue_head(&dlm->migration_wq);
1406 INIT_LIST_HEAD(&dlm->master_list);
1407 INIT_LIST_HEAD(&dlm->mle_hb_events);
1409 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1410 init_waitqueue_head(&dlm->dlm_join_events);
1412 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1413 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1414 atomic_set(&dlm->local_resources, 0);
1415 atomic_set(&dlm->remote_resources, 0);
1416 atomic_set(&dlm->unknown_resources, 0);
1418 spin_lock_init(&dlm->work_lock);
1419 INIT_LIST_HEAD(&dlm->work_list);
1420 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
1422 kref_init(&dlm->dlm_refs);
1423 dlm->dlm_state = DLM_CTXT_NEW;
1425 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1427 mlog(0, "context init: refcount %u\n",
1428 atomic_read(&dlm->dlm_refs.refcount));
1435 * dlm_register_domain: one-time setup per "domain"
1437 struct dlm_ctxt * dlm_register_domain(const char *domain,
1441 struct dlm_ctxt *dlm = NULL;
1442 struct dlm_ctxt *new_ctxt = NULL;
1444 if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1445 ret = -ENAMETOOLONG;
1446 mlog(ML_ERROR, "domain name length too long\n");
1450 if (!o2hb_check_local_node_heartbeating()) {
1451 mlog(ML_ERROR, "the local node has not been configured, or is "
1452 "not heartbeating\n");
1457 mlog(0, "register called for domain \"%s\"\n", domain);
1461 if (signal_pending(current)) {
1467 spin_lock(&dlm_domain_lock);
1469 dlm = __dlm_lookup_domain(domain);
1471 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1472 spin_unlock(&dlm_domain_lock);
1474 mlog(0, "This ctxt is not joined yet!\n");
1475 wait_event_interruptible(dlm_domain_events,
1476 dlm_wait_on_domain_helper(
1484 spin_unlock(&dlm_domain_lock);
1492 spin_unlock(&dlm_domain_lock);
1494 new_ctxt = dlm_alloc_ctxt(domain, key);
1503 /* a little variable switch-a-roo here... */
1507 /* add the new domain */
1508 list_add_tail(&dlm->list, &dlm_domains);
1509 spin_unlock(&dlm_domain_lock);
1511 ret = dlm_join_domain(dlm);
1521 dlm_free_ctxt_mem(new_ctxt);
1528 EXPORT_SYMBOL_GPL(dlm_register_domain);
1530 static LIST_HEAD(dlm_join_handlers);
1532 static void dlm_unregister_net_handlers(void)
1534 o2net_unregister_handler_list(&dlm_join_handlers);
1537 static int dlm_register_net_handlers(void)
1541 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1542 sizeof(struct dlm_query_join_request),
1543 dlm_query_join_handler,
1544 NULL, NULL, &dlm_join_handlers);
1548 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1549 sizeof(struct dlm_assert_joined),
1550 dlm_assert_joined_handler,
1551 NULL, NULL, &dlm_join_handlers);
1555 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1556 sizeof(struct dlm_cancel_join),
1557 dlm_cancel_join_handler,
1558 NULL, NULL, &dlm_join_handlers);
1562 dlm_unregister_net_handlers();
1567 /* Domain eviction callback handling.
1569 * The file system requires notification of node death *before* the
1570 * dlm completes it's recovery work, otherwise it may be able to
1571 * acquire locks on resources requiring recovery. Since the dlm can
1572 * evict a node from it's domain *before* heartbeat fires, a similar
1573 * mechanism is required. */
1575 /* Eviction is not expected to happen often, so a per-domain lock is
1576 * not necessary. Eviction callbacks are allowed to sleep for short
1577 * periods of time. */
1578 static DECLARE_RWSEM(dlm_callback_sem);
1580 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1583 struct list_head *iter;
1584 struct dlm_eviction_cb *cb;
1586 down_read(&dlm_callback_sem);
1587 list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1588 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1590 cb->ec_func(node_num, cb->ec_data);
1592 up_read(&dlm_callback_sem);
1595 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1596 dlm_eviction_func *f,
1599 INIT_LIST_HEAD(&cb->ec_item);
1603 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1605 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1606 struct dlm_eviction_cb *cb)
1608 down_write(&dlm_callback_sem);
1609 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1610 up_write(&dlm_callback_sem);
1612 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1614 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1616 down_write(&dlm_callback_sem);
1617 list_del_init(&cb->ec_item);
1618 up_write(&dlm_callback_sem);
1620 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1622 static int __init dlm_init(void)
1626 dlm_print_version();
1628 status = dlm_init_mle_cache();
1632 status = dlm_register_net_handlers();
1634 dlm_destroy_mle_cache();
1641 static void __exit dlm_exit (void)
1643 dlm_unregister_net_handlers();
1644 dlm_destroy_mle_cache();
1647 MODULE_AUTHOR("Oracle");
1648 MODULE_LICENSE("GPL");
1650 module_init(dlm_init);
1651 module_exit(dlm_exit);