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