Merge branch 'core-iommu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-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->dlm_locking_proto.pv_major > 1 ||
1618             dlm->dlm_locking_proto.pv_minor > 0) {
1619                 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1620                 if (status) {
1621                         mlog_errno(status);
1622                         goto bail;
1623                 }
1624                 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1625                 if (status) {
1626                         mlog_errno(status);
1627                         goto bail;
1628                 }
1629         }
1630
1631         dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1632
1633         /* Joined state *must* be set before the joining node
1634          * information, otherwise the query_join handler may read no
1635          * current joiner but a state of NEW and tell joining nodes
1636          * we're not in the domain. */
1637         spin_lock(&dlm_domain_lock);
1638         dlm->dlm_state = DLM_CTXT_JOINED;
1639         dlm->num_joins++;
1640         spin_unlock(&dlm_domain_lock);
1641
1642 bail:
1643         spin_lock(&dlm->spinlock);
1644         __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1645         if (!status)
1646                 __dlm_print_nodes(dlm);
1647         spin_unlock(&dlm->spinlock);
1648
1649         if (ctxt) {
1650                 /* Do we need to send a cancel message to any nodes? */
1651                 if (status < 0) {
1652                         tmpstat = dlm_send_join_cancels(dlm,
1653                                                         ctxt->yes_resp_map,
1654                                                         sizeof(ctxt->yes_resp_map));
1655                         if (tmpstat < 0)
1656                                 mlog_errno(tmpstat);
1657                 }
1658                 kfree(ctxt);
1659         }
1660
1661         mlog(0, "returning %d\n", status);
1662         return status;
1663 }
1664
1665 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1666 {
1667         o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1668         o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1669         o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1670 }
1671
1672 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1673 {
1674         int status;
1675
1676         mlog(0, "registering handlers.\n");
1677
1678         o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1679                             dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1680         status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1681         if (status)
1682                 goto bail;
1683
1684         o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1685                             dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1686         status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1687         if (status)
1688                 goto bail;
1689
1690         status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1691                                         sizeof(struct dlm_master_request),
1692                                         dlm_master_request_handler,
1693                                         dlm, NULL, &dlm->dlm_domain_handlers);
1694         if (status)
1695                 goto bail;
1696
1697         status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1698                                         sizeof(struct dlm_assert_master),
1699                                         dlm_assert_master_handler,
1700                                         dlm, dlm_assert_master_post_handler,
1701                                         &dlm->dlm_domain_handlers);
1702         if (status)
1703                 goto bail;
1704
1705         status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1706                                         sizeof(struct dlm_create_lock),
1707                                         dlm_create_lock_handler,
1708                                         dlm, NULL, &dlm->dlm_domain_handlers);
1709         if (status)
1710                 goto bail;
1711
1712         status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1713                                         DLM_CONVERT_LOCK_MAX_LEN,
1714                                         dlm_convert_lock_handler,
1715                                         dlm, NULL, &dlm->dlm_domain_handlers);
1716         if (status)
1717                 goto bail;
1718
1719         status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1720                                         DLM_UNLOCK_LOCK_MAX_LEN,
1721                                         dlm_unlock_lock_handler,
1722                                         dlm, NULL, &dlm->dlm_domain_handlers);
1723         if (status)
1724                 goto bail;
1725
1726         status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1727                                         DLM_PROXY_AST_MAX_LEN,
1728                                         dlm_proxy_ast_handler,
1729                                         dlm, NULL, &dlm->dlm_domain_handlers);
1730         if (status)
1731                 goto bail;
1732
1733         status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1734                                         sizeof(struct dlm_exit_domain),
1735                                         dlm_exit_domain_handler,
1736                                         dlm, NULL, &dlm->dlm_domain_handlers);
1737         if (status)
1738                 goto bail;
1739
1740         status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1741                                         sizeof(struct dlm_deref_lockres),
1742                                         dlm_deref_lockres_handler,
1743                                         dlm, NULL, &dlm->dlm_domain_handlers);
1744         if (status)
1745                 goto bail;
1746
1747         status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1748                                         sizeof(struct dlm_migrate_request),
1749                                         dlm_migrate_request_handler,
1750                                         dlm, NULL, &dlm->dlm_domain_handlers);
1751         if (status)
1752                 goto bail;
1753
1754         status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1755                                         DLM_MIG_LOCKRES_MAX_LEN,
1756                                         dlm_mig_lockres_handler,
1757                                         dlm, NULL, &dlm->dlm_domain_handlers);
1758         if (status)
1759                 goto bail;
1760
1761         status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1762                                         sizeof(struct dlm_master_requery),
1763                                         dlm_master_requery_handler,
1764                                         dlm, NULL, &dlm->dlm_domain_handlers);
1765         if (status)
1766                 goto bail;
1767
1768         status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1769                                         sizeof(struct dlm_lock_request),
1770                                         dlm_request_all_locks_handler,
1771                                         dlm, NULL, &dlm->dlm_domain_handlers);
1772         if (status)
1773                 goto bail;
1774
1775         status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1776                                         sizeof(struct dlm_reco_data_done),
1777                                         dlm_reco_data_done_handler,
1778                                         dlm, NULL, &dlm->dlm_domain_handlers);
1779         if (status)
1780                 goto bail;
1781
1782         status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1783                                         sizeof(struct dlm_begin_reco),
1784                                         dlm_begin_reco_handler,
1785                                         dlm, NULL, &dlm->dlm_domain_handlers);
1786         if (status)
1787                 goto bail;
1788
1789         status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1790                                         sizeof(struct dlm_finalize_reco),
1791                                         dlm_finalize_reco_handler,
1792                                         dlm, NULL, &dlm->dlm_domain_handlers);
1793         if (status)
1794                 goto bail;
1795
1796 bail:
1797         if (status)
1798                 dlm_unregister_domain_handlers(dlm);
1799
1800         return status;
1801 }
1802
1803 static int dlm_join_domain(struct dlm_ctxt *dlm)
1804 {
1805         int status;
1806         unsigned int backoff;
1807         unsigned int total_backoff = 0;
1808
1809         BUG_ON(!dlm);
1810
1811         mlog(0, "Join domain %s\n", dlm->name);
1812
1813         status = dlm_register_domain_handlers(dlm);
1814         if (status) {
1815                 mlog_errno(status);
1816                 goto bail;
1817         }
1818
1819         status = dlm_debug_init(dlm);
1820         if (status < 0) {
1821                 mlog_errno(status);
1822                 goto bail;
1823         }
1824
1825         status = dlm_launch_thread(dlm);
1826         if (status < 0) {
1827                 mlog_errno(status);
1828                 goto bail;
1829         }
1830
1831         status = dlm_launch_recovery_thread(dlm);
1832         if (status < 0) {
1833                 mlog_errno(status);
1834                 goto bail;
1835         }
1836
1837         dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1838         if (!dlm->dlm_worker) {
1839                 status = -ENOMEM;
1840                 mlog_errno(status);
1841                 goto bail;
1842         }
1843
1844         do {
1845                 status = dlm_try_to_join_domain(dlm);
1846
1847                 /* If we're racing another node to the join, then we
1848                  * need to back off temporarily and let them
1849                  * complete. */
1850 #define DLM_JOIN_TIMEOUT_MSECS  90000
1851                 if (status == -EAGAIN) {
1852                         if (signal_pending(current)) {
1853                                 status = -ERESTARTSYS;
1854                                 goto bail;
1855                         }
1856
1857                         if (total_backoff >
1858                             msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1859                                 status = -ERESTARTSYS;
1860                                 mlog(ML_NOTICE, "Timed out joining dlm domain "
1861                                      "%s after %u msecs\n", dlm->name,
1862                                      jiffies_to_msecs(total_backoff));
1863                                 goto bail;
1864                         }
1865
1866                         /*
1867                          * <chip> After you!
1868                          * <dale> No, after you!
1869                          * <chip> I insist!
1870                          * <dale> But you first!
1871                          * ...
1872                          */
1873                         backoff = (unsigned int)(jiffies & 0x3);
1874                         backoff *= DLM_DOMAIN_BACKOFF_MS;
1875                         total_backoff += backoff;
1876                         mlog(0, "backoff %d\n", backoff);
1877                         msleep(backoff);
1878                 }
1879         } while (status == -EAGAIN);
1880
1881         if (status < 0) {
1882                 mlog_errno(status);
1883                 goto bail;
1884         }
1885
1886         status = 0;
1887 bail:
1888         wake_up(&dlm_domain_events);
1889
1890         if (status) {
1891                 dlm_unregister_domain_handlers(dlm);
1892                 dlm_debug_shutdown(dlm);
1893                 dlm_complete_thread(dlm);
1894                 dlm_complete_recovery_thread(dlm);
1895                 dlm_destroy_dlm_worker(dlm);
1896         }
1897
1898         return status;
1899 }
1900
1901 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1902                                 u32 key)
1903 {
1904         int i;
1905         int ret;
1906         struct dlm_ctxt *dlm = NULL;
1907
1908         dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1909         if (!dlm) {
1910                 mlog_errno(-ENOMEM);
1911                 goto leave;
1912         }
1913
1914         dlm->name = kstrdup(domain, GFP_KERNEL);
1915         if (dlm->name == NULL) {
1916                 mlog_errno(-ENOMEM);
1917                 kfree(dlm);
1918                 dlm = NULL;
1919                 goto leave;
1920         }
1921
1922         dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1923         if (!dlm->lockres_hash) {
1924                 mlog_errno(-ENOMEM);
1925                 kfree(dlm->name);
1926                 kfree(dlm);
1927                 dlm = NULL;
1928                 goto leave;
1929         }
1930
1931         for (i = 0; i < DLM_HASH_BUCKETS; i++)
1932                 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1933
1934         dlm->master_hash = (struct hlist_head **)
1935                                 dlm_alloc_pagevec(DLM_HASH_PAGES);
1936         if (!dlm->master_hash) {
1937                 mlog_errno(-ENOMEM);
1938                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1939                 kfree(dlm->name);
1940                 kfree(dlm);
1941                 dlm = NULL;
1942                 goto leave;
1943         }
1944
1945         for (i = 0; i < DLM_HASH_BUCKETS; i++)
1946                 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1947
1948         dlm->key = key;
1949         dlm->node_num = o2nm_this_node();
1950
1951         ret = dlm_create_debugfs_subroot(dlm);
1952         if (ret < 0) {
1953                 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
1954                 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1955                 kfree(dlm->name);
1956                 kfree(dlm);
1957                 dlm = NULL;
1958                 goto leave;
1959         }
1960
1961         spin_lock_init(&dlm->spinlock);
1962         spin_lock_init(&dlm->master_lock);
1963         spin_lock_init(&dlm->ast_lock);
1964         spin_lock_init(&dlm->track_lock);
1965         INIT_LIST_HEAD(&dlm->list);
1966         INIT_LIST_HEAD(&dlm->dirty_list);
1967         INIT_LIST_HEAD(&dlm->reco.resources);
1968         INIT_LIST_HEAD(&dlm->reco.received);
1969         INIT_LIST_HEAD(&dlm->reco.node_data);
1970         INIT_LIST_HEAD(&dlm->purge_list);
1971         INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1972         INIT_LIST_HEAD(&dlm->tracking_list);
1973         dlm->reco.state = 0;
1974
1975         INIT_LIST_HEAD(&dlm->pending_asts);
1976         INIT_LIST_HEAD(&dlm->pending_basts);
1977
1978         mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1979                   dlm->recovery_map, &(dlm->recovery_map[0]));
1980
1981         memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1982         memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1983         memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1984
1985         dlm->dlm_thread_task = NULL;
1986         dlm->dlm_reco_thread_task = NULL;
1987         dlm->dlm_worker = NULL;
1988         init_waitqueue_head(&dlm->dlm_thread_wq);
1989         init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1990         init_waitqueue_head(&dlm->reco.event);
1991         init_waitqueue_head(&dlm->ast_wq);
1992         init_waitqueue_head(&dlm->migration_wq);
1993         INIT_LIST_HEAD(&dlm->mle_hb_events);
1994
1995         dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1996         init_waitqueue_head(&dlm->dlm_join_events);
1997
1998         dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1999         dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2000
2001         atomic_set(&dlm->res_tot_count, 0);
2002         atomic_set(&dlm->res_cur_count, 0);
2003         for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2004                 atomic_set(&dlm->mle_tot_count[i], 0);
2005                 atomic_set(&dlm->mle_cur_count[i], 0);
2006         }
2007
2008         spin_lock_init(&dlm->work_lock);
2009         INIT_LIST_HEAD(&dlm->work_list);
2010         INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2011
2012         kref_init(&dlm->dlm_refs);
2013         dlm->dlm_state = DLM_CTXT_NEW;
2014
2015         INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2016
2017         mlog(0, "context init: refcount %u\n",
2018                   atomic_read(&dlm->dlm_refs.refcount));
2019
2020 leave:
2021         return dlm;
2022 }
2023
2024 /*
2025  * Compare a requested locking protocol version against the current one.
2026  *
2027  * If the major numbers are different, they are incompatible.
2028  * If the current minor is greater than the request, they are incompatible.
2029  * If the current minor is less than or equal to the request, they are
2030  * compatible, and the requester should run at the current minor version.
2031  */
2032 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2033                                 struct dlm_protocol_version *request)
2034 {
2035         if (existing->pv_major != request->pv_major)
2036                 return 1;
2037
2038         if (existing->pv_minor > request->pv_minor)
2039                 return 1;
2040
2041         if (existing->pv_minor < request->pv_minor)
2042                 request->pv_minor = existing->pv_minor;
2043
2044         return 0;
2045 }
2046
2047 /*
2048  * dlm_register_domain: one-time setup per "domain".
2049  *
2050  * The filesystem passes in the requested locking version via proto.
2051  * If registration was successful, proto will contain the negotiated
2052  * locking protocol.
2053  */
2054 struct dlm_ctxt * dlm_register_domain(const char *domain,
2055                                u32 key,
2056                                struct dlm_protocol_version *fs_proto)
2057 {
2058         int ret;
2059         struct dlm_ctxt *dlm = NULL;
2060         struct dlm_ctxt *new_ctxt = NULL;
2061
2062         if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2063                 ret = -ENAMETOOLONG;
2064                 mlog(ML_ERROR, "domain name length too long\n");
2065                 goto leave;
2066         }
2067
2068         if (!o2hb_check_local_node_heartbeating()) {
2069                 mlog(ML_ERROR, "the local node has not been configured, or is "
2070                      "not heartbeating\n");
2071                 ret = -EPROTO;
2072                 goto leave;
2073         }
2074
2075         mlog(0, "register called for domain \"%s\"\n", domain);
2076
2077 retry:
2078         dlm = NULL;
2079         if (signal_pending(current)) {
2080                 ret = -ERESTARTSYS;
2081                 mlog_errno(ret);
2082                 goto leave;
2083         }
2084
2085         spin_lock(&dlm_domain_lock);
2086
2087         dlm = __dlm_lookup_domain(domain);
2088         if (dlm) {
2089                 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2090                         spin_unlock(&dlm_domain_lock);
2091
2092                         mlog(0, "This ctxt is not joined yet!\n");
2093                         wait_event_interruptible(dlm_domain_events,
2094                                                  dlm_wait_on_domain_helper(
2095                                                          domain));
2096                         goto retry;
2097                 }
2098
2099                 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2100                         spin_unlock(&dlm_domain_lock);
2101                         mlog(ML_ERROR,
2102                              "Requested locking protocol version is not "
2103                              "compatible with already registered domain "
2104                              "\"%s\"\n", domain);
2105                         ret = -EPROTO;
2106                         goto leave;
2107                 }
2108
2109                 __dlm_get(dlm);
2110                 dlm->num_joins++;
2111
2112                 spin_unlock(&dlm_domain_lock);
2113
2114                 ret = 0;
2115                 goto leave;
2116         }
2117
2118         /* doesn't exist */
2119         if (!new_ctxt) {
2120                 spin_unlock(&dlm_domain_lock);
2121
2122                 new_ctxt = dlm_alloc_ctxt(domain, key);
2123                 if (new_ctxt)
2124                         goto retry;
2125
2126                 ret = -ENOMEM;
2127                 mlog_errno(ret);
2128                 goto leave;
2129         }
2130
2131         /* a little variable switch-a-roo here... */
2132         dlm = new_ctxt;
2133         new_ctxt = NULL;
2134
2135         /* add the new domain */
2136         list_add_tail(&dlm->list, &dlm_domains);
2137         spin_unlock(&dlm_domain_lock);
2138
2139         /*
2140          * Pass the locking protocol version into the join.  If the join
2141          * succeeds, it will have the negotiated protocol set.
2142          */
2143         dlm->dlm_locking_proto = dlm_protocol;
2144         dlm->fs_locking_proto = *fs_proto;
2145
2146         ret = dlm_join_domain(dlm);
2147         if (ret) {
2148                 mlog_errno(ret);
2149                 dlm_put(dlm);
2150                 goto leave;
2151         }
2152
2153         /* Tell the caller what locking protocol we negotiated */
2154         *fs_proto = dlm->fs_locking_proto;
2155
2156         ret = 0;
2157 leave:
2158         if (new_ctxt)
2159                 dlm_free_ctxt_mem(new_ctxt);
2160
2161         if (ret < 0)
2162                 dlm = ERR_PTR(ret);
2163
2164         return dlm;
2165 }
2166 EXPORT_SYMBOL_GPL(dlm_register_domain);
2167
2168 static LIST_HEAD(dlm_join_handlers);
2169
2170 static void dlm_unregister_net_handlers(void)
2171 {
2172         o2net_unregister_handler_list(&dlm_join_handlers);
2173 }
2174
2175 static int dlm_register_net_handlers(void)
2176 {
2177         int status = 0;
2178
2179         status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2180                                         sizeof(struct dlm_query_join_request),
2181                                         dlm_query_join_handler,
2182                                         NULL, NULL, &dlm_join_handlers);
2183         if (status)
2184                 goto bail;
2185
2186         status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2187                                         sizeof(struct dlm_assert_joined),
2188                                         dlm_assert_joined_handler,
2189                                         NULL, NULL, &dlm_join_handlers);
2190         if (status)
2191                 goto bail;
2192
2193         status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2194                                         sizeof(struct dlm_cancel_join),
2195                                         dlm_cancel_join_handler,
2196                                         NULL, NULL, &dlm_join_handlers);
2197         if (status)
2198                 goto bail;
2199
2200         status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2201                                         sizeof(struct dlm_query_region),
2202                                         dlm_query_region_handler,
2203                                         NULL, NULL, &dlm_join_handlers);
2204
2205         if (status)
2206                 goto bail;
2207
2208         status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2209                                         sizeof(struct dlm_query_nodeinfo),
2210                                         dlm_query_nodeinfo_handler,
2211                                         NULL, NULL, &dlm_join_handlers);
2212 bail:
2213         if (status < 0)
2214                 dlm_unregister_net_handlers();
2215
2216         return status;
2217 }
2218
2219 /* Domain eviction callback handling.
2220  *
2221  * The file system requires notification of node death *before* the
2222  * dlm completes it's recovery work, otherwise it may be able to
2223  * acquire locks on resources requiring recovery. Since the dlm can
2224  * evict a node from it's domain *before* heartbeat fires, a similar
2225  * mechanism is required. */
2226
2227 /* Eviction is not expected to happen often, so a per-domain lock is
2228  * not necessary. Eviction callbacks are allowed to sleep for short
2229  * periods of time. */
2230 static DECLARE_RWSEM(dlm_callback_sem);
2231
2232 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2233                                         int node_num)
2234 {
2235         struct list_head *iter;
2236         struct dlm_eviction_cb *cb;
2237
2238         down_read(&dlm_callback_sem);
2239         list_for_each(iter, &dlm->dlm_eviction_callbacks) {
2240                 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
2241
2242                 cb->ec_func(node_num, cb->ec_data);
2243         }
2244         up_read(&dlm_callback_sem);
2245 }
2246
2247 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2248                            dlm_eviction_func *f,
2249                            void *data)
2250 {
2251         INIT_LIST_HEAD(&cb->ec_item);
2252         cb->ec_func = f;
2253         cb->ec_data = data;
2254 }
2255 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2256
2257 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2258                               struct dlm_eviction_cb *cb)
2259 {
2260         down_write(&dlm_callback_sem);
2261         list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2262         up_write(&dlm_callback_sem);
2263 }
2264 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2265
2266 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2267 {
2268         down_write(&dlm_callback_sem);
2269         list_del_init(&cb->ec_item);
2270         up_write(&dlm_callback_sem);
2271 }
2272 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2273
2274 static int __init dlm_init(void)
2275 {
2276         int status;
2277
2278         dlm_print_version();
2279
2280         status = dlm_init_mle_cache();
2281         if (status) {
2282                 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2283                 goto error;
2284         }
2285
2286         status = dlm_init_master_caches();
2287         if (status) {
2288                 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2289                      "o2dlm_lockname slabcaches\n");
2290                 goto error;
2291         }
2292
2293         status = dlm_init_lock_cache();
2294         if (status) {
2295                 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2296                 goto error;
2297         }
2298
2299         status = dlm_register_net_handlers();
2300         if (status) {
2301                 mlog(ML_ERROR, "Unable to register network handlers\n");
2302                 goto error;
2303         }
2304
2305         status = dlm_create_debugfs_root();
2306         if (status)
2307                 goto error;
2308
2309         return 0;
2310 error:
2311         dlm_unregister_net_handlers();
2312         dlm_destroy_lock_cache();
2313         dlm_destroy_master_caches();
2314         dlm_destroy_mle_cache();
2315         return -1;
2316 }
2317
2318 static void __exit dlm_exit (void)
2319 {
2320         dlm_destroy_debugfs_root();
2321         dlm_unregister_net_handlers();
2322         dlm_destroy_lock_cache();
2323         dlm_destroy_master_caches();
2324         dlm_destroy_mle_cache();
2325 }
2326
2327 MODULE_AUTHOR("Oracle");
2328 MODULE_LICENSE("GPL");
2329
2330 module_init(dlm_init);
2331 module_exit(dlm_exit);