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