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