Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[sfrench/cifs-2.6.git] / fs / ocfs2 / dlm / dlmdomain.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmdomain.c
5  *
6  * defines domain join / leave apis
7  *
8  * Copyright (C) 2004 Oracle.  All rights reserved.
9  *
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.
14  *
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.
19  *
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.
24  *
25  */
26
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>
36
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
40
41 #include "dlmapi.h"
42 #include "dlmcommon.h"
43
44 #include "dlmdomain.h"
45
46 #include "dlmver.h"
47
48 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
49 #include "cluster/masklog.h"
50
51 static void dlm_free_pagevec(void **vec, int pages)
52 {
53         while (pages--)
54                 free_page((unsigned long)vec[pages]);
55         kfree(vec);
56 }
57
58 static void **dlm_alloc_pagevec(int pages)
59 {
60         void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
61         int i;
62
63         if (!vec)
64                 return NULL;
65
66         for (i = 0; i < pages; i++)
67                 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
68                         goto out_free;
69
70         mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
71              pages, (unsigned long)DLM_HASH_PAGES,
72              (unsigned long)DLM_BUCKETS_PER_PAGE);
73         return vec;
74 out_free:
75         dlm_free_pagevec(vec, i);
76         return NULL;
77 }
78
79 /*
80  *
81  * spinlock lock ordering: if multiple locks are needed, obey this ordering:
82  *    dlm_domain_lock
83  *    struct dlm_ctxt->spinlock
84  *    struct dlm_lock_resource->spinlock
85  *    struct dlm_ctxt->master_lock
86  *    struct dlm_ctxt->ast_lock
87  *    dlm_master_list_entry->spinlock
88  *    dlm_lock->spinlock
89  *
90  */
91
92 DEFINE_SPINLOCK(dlm_domain_lock);
93 LIST_HEAD(dlm_domains);
94 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
95
96 #define DLM_DOMAIN_BACKOFF_MS 200
97
98 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data);
99 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data);
100 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data);
101 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data);
102
103 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
104
105 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
106 {
107         hlist_del_init(&lockres->hash_node);
108         dlm_lockres_put(lockres);
109 }
110
111 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
112                        struct dlm_lock_resource *res)
113 {
114         struct hlist_head *bucket;
115         struct qstr *q;
116
117         assert_spin_locked(&dlm->spinlock);
118
119         q = &res->lockname;
120         bucket = dlm_lockres_hash(dlm, q->hash);
121
122         /* get a reference for our hashtable */
123         dlm_lockres_get(res);
124
125         hlist_add_head(&res->hash_node, bucket);
126 }
127
128 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
129                                                 const char *name,
130                                                 unsigned int len,
131                                                 unsigned int hash)
132 {
133         struct hlist_head *bucket;
134         struct hlist_node *list;
135
136         mlog_entry("%.*s\n", len, name);
137
138         assert_spin_locked(&dlm->spinlock);
139
140         bucket = dlm_lockres_hash(dlm, hash);
141
142         hlist_for_each(list, bucket) {
143                 struct dlm_lock_resource *res = hlist_entry(list,
144                         struct dlm_lock_resource, hash_node);
145                 if (res->lockname.name[0] != name[0])
146                         continue;
147                 if (unlikely(res->lockname.len != len))
148                         continue;
149                 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
150                         continue;
151                 dlm_lockres_get(res);
152                 return res;
153         }
154         return NULL;
155 }
156
157 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
158                                     const char *name,
159                                     unsigned int len)
160 {
161         struct dlm_lock_resource *res;
162         unsigned int hash = dlm_lockid_hash(name, len);
163
164         spin_lock(&dlm->spinlock);
165         res = __dlm_lookup_lockres(dlm, name, len, hash);
166         spin_unlock(&dlm->spinlock);
167         return res;
168 }
169
170 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
171 {
172         struct dlm_ctxt *tmp = NULL;
173         struct list_head *iter;
174
175         assert_spin_locked(&dlm_domain_lock);
176
177         /* tmp->name here is always NULL terminated,
178          * but domain may not be! */
179         list_for_each(iter, &dlm_domains) {
180                 tmp = list_entry (iter, struct dlm_ctxt, list);
181                 if (strlen(tmp->name) == len &&
182                     memcmp(tmp->name, domain, len)==0)
183                         break;
184                 tmp = NULL;
185         }
186
187         return tmp;
188 }
189
190 /* For null terminated domain strings ONLY */
191 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
192 {
193         assert_spin_locked(&dlm_domain_lock);
194
195         return __dlm_lookup_domain_full(domain, strlen(domain));
196 }
197
198
199 /* returns true on one of two conditions:
200  * 1) the domain does not exist
201  * 2) the domain exists and it's state is "joined" */
202 static int dlm_wait_on_domain_helper(const char *domain)
203 {
204         int ret = 0;
205         struct dlm_ctxt *tmp = NULL;
206
207         spin_lock(&dlm_domain_lock);
208
209         tmp = __dlm_lookup_domain(domain);
210         if (!tmp)
211                 ret = 1;
212         else if (tmp->dlm_state == DLM_CTXT_JOINED)
213                 ret = 1;
214
215         spin_unlock(&dlm_domain_lock);
216         return ret;
217 }
218
219 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
220 {
221         if (dlm->lockres_hash)
222                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
223
224         if (dlm->name)
225                 kfree(dlm->name);
226
227         kfree(dlm);
228 }
229
230 /* A little strange - this function will be called while holding
231  * dlm_domain_lock and is expected to be holding it on the way out. We
232  * will however drop and reacquire it multiple times */
233 static void dlm_ctxt_release(struct kref *kref)
234 {
235         struct dlm_ctxt *dlm;
236
237         dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
238
239         BUG_ON(dlm->num_joins);
240         BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
241
242         /* we may still be in the list if we hit an error during join. */
243         list_del_init(&dlm->list);
244
245         spin_unlock(&dlm_domain_lock);
246
247         mlog(0, "freeing memory from domain %s\n", dlm->name);
248
249         wake_up(&dlm_domain_events);
250
251         dlm_free_ctxt_mem(dlm);
252
253         spin_lock(&dlm_domain_lock);
254 }
255
256 void dlm_put(struct dlm_ctxt *dlm)
257 {
258         spin_lock(&dlm_domain_lock);
259         kref_put(&dlm->dlm_refs, dlm_ctxt_release);
260         spin_unlock(&dlm_domain_lock);
261 }
262
263 static void __dlm_get(struct dlm_ctxt *dlm)
264 {
265         kref_get(&dlm->dlm_refs);
266 }
267
268 /* given a questionable reference to a dlm object, gets a reference if
269  * it can find it in the list, otherwise returns NULL in which case
270  * you shouldn't trust your pointer. */
271 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
272 {
273         struct list_head *iter;
274         struct dlm_ctxt *target = NULL;
275
276         spin_lock(&dlm_domain_lock);
277
278         list_for_each(iter, &dlm_domains) {
279                 target = list_entry (iter, struct dlm_ctxt, list);
280
281                 if (target == dlm) {
282                         __dlm_get(target);
283                         break;
284                 }
285
286                 target = NULL;
287         }
288
289         spin_unlock(&dlm_domain_lock);
290
291         return target;
292 }
293
294 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
295 {
296         int ret;
297
298         spin_lock(&dlm_domain_lock);
299         ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
300                 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
301         spin_unlock(&dlm_domain_lock);
302
303         return ret;
304 }
305
306 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
307 {
308         if (dlm->dlm_worker) {
309                 flush_workqueue(dlm->dlm_worker);
310                 destroy_workqueue(dlm->dlm_worker);
311                 dlm->dlm_worker = NULL;
312         }
313 }
314
315 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
316 {
317         dlm_unregister_domain_handlers(dlm);
318         dlm_complete_thread(dlm);
319         dlm_complete_recovery_thread(dlm);
320         dlm_destroy_dlm_worker(dlm);
321
322         /* We've left the domain. Now we can take ourselves out of the
323          * list and allow the kref stuff to help us free the
324          * memory. */
325         spin_lock(&dlm_domain_lock);
326         list_del_init(&dlm->list);
327         spin_unlock(&dlm_domain_lock);
328
329         /* Wake up anyone waiting for us to remove this domain */
330         wake_up(&dlm_domain_events);
331 }
332
333 static void dlm_migrate_all_locks(struct dlm_ctxt *dlm)
334 {
335         int i;
336         struct dlm_lock_resource *res;
337
338         mlog(0, "Migrating locks from domain %s\n", dlm->name);
339 restart:
340         spin_lock(&dlm->spinlock);
341         for (i = 0; i < DLM_HASH_BUCKETS; i++) {
342                 while (!hlist_empty(dlm_lockres_hash(dlm, i))) {
343                         res = hlist_entry(dlm_lockres_hash(dlm, i)->first,
344                                           struct dlm_lock_resource, hash_node);
345                         /* need reference when manually grabbing lockres */
346                         dlm_lockres_get(res);
347                         /* this should unhash the lockres
348                          * and exit with dlm->spinlock */
349                         mlog(0, "purging res=%p\n", res);
350                         if (dlm_lockres_is_dirty(dlm, res)) {
351                                 /* HACK!  this should absolutely go.
352                                  * need to figure out why some empty
353                                  * lockreses are still marked dirty */
354                                 mlog(ML_ERROR, "lockres %.*s dirty!\n",
355                                      res->lockname.len, res->lockname.name);
356
357                                 spin_unlock(&dlm->spinlock);
358                                 dlm_kick_thread(dlm, res);
359                                 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
360                                 dlm_lockres_put(res);
361                                 goto restart;
362                         }
363                         dlm_purge_lockres(dlm, res);
364                         dlm_lockres_put(res);
365                 }
366         }
367         spin_unlock(&dlm->spinlock);
368
369         mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
370 }
371
372 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
373 {
374         int ret;
375
376         spin_lock(&dlm->spinlock);
377         ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
378         spin_unlock(&dlm->spinlock);
379
380         return ret;
381 }
382
383 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
384 {
385         /* Yikes, a double spinlock! I need domain_lock for the dlm
386          * state and the dlm spinlock for join state... Sorry! */
387 again:
388         spin_lock(&dlm_domain_lock);
389         spin_lock(&dlm->spinlock);
390
391         if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
392                 mlog(0, "Node %d is joining, we wait on it.\n",
393                           dlm->joining_node);
394                 spin_unlock(&dlm->spinlock);
395                 spin_unlock(&dlm_domain_lock);
396
397                 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
398                 goto again;
399         }
400
401         dlm->dlm_state = DLM_CTXT_LEAVING;
402         spin_unlock(&dlm->spinlock);
403         spin_unlock(&dlm_domain_lock);
404 }
405
406 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
407 {
408         int node = -1;
409
410         assert_spin_locked(&dlm->spinlock);
411
412         printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
413
414         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
415                                      node + 1)) < O2NM_MAX_NODES) {
416                 printk("%d ", node);
417         }
418         printk("\n");
419 }
420
421 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data)
422 {
423         struct dlm_ctxt *dlm = data;
424         unsigned int node;
425         struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
426
427         mlog_entry("%p %u %p", msg, len, data);
428
429         if (!dlm_grab(dlm))
430                 return 0;
431
432         node = exit_msg->node_idx;
433
434         printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
435
436         spin_lock(&dlm->spinlock);
437         clear_bit(node, dlm->domain_map);
438         __dlm_print_nodes(dlm);
439
440         /* notify anything attached to the heartbeat events */
441         dlm_hb_event_notify_attached(dlm, node, 0);
442
443         spin_unlock(&dlm->spinlock);
444
445         dlm_put(dlm);
446
447         return 0;
448 }
449
450 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
451                                     unsigned int node)
452 {
453         int status;
454         struct dlm_exit_domain leave_msg;
455
456         mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
457                   node, dlm->name, dlm->node_num);
458
459         memset(&leave_msg, 0, sizeof(leave_msg));
460         leave_msg.node_idx = dlm->node_num;
461
462         status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
463                                     &leave_msg, sizeof(leave_msg), node,
464                                     NULL);
465
466         mlog(0, "status return %d from o2net_send_message\n", status);
467
468         return status;
469 }
470
471
472 static void dlm_leave_domain(struct dlm_ctxt *dlm)
473 {
474         int node, clear_node, status;
475
476         /* At this point we've migrated away all our locks and won't
477          * accept mastership of new ones. The dlm is responsible for
478          * almost nothing now. We make sure not to confuse any joining
479          * nodes and then commence shutdown procedure. */
480
481         spin_lock(&dlm->spinlock);
482         /* Clear ourselves from the domain map */
483         clear_bit(dlm->node_num, dlm->domain_map);
484         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
485                                      0)) < O2NM_MAX_NODES) {
486                 /* Drop the dlm spinlock. This is safe wrt the domain_map.
487                  * -nodes cannot be added now as the
488                  *   query_join_handlers knows to respond with OK_NO_MAP
489                  * -we catch the right network errors if a node is
490                  *   removed from the map while we're sending him the
491                  *   exit message. */
492                 spin_unlock(&dlm->spinlock);
493
494                 clear_node = 1;
495
496                 status = dlm_send_one_domain_exit(dlm, node);
497                 if (status < 0 &&
498                     status != -ENOPROTOOPT &&
499                     status != -ENOTCONN) {
500                         mlog(ML_NOTICE, "Error %d sending domain exit message "
501                              "to node %d\n", status, node);
502
503                         /* Not sure what to do here but lets sleep for
504                          * a bit in case this was a transient
505                          * error... */
506                         msleep(DLM_DOMAIN_BACKOFF_MS);
507                         clear_node = 0;
508                 }
509
510                 spin_lock(&dlm->spinlock);
511                 /* If we're not clearing the node bit then we intend
512                  * to loop back around to try again. */
513                 if (clear_node)
514                         clear_bit(node, dlm->domain_map);
515         }
516         spin_unlock(&dlm->spinlock);
517 }
518
519 int dlm_joined(struct dlm_ctxt *dlm)
520 {
521         int ret = 0;
522
523         spin_lock(&dlm_domain_lock);
524
525         if (dlm->dlm_state == DLM_CTXT_JOINED)
526                 ret = 1;
527
528         spin_unlock(&dlm_domain_lock);
529
530         return ret;
531 }
532
533 int dlm_shutting_down(struct dlm_ctxt *dlm)
534 {
535         int ret = 0;
536
537         spin_lock(&dlm_domain_lock);
538
539         if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
540                 ret = 1;
541
542         spin_unlock(&dlm_domain_lock);
543
544         return ret;
545 }
546
547 void dlm_unregister_domain(struct dlm_ctxt *dlm)
548 {
549         int leave = 0;
550
551         spin_lock(&dlm_domain_lock);
552         BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
553         BUG_ON(!dlm->num_joins);
554
555         dlm->num_joins--;
556         if (!dlm->num_joins) {
557                 /* We mark it "in shutdown" now so new register
558                  * requests wait until we've completely left the
559                  * domain. Don't use DLM_CTXT_LEAVING yet as we still
560                  * want new domain joins to communicate with us at
561                  * least until we've completed migration of our
562                  * resources. */
563                 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
564                 leave = 1;
565         }
566         spin_unlock(&dlm_domain_lock);
567
568         if (leave) {
569                 mlog(0, "shutting down domain %s\n", dlm->name);
570
571                 /* We changed dlm state, notify the thread */
572                 dlm_kick_thread(dlm, NULL);
573
574                 dlm_migrate_all_locks(dlm);
575                 dlm_mark_domain_leaving(dlm);
576                 dlm_leave_domain(dlm);
577                 dlm_complete_dlm_shutdown(dlm);
578         }
579         dlm_put(dlm);
580 }
581 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
582
583 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data)
584 {
585         struct dlm_query_join_request *query;
586         enum dlm_query_join_response response;
587         struct dlm_ctxt *dlm = NULL;
588
589         query = (struct dlm_query_join_request *) msg->buf;
590
591         mlog(0, "node %u wants to join domain %s\n", query->node_idx,
592                   query->domain);
593
594         /*
595          * If heartbeat doesn't consider the node live, tell it
596          * to back off and try again.  This gives heartbeat a chance
597          * to catch up.
598          */
599         if (!o2hb_check_node_heartbeating(query->node_idx)) {
600                 mlog(0, "node %u is not in our live map yet\n",
601                      query->node_idx);
602
603                 response = JOIN_DISALLOW;
604                 goto respond;
605         }
606
607         response = JOIN_OK_NO_MAP;
608
609         spin_lock(&dlm_domain_lock);
610         dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
611         /* Once the dlm ctxt is marked as leaving then we don't want
612          * to be put in someone's domain map. 
613          * Also, explicitly disallow joining at certain troublesome
614          * times (ie. during recovery). */
615         if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
616                 int bit = query->node_idx;
617                 spin_lock(&dlm->spinlock);
618
619                 if (dlm->dlm_state == DLM_CTXT_NEW &&
620                     dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
621                         /*If this is a brand new context and we
622                          * haven't started our join process yet, then
623                          * the other node won the race. */
624                         response = JOIN_OK_NO_MAP;
625                 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
626                         /* Disallow parallel joins. */
627                         response = JOIN_DISALLOW;
628                 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
629                         mlog(ML_NOTICE, "node %u trying to join, but recovery "
630                              "is ongoing.\n", bit);
631                         response = JOIN_DISALLOW;
632                 } else if (test_bit(bit, dlm->recovery_map)) {
633                         mlog(ML_NOTICE, "node %u trying to join, but it "
634                              "still needs recovery.\n", bit);
635                         response = JOIN_DISALLOW;
636                 } else if (test_bit(bit, dlm->domain_map)) {
637                         mlog(ML_NOTICE, "node %u trying to join, but it "
638                              "is still in the domain! needs recovery?\n",
639                              bit);
640                         response = JOIN_DISALLOW;
641                 } else {
642                         /* Alright we're fully a part of this domain
643                          * so we keep some state as to who's joining
644                          * and indicate to him that needs to be fixed
645                          * up. */
646                         response = JOIN_OK;
647                         __dlm_set_joining_node(dlm, query->node_idx);
648                 }
649
650                 spin_unlock(&dlm->spinlock);
651         }
652         spin_unlock(&dlm_domain_lock);
653
654 respond:
655         mlog(0, "We respond with %u\n", response);
656
657         return response;
658 }
659
660 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
661 {
662         struct dlm_assert_joined *assert;
663         struct dlm_ctxt *dlm = NULL;
664
665         assert = (struct dlm_assert_joined *) msg->buf;
666
667         mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
668                   assert->domain);
669
670         spin_lock(&dlm_domain_lock);
671         dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
672         /* XXX should we consider no dlm ctxt an error? */
673         if (dlm) {
674                 spin_lock(&dlm->spinlock);
675
676                 /* Alright, this node has officially joined our
677                  * domain. Set him in the map and clean up our
678                  * leftover join state. */
679                 BUG_ON(dlm->joining_node != assert->node_idx);
680                 set_bit(assert->node_idx, dlm->domain_map);
681                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
682
683                 printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
684                        assert->node_idx, dlm->name);
685                 __dlm_print_nodes(dlm);
686
687                 /* notify anything attached to the heartbeat events */
688                 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
689
690                 spin_unlock(&dlm->spinlock);
691         }
692         spin_unlock(&dlm_domain_lock);
693
694         return 0;
695 }
696
697 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
698 {
699         struct dlm_cancel_join *cancel;
700         struct dlm_ctxt *dlm = NULL;
701
702         cancel = (struct dlm_cancel_join *) msg->buf;
703
704         mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
705                   cancel->domain);
706
707         spin_lock(&dlm_domain_lock);
708         dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
709
710         if (dlm) {
711                 spin_lock(&dlm->spinlock);
712
713                 /* Yikes, this guy wants to cancel his join. No
714                  * problem, we simply cleanup our join state. */
715                 BUG_ON(dlm->joining_node != cancel->node_idx);
716                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
717
718                 spin_unlock(&dlm->spinlock);
719         }
720         spin_unlock(&dlm_domain_lock);
721
722         return 0;
723 }
724
725 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
726                                     unsigned int node)
727 {
728         int status;
729         struct dlm_cancel_join cancel_msg;
730
731         memset(&cancel_msg, 0, sizeof(cancel_msg));
732         cancel_msg.node_idx = dlm->node_num;
733         cancel_msg.name_len = strlen(dlm->name);
734         memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
735
736         status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
737                                     &cancel_msg, sizeof(cancel_msg), node,
738                                     NULL);
739         if (status < 0) {
740                 mlog_errno(status);
741                 goto bail;
742         }
743
744 bail:
745         return status;
746 }
747
748 /* map_size should be in bytes. */
749 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
750                                  unsigned long *node_map,
751                                  unsigned int map_size)
752 {
753         int status, tmpstat;
754         unsigned int node;
755
756         if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
757                          sizeof(unsigned long))) {
758                 mlog(ML_ERROR,
759                      "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
760                      map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
761                 return -EINVAL;
762         }
763
764         status = 0;
765         node = -1;
766         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
767                                      node + 1)) < O2NM_MAX_NODES) {
768                 if (node == dlm->node_num)
769                         continue;
770
771                 tmpstat = dlm_send_one_join_cancel(dlm, node);
772                 if (tmpstat) {
773                         mlog(ML_ERROR, "Error return %d cancelling join on "
774                              "node %d\n", tmpstat, node);
775                         if (!status)
776                                 status = tmpstat;
777                 }
778         }
779
780         if (status)
781                 mlog_errno(status);
782         return status;
783 }
784
785 static int dlm_request_join(struct dlm_ctxt *dlm,
786                             int node,
787                             enum dlm_query_join_response *response)
788 {
789         int status, retval;
790         struct dlm_query_join_request join_msg;
791
792         mlog(0, "querying node %d\n", node);
793
794         memset(&join_msg, 0, sizeof(join_msg));
795         join_msg.node_idx = dlm->node_num;
796         join_msg.name_len = strlen(dlm->name);
797         memcpy(join_msg.domain, dlm->name, join_msg.name_len);
798
799         status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
800                                     sizeof(join_msg), node, &retval);
801         if (status < 0 && status != -ENOPROTOOPT) {
802                 mlog_errno(status);
803                 goto bail;
804         }
805
806         /* -ENOPROTOOPT from the net code means the other side isn't
807             listening for our message type -- that's fine, it means
808             his dlm isn't up, so we can consider him a 'yes' but not
809             joined into the domain.  */
810         if (status == -ENOPROTOOPT) {
811                 status = 0;
812                 *response = JOIN_OK_NO_MAP;
813         } else if (retval == JOIN_DISALLOW ||
814                    retval == JOIN_OK ||
815                    retval == JOIN_OK_NO_MAP) {
816                 *response = retval;
817         } else {
818                 status = -EINVAL;
819                 mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
820                      node);
821         }
822
823         mlog(0, "status %d, node %d response is %d\n", status, node,
824                   *response);
825
826 bail:
827         return status;
828 }
829
830 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
831                                     unsigned int node)
832 {
833         int status;
834         struct dlm_assert_joined assert_msg;
835
836         mlog(0, "Sending join assert to node %u\n", node);
837
838         memset(&assert_msg, 0, sizeof(assert_msg));
839         assert_msg.node_idx = dlm->node_num;
840         assert_msg.name_len = strlen(dlm->name);
841         memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
842
843         status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
844                                     &assert_msg, sizeof(assert_msg), node,
845                                     NULL);
846         if (status < 0)
847                 mlog_errno(status);
848
849         return status;
850 }
851
852 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
853                                   unsigned long *node_map)
854 {
855         int status, node, live;
856
857         status = 0;
858         node = -1;
859         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
860                                      node + 1)) < O2NM_MAX_NODES) {
861                 if (node == dlm->node_num)
862                         continue;
863
864                 do {
865                         /* It is very important that this message be
866                          * received so we spin until either the node
867                          * has died or it gets the message. */
868                         status = dlm_send_one_join_assert(dlm, node);
869
870                         spin_lock(&dlm->spinlock);
871                         live = test_bit(node, dlm->live_nodes_map);
872                         spin_unlock(&dlm->spinlock);
873
874                         if (status) {
875                                 mlog(ML_ERROR, "Error return %d asserting "
876                                      "join on node %d\n", status, node);
877
878                                 /* give us some time between errors... */
879                                 if (live)
880                                         msleep(DLM_DOMAIN_BACKOFF_MS);
881                         }
882                 } while (status && live);
883         }
884 }
885
886 struct domain_join_ctxt {
887         unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
888         unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
889 };
890
891 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
892                                    struct domain_join_ctxt *ctxt,
893                                    enum dlm_query_join_response response)
894 {
895         int ret;
896
897         if (response == JOIN_DISALLOW) {
898                 mlog(0, "Latest response of disallow -- should restart\n");
899                 return 1;
900         }
901
902         spin_lock(&dlm->spinlock);
903         /* For now, we restart the process if the node maps have
904          * changed at all */
905         ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
906                      sizeof(dlm->live_nodes_map));
907         spin_unlock(&dlm->spinlock);
908
909         if (ret)
910                 mlog(0, "Node maps changed -- should restart\n");
911
912         return ret;
913 }
914
915 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
916 {
917         int status = 0, tmpstat, node;
918         struct domain_join_ctxt *ctxt;
919         enum dlm_query_join_response response;
920
921         mlog_entry("%p", dlm);
922
923         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
924         if (!ctxt) {
925                 status = -ENOMEM;
926                 mlog_errno(status);
927                 goto bail;
928         }
929
930         /* group sem locking should work for us here -- we're already
931          * registered for heartbeat events so filling this should be
932          * atomic wrt getting those handlers called. */
933         o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
934
935         spin_lock(&dlm->spinlock);
936         memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
937
938         __dlm_set_joining_node(dlm, dlm->node_num);
939
940         spin_unlock(&dlm->spinlock);
941
942         node = -1;
943         while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
944                                      node + 1)) < O2NM_MAX_NODES) {
945                 if (node == dlm->node_num)
946                         continue;
947
948                 status = dlm_request_join(dlm, node, &response);
949                 if (status < 0) {
950                         mlog_errno(status);
951                         goto bail;
952                 }
953
954                 /* Ok, either we got a response or the node doesn't have a
955                  * dlm up. */
956                 if (response == JOIN_OK)
957                         set_bit(node, ctxt->yes_resp_map);
958
959                 if (dlm_should_restart_join(dlm, ctxt, response)) {
960                         status = -EAGAIN;
961                         goto bail;
962                 }
963         }
964
965         mlog(0, "Yay, done querying nodes!\n");
966
967         /* Yay, everyone agree's we can join the domain. My domain is
968          * comprised of all nodes who were put in the
969          * yes_resp_map. Copy that into our domain map and send a join
970          * assert message to clean up everyone elses state. */
971         spin_lock(&dlm->spinlock);
972         memcpy(dlm->domain_map, ctxt->yes_resp_map,
973                sizeof(ctxt->yes_resp_map));
974         set_bit(dlm->node_num, dlm->domain_map);
975         spin_unlock(&dlm->spinlock);
976
977         dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
978
979         /* Joined state *must* be set before the joining node
980          * information, otherwise the query_join handler may read no
981          * current joiner but a state of NEW and tell joining nodes
982          * we're not in the domain. */
983         spin_lock(&dlm_domain_lock);
984         dlm->dlm_state = DLM_CTXT_JOINED;
985         dlm->num_joins++;
986         spin_unlock(&dlm_domain_lock);
987
988 bail:
989         spin_lock(&dlm->spinlock);
990         __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
991         if (!status)
992                 __dlm_print_nodes(dlm);
993         spin_unlock(&dlm->spinlock);
994
995         if (ctxt) {
996                 /* Do we need to send a cancel message to any nodes? */
997                 if (status < 0) {
998                         tmpstat = dlm_send_join_cancels(dlm,
999                                                         ctxt->yes_resp_map,
1000                                                         sizeof(ctxt->yes_resp_map));
1001                         if (tmpstat < 0)
1002                                 mlog_errno(tmpstat);
1003                 }
1004                 kfree(ctxt);
1005         }
1006
1007         mlog(0, "returning %d\n", status);
1008         return status;
1009 }
1010
1011 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1012 {
1013         o2hb_unregister_callback(&dlm->dlm_hb_up);
1014         o2hb_unregister_callback(&dlm->dlm_hb_down);
1015         o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1016 }
1017
1018 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1019 {
1020         int status;
1021
1022         mlog(0, "registering handlers.\n");
1023
1024         o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1025                             dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1026         status = o2hb_register_callback(&dlm->dlm_hb_down);
1027         if (status)
1028                 goto bail;
1029
1030         o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1031                             dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1032         status = o2hb_register_callback(&dlm->dlm_hb_up);
1033         if (status)
1034                 goto bail;
1035
1036         status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1037                                         sizeof(struct dlm_master_request),
1038                                         dlm_master_request_handler,
1039                                         dlm, &dlm->dlm_domain_handlers);
1040         if (status)
1041                 goto bail;
1042
1043         status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1044                                         sizeof(struct dlm_assert_master),
1045                                         dlm_assert_master_handler,
1046                                         dlm, &dlm->dlm_domain_handlers);
1047         if (status)
1048                 goto bail;
1049
1050         status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1051                                         sizeof(struct dlm_create_lock),
1052                                         dlm_create_lock_handler,
1053                                         dlm, &dlm->dlm_domain_handlers);
1054         if (status)
1055                 goto bail;
1056
1057         status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1058                                         DLM_CONVERT_LOCK_MAX_LEN,
1059                                         dlm_convert_lock_handler,
1060                                         dlm, &dlm->dlm_domain_handlers);
1061         if (status)
1062                 goto bail;
1063
1064         status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1065                                         DLM_UNLOCK_LOCK_MAX_LEN,
1066                                         dlm_unlock_lock_handler,
1067                                         dlm, &dlm->dlm_domain_handlers);
1068         if (status)
1069                 goto bail;
1070
1071         status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1072                                         DLM_PROXY_AST_MAX_LEN,
1073                                         dlm_proxy_ast_handler,
1074                                         dlm, &dlm->dlm_domain_handlers);
1075         if (status)
1076                 goto bail;
1077
1078         status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1079                                         sizeof(struct dlm_exit_domain),
1080                                         dlm_exit_domain_handler,
1081                                         dlm, &dlm->dlm_domain_handlers);
1082         if (status)
1083                 goto bail;
1084
1085         status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1086                                         sizeof(struct dlm_migrate_request),
1087                                         dlm_migrate_request_handler,
1088                                         dlm, &dlm->dlm_domain_handlers);
1089         if (status)
1090                 goto bail;
1091
1092         status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1093                                         DLM_MIG_LOCKRES_MAX_LEN,
1094                                         dlm_mig_lockres_handler,
1095                                         dlm, &dlm->dlm_domain_handlers);
1096         if (status)
1097                 goto bail;
1098
1099         status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1100                                         sizeof(struct dlm_master_requery),
1101                                         dlm_master_requery_handler,
1102                                         dlm, &dlm->dlm_domain_handlers);
1103         if (status)
1104                 goto bail;
1105
1106         status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1107                                         sizeof(struct dlm_lock_request),
1108                                         dlm_request_all_locks_handler,
1109                                         dlm, &dlm->dlm_domain_handlers);
1110         if (status)
1111                 goto bail;
1112
1113         status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1114                                         sizeof(struct dlm_reco_data_done),
1115                                         dlm_reco_data_done_handler,
1116                                         dlm, &dlm->dlm_domain_handlers);
1117         if (status)
1118                 goto bail;
1119
1120         status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1121                                         sizeof(struct dlm_begin_reco),
1122                                         dlm_begin_reco_handler,
1123                                         dlm, &dlm->dlm_domain_handlers);
1124         if (status)
1125                 goto bail;
1126
1127         status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1128                                         sizeof(struct dlm_finalize_reco),
1129                                         dlm_finalize_reco_handler,
1130                                         dlm, &dlm->dlm_domain_handlers);
1131         if (status)
1132                 goto bail;
1133
1134 bail:
1135         if (status)
1136                 dlm_unregister_domain_handlers(dlm);
1137
1138         return status;
1139 }
1140
1141 static int dlm_join_domain(struct dlm_ctxt *dlm)
1142 {
1143         int status;
1144
1145         BUG_ON(!dlm);
1146
1147         mlog(0, "Join domain %s\n", dlm->name);
1148
1149         status = dlm_register_domain_handlers(dlm);
1150         if (status) {
1151                 mlog_errno(status);
1152                 goto bail;
1153         }
1154
1155         status = dlm_launch_thread(dlm);
1156         if (status < 0) {
1157                 mlog_errno(status);
1158                 goto bail;
1159         }
1160
1161         status = dlm_launch_recovery_thread(dlm);
1162         if (status < 0) {
1163                 mlog_errno(status);
1164                 goto bail;
1165         }
1166
1167         dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1168         if (!dlm->dlm_worker) {
1169                 status = -ENOMEM;
1170                 mlog_errno(status);
1171                 goto bail;
1172         }
1173
1174         do {
1175                 unsigned int backoff;
1176                 status = dlm_try_to_join_domain(dlm);
1177
1178                 /* If we're racing another node to the join, then we
1179                  * need to back off temporarily and let them
1180                  * complete. */
1181                 if (status == -EAGAIN) {
1182                         if (signal_pending(current)) {
1183                                 status = -ERESTARTSYS;
1184                                 goto bail;
1185                         }
1186
1187                         /*
1188                          * <chip> After you!
1189                          * <dale> No, after you!
1190                          * <chip> I insist!
1191                          * <dale> But you first!
1192                          * ...
1193                          */
1194                         backoff = (unsigned int)(jiffies & 0x3);
1195                         backoff *= DLM_DOMAIN_BACKOFF_MS;
1196                         mlog(0, "backoff %d\n", backoff);
1197                         msleep(backoff);
1198                 }
1199         } while (status == -EAGAIN);
1200
1201         if (status < 0) {
1202                 mlog_errno(status);
1203                 goto bail;
1204         }
1205
1206         status = 0;
1207 bail:
1208         wake_up(&dlm_domain_events);
1209
1210         if (status) {
1211                 dlm_unregister_domain_handlers(dlm);
1212                 dlm_complete_thread(dlm);
1213                 dlm_complete_recovery_thread(dlm);
1214                 dlm_destroy_dlm_worker(dlm);
1215         }
1216
1217         return status;
1218 }
1219
1220 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1221                                 u32 key)
1222 {
1223         int i;
1224         struct dlm_ctxt *dlm = NULL;
1225
1226         dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1227         if (!dlm) {
1228                 mlog_errno(-ENOMEM);
1229                 goto leave;
1230         }
1231
1232         dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1233         if (dlm->name == NULL) {
1234                 mlog_errno(-ENOMEM);
1235                 kfree(dlm);
1236                 dlm = NULL;
1237                 goto leave;
1238         }
1239
1240         dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1241         if (!dlm->lockres_hash) {
1242                 mlog_errno(-ENOMEM);
1243                 kfree(dlm->name);
1244                 kfree(dlm);
1245                 dlm = NULL;
1246                 goto leave;
1247         }
1248
1249         for (i = 0; i < DLM_HASH_BUCKETS; i++)
1250                 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1251
1252         strcpy(dlm->name, domain);
1253         dlm->key = key;
1254         dlm->node_num = o2nm_this_node();
1255
1256         spin_lock_init(&dlm->spinlock);
1257         spin_lock_init(&dlm->master_lock);
1258         spin_lock_init(&dlm->ast_lock);
1259         INIT_LIST_HEAD(&dlm->list);
1260         INIT_LIST_HEAD(&dlm->dirty_list);
1261         INIT_LIST_HEAD(&dlm->reco.resources);
1262         INIT_LIST_HEAD(&dlm->reco.received);
1263         INIT_LIST_HEAD(&dlm->reco.node_data);
1264         INIT_LIST_HEAD(&dlm->purge_list);
1265         INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1266         dlm->reco.state = 0;
1267
1268         INIT_LIST_HEAD(&dlm->pending_asts);
1269         INIT_LIST_HEAD(&dlm->pending_basts);
1270
1271         mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1272                   dlm->recovery_map, &(dlm->recovery_map[0]));
1273
1274         memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1275         memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1276         memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1277
1278         dlm->dlm_thread_task = NULL;
1279         dlm->dlm_reco_thread_task = NULL;
1280         dlm->dlm_worker = NULL;
1281         init_waitqueue_head(&dlm->dlm_thread_wq);
1282         init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1283         init_waitqueue_head(&dlm->reco.event);
1284         init_waitqueue_head(&dlm->ast_wq);
1285         init_waitqueue_head(&dlm->migration_wq);
1286         INIT_LIST_HEAD(&dlm->master_list);
1287         INIT_LIST_HEAD(&dlm->mle_hb_events);
1288
1289         dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1290         init_waitqueue_head(&dlm->dlm_join_events);
1291
1292         dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1293         dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1294         atomic_set(&dlm->local_resources, 0);
1295         atomic_set(&dlm->remote_resources, 0);
1296         atomic_set(&dlm->unknown_resources, 0);
1297
1298         spin_lock_init(&dlm->work_lock);
1299         INIT_LIST_HEAD(&dlm->work_list);
1300         INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
1301
1302         kref_init(&dlm->dlm_refs);
1303         dlm->dlm_state = DLM_CTXT_NEW;
1304
1305         INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1306
1307         mlog(0, "context init: refcount %u\n",
1308                   atomic_read(&dlm->dlm_refs.refcount));
1309
1310 leave:
1311         return dlm;
1312 }
1313
1314 /*
1315  * dlm_register_domain: one-time setup per "domain"
1316  */
1317 struct dlm_ctxt * dlm_register_domain(const char *domain,
1318                                u32 key)
1319 {
1320         int ret;
1321         struct dlm_ctxt *dlm = NULL;
1322         struct dlm_ctxt *new_ctxt = NULL;
1323
1324         if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1325                 ret = -ENAMETOOLONG;
1326                 mlog(ML_ERROR, "domain name length too long\n");
1327                 goto leave;
1328         }
1329
1330         if (!o2hb_check_local_node_heartbeating()) {
1331                 mlog(ML_ERROR, "the local node has not been configured, or is "
1332                      "not heartbeating\n");
1333                 ret = -EPROTO;
1334                 goto leave;
1335         }
1336
1337         mlog(0, "register called for domain \"%s\"\n", domain);
1338
1339 retry:
1340         dlm = NULL;
1341         if (signal_pending(current)) {
1342                 ret = -ERESTARTSYS;
1343                 mlog_errno(ret);
1344                 goto leave;
1345         }
1346
1347         spin_lock(&dlm_domain_lock);
1348
1349         dlm = __dlm_lookup_domain(domain);
1350         if (dlm) {
1351                 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1352                         spin_unlock(&dlm_domain_lock);
1353
1354                         mlog(0, "This ctxt is not joined yet!\n");
1355                         wait_event_interruptible(dlm_domain_events,
1356                                                  dlm_wait_on_domain_helper(
1357                                                          domain));
1358                         goto retry;
1359                 }
1360
1361                 __dlm_get(dlm);
1362                 dlm->num_joins++;
1363
1364                 spin_unlock(&dlm_domain_lock);
1365
1366                 ret = 0;
1367                 goto leave;
1368         }
1369
1370         /* doesn't exist */
1371         if (!new_ctxt) {
1372                 spin_unlock(&dlm_domain_lock);
1373
1374                 new_ctxt = dlm_alloc_ctxt(domain, key);
1375                 if (new_ctxt)
1376                         goto retry;
1377
1378                 ret = -ENOMEM;
1379                 mlog_errno(ret);
1380                 goto leave;
1381         }
1382
1383         /* a little variable switch-a-roo here... */
1384         dlm = new_ctxt;
1385         new_ctxt = NULL;
1386
1387         /* add the new domain */
1388         list_add_tail(&dlm->list, &dlm_domains);
1389         spin_unlock(&dlm_domain_lock);
1390
1391         ret = dlm_join_domain(dlm);
1392         if (ret) {
1393                 mlog_errno(ret);
1394                 dlm_put(dlm);
1395                 goto leave;
1396         }
1397
1398         ret = 0;
1399 leave:
1400         if (new_ctxt)
1401                 dlm_free_ctxt_mem(new_ctxt);
1402
1403         if (ret < 0)
1404                 dlm = ERR_PTR(ret);
1405
1406         return dlm;
1407 }
1408 EXPORT_SYMBOL_GPL(dlm_register_domain);
1409
1410 static LIST_HEAD(dlm_join_handlers);
1411
1412 static void dlm_unregister_net_handlers(void)
1413 {
1414         o2net_unregister_handler_list(&dlm_join_handlers);
1415 }
1416
1417 static int dlm_register_net_handlers(void)
1418 {
1419         int status = 0;
1420
1421         status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1422                                         sizeof(struct dlm_query_join_request),
1423                                         dlm_query_join_handler,
1424                                         NULL, &dlm_join_handlers);
1425         if (status)
1426                 goto bail;
1427
1428         status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1429                                         sizeof(struct dlm_assert_joined),
1430                                         dlm_assert_joined_handler,
1431                                         NULL, &dlm_join_handlers);
1432         if (status)
1433                 goto bail;
1434
1435         status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1436                                         sizeof(struct dlm_cancel_join),
1437                                         dlm_cancel_join_handler,
1438                                         NULL, &dlm_join_handlers);
1439
1440 bail:
1441         if (status < 0)
1442                 dlm_unregister_net_handlers();
1443
1444         return status;
1445 }
1446
1447 /* Domain eviction callback handling.
1448  *
1449  * The file system requires notification of node death *before* the
1450  * dlm completes it's recovery work, otherwise it may be able to
1451  * acquire locks on resources requiring recovery. Since the dlm can
1452  * evict a node from it's domain *before* heartbeat fires, a similar
1453  * mechanism is required. */
1454
1455 /* Eviction is not expected to happen often, so a per-domain lock is
1456  * not necessary. Eviction callbacks are allowed to sleep for short
1457  * periods of time. */
1458 static DECLARE_RWSEM(dlm_callback_sem);
1459
1460 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1461                                         int node_num)
1462 {
1463         struct list_head *iter;
1464         struct dlm_eviction_cb *cb;
1465
1466         down_read(&dlm_callback_sem);
1467         list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1468                 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1469
1470                 cb->ec_func(node_num, cb->ec_data);
1471         }
1472         up_read(&dlm_callback_sem);
1473 }
1474
1475 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1476                            dlm_eviction_func *f,
1477                            void *data)
1478 {
1479         INIT_LIST_HEAD(&cb->ec_item);
1480         cb->ec_func = f;
1481         cb->ec_data = data;
1482 }
1483 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1484
1485 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1486                               struct dlm_eviction_cb *cb)
1487 {
1488         down_write(&dlm_callback_sem);
1489         list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1490         up_write(&dlm_callback_sem);
1491 }
1492 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1493
1494 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1495 {
1496         down_write(&dlm_callback_sem);
1497         list_del_init(&cb->ec_item);
1498         up_write(&dlm_callback_sem);
1499 }
1500 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1501
1502 static int __init dlm_init(void)
1503 {
1504         int status;
1505
1506         dlm_print_version();
1507
1508         status = dlm_init_mle_cache();
1509         if (status)
1510                 return -1;
1511
1512         status = dlm_register_net_handlers();
1513         if (status) {
1514                 dlm_destroy_mle_cache();
1515                 return -1;
1516         }
1517
1518         return 0;
1519 }
1520
1521 static void __exit dlm_exit (void)
1522 {
1523         dlm_unregister_net_handlers();
1524         dlm_destroy_mle_cache();
1525 }
1526
1527 MODULE_AUTHOR("Oracle");
1528 MODULE_LICENSE("GPL");
1529
1530 module_init(dlm_init);
1531 module_exit(dlm_exit);