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"
45 #include "dlmdomain.h"
49 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
50 #include "cluster/masklog.h"
54 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
56 * struct dlm_ctxt->spinlock
57 * struct dlm_lock_resource->spinlock
58 * struct dlm_ctxt->master_lock
59 * struct dlm_ctxt->ast_lock
60 * dlm_master_list_entry->spinlock
65 spinlock_t dlm_domain_lock = SPIN_LOCK_UNLOCKED;
66 LIST_HEAD(dlm_domains);
67 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
69 #define DLM_DOMAIN_BACKOFF_MS 200
71 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data);
72 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data);
73 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data);
74 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data);
76 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
78 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
80 hlist_del_init(&lockres->hash_node);
81 dlm_lockres_put(lockres);
84 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
85 struct dlm_lock_resource *res)
87 struct hlist_head *bucket;
90 assert_spin_locked(&dlm->spinlock);
93 q->hash = full_name_hash(q->name, q->len);
94 bucket = &(dlm->lockres_hash[q->hash % DLM_HASH_BUCKETS]);
96 /* get a reference for our hashtable */
99 hlist_add_head(&res->hash_node, bucket);
102 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
107 struct hlist_node *iter;
108 struct dlm_lock_resource *tmpres=NULL;
109 struct hlist_head *bucket;
111 mlog_entry("%.*s\n", len, name);
113 assert_spin_locked(&dlm->spinlock);
115 hash = full_name_hash(name, len);
117 bucket = &(dlm->lockres_hash[hash % DLM_HASH_BUCKETS]);
119 /* check for pre-existing lock */
120 hlist_for_each(iter, bucket) {
121 tmpres = hlist_entry(iter, struct dlm_lock_resource, hash_node);
122 if (tmpres->lockname.len == len &&
123 memcmp(tmpres->lockname.name, name, len) == 0) {
124 dlm_lockres_get(tmpres);
133 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
137 struct dlm_lock_resource *res;
139 spin_lock(&dlm->spinlock);
140 res = __dlm_lookup_lockres(dlm, name, len);
141 spin_unlock(&dlm->spinlock);
145 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
147 struct dlm_ctxt *tmp = NULL;
148 struct list_head *iter;
150 assert_spin_locked(&dlm_domain_lock);
152 /* tmp->name here is always NULL terminated,
153 * but domain may not be! */
154 list_for_each(iter, &dlm_domains) {
155 tmp = list_entry (iter, struct dlm_ctxt, list);
156 if (strlen(tmp->name) == len &&
157 memcmp(tmp->name, domain, len)==0)
165 /* For null terminated domain strings ONLY */
166 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
168 assert_spin_locked(&dlm_domain_lock);
170 return __dlm_lookup_domain_full(domain, strlen(domain));
174 /* returns true on one of two conditions:
175 * 1) the domain does not exist
176 * 2) the domain exists and it's state is "joined" */
177 static int dlm_wait_on_domain_helper(const char *domain)
180 struct dlm_ctxt *tmp = NULL;
182 spin_lock(&dlm_domain_lock);
184 tmp = __dlm_lookup_domain(domain);
187 else if (tmp->dlm_state == DLM_CTXT_JOINED)
190 spin_unlock(&dlm_domain_lock);
194 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
196 if (dlm->lockres_hash)
197 free_page((unsigned long) dlm->lockres_hash);
205 /* A little strange - this function will be called while holding
206 * dlm_domain_lock and is expected to be holding it on the way out. We
207 * will however drop and reacquire it multiple times */
208 static void dlm_ctxt_release(struct kref *kref)
210 struct dlm_ctxt *dlm;
212 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
214 BUG_ON(dlm->num_joins);
215 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
217 /* we may still be in the list if we hit an error during join. */
218 list_del_init(&dlm->list);
220 spin_unlock(&dlm_domain_lock);
222 mlog(0, "freeing memory from domain %s\n", dlm->name);
224 wake_up(&dlm_domain_events);
226 dlm_free_ctxt_mem(dlm);
228 spin_lock(&dlm_domain_lock);
231 void dlm_put(struct dlm_ctxt *dlm)
233 spin_lock(&dlm_domain_lock);
234 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
235 spin_unlock(&dlm_domain_lock);
238 static void __dlm_get(struct dlm_ctxt *dlm)
240 kref_get(&dlm->dlm_refs);
243 /* given a questionable reference to a dlm object, gets a reference if
244 * it can find it in the list, otherwise returns NULL in which case
245 * you shouldn't trust your pointer. */
246 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
248 struct list_head *iter;
249 struct dlm_ctxt *target = NULL;
251 spin_lock(&dlm_domain_lock);
253 list_for_each(iter, &dlm_domains) {
254 target = list_entry (iter, struct dlm_ctxt, list);
264 spin_unlock(&dlm_domain_lock);
269 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
273 spin_lock(&dlm_domain_lock);
274 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
275 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
276 spin_unlock(&dlm_domain_lock);
281 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
283 dlm_unregister_domain_handlers(dlm);
284 dlm_complete_thread(dlm);
285 dlm_complete_recovery_thread(dlm);
287 /* We've left the domain. Now we can take ourselves out of the
288 * list and allow the kref stuff to help us free the
290 spin_lock(&dlm_domain_lock);
291 list_del_init(&dlm->list);
292 spin_unlock(&dlm_domain_lock);
294 /* Wake up anyone waiting for us to remove this domain */
295 wake_up(&dlm_domain_events);
298 static void dlm_migrate_all_locks(struct dlm_ctxt *dlm)
301 struct dlm_lock_resource *res;
303 mlog(0, "Migrating locks from domain %s\n", dlm->name);
305 spin_lock(&dlm->spinlock);
306 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
307 while (!hlist_empty(&dlm->lockres_hash[i])) {
308 res = hlist_entry(dlm->lockres_hash[i].first,
309 struct dlm_lock_resource, hash_node);
310 /* need reference when manually grabbing lockres */
311 dlm_lockres_get(res);
312 /* this should unhash the lockres
313 * and exit with dlm->spinlock */
314 mlog(0, "purging res=%p\n", res);
315 if (dlm_lockres_is_dirty(dlm, res)) {
316 /* HACK! this should absolutely go.
317 * need to figure out why some empty
318 * lockreses are still marked dirty */
319 mlog(ML_ERROR, "lockres %.*s dirty!\n",
320 res->lockname.len, res->lockname.name);
322 spin_unlock(&dlm->spinlock);
323 dlm_kick_thread(dlm, res);
324 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
325 dlm_lockres_put(res);
328 dlm_purge_lockres(dlm, res);
329 dlm_lockres_put(res);
332 spin_unlock(&dlm->spinlock);
334 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
337 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
341 spin_lock(&dlm->spinlock);
342 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
343 spin_unlock(&dlm->spinlock);
348 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
350 /* Yikes, a double spinlock! I need domain_lock for the dlm
351 * state and the dlm spinlock for join state... Sorry! */
353 spin_lock(&dlm_domain_lock);
354 spin_lock(&dlm->spinlock);
356 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
357 mlog(0, "Node %d is joining, we wait on it.\n",
359 spin_unlock(&dlm->spinlock);
360 spin_unlock(&dlm_domain_lock);
362 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
366 dlm->dlm_state = DLM_CTXT_LEAVING;
367 spin_unlock(&dlm->spinlock);
368 spin_unlock(&dlm_domain_lock);
371 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
375 assert_spin_locked(&dlm->spinlock);
377 mlog(ML_NOTICE, "Nodes in my domain (\"%s\"):\n", dlm->name);
379 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
380 node + 1)) < O2NM_MAX_NODES) {
381 mlog(ML_NOTICE, " node %d\n", node);
385 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data)
387 struct dlm_ctxt *dlm = data;
389 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
391 mlog_entry("%p %u %p", msg, len, data);
396 node = exit_msg->node_idx;
398 mlog(0, "Node %u leaves domain %s\n", node, dlm->name);
400 spin_lock(&dlm->spinlock);
401 clear_bit(node, dlm->domain_map);
402 __dlm_print_nodes(dlm);
404 /* notify anything attached to the heartbeat events */
405 dlm_hb_event_notify_attached(dlm, node, 0);
407 spin_unlock(&dlm->spinlock);
414 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
418 struct dlm_exit_domain leave_msg;
420 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
421 node, dlm->name, dlm->node_num);
423 memset(&leave_msg, 0, sizeof(leave_msg));
424 leave_msg.node_idx = dlm->node_num;
426 status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
427 &leave_msg, sizeof(leave_msg), node,
430 mlog(0, "status return %d from o2net_send_message\n", status);
436 static void dlm_leave_domain(struct dlm_ctxt *dlm)
438 int node, clear_node, status;
440 /* At this point we've migrated away all our locks and won't
441 * accept mastership of new ones. The dlm is responsible for
442 * almost nothing now. We make sure not to confuse any joining
443 * nodes and then commence shutdown procedure. */
445 spin_lock(&dlm->spinlock);
446 /* Clear ourselves from the domain map */
447 clear_bit(dlm->node_num, dlm->domain_map);
448 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
449 0)) < O2NM_MAX_NODES) {
450 /* Drop the dlm spinlock. This is safe wrt the domain_map.
451 * -nodes cannot be added now as the
452 * query_join_handlers knows to respond with OK_NO_MAP
453 * -we catch the right network errors if a node is
454 * removed from the map while we're sending him the
456 spin_unlock(&dlm->spinlock);
460 status = dlm_send_one_domain_exit(dlm, node);
462 status != -ENOPROTOOPT &&
463 status != -ENOTCONN) {
464 mlog(ML_NOTICE, "Error %d sending domain exit message "
465 "to node %d\n", status, node);
467 /* Not sure what to do here but lets sleep for
468 * a bit in case this was a transient
470 msleep(DLM_DOMAIN_BACKOFF_MS);
474 spin_lock(&dlm->spinlock);
475 /* If we're not clearing the node bit then we intend
476 * to loop back around to try again. */
478 clear_bit(node, dlm->domain_map);
480 spin_unlock(&dlm->spinlock);
483 int dlm_joined(struct dlm_ctxt *dlm)
487 spin_lock(&dlm_domain_lock);
489 if (dlm->dlm_state == DLM_CTXT_JOINED)
492 spin_unlock(&dlm_domain_lock);
497 int dlm_shutting_down(struct dlm_ctxt *dlm)
501 spin_lock(&dlm_domain_lock);
503 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
506 spin_unlock(&dlm_domain_lock);
511 void dlm_unregister_domain(struct dlm_ctxt *dlm)
515 spin_lock(&dlm_domain_lock);
516 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
517 BUG_ON(!dlm->num_joins);
520 if (!dlm->num_joins) {
521 /* We mark it "in shutdown" now so new register
522 * requests wait until we've completely left the
523 * domain. Don't use DLM_CTXT_LEAVING yet as we still
524 * want new domain joins to communicate with us at
525 * least until we've completed migration of our
527 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
530 spin_unlock(&dlm_domain_lock);
533 mlog(0, "shutting down domain %s\n", dlm->name);
535 /* We changed dlm state, notify the thread */
536 dlm_kick_thread(dlm, NULL);
538 dlm_migrate_all_locks(dlm);
539 dlm_mark_domain_leaving(dlm);
540 dlm_leave_domain(dlm);
541 dlm_complete_dlm_shutdown(dlm);
545 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
547 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data)
549 struct dlm_query_join_request *query;
550 enum dlm_query_join_response response;
551 struct dlm_ctxt *dlm = NULL;
553 query = (struct dlm_query_join_request *) msg->buf;
555 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
559 * If heartbeat doesn't consider the node live, tell it
560 * to back off and try again. This gives heartbeat a chance
563 if (!o2hb_check_node_heartbeating(query->node_idx)) {
564 mlog(0, "node %u is not in our live map yet\n",
567 response = JOIN_DISALLOW;
571 response = JOIN_OK_NO_MAP;
573 spin_lock(&dlm_domain_lock);
574 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
575 /* Once the dlm ctxt is marked as leaving then we don't want
576 * to be put in someone's domain map.
577 * Also, explicitly disallow joining at certain troublesome
578 * times (ie. during recovery). */
579 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
580 int bit = query->node_idx;
581 spin_lock(&dlm->spinlock);
583 if (dlm->dlm_state == DLM_CTXT_NEW &&
584 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
585 /*If this is a brand new context and we
586 * haven't started our join process yet, then
587 * the other node won the race. */
588 response = JOIN_OK_NO_MAP;
589 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
590 /* Disallow parallel joins. */
591 response = JOIN_DISALLOW;
592 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
593 mlog(ML_NOTICE, "node %u trying to join, but recovery "
594 "is ongoing.\n", bit);
595 response = JOIN_DISALLOW;
596 } else if (test_bit(bit, dlm->recovery_map)) {
597 mlog(ML_NOTICE, "node %u trying to join, but it "
598 "still needs recovery.\n", bit);
599 response = JOIN_DISALLOW;
600 } else if (test_bit(bit, dlm->domain_map)) {
601 mlog(ML_NOTICE, "node %u trying to join, but it "
602 "is still in the domain! needs recovery?\n",
604 response = JOIN_DISALLOW;
606 /* Alright we're fully a part of this domain
607 * so we keep some state as to who's joining
608 * and indicate to him that needs to be fixed
611 __dlm_set_joining_node(dlm, query->node_idx);
614 spin_unlock(&dlm->spinlock);
616 spin_unlock(&dlm_domain_lock);
619 mlog(0, "We respond with %u\n", response);
624 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
626 struct dlm_assert_joined *assert;
627 struct dlm_ctxt *dlm = NULL;
629 assert = (struct dlm_assert_joined *) msg->buf;
631 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
634 spin_lock(&dlm_domain_lock);
635 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
636 /* XXX should we consider no dlm ctxt an error? */
638 spin_lock(&dlm->spinlock);
640 /* Alright, this node has officially joined our
641 * domain. Set him in the map and clean up our
642 * leftover join state. */
643 BUG_ON(dlm->joining_node != assert->node_idx);
644 set_bit(assert->node_idx, dlm->domain_map);
645 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
647 __dlm_print_nodes(dlm);
649 /* notify anything attached to the heartbeat events */
650 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
652 spin_unlock(&dlm->spinlock);
654 spin_unlock(&dlm_domain_lock);
659 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
661 struct dlm_cancel_join *cancel;
662 struct dlm_ctxt *dlm = NULL;
664 cancel = (struct dlm_cancel_join *) msg->buf;
666 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
669 spin_lock(&dlm_domain_lock);
670 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
673 spin_lock(&dlm->spinlock);
675 /* Yikes, this guy wants to cancel his join. No
676 * problem, we simply cleanup our join state. */
677 BUG_ON(dlm->joining_node != cancel->node_idx);
678 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
680 spin_unlock(&dlm->spinlock);
682 spin_unlock(&dlm_domain_lock);
687 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
691 struct dlm_cancel_join cancel_msg;
693 memset(&cancel_msg, 0, sizeof(cancel_msg));
694 cancel_msg.node_idx = dlm->node_num;
695 cancel_msg.name_len = strlen(dlm->name);
696 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
698 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
699 &cancel_msg, sizeof(cancel_msg), node,
710 /* map_size should be in bytes. */
711 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
712 unsigned long *node_map,
713 unsigned int map_size)
718 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
719 sizeof(unsigned long))) {
721 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
722 map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
728 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
729 node + 1)) < O2NM_MAX_NODES) {
730 if (node == dlm->node_num)
733 tmpstat = dlm_send_one_join_cancel(dlm, node);
735 mlog(ML_ERROR, "Error return %d cancelling join on "
736 "node %d\n", tmpstat, node);
747 static int dlm_request_join(struct dlm_ctxt *dlm,
749 enum dlm_query_join_response *response)
752 struct dlm_query_join_request join_msg;
754 mlog(0, "querying node %d\n", node);
756 memset(&join_msg, 0, sizeof(join_msg));
757 join_msg.node_idx = dlm->node_num;
758 join_msg.name_len = strlen(dlm->name);
759 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
761 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
762 sizeof(join_msg), node, &retval);
763 if (status < 0 && status != -ENOPROTOOPT) {
768 /* -ENOPROTOOPT from the net code means the other side isn't
769 listening for our message type -- that's fine, it means
770 his dlm isn't up, so we can consider him a 'yes' but not
771 joined into the domain. */
772 if (status == -ENOPROTOOPT) {
774 *response = JOIN_OK_NO_MAP;
775 } else if (retval == JOIN_DISALLOW ||
777 retval == JOIN_OK_NO_MAP) {
781 mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
785 mlog(0, "status %d, node %d response is %d\n", status, node,
792 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
796 struct dlm_assert_joined assert_msg;
798 mlog(0, "Sending join assert to node %u\n", node);
800 memset(&assert_msg, 0, sizeof(assert_msg));
801 assert_msg.node_idx = dlm->node_num;
802 assert_msg.name_len = strlen(dlm->name);
803 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
805 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
806 &assert_msg, sizeof(assert_msg), node,
814 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
815 unsigned long *node_map)
817 int status, node, live;
821 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
822 node + 1)) < O2NM_MAX_NODES) {
823 if (node == dlm->node_num)
827 /* It is very important that this message be
828 * received so we spin until either the node
829 * has died or it gets the message. */
830 status = dlm_send_one_join_assert(dlm, node);
832 spin_lock(&dlm->spinlock);
833 live = test_bit(node, dlm->live_nodes_map);
834 spin_unlock(&dlm->spinlock);
837 mlog(ML_ERROR, "Error return %d asserting "
838 "join on node %d\n", status, node);
840 /* give us some time between errors... */
842 msleep(DLM_DOMAIN_BACKOFF_MS);
844 } while (status && live);
848 struct domain_join_ctxt {
849 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
850 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
853 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
854 struct domain_join_ctxt *ctxt,
855 enum dlm_query_join_response response)
859 if (response == JOIN_DISALLOW) {
860 mlog(0, "Latest response of disallow -- should restart\n");
864 spin_lock(&dlm->spinlock);
865 /* For now, we restart the process if the node maps have
867 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
868 sizeof(dlm->live_nodes_map));
869 spin_unlock(&dlm->spinlock);
872 mlog(0, "Node maps changed -- should restart\n");
877 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
879 int status = 0, tmpstat, node;
880 struct domain_join_ctxt *ctxt;
881 enum dlm_query_join_response response;
883 mlog_entry("%p", dlm);
885 ctxt = kcalloc(1, sizeof(*ctxt), GFP_KERNEL);
892 /* group sem locking should work for us here -- we're already
893 * registered for heartbeat events so filling this should be
894 * atomic wrt getting those handlers called. */
895 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
897 spin_lock(&dlm->spinlock);
898 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
900 __dlm_set_joining_node(dlm, dlm->node_num);
902 spin_unlock(&dlm->spinlock);
905 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
906 node + 1)) < O2NM_MAX_NODES) {
907 if (node == dlm->node_num)
910 status = dlm_request_join(dlm, node, &response);
916 /* Ok, either we got a response or the node doesn't have a
918 if (response == JOIN_OK)
919 set_bit(node, ctxt->yes_resp_map);
921 if (dlm_should_restart_join(dlm, ctxt, response)) {
927 mlog(0, "Yay, done querying nodes!\n");
929 /* Yay, everyone agree's we can join the domain. My domain is
930 * comprised of all nodes who were put in the
931 * yes_resp_map. Copy that into our domain map and send a join
932 * assert message to clean up everyone elses state. */
933 spin_lock(&dlm->spinlock);
934 memcpy(dlm->domain_map, ctxt->yes_resp_map,
935 sizeof(ctxt->yes_resp_map));
936 set_bit(dlm->node_num, dlm->domain_map);
937 spin_unlock(&dlm->spinlock);
939 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
941 /* Joined state *must* be set before the joining node
942 * information, otherwise the query_join handler may read no
943 * current joiner but a state of NEW and tell joining nodes
944 * we're not in the domain. */
945 spin_lock(&dlm_domain_lock);
946 dlm->dlm_state = DLM_CTXT_JOINED;
948 spin_unlock(&dlm_domain_lock);
951 spin_lock(&dlm->spinlock);
952 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
954 __dlm_print_nodes(dlm);
955 spin_unlock(&dlm->spinlock);
958 /* Do we need to send a cancel message to any nodes? */
960 tmpstat = dlm_send_join_cancels(dlm,
962 sizeof(ctxt->yes_resp_map));
969 mlog(0, "returning %d\n", status);
973 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
975 o2hb_unregister_callback(&dlm->dlm_hb_up);
976 o2hb_unregister_callback(&dlm->dlm_hb_down);
977 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
980 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
984 mlog(0, "registering handlers.\n");
986 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
987 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
988 status = o2hb_register_callback(&dlm->dlm_hb_down);
992 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
993 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
994 status = o2hb_register_callback(&dlm->dlm_hb_up);
998 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
999 sizeof(struct dlm_master_request),
1000 dlm_master_request_handler,
1001 dlm, &dlm->dlm_domain_handlers);
1005 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1006 sizeof(struct dlm_assert_master),
1007 dlm_assert_master_handler,
1008 dlm, &dlm->dlm_domain_handlers);
1012 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1013 sizeof(struct dlm_create_lock),
1014 dlm_create_lock_handler,
1015 dlm, &dlm->dlm_domain_handlers);
1019 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1020 DLM_CONVERT_LOCK_MAX_LEN,
1021 dlm_convert_lock_handler,
1022 dlm, &dlm->dlm_domain_handlers);
1026 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1027 DLM_UNLOCK_LOCK_MAX_LEN,
1028 dlm_unlock_lock_handler,
1029 dlm, &dlm->dlm_domain_handlers);
1033 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1034 DLM_PROXY_AST_MAX_LEN,
1035 dlm_proxy_ast_handler,
1036 dlm, &dlm->dlm_domain_handlers);
1040 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1041 sizeof(struct dlm_exit_domain),
1042 dlm_exit_domain_handler,
1043 dlm, &dlm->dlm_domain_handlers);
1047 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1048 sizeof(struct dlm_migrate_request),
1049 dlm_migrate_request_handler,
1050 dlm, &dlm->dlm_domain_handlers);
1054 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1055 DLM_MIG_LOCKRES_MAX_LEN,
1056 dlm_mig_lockres_handler,
1057 dlm, &dlm->dlm_domain_handlers);
1061 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1062 sizeof(struct dlm_master_requery),
1063 dlm_master_requery_handler,
1064 dlm, &dlm->dlm_domain_handlers);
1068 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1069 sizeof(struct dlm_lock_request),
1070 dlm_request_all_locks_handler,
1071 dlm, &dlm->dlm_domain_handlers);
1075 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1076 sizeof(struct dlm_reco_data_done),
1077 dlm_reco_data_done_handler,
1078 dlm, &dlm->dlm_domain_handlers);
1082 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1083 sizeof(struct dlm_begin_reco),
1084 dlm_begin_reco_handler,
1085 dlm, &dlm->dlm_domain_handlers);
1089 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1090 sizeof(struct dlm_finalize_reco),
1091 dlm_finalize_reco_handler,
1092 dlm, &dlm->dlm_domain_handlers);
1098 dlm_unregister_domain_handlers(dlm);
1103 static int dlm_join_domain(struct dlm_ctxt *dlm)
1109 mlog(0, "Join domain %s\n", dlm->name);
1111 status = dlm_register_domain_handlers(dlm);
1117 status = dlm_launch_thread(dlm);
1123 status = dlm_launch_recovery_thread(dlm);
1130 unsigned int backoff;
1131 status = dlm_try_to_join_domain(dlm);
1133 /* If we're racing another node to the join, then we
1134 * need to back off temporarily and let them
1136 if (status == -EAGAIN) {
1137 if (signal_pending(current)) {
1138 status = -ERESTARTSYS;
1144 * <dale> No, after you!
1146 * <dale> But you first!
1149 backoff = (unsigned int)(jiffies & 0x3);
1150 backoff *= DLM_DOMAIN_BACKOFF_MS;
1151 mlog(0, "backoff %d\n", backoff);
1154 } while (status == -EAGAIN);
1163 wake_up(&dlm_domain_events);
1166 dlm_unregister_domain_handlers(dlm);
1167 dlm_complete_thread(dlm);
1168 dlm_complete_recovery_thread(dlm);
1174 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1178 struct dlm_ctxt *dlm = NULL;
1180 dlm = kcalloc(1, sizeof(*dlm), GFP_KERNEL);
1182 mlog_errno(-ENOMEM);
1186 dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1187 if (dlm->name == NULL) {
1188 mlog_errno(-ENOMEM);
1194 dlm->lockres_hash = (struct hlist_head *) __get_free_page(GFP_KERNEL);
1195 if (!dlm->lockres_hash) {
1196 mlog_errno(-ENOMEM);
1203 for (i=0; i<DLM_HASH_BUCKETS; i++)
1204 INIT_HLIST_HEAD(&dlm->lockres_hash[i]);
1206 strcpy(dlm->name, domain);
1208 dlm->node_num = o2nm_this_node();
1210 spin_lock_init(&dlm->spinlock);
1211 spin_lock_init(&dlm->master_lock);
1212 spin_lock_init(&dlm->ast_lock);
1213 INIT_LIST_HEAD(&dlm->list);
1214 INIT_LIST_HEAD(&dlm->dirty_list);
1215 INIT_LIST_HEAD(&dlm->reco.resources);
1216 INIT_LIST_HEAD(&dlm->reco.received);
1217 INIT_LIST_HEAD(&dlm->reco.node_data);
1218 INIT_LIST_HEAD(&dlm->purge_list);
1219 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1220 dlm->reco.state = 0;
1222 INIT_LIST_HEAD(&dlm->pending_asts);
1223 INIT_LIST_HEAD(&dlm->pending_basts);
1225 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1226 dlm->recovery_map, &(dlm->recovery_map[0]));
1228 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1229 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1230 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1232 dlm->dlm_thread_task = NULL;
1233 dlm->dlm_reco_thread_task = NULL;
1234 init_waitqueue_head(&dlm->dlm_thread_wq);
1235 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1236 init_waitqueue_head(&dlm->reco.event);
1237 init_waitqueue_head(&dlm->ast_wq);
1238 init_waitqueue_head(&dlm->migration_wq);
1239 INIT_LIST_HEAD(&dlm->master_list);
1240 INIT_LIST_HEAD(&dlm->mle_hb_events);
1242 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1243 init_waitqueue_head(&dlm->dlm_join_events);
1245 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1246 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1247 atomic_set(&dlm->local_resources, 0);
1248 atomic_set(&dlm->remote_resources, 0);
1249 atomic_set(&dlm->unknown_resources, 0);
1251 spin_lock_init(&dlm->work_lock);
1252 INIT_LIST_HEAD(&dlm->work_list);
1253 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work, dlm);
1255 kref_init(&dlm->dlm_refs);
1256 dlm->dlm_state = DLM_CTXT_NEW;
1258 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1260 mlog(0, "context init: refcount %u\n",
1261 atomic_read(&dlm->dlm_refs.refcount));
1268 * dlm_register_domain: one-time setup per "domain"
1270 struct dlm_ctxt * dlm_register_domain(const char *domain,
1274 struct dlm_ctxt *dlm = NULL;
1275 struct dlm_ctxt *new_ctxt = NULL;
1277 if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1278 ret = -ENAMETOOLONG;
1279 mlog(ML_ERROR, "domain name length too long\n");
1283 if (!o2hb_check_local_node_heartbeating()) {
1284 mlog(ML_ERROR, "the local node has not been configured, or is "
1285 "not heartbeating\n");
1290 mlog(0, "register called for domain \"%s\"\n", domain);
1294 if (signal_pending(current)) {
1300 spin_lock(&dlm_domain_lock);
1302 dlm = __dlm_lookup_domain(domain);
1304 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1305 spin_unlock(&dlm_domain_lock);
1307 mlog(0, "This ctxt is not joined yet!\n");
1308 wait_event_interruptible(dlm_domain_events,
1309 dlm_wait_on_domain_helper(
1317 spin_unlock(&dlm_domain_lock);
1325 spin_unlock(&dlm_domain_lock);
1327 new_ctxt = dlm_alloc_ctxt(domain, key);
1336 /* a little variable switch-a-roo here... */
1340 /* add the new domain */
1341 list_add_tail(&dlm->list, &dlm_domains);
1342 spin_unlock(&dlm_domain_lock);
1344 ret = dlm_join_domain(dlm);
1354 dlm_free_ctxt_mem(new_ctxt);
1361 EXPORT_SYMBOL_GPL(dlm_register_domain);
1363 static LIST_HEAD(dlm_join_handlers);
1365 static void dlm_unregister_net_handlers(void)
1367 o2net_unregister_handler_list(&dlm_join_handlers);
1370 static int dlm_register_net_handlers(void)
1374 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1375 sizeof(struct dlm_query_join_request),
1376 dlm_query_join_handler,
1377 NULL, &dlm_join_handlers);
1381 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1382 sizeof(struct dlm_assert_joined),
1383 dlm_assert_joined_handler,
1384 NULL, &dlm_join_handlers);
1388 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1389 sizeof(struct dlm_cancel_join),
1390 dlm_cancel_join_handler,
1391 NULL, &dlm_join_handlers);
1395 dlm_unregister_net_handlers();
1400 /* Domain eviction callback handling.
1402 * The file system requires notification of node death *before* the
1403 * dlm completes it's recovery work, otherwise it may be able to
1404 * acquire locks on resources requiring recovery. Since the dlm can
1405 * evict a node from it's domain *before* heartbeat fires, a similar
1406 * mechanism is required. */
1408 /* Eviction is not expected to happen often, so a per-domain lock is
1409 * not necessary. Eviction callbacks are allowed to sleep for short
1410 * periods of time. */
1411 static DECLARE_RWSEM(dlm_callback_sem);
1413 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1416 struct list_head *iter;
1417 struct dlm_eviction_cb *cb;
1419 down_read(&dlm_callback_sem);
1420 list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1421 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1423 cb->ec_func(node_num, cb->ec_data);
1425 up_read(&dlm_callback_sem);
1428 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1429 dlm_eviction_func *f,
1432 INIT_LIST_HEAD(&cb->ec_item);
1436 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1438 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1439 struct dlm_eviction_cb *cb)
1441 down_write(&dlm_callback_sem);
1442 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1443 up_write(&dlm_callback_sem);
1445 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1447 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1449 down_write(&dlm_callback_sem);
1450 list_del_init(&cb->ec_item);
1451 up_write(&dlm_callback_sem);
1453 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1455 static int __init dlm_init(void)
1459 dlm_print_version();
1461 status = dlm_init_mle_cache();
1465 status = dlm_register_net_handlers();
1467 dlm_destroy_mle_cache();
1474 static void __exit dlm_exit (void)
1476 dlm_unregister_net_handlers();
1477 dlm_destroy_mle_cache();
1480 MODULE_AUTHOR("Oracle");
1481 MODULE_LICENSE("GPL");
1483 module_init(dlm_init);
1484 module_exit(dlm_exit);