Merge branch 's5p-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / fs / ocfs2 / dlm / dlmdomain.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmdomain.c
5  *
6  * defines domain join / leave apis
7  *
8  * Copyright (C) 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  *
25  */
26
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/debugfs.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 #include "dlmdomain.h"
44 #include "dlmdebug.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 /*
127  * The supported protocol version for DLM communication.  Running domains
128  * will have a negotiated version with the same major number and a minor
129  * number equal or smaller.  The dlm_ctxt->dlm_locking_proto field should
130  * be used to determine what a running domain is actually using.
131  *
132  * New in version 1.1:
133  *      - Message DLM_QUERY_REGION added to support global heartbeat
134  *      - Message DLM_QUERY_NODEINFO added to allow online node removes
135  */
136 static const struct dlm_protocol_version dlm_protocol = {
137         .pv_major = 1,
138         .pv_minor = 1,
139 };
140
141 #define DLM_DOMAIN_BACKOFF_MS 200
142
143 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
144                                   void **ret_data);
145 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
146                                      void **ret_data);
147 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
148                                    void **ret_data);
149 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
150                                     void *data, void **ret_data);
151 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
152                                    void **ret_data);
153 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
154                                 struct dlm_protocol_version *request);
155
156 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
157
158 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
159 {
160         if (!hlist_unhashed(&lockres->hash_node)) {
161                 hlist_del_init(&lockres->hash_node);
162                 dlm_lockres_put(lockres);
163         }
164 }
165
166 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
167                        struct dlm_lock_resource *res)
168 {
169         struct hlist_head *bucket;
170         struct qstr *q;
171
172         assert_spin_locked(&dlm->spinlock);
173
174         q = &res->lockname;
175         bucket = dlm_lockres_hash(dlm, q->hash);
176
177         /* get a reference for our hashtable */
178         dlm_lockres_get(res);
179
180         hlist_add_head(&res->hash_node, bucket);
181 }
182
183 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
184                                                      const char *name,
185                                                      unsigned int len,
186                                                      unsigned int hash)
187 {
188         struct hlist_head *bucket;
189         struct hlist_node *list;
190
191         mlog(0, "%.*s\n", len, name);
192
193         assert_spin_locked(&dlm->spinlock);
194
195         bucket = dlm_lockres_hash(dlm, hash);
196
197         hlist_for_each(list, bucket) {
198                 struct dlm_lock_resource *res = hlist_entry(list,
199                         struct dlm_lock_resource, hash_node);
200                 if (res->lockname.name[0] != name[0])
201                         continue;
202                 if (unlikely(res->lockname.len != len))
203                         continue;
204                 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
205                         continue;
206                 dlm_lockres_get(res);
207                 return res;
208         }
209         return NULL;
210 }
211
212 /* intended to be called by functions which do not care about lock
213  * resources which are being purged (most net _handler functions).
214  * this will return NULL for any lock resource which is found but
215  * currently in the process of dropping its mastery reference.
216  * use __dlm_lookup_lockres_full when you need the lock resource
217  * regardless (e.g. dlm_get_lock_resource) */
218 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
219                                                 const char *name,
220                                                 unsigned int len,
221                                                 unsigned int hash)
222 {
223         struct dlm_lock_resource *res = NULL;
224
225         mlog(0, "%.*s\n", len, name);
226
227         assert_spin_locked(&dlm->spinlock);
228
229         res = __dlm_lookup_lockres_full(dlm, name, len, hash);
230         if (res) {
231                 spin_lock(&res->spinlock);
232                 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
233                         spin_unlock(&res->spinlock);
234                         dlm_lockres_put(res);
235                         return NULL;
236                 }
237                 spin_unlock(&res->spinlock);
238         }
239
240         return res;
241 }
242
243 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
244                                     const char *name,
245                                     unsigned int len)
246 {
247         struct dlm_lock_resource *res;
248         unsigned int hash = dlm_lockid_hash(name, len);
249
250         spin_lock(&dlm->spinlock);
251         res = __dlm_lookup_lockres(dlm, name, len, hash);
252         spin_unlock(&dlm->spinlock);
253         return res;
254 }
255
256 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
257 {
258         struct dlm_ctxt *tmp = NULL;
259         struct list_head *iter;
260
261         assert_spin_locked(&dlm_domain_lock);
262
263         /* tmp->name here is always NULL terminated,
264          * but domain may not be! */
265         list_for_each(iter, &dlm_domains) {
266                 tmp = list_entry (iter, struct dlm_ctxt, list);
267                 if (strlen(tmp->name) == len &&
268                     memcmp(tmp->name, domain, len)==0)
269                         break;
270                 tmp = NULL;
271         }
272
273         return tmp;
274 }
275
276 /* For null terminated domain strings ONLY */
277 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
278 {
279         assert_spin_locked(&dlm_domain_lock);
280
281         return __dlm_lookup_domain_full(domain, strlen(domain));
282 }
283
284
285 /* returns true on one of two conditions:
286  * 1) the domain does not exist
287  * 2) the domain exists and it's state is "joined" */
288 static int dlm_wait_on_domain_helper(const char *domain)
289 {
290         int ret = 0;
291         struct dlm_ctxt *tmp = NULL;
292
293         spin_lock(&dlm_domain_lock);
294
295         tmp = __dlm_lookup_domain(domain);
296         if (!tmp)
297                 ret = 1;
298         else if (tmp->dlm_state == DLM_CTXT_JOINED)
299                 ret = 1;
300
301         spin_unlock(&dlm_domain_lock);
302         return ret;
303 }
304
305 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
306 {
307         dlm_destroy_debugfs_subroot(dlm);
308
309         if (dlm->lockres_hash)
310                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
311
312         if (dlm->master_hash)
313                 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
314
315         if (dlm->name)
316                 kfree(dlm->name);
317
318         kfree(dlm);
319 }
320
321 /* A little strange - this function will be called while holding
322  * dlm_domain_lock and is expected to be holding it on the way out. We
323  * will however drop and reacquire it multiple times */
324 static void dlm_ctxt_release(struct kref *kref)
325 {
326         struct dlm_ctxt *dlm;
327
328         dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
329
330         BUG_ON(dlm->num_joins);
331         BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
332
333         /* we may still be in the list if we hit an error during join. */
334         list_del_init(&dlm->list);
335
336         spin_unlock(&dlm_domain_lock);
337
338         mlog(0, "freeing memory from domain %s\n", dlm->name);
339
340         wake_up(&dlm_domain_events);
341
342         dlm_free_ctxt_mem(dlm);
343
344         spin_lock(&dlm_domain_lock);
345 }
346
347 void dlm_put(struct dlm_ctxt *dlm)
348 {
349         spin_lock(&dlm_domain_lock);
350         kref_put(&dlm->dlm_refs, dlm_ctxt_release);
351         spin_unlock(&dlm_domain_lock);
352 }
353
354 static void __dlm_get(struct dlm_ctxt *dlm)
355 {
356         kref_get(&dlm->dlm_refs);
357 }
358
359 /* given a questionable reference to a dlm object, gets a reference if
360  * it can find it in the list, otherwise returns NULL in which case
361  * you shouldn't trust your pointer. */
362 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
363 {
364         struct list_head *iter;
365         struct dlm_ctxt *target = NULL;
366
367         spin_lock(&dlm_domain_lock);
368
369         list_for_each(iter, &dlm_domains) {
370                 target = list_entry (iter, struct dlm_ctxt, list);
371
372                 if (target == dlm) {
373                         __dlm_get(target);
374                         break;
375                 }
376
377                 target = NULL;
378         }
379
380         spin_unlock(&dlm_domain_lock);
381
382         return target;
383 }
384
385 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
386 {
387         int ret;
388
389         spin_lock(&dlm_domain_lock);
390         ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
391                 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
392         spin_unlock(&dlm_domain_lock);
393
394         return ret;
395 }
396
397 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
398 {
399         if (dlm->dlm_worker) {
400                 flush_workqueue(dlm->dlm_worker);
401                 destroy_workqueue(dlm->dlm_worker);
402                 dlm->dlm_worker = NULL;
403         }
404 }
405
406 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
407 {
408         dlm_unregister_domain_handlers(dlm);
409         dlm_debug_shutdown(dlm);
410         dlm_complete_thread(dlm);
411         dlm_complete_recovery_thread(dlm);
412         dlm_destroy_dlm_worker(dlm);
413
414         /* We've left the domain. Now we can take ourselves out of the
415          * list and allow the kref stuff to help us free the
416          * memory. */
417         spin_lock(&dlm_domain_lock);
418         list_del_init(&dlm->list);
419         spin_unlock(&dlm_domain_lock);
420
421         /* Wake up anyone waiting for us to remove this domain */
422         wake_up(&dlm_domain_events);
423 }
424
425 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
426 {
427         int i, num, n, ret = 0;
428         struct dlm_lock_resource *res;
429         struct hlist_node *iter;
430         struct hlist_head *bucket;
431         int dropped;
432
433         mlog(0, "Migrating locks from domain %s\n", dlm->name);
434
435         num = 0;
436         spin_lock(&dlm->spinlock);
437         for (i = 0; i < DLM_HASH_BUCKETS; i++) {
438 redo_bucket:
439                 n = 0;
440                 bucket = dlm_lockres_hash(dlm, i);
441                 iter = bucket->first;
442                 while (iter) {
443                         n++;
444                         res = hlist_entry(iter, struct dlm_lock_resource,
445                                           hash_node);
446                         dlm_lockres_get(res);
447                         /* migrate, if necessary.  this will drop the dlm
448                          * spinlock and retake it if it does migration. */
449                         dropped = dlm_empty_lockres(dlm, res);
450
451                         spin_lock(&res->spinlock);
452                         __dlm_lockres_calc_usage(dlm, res);
453                         iter = res->hash_node.next;
454                         spin_unlock(&res->spinlock);
455
456                         dlm_lockres_put(res);
457
458                         if (dropped)
459                                 goto redo_bucket;
460                 }
461                 cond_resched_lock(&dlm->spinlock);
462                 num += n;
463         }
464         spin_unlock(&dlm->spinlock);
465         wake_up(&dlm->dlm_thread_wq);
466
467         /* let the dlm thread take care of purging, keep scanning until
468          * nothing remains in the hash */
469         if (num) {
470                 mlog(0, "%s: %d lock resources in hash last pass\n",
471                      dlm->name, num);
472                 ret = -EAGAIN;
473         }
474         mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
475         return ret;
476 }
477
478 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
479 {
480         int ret;
481
482         spin_lock(&dlm->spinlock);
483         ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
484         spin_unlock(&dlm->spinlock);
485
486         return ret;
487 }
488
489 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
490 {
491         /* Yikes, a double spinlock! I need domain_lock for the dlm
492          * state and the dlm spinlock for join state... Sorry! */
493 again:
494         spin_lock(&dlm_domain_lock);
495         spin_lock(&dlm->spinlock);
496
497         if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
498                 mlog(0, "Node %d is joining, we wait on it.\n",
499                           dlm->joining_node);
500                 spin_unlock(&dlm->spinlock);
501                 spin_unlock(&dlm_domain_lock);
502
503                 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
504                 goto again;
505         }
506
507         dlm->dlm_state = DLM_CTXT_LEAVING;
508         spin_unlock(&dlm->spinlock);
509         spin_unlock(&dlm_domain_lock);
510 }
511
512 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
513 {
514         int node = -1;
515
516         assert_spin_locked(&dlm->spinlock);
517
518         printk(KERN_NOTICE "o2dlm: Nodes in domain %s: ", dlm->name);
519
520         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
521                                      node + 1)) < O2NM_MAX_NODES) {
522                 printk("%d ", node);
523         }
524         printk("\n");
525 }
526
527 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
528                                    void **ret_data)
529 {
530         struct dlm_ctxt *dlm = data;
531         unsigned int node;
532         struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
533
534         mlog(0, "%p %u %p", msg, len, data);
535
536         if (!dlm_grab(dlm))
537                 return 0;
538
539         node = exit_msg->node_idx;
540
541         printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s\n", node, dlm->name);
542
543         spin_lock(&dlm->spinlock);
544         clear_bit(node, dlm->domain_map);
545         __dlm_print_nodes(dlm);
546
547         /* notify anything attached to the heartbeat events */
548         dlm_hb_event_notify_attached(dlm, node, 0);
549
550         spin_unlock(&dlm->spinlock);
551
552         dlm_put(dlm);
553
554         return 0;
555 }
556
557 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
558                                     unsigned int node)
559 {
560         int status;
561         struct dlm_exit_domain leave_msg;
562
563         mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
564                   node, dlm->name, dlm->node_num);
565
566         memset(&leave_msg, 0, sizeof(leave_msg));
567         leave_msg.node_idx = dlm->node_num;
568
569         status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
570                                     &leave_msg, sizeof(leave_msg), node,
571                                     NULL);
572         if (status < 0)
573                 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
574                      "node %u\n", status, DLM_EXIT_DOMAIN_MSG, dlm->key, node);
575         mlog(0, "status return %d from o2net_send_message\n", status);
576
577         return status;
578 }
579
580
581 static void dlm_leave_domain(struct dlm_ctxt *dlm)
582 {
583         int node, clear_node, status;
584
585         /* At this point we've migrated away all our locks and won't
586          * accept mastership of new ones. The dlm is responsible for
587          * almost nothing now. We make sure not to confuse any joining
588          * nodes and then commence shutdown procedure. */
589
590         spin_lock(&dlm->spinlock);
591         /* Clear ourselves from the domain map */
592         clear_bit(dlm->node_num, dlm->domain_map);
593         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
594                                      0)) < O2NM_MAX_NODES) {
595                 /* Drop the dlm spinlock. This is safe wrt the domain_map.
596                  * -nodes cannot be added now as the
597                  *   query_join_handlers knows to respond with OK_NO_MAP
598                  * -we catch the right network errors if a node is
599                  *   removed from the map while we're sending him the
600                  *   exit message. */
601                 spin_unlock(&dlm->spinlock);
602
603                 clear_node = 1;
604
605                 status = dlm_send_one_domain_exit(dlm, node);
606                 if (status < 0 &&
607                     status != -ENOPROTOOPT &&
608                     status != -ENOTCONN) {
609                         mlog(ML_NOTICE, "Error %d sending domain exit message "
610                              "to node %d\n", status, node);
611
612                         /* Not sure what to do here but lets sleep for
613                          * a bit in case this was a transient
614                          * error... */
615                         msleep(DLM_DOMAIN_BACKOFF_MS);
616                         clear_node = 0;
617                 }
618
619                 spin_lock(&dlm->spinlock);
620                 /* If we're not clearing the node bit then we intend
621                  * to loop back around to try again. */
622                 if (clear_node)
623                         clear_bit(node, dlm->domain_map);
624         }
625         spin_unlock(&dlm->spinlock);
626 }
627
628 int dlm_joined(struct dlm_ctxt *dlm)
629 {
630         int ret = 0;
631
632         spin_lock(&dlm_domain_lock);
633
634         if (dlm->dlm_state == DLM_CTXT_JOINED)
635                 ret = 1;
636
637         spin_unlock(&dlm_domain_lock);
638
639         return ret;
640 }
641
642 int dlm_shutting_down(struct dlm_ctxt *dlm)
643 {
644         int ret = 0;
645
646         spin_lock(&dlm_domain_lock);
647
648         if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
649                 ret = 1;
650
651         spin_unlock(&dlm_domain_lock);
652
653         return ret;
654 }
655
656 void dlm_unregister_domain(struct dlm_ctxt *dlm)
657 {
658         int leave = 0;
659         struct dlm_lock_resource *res;
660
661         spin_lock(&dlm_domain_lock);
662         BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
663         BUG_ON(!dlm->num_joins);
664
665         dlm->num_joins--;
666         if (!dlm->num_joins) {
667                 /* We mark it "in shutdown" now so new register
668                  * requests wait until we've completely left the
669                  * domain. Don't use DLM_CTXT_LEAVING yet as we still
670                  * want new domain joins to communicate with us at
671                  * least until we've completed migration of our
672                  * resources. */
673                 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
674                 leave = 1;
675         }
676         spin_unlock(&dlm_domain_lock);
677
678         if (leave) {
679                 mlog(0, "shutting down domain %s\n", dlm->name);
680
681                 /* We changed dlm state, notify the thread */
682                 dlm_kick_thread(dlm, NULL);
683
684                 while (dlm_migrate_all_locks(dlm)) {
685                         /* Give dlm_thread time to purge the lockres' */
686                         msleep(500);
687                         mlog(0, "%s: more migration to do\n", dlm->name);
688                 }
689
690                 /* This list should be empty. If not, print remaining lockres */
691                 if (!list_empty(&dlm->tracking_list)) {
692                         mlog(ML_ERROR, "Following lockres' are still on the "
693                              "tracking list:\n");
694                         list_for_each_entry(res, &dlm->tracking_list, tracking)
695                                 dlm_print_one_lock_resource(res);
696                 }
697
698                 dlm_mark_domain_leaving(dlm);
699                 dlm_leave_domain(dlm);
700                 dlm_force_free_mles(dlm);
701                 dlm_complete_dlm_shutdown(dlm);
702         }
703         dlm_put(dlm);
704 }
705 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
706
707 static int dlm_query_join_proto_check(char *proto_type, int node,
708                                       struct dlm_protocol_version *ours,
709                                       struct dlm_protocol_version *request)
710 {
711         int rc;
712         struct dlm_protocol_version proto = *request;
713
714         if (!dlm_protocol_compare(ours, &proto)) {
715                 mlog(0,
716                      "node %u wanted to join with %s locking protocol "
717                      "%u.%u, we respond with %u.%u\n",
718                      node, proto_type,
719                      request->pv_major,
720                      request->pv_minor,
721                      proto.pv_major, proto.pv_minor);
722                 request->pv_minor = proto.pv_minor;
723                 rc = 0;
724         } else {
725                 mlog(ML_NOTICE,
726                      "Node %u wanted to join with %s locking "
727                      "protocol %u.%u, but we have %u.%u, disallowing\n",
728                      node, proto_type,
729                      request->pv_major,
730                      request->pv_minor,
731                      ours->pv_major,
732                      ours->pv_minor);
733                 rc = 1;
734         }
735
736         return rc;
737 }
738
739 /*
740  * struct dlm_query_join_packet is made up of four one-byte fields.  They
741  * are effectively in big-endian order already.  However, little-endian
742  * machines swap them before putting the packet on the wire (because
743  * query_join's response is a status, and that status is treated as a u32
744  * on the wire).  Thus, a big-endian and little-endian machines will treat
745  * this structure differently.
746  *
747  * The solution is to have little-endian machines swap the structure when
748  * converting from the structure to the u32 representation.  This will
749  * result in the structure having the correct format on the wire no matter
750  * the host endian format.
751  */
752 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
753                                           u32 *wire)
754 {
755         union dlm_query_join_response response;
756
757         response.packet = *packet;
758         *wire = cpu_to_be32(response.intval);
759 }
760
761 static void dlm_query_join_wire_to_packet(u32 wire,
762                                           struct dlm_query_join_packet *packet)
763 {
764         union dlm_query_join_response response;
765
766         response.intval = cpu_to_be32(wire);
767         *packet = response.packet;
768 }
769
770 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
771                                   void **ret_data)
772 {
773         struct dlm_query_join_request *query;
774         struct dlm_query_join_packet packet = {
775                 .code = JOIN_DISALLOW,
776         };
777         struct dlm_ctxt *dlm = NULL;
778         u32 response;
779         u8 nodenum;
780
781         query = (struct dlm_query_join_request *) msg->buf;
782
783         mlog(0, "node %u wants to join domain %s\n", query->node_idx,
784                   query->domain);
785
786         /*
787          * If heartbeat doesn't consider the node live, tell it
788          * to back off and try again.  This gives heartbeat a chance
789          * to catch up.
790          */
791         if (!o2hb_check_node_heartbeating(query->node_idx)) {
792                 mlog(0, "node %u is not in our live map yet\n",
793                      query->node_idx);
794
795                 packet.code = JOIN_DISALLOW;
796                 goto respond;
797         }
798
799         packet.code = JOIN_OK_NO_MAP;
800
801         spin_lock(&dlm_domain_lock);
802         dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
803         if (!dlm)
804                 goto unlock_respond;
805
806         /*
807          * There is a small window where the joining node may not see the
808          * node(s) that just left but still part of the cluster. DISALLOW
809          * join request if joining node has different node map.
810          */
811         nodenum=0;
812         while (nodenum < O2NM_MAX_NODES) {
813                 if (test_bit(nodenum, dlm->domain_map)) {
814                         if (!byte_test_bit(nodenum, query->node_map)) {
815                                 mlog(0, "disallow join as node %u does not "
816                                      "have node %u in its nodemap\n",
817                                      query->node_idx, nodenum);
818                                 packet.code = JOIN_DISALLOW;
819                                 goto unlock_respond;
820                         }
821                 }
822                 nodenum++;
823         }
824
825         /* Once the dlm ctxt is marked as leaving then we don't want
826          * to be put in someone's domain map.
827          * Also, explicitly disallow joining at certain troublesome
828          * times (ie. during recovery). */
829         if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
830                 int bit = query->node_idx;
831                 spin_lock(&dlm->spinlock);
832
833                 if (dlm->dlm_state == DLM_CTXT_NEW &&
834                     dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
835                         /*If this is a brand new context and we
836                          * haven't started our join process yet, then
837                          * the other node won the race. */
838                         packet.code = JOIN_OK_NO_MAP;
839                 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
840                         /* Disallow parallel joins. */
841                         packet.code = JOIN_DISALLOW;
842                 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
843                         mlog(0, "node %u trying to join, but recovery "
844                              "is ongoing.\n", bit);
845                         packet.code = JOIN_DISALLOW;
846                 } else if (test_bit(bit, dlm->recovery_map)) {
847                         mlog(0, "node %u trying to join, but it "
848                              "still needs recovery.\n", bit);
849                         packet.code = JOIN_DISALLOW;
850                 } else if (test_bit(bit, dlm->domain_map)) {
851                         mlog(0, "node %u trying to join, but it "
852                              "is still in the domain! needs recovery?\n",
853                              bit);
854                         packet.code = JOIN_DISALLOW;
855                 } else {
856                         /* Alright we're fully a part of this domain
857                          * so we keep some state as to who's joining
858                          * and indicate to him that needs to be fixed
859                          * up. */
860
861                         /* Make sure we speak compatible locking protocols.  */
862                         if (dlm_query_join_proto_check("DLM", bit,
863                                                        &dlm->dlm_locking_proto,
864                                                        &query->dlm_proto)) {
865                                 packet.code = JOIN_PROTOCOL_MISMATCH;
866                         } else if (dlm_query_join_proto_check("fs", bit,
867                                                               &dlm->fs_locking_proto,
868                                                               &query->fs_proto)) {
869                                 packet.code = JOIN_PROTOCOL_MISMATCH;
870                         } else {
871                                 packet.dlm_minor = query->dlm_proto.pv_minor;
872                                 packet.fs_minor = query->fs_proto.pv_minor;
873                                 packet.code = JOIN_OK;
874                                 __dlm_set_joining_node(dlm, query->node_idx);
875                         }
876                 }
877
878                 spin_unlock(&dlm->spinlock);
879         }
880 unlock_respond:
881         spin_unlock(&dlm_domain_lock);
882
883 respond:
884         mlog(0, "We respond with %u\n", packet.code);
885
886         dlm_query_join_packet_to_wire(&packet, &response);
887         return response;
888 }
889
890 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
891                                      void **ret_data)
892 {
893         struct dlm_assert_joined *assert;
894         struct dlm_ctxt *dlm = NULL;
895
896         assert = (struct dlm_assert_joined *) msg->buf;
897
898         mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
899                   assert->domain);
900
901         spin_lock(&dlm_domain_lock);
902         dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
903         /* XXX should we consider no dlm ctxt an error? */
904         if (dlm) {
905                 spin_lock(&dlm->spinlock);
906
907                 /* Alright, this node has officially joined our
908                  * domain. Set him in the map and clean up our
909                  * leftover join state. */
910                 BUG_ON(dlm->joining_node != assert->node_idx);
911                 set_bit(assert->node_idx, dlm->domain_map);
912                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
913
914                 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s\n",
915                        assert->node_idx, dlm->name);
916                 __dlm_print_nodes(dlm);
917
918                 /* notify anything attached to the heartbeat events */
919                 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
920
921                 spin_unlock(&dlm->spinlock);
922         }
923         spin_unlock(&dlm_domain_lock);
924
925         return 0;
926 }
927
928 static int dlm_match_regions(struct dlm_ctxt *dlm,
929                              struct dlm_query_region *qr,
930                              char *local, int locallen)
931 {
932         char *remote = qr->qr_regions;
933         char *l, *r;
934         int localnr, i, j, foundit;
935         int status = 0;
936
937         if (!o2hb_global_heartbeat_active()) {
938                 if (qr->qr_numregions) {
939                         mlog(ML_ERROR, "Domain %s: Joining node %d has global "
940                              "heartbeat enabled but local node %d does not\n",
941                              qr->qr_domain, qr->qr_node, dlm->node_num);
942                         status = -EINVAL;
943                 }
944                 goto bail;
945         }
946
947         if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
948                 mlog(ML_ERROR, "Domain %s: Local node %d has global "
949                      "heartbeat enabled but joining node %d does not\n",
950                      qr->qr_domain, dlm->node_num, qr->qr_node);
951                 status = -EINVAL;
952                 goto bail;
953         }
954
955         r = remote;
956         for (i = 0; i < qr->qr_numregions; ++i) {
957                 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
958                 r += O2HB_MAX_REGION_NAME_LEN;
959         }
960
961         localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
962         localnr = o2hb_get_all_regions(local, (u8)localnr);
963
964         /* compare local regions with remote */
965         l = local;
966         for (i = 0; i < localnr; ++i) {
967                 foundit = 0;
968                 r = remote;
969                 for (j = 0; j <= qr->qr_numregions; ++j) {
970                         if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
971                                 foundit = 1;
972                                 break;
973                         }
974                         r += O2HB_MAX_REGION_NAME_LEN;
975                 }
976                 if (!foundit) {
977                         status = -EINVAL;
978                         mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
979                              "in local node %d but not in joining node %d\n",
980                              qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
981                              dlm->node_num, qr->qr_node);
982                         goto bail;
983                 }
984                 l += O2HB_MAX_REGION_NAME_LEN;
985         }
986
987         /* compare remote with local regions */
988         r = remote;
989         for (i = 0; i < qr->qr_numregions; ++i) {
990                 foundit = 0;
991                 l = local;
992                 for (j = 0; j < localnr; ++j) {
993                         if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
994                                 foundit = 1;
995                                 break;
996                         }
997                         l += O2HB_MAX_REGION_NAME_LEN;
998                 }
999                 if (!foundit) {
1000                         status = -EINVAL;
1001                         mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1002                              "in joining node %d but not in local node %d\n",
1003                              qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1004                              qr->qr_node, dlm->node_num);
1005                         goto bail;
1006                 }
1007                 r += O2HB_MAX_REGION_NAME_LEN;
1008         }
1009
1010 bail:
1011         return status;
1012 }
1013
1014 static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1015 {
1016         struct dlm_query_region *qr = NULL;
1017         int status, ret = 0, i;
1018         char *p;
1019
1020         if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1021                 goto bail;
1022
1023         qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1024         if (!qr) {
1025                 ret = -ENOMEM;
1026                 mlog_errno(ret);
1027                 goto bail;
1028         }
1029
1030         qr->qr_node = dlm->node_num;
1031         qr->qr_namelen = strlen(dlm->name);
1032         memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1033         /* if local hb, the numregions will be zero */
1034         if (o2hb_global_heartbeat_active())
1035                 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1036                                                          O2NM_MAX_REGIONS);
1037
1038         p = qr->qr_regions;
1039         for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1040                 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1041
1042         i = -1;
1043         while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1044                                   i + 1)) < O2NM_MAX_NODES) {
1045                 if (i == dlm->node_num)
1046                         continue;
1047
1048                 mlog(0, "Sending regions to node %d\n", i);
1049
1050                 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1051                                          sizeof(struct dlm_query_region),
1052                                          i, &status);
1053                 if (ret >= 0)
1054                         ret = status;
1055                 if (ret) {
1056                         mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1057                              ret, i);
1058                         break;
1059                 }
1060         }
1061
1062 bail:
1063         kfree(qr);
1064         return ret;
1065 }
1066
1067 static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1068                                     void *data, void **ret_data)
1069 {
1070         struct dlm_query_region *qr;
1071         struct dlm_ctxt *dlm = NULL;
1072         char *local = NULL;
1073         int status = 0;
1074         int locked = 0;
1075
1076         qr = (struct dlm_query_region *) msg->buf;
1077
1078         mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1079              qr->qr_domain);
1080
1081         /* buffer used in dlm_mast_regions() */
1082         local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1083         if (!local) {
1084                 status = -ENOMEM;
1085                 goto bail;
1086         }
1087
1088         status = -EINVAL;
1089
1090         spin_lock(&dlm_domain_lock);
1091         dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1092         if (!dlm) {
1093                 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1094                      "before join domain\n", qr->qr_node, qr->qr_domain);
1095                 goto bail;
1096         }
1097
1098         spin_lock(&dlm->spinlock);
1099         locked = 1;
1100         if (dlm->joining_node != qr->qr_node) {
1101                 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1102                      "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1103                      dlm->joining_node);
1104                 goto bail;
1105         }
1106
1107         /* Support for global heartbeat was added in 1.1 */
1108         if (dlm->dlm_locking_proto.pv_major == 1 &&
1109             dlm->dlm_locking_proto.pv_minor == 0) {
1110                 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1111                      "but active dlm protocol is %d.%d\n", qr->qr_node,
1112                      qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1113                      dlm->dlm_locking_proto.pv_minor);
1114                 goto bail;
1115         }
1116
1117         status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1118
1119 bail:
1120         if (locked)
1121                 spin_unlock(&dlm->spinlock);
1122         spin_unlock(&dlm_domain_lock);
1123
1124         kfree(local);
1125
1126         return status;
1127 }
1128
1129 static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1130 {
1131         struct o2nm_node *local;
1132         struct dlm_node_info *remote;
1133         int i, j;
1134         int status = 0;
1135
1136         for (j = 0; j < qn->qn_numnodes; ++j)
1137                 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1138                      &(qn->qn_nodes[j].ni_ipv4_address),
1139                      ntohs(qn->qn_nodes[j].ni_ipv4_port));
1140
1141         for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1142                 local = o2nm_get_node_by_num(i);
1143                 remote = NULL;
1144                 for (j = 0; j < qn->qn_numnodes; ++j) {
1145                         if (qn->qn_nodes[j].ni_nodenum == i) {
1146                                 remote = &(qn->qn_nodes[j]);
1147                                 break;
1148                         }
1149                 }
1150
1151                 if (!local && !remote)
1152                         continue;
1153
1154                 if ((local && !remote) || (!local && remote))
1155                         status = -EINVAL;
1156
1157                 if (!status &&
1158                     ((remote->ni_nodenum != local->nd_num) ||
1159                      (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1160                      (remote->ni_ipv4_address != local->nd_ipv4_address)))
1161                         status = -EINVAL;
1162
1163                 if (status) {
1164                         if (remote && !local)
1165                                 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1166                                      "registered in joining node %d but not in "
1167                                      "local node %d\n", qn->qn_domain,
1168                                      remote->ni_nodenum,
1169                                      &(remote->ni_ipv4_address),
1170                                      ntohs(remote->ni_ipv4_port),
1171                                      qn->qn_nodenum, dlm->node_num);
1172                         if (local && !remote)
1173                                 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1174                                      "registered in local node %d but not in "
1175                                      "joining node %d\n", qn->qn_domain,
1176                                      local->nd_num, &(local->nd_ipv4_address),
1177                                      ntohs(local->nd_ipv4_port),
1178                                      dlm->node_num, qn->qn_nodenum);
1179                         BUG_ON((!local && !remote));
1180                 }
1181
1182                 if (local)
1183                         o2nm_node_put(local);
1184         }
1185
1186         return status;
1187 }
1188
1189 static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1190 {
1191         struct dlm_query_nodeinfo *qn = NULL;
1192         struct o2nm_node *node;
1193         int ret = 0, status, count, i;
1194
1195         if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1196                 goto bail;
1197
1198         qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1199         if (!qn) {
1200                 ret = -ENOMEM;
1201                 mlog_errno(ret);
1202                 goto bail;
1203         }
1204
1205         for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1206                 node = o2nm_get_node_by_num(i);
1207                 if (!node)
1208                         continue;
1209                 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1210                 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1211                 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1212                 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1213                      &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1214                 ++count;
1215                 o2nm_node_put(node);
1216         }
1217
1218         qn->qn_nodenum = dlm->node_num;
1219         qn->qn_numnodes = count;
1220         qn->qn_namelen = strlen(dlm->name);
1221         memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1222
1223         i = -1;
1224         while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1225                                   i + 1)) < O2NM_MAX_NODES) {
1226                 if (i == dlm->node_num)
1227                         continue;
1228
1229                 mlog(0, "Sending nodeinfo to node %d\n", i);
1230
1231                 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1232                                          qn, sizeof(struct dlm_query_nodeinfo),
1233                                          i, &status);
1234                 if (ret >= 0)
1235                         ret = status;
1236                 if (ret) {
1237                         mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1238                         break;
1239                 }
1240         }
1241
1242 bail:
1243         kfree(qn);
1244         return ret;
1245 }
1246
1247 static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1248                                       void *data, void **ret_data)
1249 {
1250         struct dlm_query_nodeinfo *qn;
1251         struct dlm_ctxt *dlm = NULL;
1252         int locked = 0, status = -EINVAL;
1253
1254         qn = (struct dlm_query_nodeinfo *) msg->buf;
1255
1256         mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1257              qn->qn_domain);
1258
1259         spin_lock(&dlm_domain_lock);
1260         dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1261         if (!dlm) {
1262                 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1263                      "join domain\n", qn->qn_nodenum, qn->qn_domain);
1264                 goto bail;
1265         }
1266
1267         spin_lock(&dlm->spinlock);
1268         locked = 1;
1269         if (dlm->joining_node != qn->qn_nodenum) {
1270                 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1271                      "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1272                      dlm->joining_node);
1273                 goto bail;
1274         }
1275
1276         /* Support for node query was added in 1.1 */
1277         if (dlm->dlm_locking_proto.pv_major == 1 &&
1278             dlm->dlm_locking_proto.pv_minor == 0) {
1279                 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1280                      "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1281                      qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1282                      dlm->dlm_locking_proto.pv_minor);
1283                 goto bail;
1284         }
1285
1286         status = dlm_match_nodes(dlm, qn);
1287
1288 bail:
1289         if (locked)
1290                 spin_unlock(&dlm->spinlock);
1291         spin_unlock(&dlm_domain_lock);
1292
1293         return status;
1294 }
1295
1296 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1297                                    void **ret_data)
1298 {
1299         struct dlm_cancel_join *cancel;
1300         struct dlm_ctxt *dlm = NULL;
1301
1302         cancel = (struct dlm_cancel_join *) msg->buf;
1303
1304         mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1305                   cancel->domain);
1306
1307         spin_lock(&dlm_domain_lock);
1308         dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1309
1310         if (dlm) {
1311                 spin_lock(&dlm->spinlock);
1312
1313                 /* Yikes, this guy wants to cancel his join. No
1314                  * problem, we simply cleanup our join state. */
1315                 BUG_ON(dlm->joining_node != cancel->node_idx);
1316                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1317
1318                 spin_unlock(&dlm->spinlock);
1319         }
1320         spin_unlock(&dlm_domain_lock);
1321
1322         return 0;
1323 }
1324
1325 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1326                                     unsigned int node)
1327 {
1328         int status;
1329         struct dlm_cancel_join cancel_msg;
1330
1331         memset(&cancel_msg, 0, sizeof(cancel_msg));
1332         cancel_msg.node_idx = dlm->node_num;
1333         cancel_msg.name_len = strlen(dlm->name);
1334         memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1335
1336         status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1337                                     &cancel_msg, sizeof(cancel_msg), node,
1338                                     NULL);
1339         if (status < 0) {
1340                 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1341                      "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1342                      node);
1343                 goto bail;
1344         }
1345
1346 bail:
1347         return status;
1348 }
1349
1350 /* map_size should be in bytes. */
1351 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1352                                  unsigned long *node_map,
1353                                  unsigned int map_size)
1354 {
1355         int status, tmpstat;
1356         unsigned int node;
1357
1358         if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1359                          sizeof(unsigned long))) {
1360                 mlog(ML_ERROR,
1361                      "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1362                      map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1363                 return -EINVAL;
1364         }
1365
1366         status = 0;
1367         node = -1;
1368         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1369                                      node + 1)) < O2NM_MAX_NODES) {
1370                 if (node == dlm->node_num)
1371                         continue;
1372
1373                 tmpstat = dlm_send_one_join_cancel(dlm, node);
1374                 if (tmpstat) {
1375                         mlog(ML_ERROR, "Error return %d cancelling join on "
1376                              "node %d\n", tmpstat, node);
1377                         if (!status)
1378                                 status = tmpstat;
1379                 }
1380         }
1381
1382         if (status)
1383                 mlog_errno(status);
1384         return status;
1385 }
1386
1387 static int dlm_request_join(struct dlm_ctxt *dlm,
1388                             int node,
1389                             enum dlm_query_join_response_code *response)
1390 {
1391         int status;
1392         struct dlm_query_join_request join_msg;
1393         struct dlm_query_join_packet packet;
1394         u32 join_resp;
1395
1396         mlog(0, "querying node %d\n", node);
1397
1398         memset(&join_msg, 0, sizeof(join_msg));
1399         join_msg.node_idx = dlm->node_num;
1400         join_msg.name_len = strlen(dlm->name);
1401         memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1402         join_msg.dlm_proto = dlm->dlm_locking_proto;
1403         join_msg.fs_proto = dlm->fs_locking_proto;
1404
1405         /* copy live node map to join message */
1406         byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1407
1408         status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1409                                     sizeof(join_msg), node, &join_resp);
1410         if (status < 0 && status != -ENOPROTOOPT) {
1411                 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1412                      "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1413                      node);
1414                 goto bail;
1415         }
1416         dlm_query_join_wire_to_packet(join_resp, &packet);
1417
1418         /* -ENOPROTOOPT from the net code means the other side isn't
1419             listening for our message type -- that's fine, it means
1420             his dlm isn't up, so we can consider him a 'yes' but not
1421             joined into the domain.  */
1422         if (status == -ENOPROTOOPT) {
1423                 status = 0;
1424                 *response = JOIN_OK_NO_MAP;
1425         } else if (packet.code == JOIN_DISALLOW ||
1426                    packet.code == JOIN_OK_NO_MAP) {
1427                 *response = packet.code;
1428         } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
1429                 mlog(ML_NOTICE,
1430                      "This node requested DLM locking protocol %u.%u and "
1431                      "filesystem locking protocol %u.%u.  At least one of "
1432                      "the protocol versions on node %d is not compatible, "
1433                      "disconnecting\n",
1434                      dlm->dlm_locking_proto.pv_major,
1435                      dlm->dlm_locking_proto.pv_minor,
1436                      dlm->fs_locking_proto.pv_major,
1437                      dlm->fs_locking_proto.pv_minor,
1438                      node);
1439                 status = -EPROTO;
1440                 *response = packet.code;
1441         } else if (packet.code == JOIN_OK) {
1442                 *response = packet.code;
1443                 /* Use the same locking protocol as the remote node */
1444                 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1445                 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1446                 mlog(0,
1447                      "Node %d responds JOIN_OK with DLM locking protocol "
1448                      "%u.%u and fs locking protocol %u.%u\n",
1449                      node,
1450                      dlm->dlm_locking_proto.pv_major,
1451                      dlm->dlm_locking_proto.pv_minor,
1452                      dlm->fs_locking_proto.pv_major,
1453                      dlm->fs_locking_proto.pv_minor);
1454         } else {
1455                 status = -EINVAL;
1456                 mlog(ML_ERROR, "invalid response %d from node %u\n",
1457                      packet.code, node);
1458         }
1459
1460         mlog(0, "status %d, node %d response is %d\n", status, node,
1461              *response);
1462
1463 bail:
1464         return status;
1465 }
1466
1467 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1468                                     unsigned int node)
1469 {
1470         int status;
1471         struct dlm_assert_joined assert_msg;
1472
1473         mlog(0, "Sending join assert to node %u\n", node);
1474
1475         memset(&assert_msg, 0, sizeof(assert_msg));
1476         assert_msg.node_idx = dlm->node_num;
1477         assert_msg.name_len = strlen(dlm->name);
1478         memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1479
1480         status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1481                                     &assert_msg, sizeof(assert_msg), node,
1482                                     NULL);
1483         if (status < 0)
1484                 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1485                      "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1486                      node);
1487
1488         return status;
1489 }
1490
1491 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1492                                   unsigned long *node_map)
1493 {
1494         int status, node, live;
1495
1496         status = 0;
1497         node = -1;
1498         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1499                                      node + 1)) < O2NM_MAX_NODES) {
1500                 if (node == dlm->node_num)
1501                         continue;
1502
1503                 do {
1504                         /* It is very important that this message be
1505                          * received so we spin until either the node
1506                          * has died or it gets the message. */
1507                         status = dlm_send_one_join_assert(dlm, node);
1508
1509                         spin_lock(&dlm->spinlock);
1510                         live = test_bit(node, dlm->live_nodes_map);
1511                         spin_unlock(&dlm->spinlock);
1512
1513                         if (status) {
1514                                 mlog(ML_ERROR, "Error return %d asserting "
1515                                      "join on node %d\n", status, node);
1516
1517                                 /* give us some time between errors... */
1518                                 if (live)
1519                                         msleep(DLM_DOMAIN_BACKOFF_MS);
1520                         }
1521                 } while (status && live);
1522         }
1523 }
1524
1525 struct domain_join_ctxt {
1526         unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1527         unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1528 };
1529
1530 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1531                                    struct domain_join_ctxt *ctxt,
1532                                    enum dlm_query_join_response_code response)
1533 {
1534         int ret;
1535
1536         if (response == JOIN_DISALLOW) {
1537                 mlog(0, "Latest response of disallow -- should restart\n");
1538                 return 1;
1539         }
1540
1541         spin_lock(&dlm->spinlock);
1542         /* For now, we restart the process if the node maps have
1543          * changed at all */
1544         ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1545                      sizeof(dlm->live_nodes_map));
1546         spin_unlock(&dlm->spinlock);
1547
1548         if (ret)
1549                 mlog(0, "Node maps changed -- should restart\n");
1550
1551         return ret;
1552 }
1553
1554 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1555 {
1556         int status = 0, tmpstat, node;
1557         struct domain_join_ctxt *ctxt;
1558         enum dlm_query_join_response_code response = JOIN_DISALLOW;
1559
1560         mlog(0, "%p", dlm);
1561
1562         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1563         if (!ctxt) {
1564                 status = -ENOMEM;
1565                 mlog_errno(status);
1566                 goto bail;
1567         }
1568
1569         /* group sem locking should work for us here -- we're already
1570          * registered for heartbeat events so filling this should be
1571          * atomic wrt getting those handlers called. */
1572         o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1573
1574         spin_lock(&dlm->spinlock);
1575         memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1576
1577         __dlm_set_joining_node(dlm, dlm->node_num);
1578
1579         spin_unlock(&dlm->spinlock);
1580
1581         node = -1;
1582         while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1583                                      node + 1)) < O2NM_MAX_NODES) {
1584                 if (node == dlm->node_num)
1585                         continue;
1586
1587                 status = dlm_request_join(dlm, node, &response);
1588                 if (status < 0) {
1589                         mlog_errno(status);
1590                         goto bail;
1591                 }
1592
1593                 /* Ok, either we got a response or the node doesn't have a
1594                  * dlm up. */
1595                 if (response == JOIN_OK)
1596                         set_bit(node, ctxt->yes_resp_map);
1597
1598                 if (dlm_should_restart_join(dlm, ctxt, response)) {
1599                         status = -EAGAIN;
1600                         goto bail;
1601                 }
1602         }
1603
1604         mlog(0, "Yay, done querying nodes!\n");
1605
1606         /* Yay, everyone agree's we can join the domain. My domain is
1607          * comprised of all nodes who were put in the
1608          * yes_resp_map. Copy that into our domain map and send a join
1609          * assert message to clean up everyone elses state. */
1610         spin_lock(&dlm->spinlock);
1611         memcpy(dlm->domain_map, ctxt->yes_resp_map,
1612                sizeof(ctxt->yes_resp_map));
1613         set_bit(dlm->node_num, dlm->domain_map);
1614         spin_unlock(&dlm->spinlock);
1615
1616         /* Support for global heartbeat and node info was added in 1.1 */
1617         if (dlm_protocol.pv_major > 1 || dlm_protocol.pv_minor > 0) {
1618                 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1619                 if (status) {
1620                         mlog_errno(status);
1621                         goto bail;
1622                 }
1623                 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1624                 if (status) {
1625                         mlog_errno(status);
1626                         goto bail;
1627                 }
1628         }
1629
1630         dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1631
1632         /* Joined state *must* be set before the joining node
1633          * information, otherwise the query_join handler may read no
1634          * current joiner but a state of NEW and tell joining nodes
1635          * we're not in the domain. */
1636         spin_lock(&dlm_domain_lock);
1637         dlm->dlm_state = DLM_CTXT_JOINED;
1638         dlm->num_joins++;
1639         spin_unlock(&dlm_domain_lock);
1640
1641 bail:
1642         spin_lock(&dlm->spinlock);
1643         __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1644         if (!status)
1645                 __dlm_print_nodes(dlm);
1646         spin_unlock(&dlm->spinlock);
1647
1648         if (ctxt) {
1649                 /* Do we need to send a cancel message to any nodes? */
1650                 if (status < 0) {
1651                         tmpstat = dlm_send_join_cancels(dlm,
1652                                                         ctxt->yes_resp_map,
1653                                                         sizeof(ctxt->yes_resp_map));
1654                         if (tmpstat < 0)
1655                                 mlog_errno(tmpstat);
1656                 }
1657                 kfree(ctxt);
1658         }
1659
1660         mlog(0, "returning %d\n", status);
1661         return status;
1662 }
1663
1664 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1665 {
1666         o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1667         o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1668         o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1669 }
1670
1671 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1672 {
1673         int status;
1674
1675         mlog(0, "registering handlers.\n");
1676
1677         o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1678                             dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1679         status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1680         if (status)
1681                 goto bail;
1682
1683         o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1684                             dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1685         status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1686         if (status)
1687                 goto bail;
1688
1689         status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1690                                         sizeof(struct dlm_master_request),
1691                                         dlm_master_request_handler,
1692                                         dlm, NULL, &dlm->dlm_domain_handlers);
1693         if (status)
1694                 goto bail;
1695
1696         status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1697                                         sizeof(struct dlm_assert_master),
1698                                         dlm_assert_master_handler,
1699                                         dlm, dlm_assert_master_post_handler,
1700                                         &dlm->dlm_domain_handlers);
1701         if (status)
1702                 goto bail;
1703
1704         status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1705                                         sizeof(struct dlm_create_lock),
1706                                         dlm_create_lock_handler,
1707                                         dlm, NULL, &dlm->dlm_domain_handlers);
1708         if (status)
1709                 goto bail;
1710
1711         status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1712                                         DLM_CONVERT_LOCK_MAX_LEN,
1713                                         dlm_convert_lock_handler,
1714                                         dlm, NULL, &dlm->dlm_domain_handlers);
1715         if (status)
1716                 goto bail;
1717
1718         status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1719                                         DLM_UNLOCK_LOCK_MAX_LEN,
1720                                         dlm_unlock_lock_handler,
1721                                         dlm, NULL, &dlm->dlm_domain_handlers);
1722         if (status)
1723                 goto bail;
1724
1725         status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1726                                         DLM_PROXY_AST_MAX_LEN,
1727                                         dlm_proxy_ast_handler,
1728                                         dlm, NULL, &dlm->dlm_domain_handlers);
1729         if (status)
1730                 goto bail;
1731
1732         status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1733                                         sizeof(struct dlm_exit_domain),
1734                                         dlm_exit_domain_handler,
1735                                         dlm, NULL, &dlm->dlm_domain_handlers);
1736         if (status)
1737                 goto bail;
1738
1739         status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1740                                         sizeof(struct dlm_deref_lockres),
1741                                         dlm_deref_lockres_handler,
1742                                         dlm, NULL, &dlm->dlm_domain_handlers);
1743         if (status)
1744                 goto bail;
1745
1746         status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1747                                         sizeof(struct dlm_migrate_request),
1748                                         dlm_migrate_request_handler,
1749                                         dlm, NULL, &dlm->dlm_domain_handlers);
1750         if (status)
1751                 goto bail;
1752
1753         status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1754                                         DLM_MIG_LOCKRES_MAX_LEN,
1755                                         dlm_mig_lockres_handler,
1756                                         dlm, NULL, &dlm->dlm_domain_handlers);
1757         if (status)
1758                 goto bail;
1759
1760         status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1761                                         sizeof(struct dlm_master_requery),
1762                                         dlm_master_requery_handler,
1763                                         dlm, NULL, &dlm->dlm_domain_handlers);
1764         if (status)
1765                 goto bail;
1766
1767         status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1768                                         sizeof(struct dlm_lock_request),
1769                                         dlm_request_all_locks_handler,
1770                                         dlm, NULL, &dlm->dlm_domain_handlers);
1771         if (status)
1772                 goto bail;
1773
1774         status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1775                                         sizeof(struct dlm_reco_data_done),
1776                                         dlm_reco_data_done_handler,
1777                                         dlm, NULL, &dlm->dlm_domain_handlers);
1778         if (status)
1779                 goto bail;
1780
1781         status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1782                                         sizeof(struct dlm_begin_reco),
1783                                         dlm_begin_reco_handler,
1784                                         dlm, NULL, &dlm->dlm_domain_handlers);
1785         if (status)
1786                 goto bail;
1787
1788         status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1789                                         sizeof(struct dlm_finalize_reco),
1790                                         dlm_finalize_reco_handler,
1791                                         dlm, NULL, &dlm->dlm_domain_handlers);
1792         if (status)
1793                 goto bail;
1794
1795 bail:
1796         if (status)
1797                 dlm_unregister_domain_handlers(dlm);
1798
1799         return status;
1800 }
1801
1802 static int dlm_join_domain(struct dlm_ctxt *dlm)
1803 {
1804         int status;
1805         unsigned int backoff;
1806         unsigned int total_backoff = 0;
1807
1808         BUG_ON(!dlm);
1809
1810         mlog(0, "Join domain %s\n", dlm->name);
1811
1812         status = dlm_register_domain_handlers(dlm);
1813         if (status) {
1814                 mlog_errno(status);
1815                 goto bail;
1816         }
1817
1818         status = dlm_debug_init(dlm);
1819         if (status < 0) {
1820                 mlog_errno(status);
1821                 goto bail;
1822         }
1823
1824         status = dlm_launch_thread(dlm);
1825         if (status < 0) {
1826                 mlog_errno(status);
1827                 goto bail;
1828         }
1829
1830         status = dlm_launch_recovery_thread(dlm);
1831         if (status < 0) {
1832                 mlog_errno(status);
1833                 goto bail;
1834         }
1835
1836         dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1837         if (!dlm->dlm_worker) {
1838                 status = -ENOMEM;
1839                 mlog_errno(status);
1840                 goto bail;
1841         }
1842
1843         do {
1844                 status = dlm_try_to_join_domain(dlm);
1845
1846                 /* If we're racing another node to the join, then we
1847                  * need to back off temporarily and let them
1848                  * complete. */
1849 #define DLM_JOIN_TIMEOUT_MSECS  90000
1850                 if (status == -EAGAIN) {
1851                         if (signal_pending(current)) {
1852                                 status = -ERESTARTSYS;
1853                                 goto bail;
1854                         }
1855
1856                         if (total_backoff >
1857                             msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1858                                 status = -ERESTARTSYS;
1859                                 mlog(ML_NOTICE, "Timed out joining dlm domain "
1860                                      "%s after %u msecs\n", dlm->name,
1861                                      jiffies_to_msecs(total_backoff));
1862                                 goto bail;
1863                         }
1864
1865                         /*
1866                          * <chip> After you!
1867                          * <dale> No, after you!
1868                          * <chip> I insist!
1869                          * <dale> But you first!
1870                          * ...
1871                          */
1872                         backoff = (unsigned int)(jiffies & 0x3);
1873                         backoff *= DLM_DOMAIN_BACKOFF_MS;
1874                         total_backoff += backoff;
1875                         mlog(0, "backoff %d\n", backoff);
1876                         msleep(backoff);
1877                 }
1878         } while (status == -EAGAIN);
1879
1880         if (status < 0) {
1881                 mlog_errno(status);
1882                 goto bail;
1883         }
1884
1885         status = 0;
1886 bail:
1887         wake_up(&dlm_domain_events);
1888
1889         if (status) {
1890                 dlm_unregister_domain_handlers(dlm);
1891                 dlm_debug_shutdown(dlm);
1892                 dlm_complete_thread(dlm);
1893                 dlm_complete_recovery_thread(dlm);
1894                 dlm_destroy_dlm_worker(dlm);
1895         }
1896
1897         return status;
1898 }
1899
1900 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1901                                 u32 key)
1902 {
1903         int i;
1904         int ret;
1905         struct dlm_ctxt *dlm = NULL;
1906
1907         dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1908         if (!dlm) {
1909                 mlog_errno(-ENOMEM);
1910                 goto leave;
1911         }
1912
1913         dlm->name = kstrdup(domain, GFP_KERNEL);
1914         if (dlm->name == NULL) {
1915                 mlog_errno(-ENOMEM);
1916                 kfree(dlm);
1917                 dlm = NULL;
1918                 goto leave;
1919         }
1920
1921         dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1922         if (!dlm->lockres_hash) {
1923                 mlog_errno(-ENOMEM);
1924                 kfree(dlm->name);
1925                 kfree(dlm);
1926                 dlm = NULL;
1927                 goto leave;
1928         }
1929
1930         for (i = 0; i < DLM_HASH_BUCKETS; i++)
1931                 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1932
1933         dlm->master_hash = (struct hlist_head **)
1934                                 dlm_alloc_pagevec(DLM_HASH_PAGES);
1935         if (!dlm->master_hash) {
1936                 mlog_errno(-ENOMEM);
1937                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1938                 kfree(dlm->name);
1939                 kfree(dlm);
1940                 dlm = NULL;
1941                 goto leave;
1942         }
1943
1944         for (i = 0; i < DLM_HASH_BUCKETS; i++)
1945                 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1946
1947         dlm->key = key;
1948         dlm->node_num = o2nm_this_node();
1949
1950         ret = dlm_create_debugfs_subroot(dlm);
1951         if (ret < 0) {
1952                 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
1953                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1954                 kfree(dlm->name);
1955                 kfree(dlm);
1956                 dlm = NULL;
1957                 goto leave;
1958         }
1959
1960         spin_lock_init(&dlm->spinlock);
1961         spin_lock_init(&dlm->master_lock);
1962         spin_lock_init(&dlm->ast_lock);
1963         spin_lock_init(&dlm->track_lock);
1964         INIT_LIST_HEAD(&dlm->list);
1965         INIT_LIST_HEAD(&dlm->dirty_list);
1966         INIT_LIST_HEAD(&dlm->reco.resources);
1967         INIT_LIST_HEAD(&dlm->reco.received);
1968         INIT_LIST_HEAD(&dlm->reco.node_data);
1969         INIT_LIST_HEAD(&dlm->purge_list);
1970         INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1971         INIT_LIST_HEAD(&dlm->tracking_list);
1972         dlm->reco.state = 0;
1973
1974         INIT_LIST_HEAD(&dlm->pending_asts);
1975         INIT_LIST_HEAD(&dlm->pending_basts);
1976
1977         mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1978                   dlm->recovery_map, &(dlm->recovery_map[0]));
1979
1980         memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1981         memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1982         memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1983
1984         dlm->dlm_thread_task = NULL;
1985         dlm->dlm_reco_thread_task = NULL;
1986         dlm->dlm_worker = NULL;
1987         init_waitqueue_head(&dlm->dlm_thread_wq);
1988         init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1989         init_waitqueue_head(&dlm->reco.event);
1990         init_waitqueue_head(&dlm->ast_wq);
1991         init_waitqueue_head(&dlm->migration_wq);
1992         INIT_LIST_HEAD(&dlm->mle_hb_events);
1993
1994         dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1995         init_waitqueue_head(&dlm->dlm_join_events);
1996
1997         dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1998         dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1999
2000         atomic_set(&dlm->res_tot_count, 0);
2001         atomic_set(&dlm->res_cur_count, 0);
2002         for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2003                 atomic_set(&dlm->mle_tot_count[i], 0);
2004                 atomic_set(&dlm->mle_cur_count[i], 0);
2005         }
2006
2007         spin_lock_init(&dlm->work_lock);
2008         INIT_LIST_HEAD(&dlm->work_list);
2009         INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2010
2011         kref_init(&dlm->dlm_refs);
2012         dlm->dlm_state = DLM_CTXT_NEW;
2013
2014         INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2015
2016         mlog(0, "context init: refcount %u\n",
2017                   atomic_read(&dlm->dlm_refs.refcount));
2018
2019 leave:
2020         return dlm;
2021 }
2022
2023 /*
2024  * Compare a requested locking protocol version against the current one.
2025  *
2026  * If the major numbers are different, they are incompatible.
2027  * If the current minor is greater than the request, they are incompatible.
2028  * If the current minor is less than or equal to the request, they are
2029  * compatible, and the requester should run at the current minor version.
2030  */
2031 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2032                                 struct dlm_protocol_version *request)
2033 {
2034         if (existing->pv_major != request->pv_major)
2035                 return 1;
2036
2037         if (existing->pv_minor > request->pv_minor)
2038                 return 1;
2039
2040         if (existing->pv_minor < request->pv_minor)
2041                 request->pv_minor = existing->pv_minor;
2042
2043         return 0;
2044 }
2045
2046 /*
2047  * dlm_register_domain: one-time setup per "domain".
2048  *
2049  * The filesystem passes in the requested locking version via proto.
2050  * If registration was successful, proto will contain the negotiated
2051  * locking protocol.
2052  */
2053 struct dlm_ctxt * dlm_register_domain(const char *domain,
2054                                u32 key,
2055                                struct dlm_protocol_version *fs_proto)
2056 {
2057         int ret;
2058         struct dlm_ctxt *dlm = NULL;
2059         struct dlm_ctxt *new_ctxt = NULL;
2060
2061         if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2062                 ret = -ENAMETOOLONG;
2063                 mlog(ML_ERROR, "domain name length too long\n");
2064                 goto leave;
2065         }
2066
2067         if (!o2hb_check_local_node_heartbeating()) {
2068                 mlog(ML_ERROR, "the local node has not been configured, or is "
2069                      "not heartbeating\n");
2070                 ret = -EPROTO;
2071                 goto leave;
2072         }
2073
2074         mlog(0, "register called for domain \"%s\"\n", domain);
2075
2076 retry:
2077         dlm = NULL;
2078         if (signal_pending(current)) {
2079                 ret = -ERESTARTSYS;
2080                 mlog_errno(ret);
2081                 goto leave;
2082         }
2083
2084         spin_lock(&dlm_domain_lock);
2085
2086         dlm = __dlm_lookup_domain(domain);
2087         if (dlm) {
2088                 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2089                         spin_unlock(&dlm_domain_lock);
2090
2091                         mlog(0, "This ctxt is not joined yet!\n");
2092                         wait_event_interruptible(dlm_domain_events,
2093                                                  dlm_wait_on_domain_helper(
2094                                                          domain));
2095                         goto retry;
2096                 }
2097
2098                 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2099                         spin_unlock(&dlm_domain_lock);
2100                         mlog(ML_ERROR,
2101                              "Requested locking protocol version is not "
2102                              "compatible with already registered domain "
2103                              "\"%s\"\n", domain);
2104                         ret = -EPROTO;
2105                         goto leave;
2106                 }
2107
2108                 __dlm_get(dlm);
2109                 dlm->num_joins++;
2110
2111                 spin_unlock(&dlm_domain_lock);
2112
2113                 ret = 0;
2114                 goto leave;
2115         }
2116
2117         /* doesn't exist */
2118         if (!new_ctxt) {
2119                 spin_unlock(&dlm_domain_lock);
2120
2121                 new_ctxt = dlm_alloc_ctxt(domain, key);
2122                 if (new_ctxt)
2123                         goto retry;
2124
2125                 ret = -ENOMEM;
2126                 mlog_errno(ret);
2127                 goto leave;
2128         }
2129
2130         /* a little variable switch-a-roo here... */
2131         dlm = new_ctxt;
2132         new_ctxt = NULL;
2133
2134         /* add the new domain */
2135         list_add_tail(&dlm->list, &dlm_domains);
2136         spin_unlock(&dlm_domain_lock);
2137
2138         /*
2139          * Pass the locking protocol version into the join.  If the join
2140          * succeeds, it will have the negotiated protocol set.
2141          */
2142         dlm->dlm_locking_proto = dlm_protocol;
2143         dlm->fs_locking_proto = *fs_proto;
2144
2145         ret = dlm_join_domain(dlm);
2146         if (ret) {
2147                 mlog_errno(ret);
2148                 dlm_put(dlm);
2149                 goto leave;
2150         }
2151
2152         /* Tell the caller what locking protocol we negotiated */
2153         *fs_proto = dlm->fs_locking_proto;
2154
2155         ret = 0;
2156 leave:
2157         if (new_ctxt)
2158                 dlm_free_ctxt_mem(new_ctxt);
2159
2160         if (ret < 0)
2161                 dlm = ERR_PTR(ret);
2162
2163         return dlm;
2164 }
2165 EXPORT_SYMBOL_GPL(dlm_register_domain);
2166
2167 static LIST_HEAD(dlm_join_handlers);
2168
2169 static void dlm_unregister_net_handlers(void)
2170 {
2171         o2net_unregister_handler_list(&dlm_join_handlers);
2172 }
2173
2174 static int dlm_register_net_handlers(void)
2175 {
2176         int status = 0;
2177
2178         status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2179                                         sizeof(struct dlm_query_join_request),
2180                                         dlm_query_join_handler,
2181                                         NULL, NULL, &dlm_join_handlers);
2182         if (status)
2183                 goto bail;
2184
2185         status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2186                                         sizeof(struct dlm_assert_joined),
2187                                         dlm_assert_joined_handler,
2188                                         NULL, NULL, &dlm_join_handlers);
2189         if (status)
2190                 goto bail;
2191
2192         status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2193                                         sizeof(struct dlm_cancel_join),
2194                                         dlm_cancel_join_handler,
2195                                         NULL, NULL, &dlm_join_handlers);
2196         if (status)
2197                 goto bail;
2198
2199         status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2200                                         sizeof(struct dlm_query_region),
2201                                         dlm_query_region_handler,
2202                                         NULL, NULL, &dlm_join_handlers);
2203
2204         if (status)
2205                 goto bail;
2206
2207         status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2208                                         sizeof(struct dlm_query_nodeinfo),
2209                                         dlm_query_nodeinfo_handler,
2210                                         NULL, NULL, &dlm_join_handlers);
2211 bail:
2212         if (status < 0)
2213                 dlm_unregister_net_handlers();
2214
2215         return status;
2216 }
2217
2218 /* Domain eviction callback handling.
2219  *
2220  * The file system requires notification of node death *before* the
2221  * dlm completes it's recovery work, otherwise it may be able to
2222  * acquire locks on resources requiring recovery. Since the dlm can
2223  * evict a node from it's domain *before* heartbeat fires, a similar
2224  * mechanism is required. */
2225
2226 /* Eviction is not expected to happen often, so a per-domain lock is
2227  * not necessary. Eviction callbacks are allowed to sleep for short
2228  * periods of time. */
2229 static DECLARE_RWSEM(dlm_callback_sem);
2230
2231 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2232                                         int node_num)
2233 {
2234         struct list_head *iter;
2235         struct dlm_eviction_cb *cb;
2236
2237         down_read(&dlm_callback_sem);
2238         list_for_each(iter, &dlm->dlm_eviction_callbacks) {
2239                 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
2240
2241                 cb->ec_func(node_num, cb->ec_data);
2242         }
2243         up_read(&dlm_callback_sem);
2244 }
2245
2246 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2247                            dlm_eviction_func *f,
2248                            void *data)
2249 {
2250         INIT_LIST_HEAD(&cb->ec_item);
2251         cb->ec_func = f;
2252         cb->ec_data = data;
2253 }
2254 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2255
2256 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2257                               struct dlm_eviction_cb *cb)
2258 {
2259         down_write(&dlm_callback_sem);
2260         list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2261         up_write(&dlm_callback_sem);
2262 }
2263 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2264
2265 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2266 {
2267         down_write(&dlm_callback_sem);
2268         list_del_init(&cb->ec_item);
2269         up_write(&dlm_callback_sem);
2270 }
2271 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2272
2273 static int __init dlm_init(void)
2274 {
2275         int status;
2276
2277         dlm_print_version();
2278
2279         status = dlm_init_mle_cache();
2280         if (status) {
2281                 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2282                 goto error;
2283         }
2284
2285         status = dlm_init_master_caches();
2286         if (status) {
2287                 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2288                      "o2dlm_lockname slabcaches\n");
2289                 goto error;
2290         }
2291
2292         status = dlm_init_lock_cache();
2293         if (status) {
2294                 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2295                 goto error;
2296         }
2297
2298         status = dlm_register_net_handlers();
2299         if (status) {
2300                 mlog(ML_ERROR, "Unable to register network handlers\n");
2301                 goto error;
2302         }
2303
2304         status = dlm_create_debugfs_root();
2305         if (status)
2306                 goto error;
2307
2308         return 0;
2309 error:
2310         dlm_unregister_net_handlers();
2311         dlm_destroy_lock_cache();
2312         dlm_destroy_master_caches();
2313         dlm_destroy_mle_cache();
2314         return -1;
2315 }
2316
2317 static void __exit dlm_exit (void)
2318 {
2319         dlm_destroy_debugfs_root();
2320         dlm_unregister_net_handlers();
2321         dlm_destroy_lock_cache();
2322         dlm_destroy_master_caches();
2323         dlm_destroy_mle_cache();
2324 }
2325
2326 MODULE_AUTHOR("Oracle");
2327 MODULE_LICENSE("GPL");
2328
2329 module_init(dlm_init);
2330 module_exit(dlm_exit);