blob: d1c85f10c6342cd0d8896f24f4cb6a9a699c09b9 [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 * dlmmod.c
5 *
6 * standalone DLM module
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
28#include <linux/module.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/highmem.h>
33#include <linux/utsname.h>
34#include <linux/init.h>
35#include <linux/sysctl.h>
36#include <linux/random.h>
37#include <linux/blkdev.h>
38#include <linux/socket.h>
39#include <linux/inet.h>
40#include <linux/spinlock.h>
41#include <linux/delay.h>
42
43
44#include "cluster/heartbeat.h"
45#include "cluster/nodemanager.h"
46#include "cluster/tcp.h"
47
48#include "dlmapi.h"
49#include "dlmcommon.h"
50#include "dlmdebug.h"
Adrian Bunk82353b52005-12-19 11:16:07 -080051#include "dlmdomain.h"
Kurt Hackel6714d8e2005-12-15 14:31:23 -080052
53#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
54#include "cluster/masklog.h"
55
56enum dlm_mle_type {
57 DLM_MLE_BLOCK,
58 DLM_MLE_MASTER,
59 DLM_MLE_MIGRATION
60};
61
62struct dlm_lock_name
63{
64 u8 len;
65 u8 name[DLM_LOCKID_NAME_MAX];
66};
67
68struct dlm_master_list_entry
69{
70 struct list_head list;
71 struct list_head hb_events;
72 struct dlm_ctxt *dlm;
73 spinlock_t spinlock;
74 wait_queue_head_t wq;
75 atomic_t woken;
76 struct kref mle_refs;
77 unsigned long maybe_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
78 unsigned long vote_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
79 unsigned long response_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
80 unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
81 u8 master;
82 u8 new_master;
83 enum dlm_mle_type type;
84 struct o2hb_callback_func mle_hb_up;
85 struct o2hb_callback_func mle_hb_down;
86 union {
87 struct dlm_lock_resource *res;
88 struct dlm_lock_name name;
89 } u;
90};
91
92static void dlm_mle_node_down(struct dlm_ctxt *dlm,
93 struct dlm_master_list_entry *mle,
94 struct o2nm_node *node,
95 int idx);
96static void dlm_mle_node_up(struct dlm_ctxt *dlm,
97 struct dlm_master_list_entry *mle,
98 struct o2nm_node *node,
99 int idx);
100
101static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
102static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
103 unsigned int namelen, void *nodemap,
104 u32 flags);
105
106static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
107 struct dlm_master_list_entry *mle,
108 const char *name,
109 unsigned int namelen)
110{
111 struct dlm_lock_resource *res;
112
113 if (dlm != mle->dlm)
114 return 0;
115
116 if (mle->type == DLM_MLE_BLOCK ||
117 mle->type == DLM_MLE_MIGRATION) {
118 if (namelen != mle->u.name.len ||
119 memcmp(name, mle->u.name.name, namelen)!=0)
120 return 0;
121 } else {
122 res = mle->u.res;
123 if (namelen != res->lockname.len ||
124 memcmp(res->lockname.name, name, namelen) != 0)
125 return 0;
126 }
127 return 1;
128}
129
130#if 0
131/* Code here is included but defined out as it aids debugging */
132
Kurt Hackel95883712006-04-27 18:47:41 -0700133#define dlm_print_nodemap(m) _dlm_print_nodemap(m,#m)
134void _dlm_print_nodemap(unsigned long *map, const char *mapname)
135{
136 int i;
137 printk("%s=[ ", mapname);
138 for (i=0; i<O2NM_MAX_NODES; i++)
139 if (test_bit(i, map))
140 printk("%d ", i);
141 printk("]");
142}
143
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800144void dlm_print_one_mle(struct dlm_master_list_entry *mle)
145{
Kurt Hackel95883712006-04-27 18:47:41 -0700146 int refs;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800147 char *type;
148 char attached;
149 u8 master;
150 unsigned int namelen;
151 const char *name;
152 struct kref *k;
Kurt Hackel95883712006-04-27 18:47:41 -0700153 unsigned long *maybe = mle->maybe_map,
154 *vote = mle->vote_map,
155 *resp = mle->response_map,
156 *node = mle->node_map;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800157
158 k = &mle->mle_refs;
159 if (mle->type == DLM_MLE_BLOCK)
160 type = "BLK";
161 else if (mle->type == DLM_MLE_MASTER)
162 type = "MAS";
163 else
164 type = "MIG";
165 refs = atomic_read(&k->refcount);
166 master = mle->master;
167 attached = (list_empty(&mle->hb_events) ? 'N' : 'Y');
168
169 if (mle->type != DLM_MLE_MASTER) {
170 namelen = mle->u.name.len;
171 name = mle->u.name.name;
172 } else {
173 namelen = mle->u.res->lockname.len;
174 name = mle->u.res->lockname.name;
175 }
176
Kurt Hackel95883712006-04-27 18:47:41 -0700177 mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
178 namelen, name, type, refs, master, mle->new_master, attached,
179 mle->inuse);
180 dlm_print_nodemap(maybe);
181 printk(", ");
182 dlm_print_nodemap(vote);
183 printk(", ");
184 dlm_print_nodemap(resp);
185 printk(", ");
186 dlm_print_nodemap(node);
187 printk(", ");
188 printk("\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800189}
190
191static void dlm_dump_mles(struct dlm_ctxt *dlm)
192{
193 struct dlm_master_list_entry *mle;
194 struct list_head *iter;
195
196 mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800197 spin_lock(&dlm->master_lock);
198 list_for_each(iter, &dlm->master_list) {
199 mle = list_entry(iter, struct dlm_master_list_entry, list);
200 dlm_print_one_mle(mle);
201 }
202 spin_unlock(&dlm->master_lock);
203}
204
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800205int dlm_dump_all_mles(const char __user *data, unsigned int len)
206{
207 struct list_head *iter;
208 struct dlm_ctxt *dlm;
209
210 spin_lock(&dlm_domain_lock);
211 list_for_each(iter, &dlm_domains) {
212 dlm = list_entry (iter, struct dlm_ctxt, list);
213 mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
214 dlm_dump_mles(dlm);
215 }
216 spin_unlock(&dlm_domain_lock);
217 return len;
218}
219EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
220
221#endif /* 0 */
222
223
224static kmem_cache_t *dlm_mle_cache = NULL;
225
226
227static void dlm_mle_release(struct kref *kref);
228static void dlm_init_mle(struct dlm_master_list_entry *mle,
229 enum dlm_mle_type type,
230 struct dlm_ctxt *dlm,
231 struct dlm_lock_resource *res,
232 const char *name,
233 unsigned int namelen);
234static void dlm_put_mle(struct dlm_master_list_entry *mle);
235static void __dlm_put_mle(struct dlm_master_list_entry *mle);
236static int dlm_find_mle(struct dlm_ctxt *dlm,
237 struct dlm_master_list_entry **mle,
238 char *name, unsigned int namelen);
239
240static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to);
241
242
243static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
244 struct dlm_lock_resource *res,
245 struct dlm_master_list_entry *mle,
246 int *blocked);
247static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
248 struct dlm_lock_resource *res,
249 struct dlm_master_list_entry *mle,
250 int blocked);
251static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
252 struct dlm_lock_resource *res,
253 struct dlm_master_list_entry *mle,
254 struct dlm_master_list_entry **oldmle,
255 const char *name, unsigned int namelen,
256 u8 new_master, u8 master);
257
258static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
259 struct dlm_lock_resource *res);
260static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
261 struct dlm_lock_resource *res);
262static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
263 struct dlm_lock_resource *res,
264 u8 target);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800265static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
266 struct dlm_lock_resource *res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800267
268
269int dlm_is_host_down(int errno)
270{
271 switch (errno) {
272 case -EBADF:
273 case -ECONNREFUSED:
274 case -ENOTCONN:
275 case -ECONNRESET:
276 case -EPIPE:
277 case -EHOSTDOWN:
278 case -EHOSTUNREACH:
279 case -ETIMEDOUT:
280 case -ECONNABORTED:
281 case -ENETDOWN:
282 case -ENETUNREACH:
283 case -ENETRESET:
284 case -ESHUTDOWN:
285 case -ENOPROTOOPT:
286 case -EINVAL: /* if returned from our tcp code,
287 this means there is no socket */
288 return 1;
289 }
290 return 0;
291}
292
293
294/*
295 * MASTER LIST FUNCTIONS
296 */
297
298
299/*
300 * regarding master list entries and heartbeat callbacks:
301 *
302 * in order to avoid sleeping and allocation that occurs in
303 * heartbeat, master list entries are simply attached to the
304 * dlm's established heartbeat callbacks. the mle is attached
305 * when it is created, and since the dlm->spinlock is held at
306 * that time, any heartbeat event will be properly discovered
307 * by the mle. the mle needs to be detached from the
308 * dlm->mle_hb_events list as soon as heartbeat events are no
309 * longer useful to the mle, and before the mle is freed.
310 *
311 * as a general rule, heartbeat events are no longer needed by
312 * the mle once an "answer" regarding the lock master has been
313 * received.
314 */
315static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
316 struct dlm_master_list_entry *mle)
317{
318 assert_spin_locked(&dlm->spinlock);
319
320 list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
321}
322
323
324static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
325 struct dlm_master_list_entry *mle)
326{
327 if (!list_empty(&mle->hb_events))
328 list_del_init(&mle->hb_events);
329}
330
331
332static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
333 struct dlm_master_list_entry *mle)
334{
335 spin_lock(&dlm->spinlock);
336 __dlm_mle_detach_hb_events(dlm, mle);
337 spin_unlock(&dlm->spinlock);
338}
339
340/* remove from list and free */
341static void __dlm_put_mle(struct dlm_master_list_entry *mle)
342{
343 struct dlm_ctxt *dlm;
344 dlm = mle->dlm;
345
346 assert_spin_locked(&dlm->spinlock);
347 assert_spin_locked(&dlm->master_lock);
348 BUG_ON(!atomic_read(&mle->mle_refs.refcount));
349
350 kref_put(&mle->mle_refs, dlm_mle_release);
351}
352
353
354/* must not have any spinlocks coming in */
355static void dlm_put_mle(struct dlm_master_list_entry *mle)
356{
357 struct dlm_ctxt *dlm;
358 dlm = mle->dlm;
359
360 spin_lock(&dlm->spinlock);
361 spin_lock(&dlm->master_lock);
362 __dlm_put_mle(mle);
363 spin_unlock(&dlm->master_lock);
364 spin_unlock(&dlm->spinlock);
365}
366
367static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
368{
369 kref_get(&mle->mle_refs);
370}
371
372static void dlm_init_mle(struct dlm_master_list_entry *mle,
373 enum dlm_mle_type type,
374 struct dlm_ctxt *dlm,
375 struct dlm_lock_resource *res,
376 const char *name,
377 unsigned int namelen)
378{
379 assert_spin_locked(&dlm->spinlock);
380
381 mle->dlm = dlm;
382 mle->type = type;
383 INIT_LIST_HEAD(&mle->list);
384 INIT_LIST_HEAD(&mle->hb_events);
385 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
386 spin_lock_init(&mle->spinlock);
387 init_waitqueue_head(&mle->wq);
388 atomic_set(&mle->woken, 0);
389 kref_init(&mle->mle_refs);
390 memset(mle->response_map, 0, sizeof(mle->response_map));
391 mle->master = O2NM_MAX_NODES;
392 mle->new_master = O2NM_MAX_NODES;
393
394 if (mle->type == DLM_MLE_MASTER) {
395 BUG_ON(!res);
396 mle->u.res = res;
397 } else if (mle->type == DLM_MLE_BLOCK) {
398 BUG_ON(!name);
399 memcpy(mle->u.name.name, name, namelen);
400 mle->u.name.len = namelen;
401 } else /* DLM_MLE_MIGRATION */ {
402 BUG_ON(!name);
403 memcpy(mle->u.name.name, name, namelen);
404 mle->u.name.len = namelen;
405 }
406
407 /* copy off the node_map and register hb callbacks on our copy */
408 memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
409 memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
410 clear_bit(dlm->node_num, mle->vote_map);
411 clear_bit(dlm->node_num, mle->node_map);
412
413 /* attach the mle to the domain node up/down events */
414 __dlm_mle_attach_hb_events(dlm, mle);
415}
416
417
418/* returns 1 if found, 0 if not */
419static int dlm_find_mle(struct dlm_ctxt *dlm,
420 struct dlm_master_list_entry **mle,
421 char *name, unsigned int namelen)
422{
423 struct dlm_master_list_entry *tmpmle;
424 struct list_head *iter;
425
426 assert_spin_locked(&dlm->master_lock);
427
428 list_for_each(iter, &dlm->master_list) {
429 tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
430 if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
431 continue;
432 dlm_get_mle(tmpmle);
433 *mle = tmpmle;
434 return 1;
435 }
436 return 0;
437}
438
439void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
440{
441 struct dlm_master_list_entry *mle;
442 struct list_head *iter;
443
444 assert_spin_locked(&dlm->spinlock);
445
446 list_for_each(iter, &dlm->mle_hb_events) {
447 mle = list_entry(iter, struct dlm_master_list_entry,
448 hb_events);
449 if (node_up)
450 dlm_mle_node_up(dlm, mle, NULL, idx);
451 else
452 dlm_mle_node_down(dlm, mle, NULL, idx);
453 }
454}
455
456static void dlm_mle_node_down(struct dlm_ctxt *dlm,
457 struct dlm_master_list_entry *mle,
458 struct o2nm_node *node, int idx)
459{
460 spin_lock(&mle->spinlock);
461
462 if (!test_bit(idx, mle->node_map))
463 mlog(0, "node %u already removed from nodemap!\n", idx);
464 else
465 clear_bit(idx, mle->node_map);
466
467 spin_unlock(&mle->spinlock);
468}
469
470static void dlm_mle_node_up(struct dlm_ctxt *dlm,
471 struct dlm_master_list_entry *mle,
472 struct o2nm_node *node, int idx)
473{
474 spin_lock(&mle->spinlock);
475
476 if (test_bit(idx, mle->node_map))
477 mlog(0, "node %u already in node map!\n", idx);
478 else
479 set_bit(idx, mle->node_map);
480
481 spin_unlock(&mle->spinlock);
482}
483
484
485int dlm_init_mle_cache(void)
486{
487 dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
488 sizeof(struct dlm_master_list_entry),
489 0, SLAB_HWCACHE_ALIGN,
490 NULL, NULL);
491 if (dlm_mle_cache == NULL)
492 return -ENOMEM;
493 return 0;
494}
495
496void dlm_destroy_mle_cache(void)
497{
498 if (dlm_mle_cache)
499 kmem_cache_destroy(dlm_mle_cache);
500}
501
502static void dlm_mle_release(struct kref *kref)
503{
504 struct dlm_master_list_entry *mle;
505 struct dlm_ctxt *dlm;
506
507 mlog_entry_void();
508
509 mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
510 dlm = mle->dlm;
511
512 if (mle->type != DLM_MLE_MASTER) {
513 mlog(0, "calling mle_release for %.*s, type %d\n",
514 mle->u.name.len, mle->u.name.name, mle->type);
515 } else {
516 mlog(0, "calling mle_release for %.*s, type %d\n",
517 mle->u.res->lockname.len,
518 mle->u.res->lockname.name, mle->type);
519 }
520 assert_spin_locked(&dlm->spinlock);
521 assert_spin_locked(&dlm->master_lock);
522
523 /* remove from list if not already */
524 if (!list_empty(&mle->list))
525 list_del_init(&mle->list);
526
527 /* detach the mle from the domain node up/down events */
528 __dlm_mle_detach_hb_events(dlm, mle);
529
530 /* NOTE: kfree under spinlock here.
531 * if this is bad, we can move this to a freelist. */
532 kmem_cache_free(dlm_mle_cache, mle);
533}
534
535
536/*
537 * LOCK RESOURCE FUNCTIONS
538 */
539
540static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
541 struct dlm_lock_resource *res,
542 u8 owner)
543{
544 assert_spin_locked(&res->spinlock);
545
546 mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
547
548 if (owner == dlm->node_num)
549 atomic_inc(&dlm->local_resources);
550 else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
551 atomic_inc(&dlm->unknown_resources);
552 else
553 atomic_inc(&dlm->remote_resources);
554
555 res->owner = owner;
556}
557
558void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
559 struct dlm_lock_resource *res, u8 owner)
560{
561 assert_spin_locked(&res->spinlock);
562
563 if (owner == res->owner)
564 return;
565
566 if (res->owner == dlm->node_num)
567 atomic_dec(&dlm->local_resources);
568 else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
569 atomic_dec(&dlm->unknown_resources);
570 else
571 atomic_dec(&dlm->remote_resources);
572
573 dlm_set_lockres_owner(dlm, res, owner);
574}
575
576
577static void dlm_lockres_release(struct kref *kref)
578{
579 struct dlm_lock_resource *res;
580
581 res = container_of(kref, struct dlm_lock_resource, refs);
582
583 /* This should not happen -- all lockres' have a name
584 * associated with them at init time. */
585 BUG_ON(!res->lockname.name);
586
587 mlog(0, "destroying lockres %.*s\n", res->lockname.len,
588 res->lockname.name);
589
590 /* By the time we're ready to blow this guy away, we shouldn't
591 * be on any lists. */
Mark Fasheh81f20942006-02-28 17:31:22 -0800592 BUG_ON(!hlist_unhashed(&res->hash_node));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800593 BUG_ON(!list_empty(&res->granted));
594 BUG_ON(!list_empty(&res->converting));
595 BUG_ON(!list_empty(&res->blocked));
596 BUG_ON(!list_empty(&res->dirty));
597 BUG_ON(!list_empty(&res->recovering));
598 BUG_ON(!list_empty(&res->purge));
599
600 kfree(res->lockname.name);
601
602 kfree(res);
603}
604
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800605void dlm_lockres_put(struct dlm_lock_resource *res)
606{
607 kref_put(&res->refs, dlm_lockres_release);
608}
609
610static void dlm_init_lockres(struct dlm_ctxt *dlm,
611 struct dlm_lock_resource *res,
612 const char *name, unsigned int namelen)
613{
614 char *qname;
615
616 /* If we memset here, we lose our reference to the kmalloc'd
617 * res->lockname.name, so be sure to init every field
618 * correctly! */
619
620 qname = (char *) res->lockname.name;
621 memcpy(qname, name, namelen);
622
623 res->lockname.len = namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -0800624 res->lockname.hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800625
626 init_waitqueue_head(&res->wq);
627 spin_lock_init(&res->spinlock);
Mark Fasheh81f20942006-02-28 17:31:22 -0800628 INIT_HLIST_NODE(&res->hash_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800629 INIT_LIST_HEAD(&res->granted);
630 INIT_LIST_HEAD(&res->converting);
631 INIT_LIST_HEAD(&res->blocked);
632 INIT_LIST_HEAD(&res->dirty);
633 INIT_LIST_HEAD(&res->recovering);
634 INIT_LIST_HEAD(&res->purge);
635 atomic_set(&res->asts_reserved, 0);
636 res->migration_pending = 0;
637
638 kref_init(&res->refs);
639
640 /* just for consistency */
641 spin_lock(&res->spinlock);
642 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
643 spin_unlock(&res->spinlock);
644
645 res->state = DLM_LOCK_RES_IN_PROGRESS;
646
647 res->last_used = 0;
648
649 memset(res->lvb, 0, DLM_LVB_LEN);
650}
651
652struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
653 const char *name,
654 unsigned int namelen)
655{
656 struct dlm_lock_resource *res;
657
658 res = kmalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
659 if (!res)
660 return NULL;
661
662 res->lockname.name = kmalloc(namelen, GFP_KERNEL);
663 if (!res->lockname.name) {
664 kfree(res);
665 return NULL;
666 }
667
668 dlm_init_lockres(dlm, res, name, namelen);
669 return res;
670}
671
672/*
673 * lookup a lock resource by name.
674 * may already exist in the hashtable.
675 * lockid is null terminated
676 *
677 * if not, allocate enough for the lockres and for
678 * the temporary structure used in doing the mastering.
679 *
680 * also, do a lookup in the dlm->master_list to see
681 * if another node has begun mastering the same lock.
682 * if so, there should be a block entry in there
683 * for this name, and we should *not* attempt to master
684 * the lock here. need to wait around for that node
685 * to assert_master (or die).
686 *
687 */
688struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
689 const char *lockid,
690 int flags)
691{
692 struct dlm_lock_resource *tmpres=NULL, *res=NULL;
693 struct dlm_master_list_entry *mle = NULL;
694 struct dlm_master_list_entry *alloc_mle = NULL;
695 int blocked = 0;
696 int ret, nodenum;
697 struct dlm_node_iter iter;
Mark Fasheha3d33292006-03-09 17:55:56 -0800698 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800699 int tries = 0;
Kurt Hackelc03872f2006-03-06 14:08:49 -0800700 int bit, wait_on_recovery = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800701
702 BUG_ON(!lockid);
703
704 namelen = strlen(lockid);
Mark Fasheha3d33292006-03-09 17:55:56 -0800705 hash = dlm_lockid_hash(lockid, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800706
707 mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
708
709lookup:
710 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -0800711 tmpres = __dlm_lookup_lockres(dlm, lockid, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800712 if (tmpres) {
713 spin_unlock(&dlm->spinlock);
714 mlog(0, "found in hash!\n");
715 if (res)
716 dlm_lockres_put(res);
717 res = tmpres;
718 goto leave;
719 }
720
721 if (!res) {
722 spin_unlock(&dlm->spinlock);
723 mlog(0, "allocating a new resource\n");
724 /* nothing found and we need to allocate one. */
725 alloc_mle = (struct dlm_master_list_entry *)
726 kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
727 if (!alloc_mle)
728 goto leave;
729 res = dlm_new_lockres(dlm, lockid, namelen);
730 if (!res)
731 goto leave;
732 goto lookup;
733 }
734
735 mlog(0, "no lockres found, allocated our own: %p\n", res);
736
737 if (flags & LKM_LOCAL) {
738 /* caller knows it's safe to assume it's not mastered elsewhere
739 * DONE! return right away */
740 spin_lock(&res->spinlock);
741 dlm_change_lockres_owner(dlm, res, dlm->node_num);
742 __dlm_insert_lockres(dlm, res);
743 spin_unlock(&res->spinlock);
744 spin_unlock(&dlm->spinlock);
745 /* lockres still marked IN_PROGRESS */
746 goto wake_waiters;
747 }
748
749 /* check master list to see if another node has started mastering it */
750 spin_lock(&dlm->master_lock);
751
752 /* if we found a block, wait for lock to be mastered by another node */
753 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
754 if (blocked) {
755 if (mle->type == DLM_MLE_MASTER) {
756 mlog(ML_ERROR, "master entry for nonexistent lock!\n");
757 BUG();
758 } else if (mle->type == DLM_MLE_MIGRATION) {
759 /* migration is in progress! */
760 /* the good news is that we now know the
761 * "current" master (mle->master). */
762
763 spin_unlock(&dlm->master_lock);
764 assert_spin_locked(&dlm->spinlock);
765
766 /* set the lockres owner and hash it */
767 spin_lock(&res->spinlock);
768 dlm_set_lockres_owner(dlm, res, mle->master);
769 __dlm_insert_lockres(dlm, res);
770 spin_unlock(&res->spinlock);
771 spin_unlock(&dlm->spinlock);
772
773 /* master is known, detach */
774 dlm_mle_detach_hb_events(dlm, mle);
775 dlm_put_mle(mle);
776 mle = NULL;
777 goto wake_waiters;
778 }
779 } else {
780 /* go ahead and try to master lock on this node */
781 mle = alloc_mle;
782 /* make sure this does not get freed below */
783 alloc_mle = NULL;
784 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
785 set_bit(dlm->node_num, mle->maybe_map);
786 list_add(&mle->list, &dlm->master_list);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800787
788 /* still holding the dlm spinlock, check the recovery map
789 * to see if there are any nodes that still need to be
790 * considered. these will not appear in the mle nodemap
791 * but they might own this lockres. wait on them. */
792 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
793 if (bit < O2NM_MAX_NODES) {
794 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
795 "recover before lock mastery can begin\n",
796 dlm->name, namelen, (char *)lockid, bit);
797 wait_on_recovery = 1;
798 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800799 }
800
801 /* at this point there is either a DLM_MLE_BLOCK or a
802 * DLM_MLE_MASTER on the master list, so it's safe to add the
803 * lockres to the hashtable. anyone who finds the lock will
804 * still have to wait on the IN_PROGRESS. */
805
806 /* finally add the lockres to its hash bucket */
807 __dlm_insert_lockres(dlm, res);
808 /* get an extra ref on the mle in case this is a BLOCK
809 * if so, the creator of the BLOCK may try to put the last
810 * ref at this time in the assert master handler, so we
811 * need an extra one to keep from a bad ptr deref. */
812 dlm_get_mle(mle);
813 spin_unlock(&dlm->master_lock);
814 spin_unlock(&dlm->spinlock);
815
Kurt Hackelc03872f2006-03-06 14:08:49 -0800816 while (wait_on_recovery) {
817 /* any cluster changes that occurred after dropping the
818 * dlm spinlock would be detectable be a change on the mle,
819 * so we only need to clear out the recovery map once. */
820 if (dlm_is_recovery_lock(lockid, namelen)) {
821 mlog(ML_NOTICE, "%s: recovery map is not empty, but "
822 "must master $RECOVERY lock now\n", dlm->name);
823 if (!dlm_pre_master_reco_lockres(dlm, res))
824 wait_on_recovery = 0;
825 else {
826 mlog(0, "%s: waiting 500ms for heartbeat state "
827 "change\n", dlm->name);
828 msleep(500);
829 }
830 continue;
831 }
832
833 dlm_kick_recovery_thread(dlm);
834 msleep(100);
835 dlm_wait_for_recovery(dlm);
836
837 spin_lock(&dlm->spinlock);
838 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
839 if (bit < O2NM_MAX_NODES) {
840 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
841 "recover before lock mastery can begin\n",
842 dlm->name, namelen, (char *)lockid, bit);
843 wait_on_recovery = 1;
844 } else
845 wait_on_recovery = 0;
846 spin_unlock(&dlm->spinlock);
847 }
848
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800849 /* must wait for lock to be mastered elsewhere */
850 if (blocked)
851 goto wait;
852
853redo_request:
854 ret = -EINVAL;
855 dlm_node_iter_init(mle->vote_map, &iter);
856 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
857 ret = dlm_do_master_request(mle, nodenum);
858 if (ret < 0)
859 mlog_errno(ret);
860 if (mle->master != O2NM_MAX_NODES) {
861 /* found a master ! */
Kurt Hackel9c6510a2006-03-02 18:09:26 -0800862 if (mle->master <= nodenum)
863 break;
864 /* if our master request has not reached the master
865 * yet, keep going until it does. this is how the
866 * master will know that asserts are needed back to
867 * the lower nodes. */
868 mlog(0, "%s:%.*s: requests only up to %u but master "
869 "is %u, keep going\n", dlm->name, namelen,
870 lockid, nodenum, mle->master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800871 }
872 }
873
874wait:
875 /* keep going until the response map includes all nodes */
876 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
877 if (ret < 0) {
878 mlog(0, "%s:%.*s: node map changed, redo the "
879 "master request now, blocked=%d\n",
880 dlm->name, res->lockname.len,
881 res->lockname.name, blocked);
882 if (++tries > 20) {
883 mlog(ML_ERROR, "%s:%.*s: spinning on "
884 "dlm_wait_for_lock_mastery, blocked=%d\n",
885 dlm->name, res->lockname.len,
886 res->lockname.name, blocked);
887 dlm_print_one_lock_resource(res);
888 /* dlm_print_one_mle(mle); */
889 tries = 0;
890 }
891 goto redo_request;
892 }
893
894 mlog(0, "lockres mastered by %u\n", res->owner);
895 /* make sure we never continue without this */
896 BUG_ON(res->owner == O2NM_MAX_NODES);
897
898 /* master is known, detach if not already detached */
899 dlm_mle_detach_hb_events(dlm, mle);
900 dlm_put_mle(mle);
901 /* put the extra ref */
902 dlm_put_mle(mle);
903
904wake_waiters:
905 spin_lock(&res->spinlock);
906 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
907 spin_unlock(&res->spinlock);
908 wake_up(&res->wq);
909
910leave:
911 /* need to free the unused mle */
912 if (alloc_mle)
913 kmem_cache_free(dlm_mle_cache, alloc_mle);
914
915 return res;
916}
917
918
919#define DLM_MASTERY_TIMEOUT_MS 5000
920
921static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
922 struct dlm_lock_resource *res,
923 struct dlm_master_list_entry *mle,
924 int *blocked)
925{
926 u8 m;
927 int ret, bit;
928 int map_changed, voting_done;
929 int assert, sleep;
930
931recheck:
932 ret = 0;
933 assert = 0;
934
935 /* check if another node has already become the owner */
936 spin_lock(&res->spinlock);
937 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
Kurt Hackel9c6510a2006-03-02 18:09:26 -0800938 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
939 res->lockname.len, res->lockname.name, res->owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800940 spin_unlock(&res->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -0800941 /* this will cause the master to re-assert across
942 * the whole cluster, freeing up mles */
943 ret = dlm_do_master_request(mle, res->owner);
944 if (ret < 0) {
945 /* give recovery a chance to run */
946 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
947 msleep(500);
948 goto recheck;
949 }
950 ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800951 goto leave;
952 }
953 spin_unlock(&res->spinlock);
954
955 spin_lock(&mle->spinlock);
956 m = mle->master;
957 map_changed = (memcmp(mle->vote_map, mle->node_map,
958 sizeof(mle->vote_map)) != 0);
959 voting_done = (memcmp(mle->vote_map, mle->response_map,
960 sizeof(mle->vote_map)) == 0);
961
962 /* restart if we hit any errors */
963 if (map_changed) {
964 int b;
965 mlog(0, "%s: %.*s: node map changed, restarting\n",
966 dlm->name, res->lockname.len, res->lockname.name);
967 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
968 b = (mle->type == DLM_MLE_BLOCK);
969 if ((*blocked && !b) || (!*blocked && b)) {
970 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
971 dlm->name, res->lockname.len, res->lockname.name,
972 *blocked, b);
973 *blocked = b;
974 }
975 spin_unlock(&mle->spinlock);
976 if (ret < 0) {
977 mlog_errno(ret);
978 goto leave;
979 }
980 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
981 "rechecking now\n", dlm->name, res->lockname.len,
982 res->lockname.name);
983 goto recheck;
984 }
985
986 if (m != O2NM_MAX_NODES) {
987 /* another node has done an assert!
988 * all done! */
989 sleep = 0;
990 } else {
991 sleep = 1;
992 /* have all nodes responded? */
993 if (voting_done && !*blocked) {
994 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
995 if (dlm->node_num <= bit) {
996 /* my node number is lowest.
997 * now tell other nodes that I am
998 * mastering this. */
999 mle->master = dlm->node_num;
1000 assert = 1;
1001 sleep = 0;
1002 }
1003 /* if voting is done, but we have not received
1004 * an assert master yet, we must sleep */
1005 }
1006 }
1007
1008 spin_unlock(&mle->spinlock);
1009
1010 /* sleep if we haven't finished voting yet */
1011 if (sleep) {
1012 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1013
1014 /*
1015 if (atomic_read(&mle->mle_refs.refcount) < 2)
1016 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1017 atomic_read(&mle->mle_refs.refcount),
1018 res->lockname.len, res->lockname.name);
1019 */
1020 atomic_set(&mle->woken, 0);
1021 (void)wait_event_timeout(mle->wq,
1022 (atomic_read(&mle->woken) == 1),
1023 timeo);
1024 if (res->owner == O2NM_MAX_NODES) {
1025 mlog(0, "waiting again\n");
1026 goto recheck;
1027 }
1028 mlog(0, "done waiting, master is %u\n", res->owner);
1029 ret = 0;
1030 goto leave;
1031 }
1032
1033 ret = 0; /* done */
1034 if (assert) {
1035 m = dlm->node_num;
1036 mlog(0, "about to master %.*s here, this=%u\n",
1037 res->lockname.len, res->lockname.name, m);
1038 ret = dlm_do_assert_master(dlm, res->lockname.name,
1039 res->lockname.len, mle->vote_map, 0);
1040 if (ret) {
1041 /* This is a failure in the network path,
1042 * not in the response to the assert_master
1043 * (any nonzero response is a BUG on this node).
1044 * Most likely a socket just got disconnected
1045 * due to node death. */
1046 mlog_errno(ret);
1047 }
1048 /* no longer need to restart lock mastery.
1049 * all living nodes have been contacted. */
1050 ret = 0;
1051 }
1052
1053 /* set the lockres owner */
1054 spin_lock(&res->spinlock);
1055 dlm_change_lockres_owner(dlm, res, m);
1056 spin_unlock(&res->spinlock);
1057
1058leave:
1059 return ret;
1060}
1061
1062struct dlm_bitmap_diff_iter
1063{
1064 int curnode;
1065 unsigned long *orig_bm;
1066 unsigned long *cur_bm;
1067 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1068};
1069
1070enum dlm_node_state_change
1071{
1072 NODE_DOWN = -1,
1073 NODE_NO_CHANGE = 0,
1074 NODE_UP
1075};
1076
1077static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1078 unsigned long *orig_bm,
1079 unsigned long *cur_bm)
1080{
1081 unsigned long p1, p2;
1082 int i;
1083
1084 iter->curnode = -1;
1085 iter->orig_bm = orig_bm;
1086 iter->cur_bm = cur_bm;
1087
1088 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1089 p1 = *(iter->orig_bm + i);
1090 p2 = *(iter->cur_bm + i);
1091 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1092 }
1093}
1094
1095static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1096 enum dlm_node_state_change *state)
1097{
1098 int bit;
1099
1100 if (iter->curnode >= O2NM_MAX_NODES)
1101 return -ENOENT;
1102
1103 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1104 iter->curnode+1);
1105 if (bit >= O2NM_MAX_NODES) {
1106 iter->curnode = O2NM_MAX_NODES;
1107 return -ENOENT;
1108 }
1109
1110 /* if it was there in the original then this node died */
1111 if (test_bit(bit, iter->orig_bm))
1112 *state = NODE_DOWN;
1113 else
1114 *state = NODE_UP;
1115
1116 iter->curnode = bit;
1117 return bit;
1118}
1119
1120
1121static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1122 struct dlm_lock_resource *res,
1123 struct dlm_master_list_entry *mle,
1124 int blocked)
1125{
1126 struct dlm_bitmap_diff_iter bdi;
1127 enum dlm_node_state_change sc;
1128 int node;
1129 int ret = 0;
1130
1131 mlog(0, "something happened such that the "
1132 "master process may need to be restarted!\n");
1133
1134 assert_spin_locked(&mle->spinlock);
1135
1136 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1137 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1138 while (node >= 0) {
1139 if (sc == NODE_UP) {
Kurt Hackele2faea42006-01-12 14:24:55 -08001140 /* a node came up. clear any old vote from
1141 * the response map and set it in the vote map
1142 * then restart the mastery. */
1143 mlog(ML_NOTICE, "node %d up while restarting\n", node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001144
1145 /* redo the master request, but only for the new node */
1146 mlog(0, "sending request to new node\n");
1147 clear_bit(node, mle->response_map);
1148 set_bit(node, mle->vote_map);
1149 } else {
1150 mlog(ML_ERROR, "node down! %d\n", node);
1151
1152 /* if the node wasn't involved in mastery skip it,
1153 * but clear it out from the maps so that it will
1154 * not affect mastery of this lockres */
1155 clear_bit(node, mle->response_map);
1156 clear_bit(node, mle->vote_map);
1157 if (!test_bit(node, mle->maybe_map))
1158 goto next;
1159
1160 /* if we're already blocked on lock mastery, and the
1161 * dead node wasn't the expected master, or there is
1162 * another node in the maybe_map, keep waiting */
1163 if (blocked) {
1164 int lowest = find_next_bit(mle->maybe_map,
1165 O2NM_MAX_NODES, 0);
1166
1167 /* act like it was never there */
1168 clear_bit(node, mle->maybe_map);
1169
1170 if (node != lowest)
1171 goto next;
1172
1173 mlog(ML_ERROR, "expected master %u died while "
1174 "this node was blocked waiting on it!\n",
1175 node);
1176 lowest = find_next_bit(mle->maybe_map,
1177 O2NM_MAX_NODES,
1178 lowest+1);
1179 if (lowest < O2NM_MAX_NODES) {
1180 mlog(0, "still blocked. waiting "
1181 "on %u now\n", lowest);
1182 goto next;
1183 }
1184
1185 /* mle is an MLE_BLOCK, but there is now
1186 * nothing left to block on. we need to return
1187 * all the way back out and try again with
1188 * an MLE_MASTER. dlm_do_local_recovery_cleanup
1189 * has already run, so the mle refcount is ok */
1190 mlog(0, "no longer blocking. we can "
1191 "try to master this here\n");
1192 mle->type = DLM_MLE_MASTER;
1193 memset(mle->maybe_map, 0,
1194 sizeof(mle->maybe_map));
1195 memset(mle->response_map, 0,
1196 sizeof(mle->maybe_map));
1197 memcpy(mle->vote_map, mle->node_map,
1198 sizeof(mle->node_map));
1199 mle->u.res = res;
1200 set_bit(dlm->node_num, mle->maybe_map);
1201
1202 ret = -EAGAIN;
1203 goto next;
1204 }
1205
1206 clear_bit(node, mle->maybe_map);
1207 if (node > dlm->node_num)
1208 goto next;
1209
1210 mlog(0, "dead node in map!\n");
1211 /* yuck. go back and re-contact all nodes
1212 * in the vote_map, removing this node. */
1213 memset(mle->response_map, 0,
1214 sizeof(mle->response_map));
1215 }
1216 ret = -EAGAIN;
1217next:
1218 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1219 }
1220 return ret;
1221}
1222
1223
1224/*
1225 * DLM_MASTER_REQUEST_MSG
1226 *
1227 * returns: 0 on success,
1228 * -errno on a network error
1229 *
1230 * on error, the caller should assume the target node is "dead"
1231 *
1232 */
1233
1234static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to)
1235{
1236 struct dlm_ctxt *dlm = mle->dlm;
1237 struct dlm_master_request request;
1238 int ret, response=0, resend;
1239
1240 memset(&request, 0, sizeof(request));
1241 request.node_idx = dlm->node_num;
1242
1243 BUG_ON(mle->type == DLM_MLE_MIGRATION);
1244
1245 if (mle->type != DLM_MLE_MASTER) {
1246 request.namelen = mle->u.name.len;
1247 memcpy(request.name, mle->u.name.name, request.namelen);
1248 } else {
1249 request.namelen = mle->u.res->lockname.len;
1250 memcpy(request.name, mle->u.res->lockname.name,
1251 request.namelen);
1252 }
1253
1254again:
1255 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1256 sizeof(request), to, &response);
1257 if (ret < 0) {
1258 if (ret == -ESRCH) {
1259 /* should never happen */
1260 mlog(ML_ERROR, "TCP stack not ready!\n");
1261 BUG();
1262 } else if (ret == -EINVAL) {
1263 mlog(ML_ERROR, "bad args passed to o2net!\n");
1264 BUG();
1265 } else if (ret == -ENOMEM) {
1266 mlog(ML_ERROR, "out of memory while trying to send "
1267 "network message! retrying\n");
1268 /* this is totally crude */
1269 msleep(50);
1270 goto again;
1271 } else if (!dlm_is_host_down(ret)) {
1272 /* not a network error. bad. */
1273 mlog_errno(ret);
1274 mlog(ML_ERROR, "unhandled error!");
1275 BUG();
1276 }
1277 /* all other errors should be network errors,
1278 * and likely indicate node death */
1279 mlog(ML_ERROR, "link to %d went down!\n", to);
1280 goto out;
1281 }
1282
1283 ret = 0;
1284 resend = 0;
1285 spin_lock(&mle->spinlock);
1286 switch (response) {
1287 case DLM_MASTER_RESP_YES:
1288 set_bit(to, mle->response_map);
1289 mlog(0, "node %u is the master, response=YES\n", to);
1290 mle->master = to;
1291 break;
1292 case DLM_MASTER_RESP_NO:
1293 mlog(0, "node %u not master, response=NO\n", to);
1294 set_bit(to, mle->response_map);
1295 break;
1296 case DLM_MASTER_RESP_MAYBE:
1297 mlog(0, "node %u not master, response=MAYBE\n", to);
1298 set_bit(to, mle->response_map);
1299 set_bit(to, mle->maybe_map);
1300 break;
1301 case DLM_MASTER_RESP_ERROR:
1302 mlog(0, "node %u hit an error, resending\n", to);
1303 resend = 1;
1304 response = 0;
1305 break;
1306 default:
1307 mlog(ML_ERROR, "bad response! %u\n", response);
1308 BUG();
1309 }
1310 spin_unlock(&mle->spinlock);
1311 if (resend) {
1312 /* this is also totally crude */
1313 msleep(50);
1314 goto again;
1315 }
1316
1317out:
1318 return ret;
1319}
1320
1321/*
1322 * locks that can be taken here:
1323 * dlm->spinlock
1324 * res->spinlock
1325 * mle->spinlock
1326 * dlm->master_list
1327 *
1328 * if possible, TRIM THIS DOWN!!!
1329 */
1330int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data)
1331{
1332 u8 response = DLM_MASTER_RESP_MAYBE;
1333 struct dlm_ctxt *dlm = data;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001334 struct dlm_lock_resource *res = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001335 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1336 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1337 char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08001338 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001339 int found, ret;
1340 int set_maybe;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001341 int dispatch_assert = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001342
1343 if (!dlm_grab(dlm))
1344 return DLM_MASTER_RESP_NO;
1345
1346 if (!dlm_domain_fully_joined(dlm)) {
1347 response = DLM_MASTER_RESP_NO;
1348 goto send_response;
1349 }
1350
1351 name = request->name;
1352 namelen = request->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08001353 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001354
1355 if (namelen > DLM_LOCKID_NAME_MAX) {
1356 response = DLM_IVBUFLEN;
1357 goto send_response;
1358 }
1359
1360way_up_top:
1361 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08001362 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001363 if (res) {
1364 spin_unlock(&dlm->spinlock);
1365
1366 /* take care of the easy cases up front */
1367 spin_lock(&res->spinlock);
1368 if (res->state & DLM_LOCK_RES_RECOVERING) {
1369 spin_unlock(&res->spinlock);
1370 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1371 "being recovered\n");
1372 response = DLM_MASTER_RESP_ERROR;
1373 if (mle)
1374 kmem_cache_free(dlm_mle_cache, mle);
1375 goto send_response;
1376 }
1377
1378 if (res->owner == dlm->node_num) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001379 spin_unlock(&res->spinlock);
1380 // mlog(0, "this node is the master\n");
1381 response = DLM_MASTER_RESP_YES;
1382 if (mle)
1383 kmem_cache_free(dlm_mle_cache, mle);
1384
1385 /* this node is the owner.
1386 * there is some extra work that needs to
1387 * happen now. the requesting node has
1388 * caused all nodes up to this one to
1389 * create mles. this node now needs to
1390 * go back and clean those up. */
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001391 dispatch_assert = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001392 goto send_response;
1393 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1394 spin_unlock(&res->spinlock);
1395 // mlog(0, "node %u is the master\n", res->owner);
1396 response = DLM_MASTER_RESP_NO;
1397 if (mle)
1398 kmem_cache_free(dlm_mle_cache, mle);
1399 goto send_response;
1400 }
1401
1402 /* ok, there is no owner. either this node is
1403 * being blocked, or it is actively trying to
1404 * master this lock. */
1405 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1406 mlog(ML_ERROR, "lock with no owner should be "
1407 "in-progress!\n");
1408 BUG();
1409 }
1410
1411 // mlog(0, "lockres is in progress...\n");
1412 spin_lock(&dlm->master_lock);
1413 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1414 if (!found) {
1415 mlog(ML_ERROR, "no mle found for this lock!\n");
1416 BUG();
1417 }
1418 set_maybe = 1;
1419 spin_lock(&tmpmle->spinlock);
1420 if (tmpmle->type == DLM_MLE_BLOCK) {
1421 // mlog(0, "this node is waiting for "
1422 // "lockres to be mastered\n");
1423 response = DLM_MASTER_RESP_NO;
1424 } else if (tmpmle->type == DLM_MLE_MIGRATION) {
1425 mlog(0, "node %u is master, but trying to migrate to "
1426 "node %u.\n", tmpmle->master, tmpmle->new_master);
1427 if (tmpmle->master == dlm->node_num) {
1428 response = DLM_MASTER_RESP_YES;
1429 mlog(ML_ERROR, "no owner on lockres, but this "
1430 "node is trying to migrate it to %u?!\n",
1431 tmpmle->new_master);
1432 BUG();
1433 } else {
1434 /* the real master can respond on its own */
1435 response = DLM_MASTER_RESP_NO;
1436 }
1437 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1438 set_maybe = 0;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001439 if (tmpmle->master == dlm->node_num) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001440 response = DLM_MASTER_RESP_YES;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001441 /* this node will be the owner.
1442 * go back and clean the mles on any
1443 * other nodes */
1444 dispatch_assert = 1;
1445 } else
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001446 response = DLM_MASTER_RESP_NO;
1447 } else {
1448 // mlog(0, "this node is attempting to "
1449 // "master lockres\n");
1450 response = DLM_MASTER_RESP_MAYBE;
1451 }
1452 if (set_maybe)
1453 set_bit(request->node_idx, tmpmle->maybe_map);
1454 spin_unlock(&tmpmle->spinlock);
1455
1456 spin_unlock(&dlm->master_lock);
1457 spin_unlock(&res->spinlock);
1458
1459 /* keep the mle attached to heartbeat events */
1460 dlm_put_mle(tmpmle);
1461 if (mle)
1462 kmem_cache_free(dlm_mle_cache, mle);
1463 goto send_response;
1464 }
1465
1466 /*
1467 * lockres doesn't exist on this node
1468 * if there is an MLE_BLOCK, return NO
1469 * if there is an MLE_MASTER, return MAYBE
1470 * otherwise, add an MLE_BLOCK, return NO
1471 */
1472 spin_lock(&dlm->master_lock);
1473 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1474 if (!found) {
1475 /* this lockid has never been seen on this node yet */
1476 // mlog(0, "no mle found\n");
1477 if (!mle) {
1478 spin_unlock(&dlm->master_lock);
1479 spin_unlock(&dlm->spinlock);
1480
1481 mle = (struct dlm_master_list_entry *)
1482 kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
1483 if (!mle) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001484 response = DLM_MASTER_RESP_ERROR;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001485 mlog_errno(-ENOMEM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001486 goto send_response;
1487 }
1488 spin_lock(&dlm->spinlock);
1489 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL,
1490 name, namelen);
1491 spin_unlock(&dlm->spinlock);
1492 goto way_up_top;
1493 }
1494
1495 // mlog(0, "this is second time thru, already allocated, "
1496 // "add the block.\n");
1497 set_bit(request->node_idx, mle->maybe_map);
1498 list_add(&mle->list, &dlm->master_list);
1499 response = DLM_MASTER_RESP_NO;
1500 } else {
1501 // mlog(0, "mle was found\n");
1502 set_maybe = 1;
1503 spin_lock(&tmpmle->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001504 if (tmpmle->master == dlm->node_num) {
1505 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1506 BUG();
1507 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001508 if (tmpmle->type == DLM_MLE_BLOCK)
1509 response = DLM_MASTER_RESP_NO;
1510 else if (tmpmle->type == DLM_MLE_MIGRATION) {
1511 mlog(0, "migration mle was found (%u->%u)\n",
1512 tmpmle->master, tmpmle->new_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001513 /* real master can respond on its own */
1514 response = DLM_MASTER_RESP_NO;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001515 } else
1516 response = DLM_MASTER_RESP_MAYBE;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001517 if (set_maybe)
1518 set_bit(request->node_idx, tmpmle->maybe_map);
1519 spin_unlock(&tmpmle->spinlock);
1520 }
1521 spin_unlock(&dlm->master_lock);
1522 spin_unlock(&dlm->spinlock);
1523
1524 if (found) {
1525 /* keep the mle attached to heartbeat events */
1526 dlm_put_mle(tmpmle);
1527 }
1528send_response:
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001529
1530 if (dispatch_assert) {
1531 if (response != DLM_MASTER_RESP_YES)
1532 mlog(ML_ERROR, "invalid response %d\n", response);
1533 if (!res) {
1534 mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1535 BUG();
1536 }
1537 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1538 dlm->node_num, res->lockname.len, res->lockname.name);
1539 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1540 DLM_ASSERT_MASTER_MLE_CLEANUP);
1541 if (ret < 0) {
1542 mlog(ML_ERROR, "failed to dispatch assert master work\n");
1543 response = DLM_MASTER_RESP_ERROR;
1544 }
1545 }
1546
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001547 dlm_put(dlm);
1548 return response;
1549}
1550
1551/*
1552 * DLM_ASSERT_MASTER_MSG
1553 */
1554
1555
1556/*
1557 * NOTE: this can be used for debugging
1558 * can periodically run all locks owned by this node
1559 * and re-assert across the cluster...
1560 */
1561static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
1562 unsigned int namelen, void *nodemap,
1563 u32 flags)
1564{
1565 struct dlm_assert_master assert;
1566 int to, tmpret;
1567 struct dlm_node_iter iter;
1568 int ret = 0;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001569 int reassert;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001570
1571 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001572again:
1573 reassert = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001574
1575 /* note that if this nodemap is empty, it returns 0 */
1576 dlm_node_iter_init(nodemap, &iter);
1577 while ((to = dlm_node_iter_next(&iter)) >= 0) {
1578 int r = 0;
1579 mlog(0, "sending assert master to %d (%.*s)\n", to,
1580 namelen, lockname);
1581 memset(&assert, 0, sizeof(assert));
1582 assert.node_idx = dlm->node_num;
1583 assert.namelen = namelen;
1584 memcpy(assert.name, lockname, namelen);
1585 assert.flags = cpu_to_be32(flags);
1586
1587 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1588 &assert, sizeof(assert), to, &r);
1589 if (tmpret < 0) {
1590 mlog(ML_ERROR, "assert_master returned %d!\n", tmpret);
1591 if (!dlm_is_host_down(tmpret)) {
1592 mlog(ML_ERROR, "unhandled error!\n");
1593 BUG();
1594 }
1595 /* a node died. finish out the rest of the nodes. */
1596 mlog(ML_ERROR, "link to %d went down!\n", to);
1597 /* any nonzero status return will do */
1598 ret = tmpret;
1599 } else if (r < 0) {
1600 /* ok, something horribly messed. kill thyself. */
1601 mlog(ML_ERROR,"during assert master of %.*s to %u, "
1602 "got %d.\n", namelen, lockname, to, r);
1603 dlm_dump_lock_resources(dlm);
1604 BUG();
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001605 } else if (r == EAGAIN) {
1606 mlog(0, "%.*s: node %u create mles on other "
1607 "nodes and requests a re-assert\n",
1608 namelen, lockname, to);
1609 reassert = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001610 }
1611 }
1612
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001613 if (reassert)
1614 goto again;
1615
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001616 return ret;
1617}
1618
1619/*
1620 * locks that can be taken here:
1621 * dlm->spinlock
1622 * res->spinlock
1623 * mle->spinlock
1624 * dlm->master_list
1625 *
1626 * if possible, TRIM THIS DOWN!!!
1627 */
1628int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
1629{
1630 struct dlm_ctxt *dlm = data;
1631 struct dlm_master_list_entry *mle = NULL;
1632 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1633 struct dlm_lock_resource *res = NULL;
1634 char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08001635 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001636 u32 flags;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001637 int master_request = 0;
1638 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001639
1640 if (!dlm_grab(dlm))
1641 return 0;
1642
1643 name = assert->name;
1644 namelen = assert->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08001645 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001646 flags = be32_to_cpu(assert->flags);
1647
1648 if (namelen > DLM_LOCKID_NAME_MAX) {
1649 mlog(ML_ERROR, "Invalid name length!");
1650 goto done;
1651 }
1652
1653 spin_lock(&dlm->spinlock);
1654
1655 if (flags)
1656 mlog(0, "assert_master with flags: %u\n", flags);
1657
1658 /* find the MLE */
1659 spin_lock(&dlm->master_lock);
1660 if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1661 /* not an error, could be master just re-asserting */
1662 mlog(0, "just got an assert_master from %u, but no "
1663 "MLE for it! (%.*s)\n", assert->node_idx,
1664 namelen, name);
1665 } else {
1666 int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1667 if (bit >= O2NM_MAX_NODES) {
1668 /* not necessarily an error, though less likely.
1669 * could be master just re-asserting. */
1670 mlog(ML_ERROR, "no bits set in the maybe_map, but %u "
1671 "is asserting! (%.*s)\n", assert->node_idx,
1672 namelen, name);
1673 } else if (bit != assert->node_idx) {
1674 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1675 mlog(0, "master %u was found, %u should "
1676 "back off\n", assert->node_idx, bit);
1677 } else {
1678 /* with the fix for bug 569, a higher node
1679 * number winning the mastery will respond
1680 * YES to mastery requests, but this node
1681 * had no way of knowing. let it pass. */
1682 mlog(ML_ERROR, "%u is the lowest node, "
1683 "%u is asserting. (%.*s) %u must "
1684 "have begun after %u won.\n", bit,
1685 assert->node_idx, namelen, name, bit,
1686 assert->node_idx);
1687 }
1688 }
1689 }
1690 spin_unlock(&dlm->master_lock);
1691
1692 /* ok everything checks out with the MLE
1693 * now check to see if there is a lockres */
Mark Fasheha3d33292006-03-09 17:55:56 -08001694 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001695 if (res) {
1696 spin_lock(&res->spinlock);
1697 if (res->state & DLM_LOCK_RES_RECOVERING) {
1698 mlog(ML_ERROR, "%u asserting but %.*s is "
1699 "RECOVERING!\n", assert->node_idx, namelen, name);
1700 goto kill;
1701 }
1702 if (!mle) {
1703 if (res->owner != assert->node_idx) {
1704 mlog(ML_ERROR, "assert_master from "
1705 "%u, but current owner is "
1706 "%u! (%.*s)\n",
1707 assert->node_idx, res->owner,
1708 namelen, name);
1709 goto kill;
1710 }
1711 } else if (mle->type != DLM_MLE_MIGRATION) {
1712 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1713 /* owner is just re-asserting */
1714 if (res->owner == assert->node_idx) {
1715 mlog(0, "owner %u re-asserting on "
1716 "lock %.*s\n", assert->node_idx,
1717 namelen, name);
1718 goto ok;
1719 }
1720 mlog(ML_ERROR, "got assert_master from "
1721 "node %u, but %u is the owner! "
1722 "(%.*s)\n", assert->node_idx,
1723 res->owner, namelen, name);
1724 goto kill;
1725 }
1726 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1727 mlog(ML_ERROR, "got assert from %u, but lock "
1728 "with no owner should be "
1729 "in-progress! (%.*s)\n",
1730 assert->node_idx,
1731 namelen, name);
1732 goto kill;
1733 }
1734 } else /* mle->type == DLM_MLE_MIGRATION */ {
1735 /* should only be getting an assert from new master */
1736 if (assert->node_idx != mle->new_master) {
1737 mlog(ML_ERROR, "got assert from %u, but "
1738 "new master is %u, and old master "
1739 "was %u (%.*s)\n",
1740 assert->node_idx, mle->new_master,
1741 mle->master, namelen, name);
1742 goto kill;
1743 }
1744
1745 }
1746ok:
1747 spin_unlock(&res->spinlock);
1748 }
1749 spin_unlock(&dlm->spinlock);
1750
1751 // mlog(0, "woo! got an assert_master from node %u!\n",
1752 // assert->node_idx);
1753 if (mle) {
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001754 int extra_ref = 0;
1755 int nn = -1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001756
1757 spin_lock(&mle->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001758 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1759 extra_ref = 1;
1760 else {
1761 /* MASTER mle: if any bits set in the response map
1762 * then the calling node needs to re-assert to clear
1763 * up nodes that this node contacted */
1764 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1765 nn+1)) < O2NM_MAX_NODES) {
1766 if (nn != dlm->node_num && nn != assert->node_idx)
1767 master_request = 1;
1768 }
1769 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001770 mle->master = assert->node_idx;
1771 atomic_set(&mle->woken, 1);
1772 wake_up(&mle->wq);
1773 spin_unlock(&mle->spinlock);
1774
1775 if (mle->type == DLM_MLE_MIGRATION && res) {
1776 mlog(0, "finishing off migration of lockres %.*s, "
1777 "from %u to %u\n",
1778 res->lockname.len, res->lockname.name,
1779 dlm->node_num, mle->new_master);
1780 spin_lock(&res->spinlock);
1781 res->state &= ~DLM_LOCK_RES_MIGRATING;
1782 dlm_change_lockres_owner(dlm, res, mle->new_master);
1783 BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
1784 spin_unlock(&res->spinlock);
1785 }
1786 /* master is known, detach if not already detached */
1787 dlm_mle_detach_hb_events(dlm, mle);
1788 dlm_put_mle(mle);
1789
1790 if (extra_ref) {
1791 /* the assert master message now balances the extra
1792 * ref given by the master / migration request message.
1793 * if this is the last put, it will be removed
1794 * from the list. */
1795 dlm_put_mle(mle);
1796 }
1797 }
1798
1799done:
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001800 ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001801 if (res)
1802 dlm_lockres_put(res);
1803 dlm_put(dlm);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001804 if (master_request) {
1805 mlog(0, "need to tell master to reassert\n");
1806 ret = EAGAIN; // positive. negative would shoot down the node.
1807 }
1808 return ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001809
1810kill:
1811 /* kill the caller! */
1812 spin_unlock(&res->spinlock);
1813 spin_unlock(&dlm->spinlock);
1814 dlm_lockres_put(res);
1815 mlog(ML_ERROR, "Bad message received from another node. Dumping state "
1816 "and killing the other node now! This node is OK and can continue.\n");
1817 dlm_dump_lock_resources(dlm);
1818 dlm_put(dlm);
1819 return -EINVAL;
1820}
1821
1822int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
1823 struct dlm_lock_resource *res,
1824 int ignore_higher, u8 request_from, u32 flags)
1825{
1826 struct dlm_work_item *item;
1827 item = kcalloc(1, sizeof(*item), GFP_KERNEL);
1828 if (!item)
1829 return -ENOMEM;
1830
1831
1832 /* queue up work for dlm_assert_master_worker */
1833 dlm_grab(dlm); /* get an extra ref for the work item */
1834 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
1835 item->u.am.lockres = res; /* already have a ref */
1836 /* can optionally ignore node numbers higher than this node */
1837 item->u.am.ignore_higher = ignore_higher;
1838 item->u.am.request_from = request_from;
1839 item->u.am.flags = flags;
1840
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001841 if (ignore_higher)
1842 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
1843 res->lockname.name);
1844
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001845 spin_lock(&dlm->work_lock);
1846 list_add_tail(&item->list, &dlm->work_list);
1847 spin_unlock(&dlm->work_lock);
1848
1849 schedule_work(&dlm->dispatched_work);
1850 return 0;
1851}
1852
1853static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
1854{
1855 struct dlm_ctxt *dlm = data;
1856 int ret = 0;
1857 struct dlm_lock_resource *res;
1858 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
1859 int ignore_higher;
1860 int bit;
1861 u8 request_from;
1862 u32 flags;
1863
1864 dlm = item->dlm;
1865 res = item->u.am.lockres;
1866 ignore_higher = item->u.am.ignore_higher;
1867 request_from = item->u.am.request_from;
1868 flags = item->u.am.flags;
1869
1870 spin_lock(&dlm->spinlock);
1871 memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
1872 spin_unlock(&dlm->spinlock);
1873
1874 clear_bit(dlm->node_num, nodemap);
1875 if (ignore_higher) {
1876 /* if is this just to clear up mles for nodes below
1877 * this node, do not send the message to the original
1878 * caller or any node number higher than this */
1879 clear_bit(request_from, nodemap);
1880 bit = dlm->node_num;
1881 while (1) {
1882 bit = find_next_bit(nodemap, O2NM_MAX_NODES,
1883 bit+1);
1884 if (bit >= O2NM_MAX_NODES)
1885 break;
1886 clear_bit(bit, nodemap);
1887 }
1888 }
1889
1890 /* this call now finishes out the nodemap
1891 * even if one or more nodes die */
1892 mlog(0, "worker about to master %.*s here, this=%u\n",
1893 res->lockname.len, res->lockname.name, dlm->node_num);
1894 ret = dlm_do_assert_master(dlm, res->lockname.name,
1895 res->lockname.len,
1896 nodemap, flags);
1897 if (ret < 0) {
1898 /* no need to restart, we are done */
1899 mlog_errno(ret);
1900 }
1901
1902 dlm_lockres_put(res);
1903
1904 mlog(0, "finished with dlm_assert_master_worker\n");
1905}
1906
Kurt Hackelc03872f2006-03-06 14:08:49 -08001907/* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
1908 * We cannot wait for node recovery to complete to begin mastering this
1909 * lockres because this lockres is used to kick off recovery! ;-)
1910 * So, do a pre-check on all living nodes to see if any of those nodes
1911 * think that $RECOVERY is currently mastered by a dead node. If so,
1912 * we wait a short time to allow that node to get notified by its own
1913 * heartbeat stack, then check again. All $RECOVERY lock resources
1914 * mastered by dead nodes are purged when the hearbeat callback is
1915 * fired, so we can know for sure that it is safe to continue once
1916 * the node returns a live node or no node. */
1917static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
1918 struct dlm_lock_resource *res)
1919{
1920 struct dlm_node_iter iter;
1921 int nodenum;
1922 int ret = 0;
1923 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
1924
1925 spin_lock(&dlm->spinlock);
1926 dlm_node_iter_init(dlm->domain_map, &iter);
1927 spin_unlock(&dlm->spinlock);
1928
1929 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1930 /* do not send to self */
1931 if (nodenum == dlm->node_num)
1932 continue;
1933 ret = dlm_do_master_requery(dlm, res, nodenum, &master);
1934 if (ret < 0) {
1935 mlog_errno(ret);
1936 if (!dlm_is_host_down(ret))
1937 BUG();
1938 /* host is down, so answer for that node would be
1939 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
1940 }
1941
1942 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1943 /* check to see if this master is in the recovery map */
1944 spin_lock(&dlm->spinlock);
1945 if (test_bit(master, dlm->recovery_map)) {
1946 mlog(ML_NOTICE, "%s: node %u has not seen "
1947 "node %u go down yet, and thinks the "
1948 "dead node is mastering the recovery "
1949 "lock. must wait.\n", dlm->name,
1950 nodenum, master);
1951 ret = -EAGAIN;
1952 }
1953 spin_unlock(&dlm->spinlock);
1954 mlog(0, "%s: reco lock master is %u\n", dlm->name,
1955 master);
1956 break;
1957 }
1958 }
1959 return ret;
1960}
1961
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001962
1963/*
1964 * DLM_MIGRATE_LOCKRES
1965 */
1966
1967
1968int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1969 u8 target)
1970{
1971 struct dlm_master_list_entry *mle = NULL;
1972 struct dlm_master_list_entry *oldmle = NULL;
1973 struct dlm_migratable_lockres *mres = NULL;
1974 int ret = -EINVAL;
1975 const char *name;
1976 unsigned int namelen;
1977 int mle_added = 0;
1978 struct list_head *queue, *iter;
1979 int i;
1980 struct dlm_lock *lock;
1981 int empty = 1;
1982
1983 if (!dlm_grab(dlm))
1984 return -EINVAL;
1985
1986 name = res->lockname.name;
1987 namelen = res->lockname.len;
1988
1989 mlog(0, "migrating %.*s to %u\n", namelen, name, target);
1990
1991 /*
1992 * ensure this lockres is a proper candidate for migration
1993 */
1994 spin_lock(&res->spinlock);
1995 if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1996 mlog(0, "cannot migrate lockres with unknown owner!\n");
1997 spin_unlock(&res->spinlock);
1998 goto leave;
1999 }
2000 if (res->owner != dlm->node_num) {
2001 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2002 spin_unlock(&res->spinlock);
2003 goto leave;
2004 }
2005 mlog(0, "checking queues...\n");
2006 queue = &res->granted;
2007 for (i=0; i<3; i++) {
2008 list_for_each(iter, queue) {
2009 lock = list_entry (iter, struct dlm_lock, list);
2010 empty = 0;
2011 if (lock->ml.node == dlm->node_num) {
2012 mlog(0, "found a lock owned by this node "
2013 "still on the %s queue! will not "
2014 "migrate this lockres\n",
2015 i==0 ? "granted" :
2016 (i==1 ? "converting" : "blocked"));
2017 spin_unlock(&res->spinlock);
2018 ret = -ENOTEMPTY;
2019 goto leave;
2020 }
2021 }
2022 queue++;
2023 }
2024 mlog(0, "all locks on this lockres are nonlocal. continuing\n");
2025 spin_unlock(&res->spinlock);
2026
2027 /* no work to do */
2028 if (empty) {
2029 mlog(0, "no locks were found on this lockres! done!\n");
2030 ret = 0;
2031 goto leave;
2032 }
2033
2034 /*
2035 * preallocate up front
2036 * if this fails, abort
2037 */
2038
2039 ret = -ENOMEM;
2040 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_KERNEL);
2041 if (!mres) {
2042 mlog_errno(ret);
2043 goto leave;
2044 }
2045
2046 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2047 GFP_KERNEL);
2048 if (!mle) {
2049 mlog_errno(ret);
2050 goto leave;
2051 }
2052 ret = 0;
2053
2054 /*
2055 * find a node to migrate the lockres to
2056 */
2057
2058 mlog(0, "picking a migration node\n");
2059 spin_lock(&dlm->spinlock);
2060 /* pick a new node */
2061 if (!test_bit(target, dlm->domain_map) ||
2062 target >= O2NM_MAX_NODES) {
2063 target = dlm_pick_migration_target(dlm, res);
2064 }
2065 mlog(0, "node %u chosen for migration\n", target);
2066
2067 if (target >= O2NM_MAX_NODES ||
2068 !test_bit(target, dlm->domain_map)) {
2069 /* target chosen is not alive */
2070 ret = -EINVAL;
2071 }
2072
2073 if (ret) {
2074 spin_unlock(&dlm->spinlock);
2075 goto fail;
2076 }
2077
2078 mlog(0, "continuing with target = %u\n", target);
2079
2080 /*
2081 * clear any existing master requests and
2082 * add the migration mle to the list
2083 */
2084 spin_lock(&dlm->master_lock);
2085 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2086 namelen, target, dlm->node_num);
2087 spin_unlock(&dlm->master_lock);
2088 spin_unlock(&dlm->spinlock);
2089
2090 if (ret == -EEXIST) {
2091 mlog(0, "another process is already migrating it\n");
2092 goto fail;
2093 }
2094 mle_added = 1;
2095
2096 /*
2097 * set the MIGRATING flag and flush asts
2098 * if we fail after this we need to re-dirty the lockres
2099 */
2100 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2101 mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2102 "the target went down.\n", res->lockname.len,
2103 res->lockname.name, target);
2104 spin_lock(&res->spinlock);
2105 res->state &= ~DLM_LOCK_RES_MIGRATING;
2106 spin_unlock(&res->spinlock);
2107 ret = -EINVAL;
2108 }
2109
2110fail:
2111 if (oldmle) {
2112 /* master is known, detach if not already detached */
2113 dlm_mle_detach_hb_events(dlm, oldmle);
2114 dlm_put_mle(oldmle);
2115 }
2116
2117 if (ret < 0) {
2118 if (mle_added) {
2119 dlm_mle_detach_hb_events(dlm, mle);
2120 dlm_put_mle(mle);
2121 } else if (mle) {
2122 kmem_cache_free(dlm_mle_cache, mle);
2123 }
2124 goto leave;
2125 }
2126
2127 /*
2128 * at this point, we have a migration target, an mle
2129 * in the master list, and the MIGRATING flag set on
2130 * the lockres
2131 */
2132
2133
2134 /* get an extra reference on the mle.
2135 * otherwise the assert_master from the new
2136 * master will destroy this.
2137 * also, make sure that all callers of dlm_get_mle
2138 * take both dlm->spinlock and dlm->master_lock */
2139 spin_lock(&dlm->spinlock);
2140 spin_lock(&dlm->master_lock);
2141 dlm_get_mle(mle);
2142 spin_unlock(&dlm->master_lock);
2143 spin_unlock(&dlm->spinlock);
2144
2145 /* notify new node and send all lock state */
2146 /* call send_one_lockres with migration flag.
2147 * this serves as notice to the target node that a
2148 * migration is starting. */
2149 ret = dlm_send_one_lockres(dlm, res, mres, target,
2150 DLM_MRES_MIGRATION);
2151
2152 if (ret < 0) {
2153 mlog(0, "migration to node %u failed with %d\n",
2154 target, ret);
2155 /* migration failed, detach and clean up mle */
2156 dlm_mle_detach_hb_events(dlm, mle);
2157 dlm_put_mle(mle);
2158 dlm_put_mle(mle);
2159 goto leave;
2160 }
2161
2162 /* at this point, the target sends a message to all nodes,
2163 * (using dlm_do_migrate_request). this node is skipped since
2164 * we had to put an mle in the list to begin the process. this
2165 * node now waits for target to do an assert master. this node
2166 * will be the last one notified, ensuring that the migration
2167 * is complete everywhere. if the target dies while this is
2168 * going on, some nodes could potentially see the target as the
2169 * master, so it is important that my recovery finds the migration
2170 * mle and sets the master to UNKNONWN. */
2171
2172
2173 /* wait for new node to assert master */
2174 while (1) {
2175 ret = wait_event_interruptible_timeout(mle->wq,
2176 (atomic_read(&mle->woken) == 1),
2177 msecs_to_jiffies(5000));
2178
2179 if (ret >= 0) {
2180 if (atomic_read(&mle->woken) == 1 ||
2181 res->owner == target)
2182 break;
2183
2184 mlog(0, "timed out during migration\n");
Kurt Hackele2faea42006-01-12 14:24:55 -08002185 /* avoid hang during shutdown when migrating lockres
2186 * to a node which also goes down */
2187 if (dlm_is_node_dead(dlm, target)) {
2188 mlog(0, "%s:%.*s: expected migration target %u "
2189 "is no longer up. restarting.\n",
2190 dlm->name, res->lockname.len,
2191 res->lockname.name, target);
2192 ret = -ERESTARTSYS;
2193 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002194 }
2195 if (ret == -ERESTARTSYS) {
2196 /* migration failed, detach and clean up mle */
2197 dlm_mle_detach_hb_events(dlm, mle);
2198 dlm_put_mle(mle);
2199 dlm_put_mle(mle);
2200 goto leave;
2201 }
2202 /* TODO: if node died: stop, clean up, return error */
2203 }
2204
2205 /* all done, set the owner, clear the flag */
2206 spin_lock(&res->spinlock);
2207 dlm_set_lockres_owner(dlm, res, target);
2208 res->state &= ~DLM_LOCK_RES_MIGRATING;
2209 dlm_remove_nonlocal_locks(dlm, res);
2210 spin_unlock(&res->spinlock);
2211 wake_up(&res->wq);
2212
2213 /* master is known, detach if not already detached */
2214 dlm_mle_detach_hb_events(dlm, mle);
2215 dlm_put_mle(mle);
2216 ret = 0;
2217
2218 dlm_lockres_calc_usage(dlm, res);
2219
2220leave:
2221 /* re-dirty the lockres if we failed */
2222 if (ret < 0)
2223 dlm_kick_thread(dlm, res);
2224
2225 /* TODO: cleanup */
2226 if (mres)
2227 free_page((unsigned long)mres);
2228
2229 dlm_put(dlm);
2230
2231 mlog(0, "returning %d\n", ret);
2232 return ret;
2233}
2234EXPORT_SYMBOL_GPL(dlm_migrate_lockres);
2235
2236int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2237{
2238 int ret;
2239 spin_lock(&dlm->ast_lock);
2240 spin_lock(&lock->spinlock);
2241 ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2242 spin_unlock(&lock->spinlock);
2243 spin_unlock(&dlm->ast_lock);
2244 return ret;
2245}
2246
2247static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2248 struct dlm_lock_resource *res,
2249 u8 mig_target)
2250{
2251 int can_proceed;
2252 spin_lock(&res->spinlock);
2253 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2254 spin_unlock(&res->spinlock);
2255
2256 /* target has died, so make the caller break out of the
2257 * wait_event, but caller must recheck the domain_map */
2258 spin_lock(&dlm->spinlock);
2259 if (!test_bit(mig_target, dlm->domain_map))
2260 can_proceed = 1;
2261 spin_unlock(&dlm->spinlock);
2262 return can_proceed;
2263}
2264
2265int dlm_lockres_is_dirty(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2266{
2267 int ret;
2268 spin_lock(&res->spinlock);
2269 ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2270 spin_unlock(&res->spinlock);
2271 return ret;
2272}
2273
2274
2275static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2276 struct dlm_lock_resource *res,
2277 u8 target)
2278{
2279 int ret = 0;
2280
2281 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2282 res->lockname.len, res->lockname.name, dlm->node_num,
2283 target);
2284 /* need to set MIGRATING flag on lockres. this is done by
2285 * ensuring that all asts have been flushed for this lockres. */
2286 spin_lock(&res->spinlock);
2287 BUG_ON(res->migration_pending);
2288 res->migration_pending = 1;
2289 /* strategy is to reserve an extra ast then release
2290 * it below, letting the release do all of the work */
2291 __dlm_lockres_reserve_ast(res);
2292 spin_unlock(&res->spinlock);
2293
2294 /* now flush all the pending asts.. hang out for a bit */
2295 dlm_kick_thread(dlm, res);
2296 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2297 dlm_lockres_release_ast(dlm, res);
2298
2299 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2300 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2301 /* if the extra ref we just put was the final one, this
2302 * will pass thru immediately. otherwise, we need to wait
2303 * for the last ast to finish. */
2304again:
2305 ret = wait_event_interruptible_timeout(dlm->migration_wq,
2306 dlm_migration_can_proceed(dlm, res, target),
2307 msecs_to_jiffies(1000));
2308 if (ret < 0) {
2309 mlog(0, "woken again: migrating? %s, dead? %s\n",
2310 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2311 test_bit(target, dlm->domain_map) ? "no":"yes");
2312 } else {
2313 mlog(0, "all is well: migrating? %s, dead? %s\n",
2314 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2315 test_bit(target, dlm->domain_map) ? "no":"yes");
2316 }
2317 if (!dlm_migration_can_proceed(dlm, res, target)) {
2318 mlog(0, "trying again...\n");
2319 goto again;
2320 }
2321
2322 /* did the target go down or die? */
2323 spin_lock(&dlm->spinlock);
2324 if (!test_bit(target, dlm->domain_map)) {
2325 mlog(ML_ERROR, "aha. migration target %u just went down\n",
2326 target);
2327 ret = -EHOSTDOWN;
2328 }
2329 spin_unlock(&dlm->spinlock);
2330
2331 /*
2332 * at this point:
2333 *
2334 * o the DLM_LOCK_RES_MIGRATING flag is set
2335 * o there are no pending asts on this lockres
2336 * o all processes trying to reserve an ast on this
2337 * lockres must wait for the MIGRATING flag to clear
2338 */
2339 return ret;
2340}
2341
2342/* last step in the migration process.
2343 * original master calls this to free all of the dlm_lock
2344 * structures that used to be for other nodes. */
2345static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2346 struct dlm_lock_resource *res)
2347{
2348 struct list_head *iter, *iter2;
2349 struct list_head *queue = &res->granted;
2350 int i;
2351 struct dlm_lock *lock;
2352
2353 assert_spin_locked(&res->spinlock);
2354
2355 BUG_ON(res->owner == dlm->node_num);
2356
2357 for (i=0; i<3; i++) {
2358 list_for_each_safe(iter, iter2, queue) {
2359 lock = list_entry (iter, struct dlm_lock, list);
2360 if (lock->ml.node != dlm->node_num) {
2361 mlog(0, "putting lock for node %u\n",
2362 lock->ml.node);
2363 /* be extra careful */
2364 BUG_ON(!list_empty(&lock->ast_list));
2365 BUG_ON(!list_empty(&lock->bast_list));
2366 BUG_ON(lock->ast_pending);
2367 BUG_ON(lock->bast_pending);
2368 list_del_init(&lock->list);
2369 dlm_lock_put(lock);
2370 }
2371 }
2372 queue++;
2373 }
2374}
2375
2376/* for now this is not too intelligent. we will
2377 * need stats to make this do the right thing.
2378 * this just finds the first lock on one of the
2379 * queues and uses that node as the target. */
2380static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
2381 struct dlm_lock_resource *res)
2382{
2383 int i;
2384 struct list_head *queue = &res->granted;
2385 struct list_head *iter;
2386 struct dlm_lock *lock;
2387 int nodenum;
2388
2389 assert_spin_locked(&dlm->spinlock);
2390
2391 spin_lock(&res->spinlock);
2392 for (i=0; i<3; i++) {
2393 list_for_each(iter, queue) {
2394 /* up to the caller to make sure this node
2395 * is alive */
2396 lock = list_entry (iter, struct dlm_lock, list);
2397 if (lock->ml.node != dlm->node_num) {
2398 spin_unlock(&res->spinlock);
2399 return lock->ml.node;
2400 }
2401 }
2402 queue++;
2403 }
2404 spin_unlock(&res->spinlock);
2405 mlog(0, "have not found a suitable target yet! checking domain map\n");
2406
2407 /* ok now we're getting desperate. pick anyone alive. */
2408 nodenum = -1;
2409 while (1) {
2410 nodenum = find_next_bit(dlm->domain_map,
2411 O2NM_MAX_NODES, nodenum+1);
2412 mlog(0, "found %d in domain map\n", nodenum);
2413 if (nodenum >= O2NM_MAX_NODES)
2414 break;
2415 if (nodenum != dlm->node_num) {
2416 mlog(0, "picking %d\n", nodenum);
2417 return nodenum;
2418 }
2419 }
2420
2421 mlog(0, "giving up. no master to migrate to\n");
2422 return DLM_LOCK_RES_OWNER_UNKNOWN;
2423}
2424
2425
2426
2427/* this is called by the new master once all lockres
2428 * data has been received */
2429static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
2430 struct dlm_lock_resource *res,
2431 u8 master, u8 new_master,
2432 struct dlm_node_iter *iter)
2433{
2434 struct dlm_migrate_request migrate;
2435 int ret, status = 0;
2436 int nodenum;
2437
2438 memset(&migrate, 0, sizeof(migrate));
2439 migrate.namelen = res->lockname.len;
2440 memcpy(migrate.name, res->lockname.name, migrate.namelen);
2441 migrate.new_master = new_master;
2442 migrate.master = master;
2443
2444 ret = 0;
2445
2446 /* send message to all nodes, except the master and myself */
2447 while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
2448 if (nodenum == master ||
2449 nodenum == new_master)
2450 continue;
2451
2452 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
2453 &migrate, sizeof(migrate), nodenum,
2454 &status);
2455 if (ret < 0)
2456 mlog_errno(ret);
2457 else if (status < 0) {
2458 mlog(0, "migrate request (node %u) returned %d!\n",
2459 nodenum, status);
2460 ret = status;
2461 }
2462 }
2463
2464 if (ret < 0)
2465 mlog_errno(ret);
2466
2467 mlog(0, "returning ret=%d\n", ret);
2468 return ret;
2469}
2470
2471
2472/* if there is an existing mle for this lockres, we now know who the master is.
2473 * (the one who sent us *this* message) we can clear it up right away.
2474 * since the process that put the mle on the list still has a reference to it,
2475 * we can unhash it now, set the master and wake the process. as a result,
2476 * we will have no mle in the list to start with. now we can add an mle for
2477 * the migration and this should be the only one found for those scanning the
2478 * list. */
2479int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data)
2480{
2481 struct dlm_ctxt *dlm = data;
2482 struct dlm_lock_resource *res = NULL;
2483 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
2484 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
2485 const char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08002486 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002487 int ret = 0;
2488
2489 if (!dlm_grab(dlm))
2490 return -EINVAL;
2491
2492 name = migrate->name;
2493 namelen = migrate->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08002494 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002495
2496 /* preallocate.. if this fails, abort */
2497 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2498 GFP_KERNEL);
2499
2500 if (!mle) {
2501 ret = -ENOMEM;
2502 goto leave;
2503 }
2504
2505 /* check for pre-existing lock */
2506 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08002507 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002508 spin_lock(&dlm->master_lock);
2509
2510 if (res) {
2511 spin_lock(&res->spinlock);
2512 if (res->state & DLM_LOCK_RES_RECOVERING) {
2513 /* if all is working ok, this can only mean that we got
2514 * a migrate request from a node that we now see as
2515 * dead. what can we do here? drop it to the floor? */
2516 spin_unlock(&res->spinlock);
2517 mlog(ML_ERROR, "Got a migrate request, but the "
2518 "lockres is marked as recovering!");
2519 kmem_cache_free(dlm_mle_cache, mle);
2520 ret = -EINVAL; /* need a better solution */
2521 goto unlock;
2522 }
2523 res->state |= DLM_LOCK_RES_MIGRATING;
2524 spin_unlock(&res->spinlock);
2525 }
2526
2527 /* ignore status. only nonzero status would BUG. */
2528 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
2529 name, namelen,
2530 migrate->new_master,
2531 migrate->master);
2532
2533unlock:
2534 spin_unlock(&dlm->master_lock);
2535 spin_unlock(&dlm->spinlock);
2536
2537 if (oldmle) {
2538 /* master is known, detach if not already detached */
2539 dlm_mle_detach_hb_events(dlm, oldmle);
2540 dlm_put_mle(oldmle);
2541 }
2542
2543 if (res)
2544 dlm_lockres_put(res);
2545leave:
2546 dlm_put(dlm);
2547 return ret;
2548}
2549
2550/* must be holding dlm->spinlock and dlm->master_lock
2551 * when adding a migration mle, we can clear any other mles
2552 * in the master list because we know with certainty that
2553 * the master is "master". so we remove any old mle from
2554 * the list after setting it's master field, and then add
2555 * the new migration mle. this way we can hold with the rule
2556 * of having only one mle for a given lock name at all times. */
2557static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
2558 struct dlm_lock_resource *res,
2559 struct dlm_master_list_entry *mle,
2560 struct dlm_master_list_entry **oldmle,
2561 const char *name, unsigned int namelen,
2562 u8 new_master, u8 master)
2563{
2564 int found;
2565 int ret = 0;
2566
2567 *oldmle = NULL;
2568
2569 mlog_entry_void();
2570
2571 assert_spin_locked(&dlm->spinlock);
2572 assert_spin_locked(&dlm->master_lock);
2573
2574 /* caller is responsible for any ref taken here on oldmle */
2575 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
2576 if (found) {
2577 struct dlm_master_list_entry *tmp = *oldmle;
2578 spin_lock(&tmp->spinlock);
2579 if (tmp->type == DLM_MLE_MIGRATION) {
2580 if (master == dlm->node_num) {
2581 /* ah another process raced me to it */
2582 mlog(0, "tried to migrate %.*s, but some "
2583 "process beat me to it\n",
2584 namelen, name);
2585 ret = -EEXIST;
2586 } else {
2587 /* bad. 2 NODES are trying to migrate! */
2588 mlog(ML_ERROR, "migration error mle: "
2589 "master=%u new_master=%u // request: "
2590 "master=%u new_master=%u // "
2591 "lockres=%.*s\n",
2592 tmp->master, tmp->new_master,
2593 master, new_master,
2594 namelen, name);
2595 BUG();
2596 }
2597 } else {
2598 /* this is essentially what assert_master does */
2599 tmp->master = master;
2600 atomic_set(&tmp->woken, 1);
2601 wake_up(&tmp->wq);
2602 /* remove it from the list so that only one
2603 * mle will be found */
2604 list_del_init(&tmp->list);
2605 }
2606 spin_unlock(&tmp->spinlock);
2607 }
2608
2609 /* now add a migration mle to the tail of the list */
2610 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
2611 mle->new_master = new_master;
2612 mle->master = master;
2613 /* do this for consistency with other mle types */
2614 set_bit(new_master, mle->maybe_map);
2615 list_add(&mle->list, &dlm->master_list);
2616
2617 return ret;
2618}
2619
2620
2621void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
2622{
2623 struct list_head *iter, *iter2;
2624 struct dlm_master_list_entry *mle;
2625 struct dlm_lock_resource *res;
Mark Fasheha3d33292006-03-09 17:55:56 -08002626 unsigned int hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002627
2628 mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
2629top:
2630 assert_spin_locked(&dlm->spinlock);
2631
2632 /* clean the master list */
2633 spin_lock(&dlm->master_lock);
2634 list_for_each_safe(iter, iter2, &dlm->master_list) {
2635 mle = list_entry(iter, struct dlm_master_list_entry, list);
2636
2637 BUG_ON(mle->type != DLM_MLE_BLOCK &&
2638 mle->type != DLM_MLE_MASTER &&
2639 mle->type != DLM_MLE_MIGRATION);
2640
2641 /* MASTER mles are initiated locally. the waiting
2642 * process will notice the node map change
2643 * shortly. let that happen as normal. */
2644 if (mle->type == DLM_MLE_MASTER)
2645 continue;
2646
2647
2648 /* BLOCK mles are initiated by other nodes.
2649 * need to clean up if the dead node would have
2650 * been the master. */
2651 if (mle->type == DLM_MLE_BLOCK) {
2652 int bit;
2653
2654 spin_lock(&mle->spinlock);
2655 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
2656 if (bit != dead_node) {
2657 mlog(0, "mle found, but dead node %u would "
2658 "not have been master\n", dead_node);
2659 spin_unlock(&mle->spinlock);
2660 } else {
2661 /* must drop the refcount by one since the
2662 * assert_master will never arrive. this
2663 * may result in the mle being unlinked and
2664 * freed, but there may still be a process
2665 * waiting in the dlmlock path which is fine. */
2666 mlog(ML_ERROR, "node %u was expected master\n",
2667 dead_node);
2668 atomic_set(&mle->woken, 1);
2669 spin_unlock(&mle->spinlock);
2670 wake_up(&mle->wq);
Kurt Hackelf671c092006-02-14 11:45:21 -08002671 /* do not need events any longer, so detach
2672 * from heartbeat */
2673 __dlm_mle_detach_hb_events(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002674 __dlm_put_mle(mle);
2675 }
2676 continue;
2677 }
2678
2679 /* everything else is a MIGRATION mle */
2680
2681 /* the rule for MIGRATION mles is that the master
2682 * becomes UNKNOWN if *either* the original or
2683 * the new master dies. all UNKNOWN lockreses
2684 * are sent to whichever node becomes the recovery
2685 * master. the new master is responsible for
2686 * determining if there is still a master for
2687 * this lockres, or if he needs to take over
2688 * mastery. either way, this node should expect
2689 * another message to resolve this. */
2690 if (mle->master != dead_node &&
2691 mle->new_master != dead_node)
2692 continue;
2693
2694 /* if we have reached this point, this mle needs to
2695 * be removed from the list and freed. */
2696
2697 /* remove from the list early. NOTE: unlinking
2698 * list_head while in list_for_each_safe */
2699 spin_lock(&mle->spinlock);
2700 list_del_init(&mle->list);
2701 atomic_set(&mle->woken, 1);
2702 spin_unlock(&mle->spinlock);
2703 wake_up(&mle->wq);
2704
2705 mlog(0, "node %u died during migration from "
2706 "%u to %u!\n", dead_node,
2707 mle->master, mle->new_master);
2708 /* if there is a lockres associated with this
2709 * mle, find it and set its owner to UNKNOWN */
Mark Fasheha3d33292006-03-09 17:55:56 -08002710 hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002711 res = __dlm_lookup_lockres(dlm, mle->u.name.name,
Mark Fasheha3d33292006-03-09 17:55:56 -08002712 mle->u.name.len, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002713 if (res) {
2714 /* unfortunately if we hit this rare case, our
2715 * lock ordering is messed. we need to drop
2716 * the master lock so that we can take the
2717 * lockres lock, meaning that we will have to
2718 * restart from the head of list. */
2719 spin_unlock(&dlm->master_lock);
2720
2721 /* move lockres onto recovery list */
2722 spin_lock(&res->spinlock);
2723 dlm_set_lockres_owner(dlm, res,
2724 DLM_LOCK_RES_OWNER_UNKNOWN);
2725 dlm_move_lockres_to_recovery_list(dlm, res);
2726 spin_unlock(&res->spinlock);
2727 dlm_lockres_put(res);
2728
Kurt Hackelf671c092006-02-14 11:45:21 -08002729 /* about to get rid of mle, detach from heartbeat */
2730 __dlm_mle_detach_hb_events(dlm, mle);
2731
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002732 /* dump the mle */
2733 spin_lock(&dlm->master_lock);
2734 __dlm_put_mle(mle);
2735 spin_unlock(&dlm->master_lock);
2736
2737 /* restart */
2738 goto top;
2739 }
2740
2741 /* this may be the last reference */
2742 __dlm_put_mle(mle);
2743 }
2744 spin_unlock(&dlm->master_lock);
2745}
2746
2747
2748int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
2749 u8 old_master)
2750{
2751 struct dlm_node_iter iter;
2752 int ret = 0;
2753
2754 spin_lock(&dlm->spinlock);
2755 dlm_node_iter_init(dlm->domain_map, &iter);
2756 clear_bit(old_master, iter.node_map);
2757 clear_bit(dlm->node_num, iter.node_map);
2758 spin_unlock(&dlm->spinlock);
2759
2760 mlog(0, "now time to do a migrate request to other nodes\n");
2761 ret = dlm_do_migrate_request(dlm, res, old_master,
2762 dlm->node_num, &iter);
2763 if (ret < 0) {
2764 mlog_errno(ret);
2765 goto leave;
2766 }
2767
2768 mlog(0, "doing assert master of %.*s to all except the original node\n",
2769 res->lockname.len, res->lockname.name);
2770 /* this call now finishes out the nodemap
2771 * even if one or more nodes die */
2772 ret = dlm_do_assert_master(dlm, res->lockname.name,
2773 res->lockname.len, iter.node_map,
2774 DLM_ASSERT_MASTER_FINISH_MIGRATION);
2775 if (ret < 0) {
2776 /* no longer need to retry. all living nodes contacted. */
2777 mlog_errno(ret);
2778 ret = 0;
2779 }
2780
2781 memset(iter.node_map, 0, sizeof(iter.node_map));
2782 set_bit(old_master, iter.node_map);
2783 mlog(0, "doing assert master of %.*s back to %u\n",
2784 res->lockname.len, res->lockname.name, old_master);
2785 ret = dlm_do_assert_master(dlm, res->lockname.name,
2786 res->lockname.len, iter.node_map,
2787 DLM_ASSERT_MASTER_FINISH_MIGRATION);
2788 if (ret < 0) {
2789 mlog(0, "assert master to original master failed "
2790 "with %d.\n", ret);
2791 /* the only nonzero status here would be because of
2792 * a dead original node. we're done. */
2793 ret = 0;
2794 }
2795
2796 /* all done, set the owner, clear the flag */
2797 spin_lock(&res->spinlock);
2798 dlm_set_lockres_owner(dlm, res, dlm->node_num);
2799 res->state &= ~DLM_LOCK_RES_MIGRATING;
2800 spin_unlock(&res->spinlock);
2801 /* re-dirty it on the new master */
2802 dlm_kick_thread(dlm, res);
2803 wake_up(&res->wq);
2804leave:
2805 return ret;
2806}
2807
2808/*
2809 * LOCKRES AST REFCOUNT
2810 * this is integral to migration
2811 */
2812
2813/* for future intent to call an ast, reserve one ahead of time.
2814 * this should be called only after waiting on the lockres
2815 * with dlm_wait_on_lockres, and while still holding the
2816 * spinlock after the call. */
2817void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
2818{
2819 assert_spin_locked(&res->spinlock);
2820 if (res->state & DLM_LOCK_RES_MIGRATING) {
2821 __dlm_print_one_lock_resource(res);
2822 }
2823 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
2824
2825 atomic_inc(&res->asts_reserved);
2826}
2827
2828/*
2829 * used to drop the reserved ast, either because it went unused,
2830 * or because the ast/bast was actually called.
2831 *
2832 * also, if there is a pending migration on this lockres,
2833 * and this was the last pending ast on the lockres,
2834 * atomically set the MIGRATING flag before we drop the lock.
2835 * this is how we ensure that migration can proceed with no
2836 * asts in progress. note that it is ok if the state of the
2837 * queues is such that a lock should be granted in the future
2838 * or that a bast should be fired, because the new master will
2839 * shuffle the lists on this lockres as soon as it is migrated.
2840 */
2841void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
2842 struct dlm_lock_resource *res)
2843{
2844 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
2845 return;
2846
2847 if (!res->migration_pending) {
2848 spin_unlock(&res->spinlock);
2849 return;
2850 }
2851
2852 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
2853 res->migration_pending = 0;
2854 res->state |= DLM_LOCK_RES_MIGRATING;
2855 spin_unlock(&res->spinlock);
2856 wake_up(&res->wq);
2857 wake_up(&dlm->migration_wq);
2858}