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