Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / ocfs2 / dlm / dlmmaster.c
blob0e7406340dd9e78b416d7574a724fbf2ed3079be
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmmod.c
6 * standalone DLM module
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
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>
44 #include "cluster/heartbeat.h"
45 #include "cluster/nodemanager.h"
46 #include "cluster/tcp.h"
48 #include "dlmapi.h"
49 #include "dlmcommon.h"
50 #include "dlmdomain.h"
52 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
53 #include "cluster/masklog.h"
55 enum dlm_mle_type {
56 DLM_MLE_BLOCK,
57 DLM_MLE_MASTER,
58 DLM_MLE_MIGRATION
61 struct dlm_lock_name
63 u8 len;
64 u8 name[DLM_LOCKID_NAME_MAX];
67 struct dlm_master_list_entry
69 struct list_head list;
70 struct list_head hb_events;
71 struct dlm_ctxt *dlm;
72 spinlock_t spinlock;
73 wait_queue_head_t wq;
74 atomic_t woken;
75 struct kref mle_refs;
76 int inuse;
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;
92 static void dlm_mle_node_down(struct dlm_ctxt *dlm,
93 struct dlm_master_list_entry *mle,
94 struct o2nm_node *node,
95 int idx);
96 static void dlm_mle_node_up(struct dlm_ctxt *dlm,
97 struct dlm_master_list_entry *mle,
98 struct o2nm_node *node,
99 int idx);
101 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
102 static int dlm_do_assert_master(struct dlm_ctxt *dlm,
103 struct dlm_lock_resource *res,
104 void *nodemap, u32 flags);
105 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
107 static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
108 struct dlm_master_list_entry *mle,
109 const char *name,
110 unsigned int namelen)
112 struct dlm_lock_resource *res;
114 if (dlm != mle->dlm)
115 return 0;
117 if (mle->type == DLM_MLE_BLOCK ||
118 mle->type == DLM_MLE_MIGRATION) {
119 if (namelen != mle->u.name.len ||
120 memcmp(name, mle->u.name.name, namelen)!=0)
121 return 0;
122 } else {
123 res = mle->u.res;
124 if (namelen != res->lockname.len ||
125 memcmp(res->lockname.name, name, namelen) != 0)
126 return 0;
128 return 1;
131 #define dlm_print_nodemap(m) _dlm_print_nodemap(m,#m)
132 static void _dlm_print_nodemap(unsigned long *map, const char *mapname)
134 int i;
135 printk("%s=[ ", mapname);
136 for (i=0; i<O2NM_MAX_NODES; i++)
137 if (test_bit(i, map))
138 printk("%d ", i);
139 printk("]");
142 static void dlm_print_one_mle(struct dlm_master_list_entry *mle)
144 int refs;
145 char *type;
146 char attached;
147 u8 master;
148 unsigned int namelen;
149 const char *name;
150 struct kref *k;
151 unsigned long *maybe = mle->maybe_map,
152 *vote = mle->vote_map,
153 *resp = mle->response_map,
154 *node = mle->node_map;
156 k = &mle->mle_refs;
157 if (mle->type == DLM_MLE_BLOCK)
158 type = "BLK";
159 else if (mle->type == DLM_MLE_MASTER)
160 type = "MAS";
161 else
162 type = "MIG";
163 refs = atomic_read(&k->refcount);
164 master = mle->master;
165 attached = (list_empty(&mle->hb_events) ? 'N' : 'Y');
167 if (mle->type != DLM_MLE_MASTER) {
168 namelen = mle->u.name.len;
169 name = mle->u.name.name;
170 } else {
171 namelen = mle->u.res->lockname.len;
172 name = mle->u.res->lockname.name;
175 mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
176 namelen, name, type, refs, master, mle->new_master, attached,
177 mle->inuse);
178 dlm_print_nodemap(maybe);
179 printk(", ");
180 dlm_print_nodemap(vote);
181 printk(", ");
182 dlm_print_nodemap(resp);
183 printk(", ");
184 dlm_print_nodemap(node);
185 printk(", ");
186 printk("\n");
189 #if 0
190 /* Code here is included but defined out as it aids debugging */
192 static void dlm_dump_mles(struct dlm_ctxt *dlm)
194 struct dlm_master_list_entry *mle;
196 mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
197 spin_lock(&dlm->master_lock);
198 list_for_each_entry(mle, &dlm->master_list, list)
199 dlm_print_one_mle(mle);
200 spin_unlock(&dlm->master_lock);
203 int dlm_dump_all_mles(const char __user *data, unsigned int len)
205 struct dlm_ctxt *dlm;
207 spin_lock(&dlm_domain_lock);
208 list_for_each_entry(dlm, &dlm_domains, list) {
209 mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
210 dlm_dump_mles(dlm);
212 spin_unlock(&dlm_domain_lock);
213 return len;
215 EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
217 #endif /* 0 */
220 static struct kmem_cache *dlm_mle_cache = NULL;
223 static void dlm_mle_release(struct kref *kref);
224 static void dlm_init_mle(struct dlm_master_list_entry *mle,
225 enum dlm_mle_type type,
226 struct dlm_ctxt *dlm,
227 struct dlm_lock_resource *res,
228 const char *name,
229 unsigned int namelen);
230 static void dlm_put_mle(struct dlm_master_list_entry *mle);
231 static void __dlm_put_mle(struct dlm_master_list_entry *mle);
232 static int dlm_find_mle(struct dlm_ctxt *dlm,
233 struct dlm_master_list_entry **mle,
234 char *name, unsigned int namelen);
236 static int dlm_do_master_request(struct dlm_lock_resource *res,
237 struct dlm_master_list_entry *mle, int to);
240 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
241 struct dlm_lock_resource *res,
242 struct dlm_master_list_entry *mle,
243 int *blocked);
244 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
245 struct dlm_lock_resource *res,
246 struct dlm_master_list_entry *mle,
247 int blocked);
248 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
249 struct dlm_lock_resource *res,
250 struct dlm_master_list_entry *mle,
251 struct dlm_master_list_entry **oldmle,
252 const char *name, unsigned int namelen,
253 u8 new_master, u8 master);
255 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
256 struct dlm_lock_resource *res);
257 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
258 struct dlm_lock_resource *res);
259 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
260 struct dlm_lock_resource *res,
261 u8 target);
262 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
263 struct dlm_lock_resource *res);
266 int dlm_is_host_down(int errno)
268 switch (errno) {
269 case -EBADF:
270 case -ECONNREFUSED:
271 case -ENOTCONN:
272 case -ECONNRESET:
273 case -EPIPE:
274 case -EHOSTDOWN:
275 case -EHOSTUNREACH:
276 case -ETIMEDOUT:
277 case -ECONNABORTED:
278 case -ENETDOWN:
279 case -ENETUNREACH:
280 case -ENETRESET:
281 case -ESHUTDOWN:
282 case -ENOPROTOOPT:
283 case -EINVAL: /* if returned from our tcp code,
284 this means there is no socket */
285 return 1;
287 return 0;
292 * MASTER LIST FUNCTIONS
297 * regarding master list entries and heartbeat callbacks:
299 * in order to avoid sleeping and allocation that occurs in
300 * heartbeat, master list entries are simply attached to the
301 * dlm's established heartbeat callbacks. the mle is attached
302 * when it is created, and since the dlm->spinlock is held at
303 * that time, any heartbeat event will be properly discovered
304 * by the mle. the mle needs to be detached from the
305 * dlm->mle_hb_events list as soon as heartbeat events are no
306 * longer useful to the mle, and before the mle is freed.
308 * as a general rule, heartbeat events are no longer needed by
309 * the mle once an "answer" regarding the lock master has been
310 * received.
312 static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
313 struct dlm_master_list_entry *mle)
315 assert_spin_locked(&dlm->spinlock);
317 list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
321 static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
322 struct dlm_master_list_entry *mle)
324 if (!list_empty(&mle->hb_events))
325 list_del_init(&mle->hb_events);
329 static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
330 struct dlm_master_list_entry *mle)
332 spin_lock(&dlm->spinlock);
333 __dlm_mle_detach_hb_events(dlm, mle);
334 spin_unlock(&dlm->spinlock);
337 static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
339 struct dlm_ctxt *dlm;
340 dlm = mle->dlm;
342 assert_spin_locked(&dlm->spinlock);
343 assert_spin_locked(&dlm->master_lock);
344 mle->inuse++;
345 kref_get(&mle->mle_refs);
348 static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
350 struct dlm_ctxt *dlm;
351 dlm = mle->dlm;
353 spin_lock(&dlm->spinlock);
354 spin_lock(&dlm->master_lock);
355 mle->inuse--;
356 __dlm_put_mle(mle);
357 spin_unlock(&dlm->master_lock);
358 spin_unlock(&dlm->spinlock);
362 /* remove from list and free */
363 static void __dlm_put_mle(struct dlm_master_list_entry *mle)
365 struct dlm_ctxt *dlm;
366 dlm = mle->dlm;
368 assert_spin_locked(&dlm->spinlock);
369 assert_spin_locked(&dlm->master_lock);
370 if (!atomic_read(&mle->mle_refs.refcount)) {
371 /* this may or may not crash, but who cares.
372 * it's a BUG. */
373 mlog(ML_ERROR, "bad mle: %p\n", mle);
374 dlm_print_one_mle(mle);
375 BUG();
376 } else
377 kref_put(&mle->mle_refs, dlm_mle_release);
381 /* must not have any spinlocks coming in */
382 static void dlm_put_mle(struct dlm_master_list_entry *mle)
384 struct dlm_ctxt *dlm;
385 dlm = mle->dlm;
387 spin_lock(&dlm->spinlock);
388 spin_lock(&dlm->master_lock);
389 __dlm_put_mle(mle);
390 spin_unlock(&dlm->master_lock);
391 spin_unlock(&dlm->spinlock);
394 static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
396 kref_get(&mle->mle_refs);
399 static void dlm_init_mle(struct dlm_master_list_entry *mle,
400 enum dlm_mle_type type,
401 struct dlm_ctxt *dlm,
402 struct dlm_lock_resource *res,
403 const char *name,
404 unsigned int namelen)
406 assert_spin_locked(&dlm->spinlock);
408 mle->dlm = dlm;
409 mle->type = type;
410 INIT_LIST_HEAD(&mle->list);
411 INIT_LIST_HEAD(&mle->hb_events);
412 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
413 spin_lock_init(&mle->spinlock);
414 init_waitqueue_head(&mle->wq);
415 atomic_set(&mle->woken, 0);
416 kref_init(&mle->mle_refs);
417 memset(mle->response_map, 0, sizeof(mle->response_map));
418 mle->master = O2NM_MAX_NODES;
419 mle->new_master = O2NM_MAX_NODES;
420 mle->inuse = 0;
422 if (mle->type == DLM_MLE_MASTER) {
423 BUG_ON(!res);
424 mle->u.res = res;
425 } else if (mle->type == DLM_MLE_BLOCK) {
426 BUG_ON(!name);
427 memcpy(mle->u.name.name, name, namelen);
428 mle->u.name.len = namelen;
429 } else /* DLM_MLE_MIGRATION */ {
430 BUG_ON(!name);
431 memcpy(mle->u.name.name, name, namelen);
432 mle->u.name.len = namelen;
435 /* copy off the node_map and register hb callbacks on our copy */
436 memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
437 memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
438 clear_bit(dlm->node_num, mle->vote_map);
439 clear_bit(dlm->node_num, mle->node_map);
441 /* attach the mle to the domain node up/down events */
442 __dlm_mle_attach_hb_events(dlm, mle);
446 /* returns 1 if found, 0 if not */
447 static int dlm_find_mle(struct dlm_ctxt *dlm,
448 struct dlm_master_list_entry **mle,
449 char *name, unsigned int namelen)
451 struct dlm_master_list_entry *tmpmle;
453 assert_spin_locked(&dlm->master_lock);
455 list_for_each_entry(tmpmle, &dlm->master_list, list) {
456 if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
457 continue;
458 dlm_get_mle(tmpmle);
459 *mle = tmpmle;
460 return 1;
462 return 0;
465 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
467 struct dlm_master_list_entry *mle;
469 assert_spin_locked(&dlm->spinlock);
471 list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) {
472 if (node_up)
473 dlm_mle_node_up(dlm, mle, NULL, idx);
474 else
475 dlm_mle_node_down(dlm, mle, NULL, idx);
479 static void dlm_mle_node_down(struct dlm_ctxt *dlm,
480 struct dlm_master_list_entry *mle,
481 struct o2nm_node *node, int idx)
483 spin_lock(&mle->spinlock);
485 if (!test_bit(idx, mle->node_map))
486 mlog(0, "node %u already removed from nodemap!\n", idx);
487 else
488 clear_bit(idx, mle->node_map);
490 spin_unlock(&mle->spinlock);
493 static void dlm_mle_node_up(struct dlm_ctxt *dlm,
494 struct dlm_master_list_entry *mle,
495 struct o2nm_node *node, int idx)
497 spin_lock(&mle->spinlock);
499 if (test_bit(idx, mle->node_map))
500 mlog(0, "node %u already in node map!\n", idx);
501 else
502 set_bit(idx, mle->node_map);
504 spin_unlock(&mle->spinlock);
508 int dlm_init_mle_cache(void)
510 dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
511 sizeof(struct dlm_master_list_entry),
512 0, SLAB_HWCACHE_ALIGN,
513 NULL);
514 if (dlm_mle_cache == NULL)
515 return -ENOMEM;
516 return 0;
519 void dlm_destroy_mle_cache(void)
521 if (dlm_mle_cache)
522 kmem_cache_destroy(dlm_mle_cache);
525 static void dlm_mle_release(struct kref *kref)
527 struct dlm_master_list_entry *mle;
528 struct dlm_ctxt *dlm;
530 mlog_entry_void();
532 mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
533 dlm = mle->dlm;
535 if (mle->type != DLM_MLE_MASTER) {
536 mlog(0, "calling mle_release for %.*s, type %d\n",
537 mle->u.name.len, mle->u.name.name, mle->type);
538 } else {
539 mlog(0, "calling mle_release for %.*s, type %d\n",
540 mle->u.res->lockname.len,
541 mle->u.res->lockname.name, mle->type);
543 assert_spin_locked(&dlm->spinlock);
544 assert_spin_locked(&dlm->master_lock);
546 /* remove from list if not already */
547 if (!list_empty(&mle->list))
548 list_del_init(&mle->list);
550 /* detach the mle from the domain node up/down events */
551 __dlm_mle_detach_hb_events(dlm, mle);
553 /* NOTE: kfree under spinlock here.
554 * if this is bad, we can move this to a freelist. */
555 kmem_cache_free(dlm_mle_cache, mle);
560 * LOCK RESOURCE FUNCTIONS
563 static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
564 struct dlm_lock_resource *res,
565 u8 owner)
567 assert_spin_locked(&res->spinlock);
569 mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
571 if (owner == dlm->node_num)
572 atomic_inc(&dlm->local_resources);
573 else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
574 atomic_inc(&dlm->unknown_resources);
575 else
576 atomic_inc(&dlm->remote_resources);
578 res->owner = owner;
581 void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
582 struct dlm_lock_resource *res, u8 owner)
584 assert_spin_locked(&res->spinlock);
586 if (owner == res->owner)
587 return;
589 if (res->owner == dlm->node_num)
590 atomic_dec(&dlm->local_resources);
591 else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
592 atomic_dec(&dlm->unknown_resources);
593 else
594 atomic_dec(&dlm->remote_resources);
596 dlm_set_lockres_owner(dlm, res, owner);
600 static void dlm_lockres_release(struct kref *kref)
602 struct dlm_lock_resource *res;
604 res = container_of(kref, struct dlm_lock_resource, refs);
606 /* This should not happen -- all lockres' have a name
607 * associated with them at init time. */
608 BUG_ON(!res->lockname.name);
610 mlog(0, "destroying lockres %.*s\n", res->lockname.len,
611 res->lockname.name);
613 if (!hlist_unhashed(&res->hash_node) ||
614 !list_empty(&res->granted) ||
615 !list_empty(&res->converting) ||
616 !list_empty(&res->blocked) ||
617 !list_empty(&res->dirty) ||
618 !list_empty(&res->recovering) ||
619 !list_empty(&res->purge)) {
620 mlog(ML_ERROR,
621 "Going to BUG for resource %.*s."
622 " We're on a list! [%c%c%c%c%c%c%c]\n",
623 res->lockname.len, res->lockname.name,
624 !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
625 !list_empty(&res->granted) ? 'G' : ' ',
626 !list_empty(&res->converting) ? 'C' : ' ',
627 !list_empty(&res->blocked) ? 'B' : ' ',
628 !list_empty(&res->dirty) ? 'D' : ' ',
629 !list_empty(&res->recovering) ? 'R' : ' ',
630 !list_empty(&res->purge) ? 'P' : ' ');
632 dlm_print_one_lock_resource(res);
635 /* By the time we're ready to blow this guy away, we shouldn't
636 * be on any lists. */
637 BUG_ON(!hlist_unhashed(&res->hash_node));
638 BUG_ON(!list_empty(&res->granted));
639 BUG_ON(!list_empty(&res->converting));
640 BUG_ON(!list_empty(&res->blocked));
641 BUG_ON(!list_empty(&res->dirty));
642 BUG_ON(!list_empty(&res->recovering));
643 BUG_ON(!list_empty(&res->purge));
645 kfree(res->lockname.name);
647 kfree(res);
650 void dlm_lockres_put(struct dlm_lock_resource *res)
652 kref_put(&res->refs, dlm_lockres_release);
655 static void dlm_init_lockres(struct dlm_ctxt *dlm,
656 struct dlm_lock_resource *res,
657 const char *name, unsigned int namelen)
659 char *qname;
661 /* If we memset here, we lose our reference to the kmalloc'd
662 * res->lockname.name, so be sure to init every field
663 * correctly! */
665 qname = (char *) res->lockname.name;
666 memcpy(qname, name, namelen);
668 res->lockname.len = namelen;
669 res->lockname.hash = dlm_lockid_hash(name, namelen);
671 init_waitqueue_head(&res->wq);
672 spin_lock_init(&res->spinlock);
673 INIT_HLIST_NODE(&res->hash_node);
674 INIT_LIST_HEAD(&res->granted);
675 INIT_LIST_HEAD(&res->converting);
676 INIT_LIST_HEAD(&res->blocked);
677 INIT_LIST_HEAD(&res->dirty);
678 INIT_LIST_HEAD(&res->recovering);
679 INIT_LIST_HEAD(&res->purge);
680 atomic_set(&res->asts_reserved, 0);
681 res->migration_pending = 0;
682 res->inflight_locks = 0;
684 kref_init(&res->refs);
686 /* just for consistency */
687 spin_lock(&res->spinlock);
688 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
689 spin_unlock(&res->spinlock);
691 res->state = DLM_LOCK_RES_IN_PROGRESS;
693 res->last_used = 0;
695 memset(res->lvb, 0, DLM_LVB_LEN);
696 memset(res->refmap, 0, sizeof(res->refmap));
699 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
700 const char *name,
701 unsigned int namelen)
703 struct dlm_lock_resource *res;
705 res = kmalloc(sizeof(struct dlm_lock_resource), GFP_NOFS);
706 if (!res)
707 return NULL;
709 res->lockname.name = kmalloc(namelen, GFP_NOFS);
710 if (!res->lockname.name) {
711 kfree(res);
712 return NULL;
715 dlm_init_lockres(dlm, res, name, namelen);
716 return res;
719 void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
720 struct dlm_lock_resource *res,
721 int new_lockres,
722 const char *file,
723 int line)
725 if (!new_lockres)
726 assert_spin_locked(&res->spinlock);
728 if (!test_bit(dlm->node_num, res->refmap)) {
729 BUG_ON(res->inflight_locks != 0);
730 dlm_lockres_set_refmap_bit(dlm->node_num, res);
732 res->inflight_locks++;
733 mlog(0, "%s:%.*s: inflight++: now %u\n",
734 dlm->name, res->lockname.len, res->lockname.name,
735 res->inflight_locks);
738 void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
739 struct dlm_lock_resource *res,
740 const char *file,
741 int line)
743 assert_spin_locked(&res->spinlock);
745 BUG_ON(res->inflight_locks == 0);
746 res->inflight_locks--;
747 mlog(0, "%s:%.*s: inflight--: now %u\n",
748 dlm->name, res->lockname.len, res->lockname.name,
749 res->inflight_locks);
750 if (res->inflight_locks == 0)
751 dlm_lockres_clear_refmap_bit(dlm->node_num, res);
752 wake_up(&res->wq);
756 * lookup a lock resource by name.
757 * may already exist in the hashtable.
758 * lockid is null terminated
760 * if not, allocate enough for the lockres and for
761 * the temporary structure used in doing the mastering.
763 * also, do a lookup in the dlm->master_list to see
764 * if another node has begun mastering the same lock.
765 * if so, there should be a block entry in there
766 * for this name, and we should *not* attempt to master
767 * the lock here. need to wait around for that node
768 * to assert_master (or die).
771 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
772 const char *lockid,
773 int namelen,
774 int flags)
776 struct dlm_lock_resource *tmpres=NULL, *res=NULL;
777 struct dlm_master_list_entry *mle = NULL;
778 struct dlm_master_list_entry *alloc_mle = NULL;
779 int blocked = 0;
780 int ret, nodenum;
781 struct dlm_node_iter iter;
782 unsigned int hash;
783 int tries = 0;
784 int bit, wait_on_recovery = 0;
785 int drop_inflight_if_nonlocal = 0;
787 BUG_ON(!lockid);
789 hash = dlm_lockid_hash(lockid, namelen);
791 mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
793 lookup:
794 spin_lock(&dlm->spinlock);
795 tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
796 if (tmpres) {
797 int dropping_ref = 0;
799 spin_lock(&tmpres->spinlock);
800 if (tmpres->owner == dlm->node_num) {
801 BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
802 dlm_lockres_grab_inflight_ref(dlm, tmpres);
803 } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
804 dropping_ref = 1;
805 spin_unlock(&tmpres->spinlock);
806 spin_unlock(&dlm->spinlock);
808 /* wait until done messaging the master, drop our ref to allow
809 * the lockres to be purged, start over. */
810 if (dropping_ref) {
811 spin_lock(&tmpres->spinlock);
812 __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
813 spin_unlock(&tmpres->spinlock);
814 dlm_lockres_put(tmpres);
815 tmpres = NULL;
816 goto lookup;
819 mlog(0, "found in hash!\n");
820 if (res)
821 dlm_lockres_put(res);
822 res = tmpres;
823 goto leave;
826 if (!res) {
827 spin_unlock(&dlm->spinlock);
828 mlog(0, "allocating a new resource\n");
829 /* nothing found and we need to allocate one. */
830 alloc_mle = (struct dlm_master_list_entry *)
831 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
832 if (!alloc_mle)
833 goto leave;
834 res = dlm_new_lockres(dlm, lockid, namelen);
835 if (!res)
836 goto leave;
837 goto lookup;
840 mlog(0, "no lockres found, allocated our own: %p\n", res);
842 if (flags & LKM_LOCAL) {
843 /* caller knows it's safe to assume it's not mastered elsewhere
844 * DONE! return right away */
845 spin_lock(&res->spinlock);
846 dlm_change_lockres_owner(dlm, res, dlm->node_num);
847 __dlm_insert_lockres(dlm, res);
848 dlm_lockres_grab_inflight_ref(dlm, res);
849 spin_unlock(&res->spinlock);
850 spin_unlock(&dlm->spinlock);
851 /* lockres still marked IN_PROGRESS */
852 goto wake_waiters;
855 /* check master list to see if another node has started mastering it */
856 spin_lock(&dlm->master_lock);
858 /* if we found a block, wait for lock to be mastered by another node */
859 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
860 if (blocked) {
861 int mig;
862 if (mle->type == DLM_MLE_MASTER) {
863 mlog(ML_ERROR, "master entry for nonexistent lock!\n");
864 BUG();
866 mig = (mle->type == DLM_MLE_MIGRATION);
867 /* if there is a migration in progress, let the migration
868 * finish before continuing. we can wait for the absence
869 * of the MIGRATION mle: either the migrate finished or
870 * one of the nodes died and the mle was cleaned up.
871 * if there is a BLOCK here, but it already has a master
872 * set, we are too late. the master does not have a ref
873 * for us in the refmap. detach the mle and drop it.
874 * either way, go back to the top and start over. */
875 if (mig || mle->master != O2NM_MAX_NODES) {
876 BUG_ON(mig && mle->master == dlm->node_num);
877 /* we arrived too late. the master does not
878 * have a ref for us. retry. */
879 mlog(0, "%s:%.*s: late on %s\n",
880 dlm->name, namelen, lockid,
881 mig ? "MIGRATION" : "BLOCK");
882 spin_unlock(&dlm->master_lock);
883 spin_unlock(&dlm->spinlock);
885 /* master is known, detach */
886 if (!mig)
887 dlm_mle_detach_hb_events(dlm, mle);
888 dlm_put_mle(mle);
889 mle = NULL;
890 /* this is lame, but we cant wait on either
891 * the mle or lockres waitqueue here */
892 if (mig)
893 msleep(100);
894 goto lookup;
896 } else {
897 /* go ahead and try to master lock on this node */
898 mle = alloc_mle;
899 /* make sure this does not get freed below */
900 alloc_mle = NULL;
901 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
902 set_bit(dlm->node_num, mle->maybe_map);
903 list_add(&mle->list, &dlm->master_list);
905 /* still holding the dlm spinlock, check the recovery map
906 * to see if there are any nodes that still need to be
907 * considered. these will not appear in the mle nodemap
908 * but they might own this lockres. wait on them. */
909 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
910 if (bit < O2NM_MAX_NODES) {
911 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
912 "recover before lock mastery can begin\n",
913 dlm->name, namelen, (char *)lockid, bit);
914 wait_on_recovery = 1;
918 /* at this point there is either a DLM_MLE_BLOCK or a
919 * DLM_MLE_MASTER on the master list, so it's safe to add the
920 * lockres to the hashtable. anyone who finds the lock will
921 * still have to wait on the IN_PROGRESS. */
923 /* finally add the lockres to its hash bucket */
924 __dlm_insert_lockres(dlm, res);
925 /* since this lockres is new it doesnt not require the spinlock */
926 dlm_lockres_grab_inflight_ref_new(dlm, res);
928 /* if this node does not become the master make sure to drop
929 * this inflight reference below */
930 drop_inflight_if_nonlocal = 1;
932 /* get an extra ref on the mle in case this is a BLOCK
933 * if so, the creator of the BLOCK may try to put the last
934 * ref at this time in the assert master handler, so we
935 * need an extra one to keep from a bad ptr deref. */
936 dlm_get_mle_inuse(mle);
937 spin_unlock(&dlm->master_lock);
938 spin_unlock(&dlm->spinlock);
940 redo_request:
941 while (wait_on_recovery) {
942 /* any cluster changes that occurred after dropping the
943 * dlm spinlock would be detectable be a change on the mle,
944 * so we only need to clear out the recovery map once. */
945 if (dlm_is_recovery_lock(lockid, namelen)) {
946 mlog(ML_NOTICE, "%s: recovery map is not empty, but "
947 "must master $RECOVERY lock now\n", dlm->name);
948 if (!dlm_pre_master_reco_lockres(dlm, res))
949 wait_on_recovery = 0;
950 else {
951 mlog(0, "%s: waiting 500ms for heartbeat state "
952 "change\n", dlm->name);
953 msleep(500);
955 continue;
958 dlm_kick_recovery_thread(dlm);
959 msleep(1000);
960 dlm_wait_for_recovery(dlm);
962 spin_lock(&dlm->spinlock);
963 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
964 if (bit < O2NM_MAX_NODES) {
965 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
966 "recover before lock mastery can begin\n",
967 dlm->name, namelen, (char *)lockid, bit);
968 wait_on_recovery = 1;
969 } else
970 wait_on_recovery = 0;
971 spin_unlock(&dlm->spinlock);
973 if (wait_on_recovery)
974 dlm_wait_for_node_recovery(dlm, bit, 10000);
977 /* must wait for lock to be mastered elsewhere */
978 if (blocked)
979 goto wait;
981 ret = -EINVAL;
982 dlm_node_iter_init(mle->vote_map, &iter);
983 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
984 ret = dlm_do_master_request(res, mle, nodenum);
985 if (ret < 0)
986 mlog_errno(ret);
987 if (mle->master != O2NM_MAX_NODES) {
988 /* found a master ! */
989 if (mle->master <= nodenum)
990 break;
991 /* if our master request has not reached the master
992 * yet, keep going until it does. this is how the
993 * master will know that asserts are needed back to
994 * the lower nodes. */
995 mlog(0, "%s:%.*s: requests only up to %u but master "
996 "is %u, keep going\n", dlm->name, namelen,
997 lockid, nodenum, mle->master);
1001 wait:
1002 /* keep going until the response map includes all nodes */
1003 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
1004 if (ret < 0) {
1005 wait_on_recovery = 1;
1006 mlog(0, "%s:%.*s: node map changed, redo the "
1007 "master request now, blocked=%d\n",
1008 dlm->name, res->lockname.len,
1009 res->lockname.name, blocked);
1010 if (++tries > 20) {
1011 mlog(ML_ERROR, "%s:%.*s: spinning on "
1012 "dlm_wait_for_lock_mastery, blocked=%d\n",
1013 dlm->name, res->lockname.len,
1014 res->lockname.name, blocked);
1015 dlm_print_one_lock_resource(res);
1016 dlm_print_one_mle(mle);
1017 tries = 0;
1019 goto redo_request;
1022 mlog(0, "lockres mastered by %u\n", res->owner);
1023 /* make sure we never continue without this */
1024 BUG_ON(res->owner == O2NM_MAX_NODES);
1026 /* master is known, detach if not already detached */
1027 dlm_mle_detach_hb_events(dlm, mle);
1028 dlm_put_mle(mle);
1029 /* put the extra ref */
1030 dlm_put_mle_inuse(mle);
1032 wake_waiters:
1033 spin_lock(&res->spinlock);
1034 if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
1035 dlm_lockres_drop_inflight_ref(dlm, res);
1036 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1037 spin_unlock(&res->spinlock);
1038 wake_up(&res->wq);
1040 leave:
1041 /* need to free the unused mle */
1042 if (alloc_mle)
1043 kmem_cache_free(dlm_mle_cache, alloc_mle);
1045 return res;
1049 #define DLM_MASTERY_TIMEOUT_MS 5000
1051 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
1052 struct dlm_lock_resource *res,
1053 struct dlm_master_list_entry *mle,
1054 int *blocked)
1056 u8 m;
1057 int ret, bit;
1058 int map_changed, voting_done;
1059 int assert, sleep;
1061 recheck:
1062 ret = 0;
1063 assert = 0;
1065 /* check if another node has already become the owner */
1066 spin_lock(&res->spinlock);
1067 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1068 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
1069 res->lockname.len, res->lockname.name, res->owner);
1070 spin_unlock(&res->spinlock);
1071 /* this will cause the master to re-assert across
1072 * the whole cluster, freeing up mles */
1073 if (res->owner != dlm->node_num) {
1074 ret = dlm_do_master_request(res, mle, res->owner);
1075 if (ret < 0) {
1076 /* give recovery a chance to run */
1077 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
1078 msleep(500);
1079 goto recheck;
1082 ret = 0;
1083 goto leave;
1085 spin_unlock(&res->spinlock);
1087 spin_lock(&mle->spinlock);
1088 m = mle->master;
1089 map_changed = (memcmp(mle->vote_map, mle->node_map,
1090 sizeof(mle->vote_map)) != 0);
1091 voting_done = (memcmp(mle->vote_map, mle->response_map,
1092 sizeof(mle->vote_map)) == 0);
1094 /* restart if we hit any errors */
1095 if (map_changed) {
1096 int b;
1097 mlog(0, "%s: %.*s: node map changed, restarting\n",
1098 dlm->name, res->lockname.len, res->lockname.name);
1099 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
1100 b = (mle->type == DLM_MLE_BLOCK);
1101 if ((*blocked && !b) || (!*blocked && b)) {
1102 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1103 dlm->name, res->lockname.len, res->lockname.name,
1104 *blocked, b);
1105 *blocked = b;
1107 spin_unlock(&mle->spinlock);
1108 if (ret < 0) {
1109 mlog_errno(ret);
1110 goto leave;
1112 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1113 "rechecking now\n", dlm->name, res->lockname.len,
1114 res->lockname.name);
1115 goto recheck;
1116 } else {
1117 if (!voting_done) {
1118 mlog(0, "map not changed and voting not done "
1119 "for %s:%.*s\n", dlm->name, res->lockname.len,
1120 res->lockname.name);
1124 if (m != O2NM_MAX_NODES) {
1125 /* another node has done an assert!
1126 * all done! */
1127 sleep = 0;
1128 } else {
1129 sleep = 1;
1130 /* have all nodes responded? */
1131 if (voting_done && !*blocked) {
1132 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
1133 if (dlm->node_num <= bit) {
1134 /* my node number is lowest.
1135 * now tell other nodes that I am
1136 * mastering this. */
1137 mle->master = dlm->node_num;
1138 /* ref was grabbed in get_lock_resource
1139 * will be dropped in dlmlock_master */
1140 assert = 1;
1141 sleep = 0;
1143 /* if voting is done, but we have not received
1144 * an assert master yet, we must sleep */
1148 spin_unlock(&mle->spinlock);
1150 /* sleep if we haven't finished voting yet */
1151 if (sleep) {
1152 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1155 if (atomic_read(&mle->mle_refs.refcount) < 2)
1156 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1157 atomic_read(&mle->mle_refs.refcount),
1158 res->lockname.len, res->lockname.name);
1160 atomic_set(&mle->woken, 0);
1161 (void)wait_event_timeout(mle->wq,
1162 (atomic_read(&mle->woken) == 1),
1163 timeo);
1164 if (res->owner == O2NM_MAX_NODES) {
1165 mlog(0, "%s:%.*s: waiting again\n", dlm->name,
1166 res->lockname.len, res->lockname.name);
1167 goto recheck;
1169 mlog(0, "done waiting, master is %u\n", res->owner);
1170 ret = 0;
1171 goto leave;
1174 ret = 0; /* done */
1175 if (assert) {
1176 m = dlm->node_num;
1177 mlog(0, "about to master %.*s here, this=%u\n",
1178 res->lockname.len, res->lockname.name, m);
1179 ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
1180 if (ret) {
1181 /* This is a failure in the network path,
1182 * not in the response to the assert_master
1183 * (any nonzero response is a BUG on this node).
1184 * Most likely a socket just got disconnected
1185 * due to node death. */
1186 mlog_errno(ret);
1188 /* no longer need to restart lock mastery.
1189 * all living nodes have been contacted. */
1190 ret = 0;
1193 /* set the lockres owner */
1194 spin_lock(&res->spinlock);
1195 /* mastery reference obtained either during
1196 * assert_master_handler or in get_lock_resource */
1197 dlm_change_lockres_owner(dlm, res, m);
1198 spin_unlock(&res->spinlock);
1200 leave:
1201 return ret;
1204 struct dlm_bitmap_diff_iter
1206 int curnode;
1207 unsigned long *orig_bm;
1208 unsigned long *cur_bm;
1209 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1212 enum dlm_node_state_change
1214 NODE_DOWN = -1,
1215 NODE_NO_CHANGE = 0,
1216 NODE_UP
1219 static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1220 unsigned long *orig_bm,
1221 unsigned long *cur_bm)
1223 unsigned long p1, p2;
1224 int i;
1226 iter->curnode = -1;
1227 iter->orig_bm = orig_bm;
1228 iter->cur_bm = cur_bm;
1230 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1231 p1 = *(iter->orig_bm + i);
1232 p2 = *(iter->cur_bm + i);
1233 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1237 static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1238 enum dlm_node_state_change *state)
1240 int bit;
1242 if (iter->curnode >= O2NM_MAX_NODES)
1243 return -ENOENT;
1245 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1246 iter->curnode+1);
1247 if (bit >= O2NM_MAX_NODES) {
1248 iter->curnode = O2NM_MAX_NODES;
1249 return -ENOENT;
1252 /* if it was there in the original then this node died */
1253 if (test_bit(bit, iter->orig_bm))
1254 *state = NODE_DOWN;
1255 else
1256 *state = NODE_UP;
1258 iter->curnode = bit;
1259 return bit;
1263 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1264 struct dlm_lock_resource *res,
1265 struct dlm_master_list_entry *mle,
1266 int blocked)
1268 struct dlm_bitmap_diff_iter bdi;
1269 enum dlm_node_state_change sc;
1270 int node;
1271 int ret = 0;
1273 mlog(0, "something happened such that the "
1274 "master process may need to be restarted!\n");
1276 assert_spin_locked(&mle->spinlock);
1278 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1279 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1280 while (node >= 0) {
1281 if (sc == NODE_UP) {
1282 /* a node came up. clear any old vote from
1283 * the response map and set it in the vote map
1284 * then restart the mastery. */
1285 mlog(ML_NOTICE, "node %d up while restarting\n", node);
1287 /* redo the master request, but only for the new node */
1288 mlog(0, "sending request to new node\n");
1289 clear_bit(node, mle->response_map);
1290 set_bit(node, mle->vote_map);
1291 } else {
1292 mlog(ML_ERROR, "node down! %d\n", node);
1293 if (blocked) {
1294 int lowest = find_next_bit(mle->maybe_map,
1295 O2NM_MAX_NODES, 0);
1297 /* act like it was never there */
1298 clear_bit(node, mle->maybe_map);
1300 if (node == lowest) {
1301 mlog(0, "expected master %u died"
1302 " while this node was blocked "
1303 "waiting on it!\n", node);
1304 lowest = find_next_bit(mle->maybe_map,
1305 O2NM_MAX_NODES,
1306 lowest+1);
1307 if (lowest < O2NM_MAX_NODES) {
1308 mlog(0, "%s:%.*s:still "
1309 "blocked. waiting on %u "
1310 "now\n", dlm->name,
1311 res->lockname.len,
1312 res->lockname.name,
1313 lowest);
1314 } else {
1315 /* mle is an MLE_BLOCK, but
1316 * there is now nothing left to
1317 * block on. we need to return
1318 * all the way back out and try
1319 * again with an MLE_MASTER.
1320 * dlm_do_local_recovery_cleanup
1321 * has already run, so the mle
1322 * refcount is ok */
1323 mlog(0, "%s:%.*s: no "
1324 "longer blocking. try to "
1325 "master this here\n",
1326 dlm->name,
1327 res->lockname.len,
1328 res->lockname.name);
1329 mle->type = DLM_MLE_MASTER;
1330 mle->u.res = res;
1335 /* now blank out everything, as if we had never
1336 * contacted anyone */
1337 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
1338 memset(mle->response_map, 0, sizeof(mle->response_map));
1339 /* reset the vote_map to the current node_map */
1340 memcpy(mle->vote_map, mle->node_map,
1341 sizeof(mle->node_map));
1342 /* put myself into the maybe map */
1343 if (mle->type != DLM_MLE_BLOCK)
1344 set_bit(dlm->node_num, mle->maybe_map);
1346 ret = -EAGAIN;
1347 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1349 return ret;
1354 * DLM_MASTER_REQUEST_MSG
1356 * returns: 0 on success,
1357 * -errno on a network error
1359 * on error, the caller should assume the target node is "dead"
1363 static int dlm_do_master_request(struct dlm_lock_resource *res,
1364 struct dlm_master_list_entry *mle, int to)
1366 struct dlm_ctxt *dlm = mle->dlm;
1367 struct dlm_master_request request;
1368 int ret, response=0, resend;
1370 memset(&request, 0, sizeof(request));
1371 request.node_idx = dlm->node_num;
1373 BUG_ON(mle->type == DLM_MLE_MIGRATION);
1375 if (mle->type != DLM_MLE_MASTER) {
1376 request.namelen = mle->u.name.len;
1377 memcpy(request.name, mle->u.name.name, request.namelen);
1378 } else {
1379 request.namelen = mle->u.res->lockname.len;
1380 memcpy(request.name, mle->u.res->lockname.name,
1381 request.namelen);
1384 again:
1385 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1386 sizeof(request), to, &response);
1387 if (ret < 0) {
1388 if (ret == -ESRCH) {
1389 /* should never happen */
1390 mlog(ML_ERROR, "TCP stack not ready!\n");
1391 BUG();
1392 } else if (ret == -EINVAL) {
1393 mlog(ML_ERROR, "bad args passed to o2net!\n");
1394 BUG();
1395 } else if (ret == -ENOMEM) {
1396 mlog(ML_ERROR, "out of memory while trying to send "
1397 "network message! retrying\n");
1398 /* this is totally crude */
1399 msleep(50);
1400 goto again;
1401 } else if (!dlm_is_host_down(ret)) {
1402 /* not a network error. bad. */
1403 mlog_errno(ret);
1404 mlog(ML_ERROR, "unhandled error!");
1405 BUG();
1407 /* all other errors should be network errors,
1408 * and likely indicate node death */
1409 mlog(ML_ERROR, "link to %d went down!\n", to);
1410 goto out;
1413 ret = 0;
1414 resend = 0;
1415 spin_lock(&mle->spinlock);
1416 switch (response) {
1417 case DLM_MASTER_RESP_YES:
1418 set_bit(to, mle->response_map);
1419 mlog(0, "node %u is the master, response=YES\n", to);
1420 mlog(0, "%s:%.*s: master node %u now knows I have a "
1421 "reference\n", dlm->name, res->lockname.len,
1422 res->lockname.name, to);
1423 mle->master = to;
1424 break;
1425 case DLM_MASTER_RESP_NO:
1426 mlog(0, "node %u not master, response=NO\n", to);
1427 set_bit(to, mle->response_map);
1428 break;
1429 case DLM_MASTER_RESP_MAYBE:
1430 mlog(0, "node %u not master, response=MAYBE\n", to);
1431 set_bit(to, mle->response_map);
1432 set_bit(to, mle->maybe_map);
1433 break;
1434 case DLM_MASTER_RESP_ERROR:
1435 mlog(0, "node %u hit an error, resending\n", to);
1436 resend = 1;
1437 response = 0;
1438 break;
1439 default:
1440 mlog(ML_ERROR, "bad response! %u\n", response);
1441 BUG();
1443 spin_unlock(&mle->spinlock);
1444 if (resend) {
1445 /* this is also totally crude */
1446 msleep(50);
1447 goto again;
1450 out:
1451 return ret;
1455 * locks that can be taken here:
1456 * dlm->spinlock
1457 * res->spinlock
1458 * mle->spinlock
1459 * dlm->master_list
1461 * if possible, TRIM THIS DOWN!!!
1463 int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
1464 void **ret_data)
1466 u8 response = DLM_MASTER_RESP_MAYBE;
1467 struct dlm_ctxt *dlm = data;
1468 struct dlm_lock_resource *res = NULL;
1469 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1470 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1471 char *name;
1472 unsigned int namelen, hash;
1473 int found, ret;
1474 int set_maybe;
1475 int dispatch_assert = 0;
1477 if (!dlm_grab(dlm))
1478 return DLM_MASTER_RESP_NO;
1480 if (!dlm_domain_fully_joined(dlm)) {
1481 response = DLM_MASTER_RESP_NO;
1482 goto send_response;
1485 name = request->name;
1486 namelen = request->namelen;
1487 hash = dlm_lockid_hash(name, namelen);
1489 if (namelen > DLM_LOCKID_NAME_MAX) {
1490 response = DLM_IVBUFLEN;
1491 goto send_response;
1494 way_up_top:
1495 spin_lock(&dlm->spinlock);
1496 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1497 if (res) {
1498 spin_unlock(&dlm->spinlock);
1500 /* take care of the easy cases up front */
1501 spin_lock(&res->spinlock);
1502 if (res->state & (DLM_LOCK_RES_RECOVERING|
1503 DLM_LOCK_RES_MIGRATING)) {
1504 spin_unlock(&res->spinlock);
1505 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1506 "being recovered/migrated\n");
1507 response = DLM_MASTER_RESP_ERROR;
1508 if (mle)
1509 kmem_cache_free(dlm_mle_cache, mle);
1510 goto send_response;
1513 if (res->owner == dlm->node_num) {
1514 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1515 dlm->name, namelen, name, request->node_idx);
1516 dlm_lockres_set_refmap_bit(request->node_idx, res);
1517 spin_unlock(&res->spinlock);
1518 response = DLM_MASTER_RESP_YES;
1519 if (mle)
1520 kmem_cache_free(dlm_mle_cache, mle);
1522 /* this node is the owner.
1523 * there is some extra work that needs to
1524 * happen now. the requesting node has
1525 * caused all nodes up to this one to
1526 * create mles. this node now needs to
1527 * go back and clean those up. */
1528 dispatch_assert = 1;
1529 goto send_response;
1530 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1531 spin_unlock(&res->spinlock);
1532 // mlog(0, "node %u is the master\n", res->owner);
1533 response = DLM_MASTER_RESP_NO;
1534 if (mle)
1535 kmem_cache_free(dlm_mle_cache, mle);
1536 goto send_response;
1539 /* ok, there is no owner. either this node is
1540 * being blocked, or it is actively trying to
1541 * master this lock. */
1542 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1543 mlog(ML_ERROR, "lock with no owner should be "
1544 "in-progress!\n");
1545 BUG();
1548 // mlog(0, "lockres is in progress...\n");
1549 spin_lock(&dlm->master_lock);
1550 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1551 if (!found) {
1552 mlog(ML_ERROR, "no mle found for this lock!\n");
1553 BUG();
1555 set_maybe = 1;
1556 spin_lock(&tmpmle->spinlock);
1557 if (tmpmle->type == DLM_MLE_BLOCK) {
1558 // mlog(0, "this node is waiting for "
1559 // "lockres to be mastered\n");
1560 response = DLM_MASTER_RESP_NO;
1561 } else if (tmpmle->type == DLM_MLE_MIGRATION) {
1562 mlog(0, "node %u is master, but trying to migrate to "
1563 "node %u.\n", tmpmle->master, tmpmle->new_master);
1564 if (tmpmle->master == dlm->node_num) {
1565 mlog(ML_ERROR, "no owner on lockres, but this "
1566 "node is trying to migrate it to %u?!\n",
1567 tmpmle->new_master);
1568 BUG();
1569 } else {
1570 /* the real master can respond on its own */
1571 response = DLM_MASTER_RESP_NO;
1573 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1574 set_maybe = 0;
1575 if (tmpmle->master == dlm->node_num) {
1576 response = DLM_MASTER_RESP_YES;
1577 /* this node will be the owner.
1578 * go back and clean the mles on any
1579 * other nodes */
1580 dispatch_assert = 1;
1581 dlm_lockres_set_refmap_bit(request->node_idx, res);
1582 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1583 dlm->name, namelen, name,
1584 request->node_idx);
1585 } else
1586 response = DLM_MASTER_RESP_NO;
1587 } else {
1588 // mlog(0, "this node is attempting to "
1589 // "master lockres\n");
1590 response = DLM_MASTER_RESP_MAYBE;
1592 if (set_maybe)
1593 set_bit(request->node_idx, tmpmle->maybe_map);
1594 spin_unlock(&tmpmle->spinlock);
1596 spin_unlock(&dlm->master_lock);
1597 spin_unlock(&res->spinlock);
1599 /* keep the mle attached to heartbeat events */
1600 dlm_put_mle(tmpmle);
1601 if (mle)
1602 kmem_cache_free(dlm_mle_cache, mle);
1603 goto send_response;
1607 * lockres doesn't exist on this node
1608 * if there is an MLE_BLOCK, return NO
1609 * if there is an MLE_MASTER, return MAYBE
1610 * otherwise, add an MLE_BLOCK, return NO
1612 spin_lock(&dlm->master_lock);
1613 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1614 if (!found) {
1615 /* this lockid has never been seen on this node yet */
1616 // mlog(0, "no mle found\n");
1617 if (!mle) {
1618 spin_unlock(&dlm->master_lock);
1619 spin_unlock(&dlm->spinlock);
1621 mle = (struct dlm_master_list_entry *)
1622 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
1623 if (!mle) {
1624 response = DLM_MASTER_RESP_ERROR;
1625 mlog_errno(-ENOMEM);
1626 goto send_response;
1628 goto way_up_top;
1631 // mlog(0, "this is second time thru, already allocated, "
1632 // "add the block.\n");
1633 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
1634 set_bit(request->node_idx, mle->maybe_map);
1635 list_add(&mle->list, &dlm->master_list);
1636 response = DLM_MASTER_RESP_NO;
1637 } else {
1638 // mlog(0, "mle was found\n");
1639 set_maybe = 1;
1640 spin_lock(&tmpmle->spinlock);
1641 if (tmpmle->master == dlm->node_num) {
1642 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1643 BUG();
1645 if (tmpmle->type == DLM_MLE_BLOCK)
1646 response = DLM_MASTER_RESP_NO;
1647 else if (tmpmle->type == DLM_MLE_MIGRATION) {
1648 mlog(0, "migration mle was found (%u->%u)\n",
1649 tmpmle->master, tmpmle->new_master);
1650 /* real master can respond on its own */
1651 response = DLM_MASTER_RESP_NO;
1652 } else
1653 response = DLM_MASTER_RESP_MAYBE;
1654 if (set_maybe)
1655 set_bit(request->node_idx, tmpmle->maybe_map);
1656 spin_unlock(&tmpmle->spinlock);
1658 spin_unlock(&dlm->master_lock);
1659 spin_unlock(&dlm->spinlock);
1661 if (found) {
1662 /* keep the mle attached to heartbeat events */
1663 dlm_put_mle(tmpmle);
1665 send_response:
1666 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
1668 =======
1670 * __dlm_lookup_lockres() grabbed a reference to this lockres.
1671 * The reference is released by dlm_assert_master_worker() under
1672 * the call to dlm_dispatch_assert_master(). If
1673 * dlm_assert_master_worker() isn't called, we drop it here.
1675 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
1676 if (dispatch_assert) {
1677 if (response != DLM_MASTER_RESP_YES)
1678 mlog(ML_ERROR, "invalid response %d\n", response);
1679 if (!res) {
1680 mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1681 BUG();
1683 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1684 dlm->node_num, res->lockname.len, res->lockname.name);
1685 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1686 DLM_ASSERT_MASTER_MLE_CLEANUP);
1687 if (ret < 0) {
1688 mlog(ML_ERROR, "failed to dispatch assert master work\n");
1689 response = DLM_MASTER_RESP_ERROR;
1690 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
1691 =======
1692 dlm_lockres_put(res);
1693 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
1695 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
1696 =======
1697 } else {
1698 if (res)
1699 dlm_lockres_put(res);
1700 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
1703 dlm_put(dlm);
1704 return response;
1708 * DLM_ASSERT_MASTER_MSG
1713 * NOTE: this can be used for debugging
1714 * can periodically run all locks owned by this node
1715 * and re-assert across the cluster...
1717 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
1718 int dlm_do_assert_master(struct dlm_ctxt *dlm,
1719 struct dlm_lock_resource *res,
1720 void *nodemap, u32 flags)
1721 =======
1722 static int dlm_do_assert_master(struct dlm_ctxt *dlm,
1723 struct dlm_lock_resource *res,
1724 void *nodemap, u32 flags)
1725 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
1727 struct dlm_assert_master assert;
1728 int to, tmpret;
1729 struct dlm_node_iter iter;
1730 int ret = 0;
1731 int reassert;
1732 const char *lockname = res->lockname.name;
1733 unsigned int namelen = res->lockname.len;
1735 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
1737 spin_lock(&res->spinlock);
1738 res->state |= DLM_LOCK_RES_SETREF_INPROG;
1739 spin_unlock(&res->spinlock);
1741 again:
1742 reassert = 0;
1744 /* note that if this nodemap is empty, it returns 0 */
1745 dlm_node_iter_init(nodemap, &iter);
1746 while ((to = dlm_node_iter_next(&iter)) >= 0) {
1747 int r = 0;
1748 struct dlm_master_list_entry *mle = NULL;
1750 mlog(0, "sending assert master to %d (%.*s)\n", to,
1751 namelen, lockname);
1752 memset(&assert, 0, sizeof(assert));
1753 assert.node_idx = dlm->node_num;
1754 assert.namelen = namelen;
1755 memcpy(assert.name, lockname, namelen);
1756 assert.flags = cpu_to_be32(flags);
1758 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1759 &assert, sizeof(assert), to, &r);
1760 if (tmpret < 0) {
1761 mlog(0, "assert_master returned %d!\n", tmpret);
1762 if (!dlm_is_host_down(tmpret)) {
1763 mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
1764 BUG();
1766 /* a node died. finish out the rest of the nodes. */
1767 mlog(0, "link to %d went down!\n", to);
1768 /* any nonzero status return will do */
1769 ret = tmpret;
1770 r = 0;
1771 } else if (r < 0) {
1772 /* ok, something horribly messed. kill thyself. */
1773 mlog(ML_ERROR,"during assert master of %.*s to %u, "
1774 "got %d.\n", namelen, lockname, to, r);
1775 spin_lock(&dlm->spinlock);
1776 spin_lock(&dlm->master_lock);
1777 if (dlm_find_mle(dlm, &mle, (char *)lockname,
1778 namelen)) {
1779 dlm_print_one_mle(mle);
1780 __dlm_put_mle(mle);
1782 spin_unlock(&dlm->master_lock);
1783 spin_unlock(&dlm->spinlock);
1784 BUG();
1787 if (r & DLM_ASSERT_RESPONSE_REASSERT &&
1788 !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
1789 mlog(ML_ERROR, "%.*s: very strange, "
1790 "master MLE but no lockres on %u\n",
1791 namelen, lockname, to);
1794 if (r & DLM_ASSERT_RESPONSE_REASSERT) {
1795 mlog(0, "%.*s: node %u create mles on other "
1796 "nodes and requests a re-assert\n",
1797 namelen, lockname, to);
1798 reassert = 1;
1800 if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
1801 mlog(0, "%.*s: node %u has a reference to this "
1802 "lockres, set the bit in the refmap\n",
1803 namelen, lockname, to);
1804 spin_lock(&res->spinlock);
1805 dlm_lockres_set_refmap_bit(to, res);
1806 spin_unlock(&res->spinlock);
1810 if (reassert)
1811 goto again;
1813 spin_lock(&res->spinlock);
1814 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
1815 spin_unlock(&res->spinlock);
1816 wake_up(&res->wq);
1818 return ret;
1822 * locks that can be taken here:
1823 * dlm->spinlock
1824 * res->spinlock
1825 * mle->spinlock
1826 * dlm->master_list
1828 * if possible, TRIM THIS DOWN!!!
1830 int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
1831 void **ret_data)
1833 struct dlm_ctxt *dlm = data;
1834 struct dlm_master_list_entry *mle = NULL;
1835 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1836 struct dlm_lock_resource *res = NULL;
1837 char *name;
1838 unsigned int namelen, hash;
1839 u32 flags;
1840 int master_request = 0, have_lockres_ref = 0;
1841 int ret = 0;
1843 if (!dlm_grab(dlm))
1844 return 0;
1846 name = assert->name;
1847 namelen = assert->namelen;
1848 hash = dlm_lockid_hash(name, namelen);
1849 flags = be32_to_cpu(assert->flags);
1851 if (namelen > DLM_LOCKID_NAME_MAX) {
1852 mlog(ML_ERROR, "Invalid name length!");
1853 goto done;
1856 spin_lock(&dlm->spinlock);
1858 if (flags)
1859 mlog(0, "assert_master with flags: %u\n", flags);
1861 /* find the MLE */
1862 spin_lock(&dlm->master_lock);
1863 if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1864 /* not an error, could be master just re-asserting */
1865 mlog(0, "just got an assert_master from %u, but no "
1866 "MLE for it! (%.*s)\n", assert->node_idx,
1867 namelen, name);
1868 } else {
1869 int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1870 if (bit >= O2NM_MAX_NODES) {
1871 /* not necessarily an error, though less likely.
1872 * could be master just re-asserting. */
1873 mlog(0, "no bits set in the maybe_map, but %u "
1874 "is asserting! (%.*s)\n", assert->node_idx,
1875 namelen, name);
1876 } else if (bit != assert->node_idx) {
1877 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1878 mlog(0, "master %u was found, %u should "
1879 "back off\n", assert->node_idx, bit);
1880 } else {
1881 /* with the fix for bug 569, a higher node
1882 * number winning the mastery will respond
1883 * YES to mastery requests, but this node
1884 * had no way of knowing. let it pass. */
1885 mlog(0, "%u is the lowest node, "
1886 "%u is asserting. (%.*s) %u must "
1887 "have begun after %u won.\n", bit,
1888 assert->node_idx, namelen, name, bit,
1889 assert->node_idx);
1892 if (mle->type == DLM_MLE_MIGRATION) {
1893 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1894 mlog(0, "%s:%.*s: got cleanup assert"
1895 " from %u for migration\n",
1896 dlm->name, namelen, name,
1897 assert->node_idx);
1898 } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
1899 mlog(0, "%s:%.*s: got unrelated assert"
1900 " from %u for migration, ignoring\n",
1901 dlm->name, namelen, name,
1902 assert->node_idx);
1903 __dlm_put_mle(mle);
1904 spin_unlock(&dlm->master_lock);
1905 spin_unlock(&dlm->spinlock);
1906 goto done;
1910 spin_unlock(&dlm->master_lock);
1912 /* ok everything checks out with the MLE
1913 * now check to see if there is a lockres */
1914 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1915 if (res) {
1916 spin_lock(&res->spinlock);
1917 if (res->state & DLM_LOCK_RES_RECOVERING) {
1918 mlog(ML_ERROR, "%u asserting but %.*s is "
1919 "RECOVERING!\n", assert->node_idx, namelen, name);
1920 goto kill;
1922 if (!mle) {
1923 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
1924 res->owner != assert->node_idx) {
1925 mlog(ML_ERROR, "assert_master from "
1926 "%u, but current owner is "
1927 "%u! (%.*s)\n",
1928 assert->node_idx, res->owner,
1929 namelen, name);
1930 goto kill;
1932 } else if (mle->type != DLM_MLE_MIGRATION) {
1933 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1934 /* owner is just re-asserting */
1935 if (res->owner == assert->node_idx) {
1936 mlog(0, "owner %u re-asserting on "
1937 "lock %.*s\n", assert->node_idx,
1938 namelen, name);
1939 goto ok;
1941 mlog(ML_ERROR, "got assert_master from "
1942 "node %u, but %u is the owner! "
1943 "(%.*s)\n", assert->node_idx,
1944 res->owner, namelen, name);
1945 goto kill;
1947 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1948 mlog(ML_ERROR, "got assert from %u, but lock "
1949 "with no owner should be "
1950 "in-progress! (%.*s)\n",
1951 assert->node_idx,
1952 namelen, name);
1953 goto kill;
1955 } else /* mle->type == DLM_MLE_MIGRATION */ {
1956 /* should only be getting an assert from new master */
1957 if (assert->node_idx != mle->new_master) {
1958 mlog(ML_ERROR, "got assert from %u, but "
1959 "new master is %u, and old master "
1960 "was %u (%.*s)\n",
1961 assert->node_idx, mle->new_master,
1962 mle->master, namelen, name);
1963 goto kill;
1968 spin_unlock(&res->spinlock);
1970 spin_unlock(&dlm->spinlock);
1972 // mlog(0, "woo! got an assert_master from node %u!\n",
1973 // assert->node_idx);
1974 if (mle) {
1975 int extra_ref = 0;
1976 int nn = -1;
1977 int rr, err = 0;
1979 spin_lock(&mle->spinlock);
1980 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1981 extra_ref = 1;
1982 else {
1983 /* MASTER mle: if any bits set in the response map
1984 * then the calling node needs to re-assert to clear
1985 * up nodes that this node contacted */
1986 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1987 nn+1)) < O2NM_MAX_NODES) {
1988 if (nn != dlm->node_num && nn != assert->node_idx)
1989 master_request = 1;
1992 mle->master = assert->node_idx;
1993 atomic_set(&mle->woken, 1);
1994 wake_up(&mle->wq);
1995 spin_unlock(&mle->spinlock);
1997 if (res) {
1998 int wake = 0;
1999 spin_lock(&res->spinlock);
2000 if (mle->type == DLM_MLE_MIGRATION) {
2001 mlog(0, "finishing off migration of lockres %.*s, "
2002 "from %u to %u\n",
2003 res->lockname.len, res->lockname.name,
2004 dlm->node_num, mle->new_master);
2005 res->state &= ~DLM_LOCK_RES_MIGRATING;
2006 wake = 1;
2007 dlm_change_lockres_owner(dlm, res, mle->new_master);
2008 BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
2009 } else {
2010 dlm_change_lockres_owner(dlm, res, mle->master);
2012 spin_unlock(&res->spinlock);
2013 have_lockres_ref = 1;
2014 if (wake)
2015 wake_up(&res->wq);
2018 /* master is known, detach if not already detached.
2019 * ensures that only one assert_master call will happen
2020 * on this mle. */
2021 spin_lock(&dlm->spinlock);
2022 spin_lock(&dlm->master_lock);
2024 rr = atomic_read(&mle->mle_refs.refcount);
2025 if (mle->inuse > 0) {
2026 if (extra_ref && rr < 3)
2027 err = 1;
2028 else if (!extra_ref && rr < 2)
2029 err = 1;
2030 } else {
2031 if (extra_ref && rr < 2)
2032 err = 1;
2033 else if (!extra_ref && rr < 1)
2034 err = 1;
2036 if (err) {
2037 mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
2038 "that will mess up this node, refs=%d, extra=%d, "
2039 "inuse=%d\n", dlm->name, namelen, name,
2040 assert->node_idx, rr, extra_ref, mle->inuse);
2041 dlm_print_one_mle(mle);
2043 list_del_init(&mle->list);
2044 __dlm_mle_detach_hb_events(dlm, mle);
2045 __dlm_put_mle(mle);
2046 if (extra_ref) {
2047 /* the assert master message now balances the extra
2048 * ref given by the master / migration request message.
2049 * if this is the last put, it will be removed
2050 * from the list. */
2051 __dlm_put_mle(mle);
2053 spin_unlock(&dlm->master_lock);
2054 spin_unlock(&dlm->spinlock);
2055 } else if (res) {
2056 if (res->owner != assert->node_idx) {
2057 mlog(0, "assert_master from %u, but current "
2058 "owner is %u (%.*s), no mle\n", assert->node_idx,
2059 res->owner, namelen, name);
2063 done:
2064 ret = 0;
2065 if (res) {
2066 spin_lock(&res->spinlock);
2067 res->state |= DLM_LOCK_RES_SETREF_INPROG;
2068 spin_unlock(&res->spinlock);
2069 *ret_data = (void *)res;
2071 dlm_put(dlm);
2072 if (master_request) {
2073 mlog(0, "need to tell master to reassert\n");
2074 /* positive. negative would shoot down the node. */
2075 ret |= DLM_ASSERT_RESPONSE_REASSERT;
2076 if (!have_lockres_ref) {
2077 mlog(ML_ERROR, "strange, got assert from %u, MASTER "
2078 "mle present here for %s:%.*s, but no lockres!\n",
2079 assert->node_idx, dlm->name, namelen, name);
2082 if (have_lockres_ref) {
2083 /* let the master know we have a reference to the lockres */
2084 ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
2085 mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
2086 dlm->name, namelen, name, assert->node_idx);
2088 return ret;
2090 kill:
2091 /* kill the caller! */
2092 mlog(ML_ERROR, "Bad message received from another node. Dumping state "
2093 "and killing the other node now! This node is OK and can continue.\n");
2094 __dlm_print_one_lock_resource(res);
2095 spin_unlock(&res->spinlock);
2096 spin_unlock(&dlm->spinlock);
2097 *ret_data = (void *)res;
2098 dlm_put(dlm);
2099 return -EINVAL;
2102 void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
2104 struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
2106 if (ret_data) {
2107 spin_lock(&res->spinlock);
2108 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
2109 spin_unlock(&res->spinlock);
2110 wake_up(&res->wq);
2111 dlm_lockres_put(res);
2113 return;
2116 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
2117 struct dlm_lock_resource *res,
2118 int ignore_higher, u8 request_from, u32 flags)
2120 struct dlm_work_item *item;
2121 item = kzalloc(sizeof(*item), GFP_NOFS);
2122 if (!item)
2123 return -ENOMEM;
2126 /* queue up work for dlm_assert_master_worker */
2127 dlm_grab(dlm); /* get an extra ref for the work item */
2128 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
2129 item->u.am.lockres = res; /* already have a ref */
2130 /* can optionally ignore node numbers higher than this node */
2131 item->u.am.ignore_higher = ignore_higher;
2132 item->u.am.request_from = request_from;
2133 item->u.am.flags = flags;
2135 if (ignore_higher)
2136 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
2137 res->lockname.name);
2139 spin_lock(&dlm->work_lock);
2140 list_add_tail(&item->list, &dlm->work_list);
2141 spin_unlock(&dlm->work_lock);
2143 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2144 return 0;
2147 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
2149 struct dlm_ctxt *dlm = data;
2150 int ret = 0;
2151 struct dlm_lock_resource *res;
2152 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
2153 int ignore_higher;
2154 int bit;
2155 u8 request_from;
2156 u32 flags;
2158 dlm = item->dlm;
2159 res = item->u.am.lockres;
2160 ignore_higher = item->u.am.ignore_higher;
2161 request_from = item->u.am.request_from;
2162 flags = item->u.am.flags;
2164 spin_lock(&dlm->spinlock);
2165 memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
2166 spin_unlock(&dlm->spinlock);
2168 clear_bit(dlm->node_num, nodemap);
2169 if (ignore_higher) {
2170 /* if is this just to clear up mles for nodes below
2171 * this node, do not send the message to the original
2172 * caller or any node number higher than this */
2173 clear_bit(request_from, nodemap);
2174 bit = dlm->node_num;
2175 while (1) {
2176 bit = find_next_bit(nodemap, O2NM_MAX_NODES,
2177 bit+1);
2178 if (bit >= O2NM_MAX_NODES)
2179 break;
2180 clear_bit(bit, nodemap);
2185 * If we're migrating this lock to someone else, we are no
2186 * longer allowed to assert out own mastery. OTOH, we need to
2187 * prevent migration from starting while we're still asserting
2188 * our dominance. The reserved ast delays migration.
2190 spin_lock(&res->spinlock);
2191 if (res->state & DLM_LOCK_RES_MIGRATING) {
2192 mlog(0, "Someone asked us to assert mastery, but we're "
2193 "in the middle of migration. Skipping assert, "
2194 "the new master will handle that.\n");
2195 spin_unlock(&res->spinlock);
2196 goto put;
2197 } else
2198 __dlm_lockres_reserve_ast(res);
2199 spin_unlock(&res->spinlock);
2201 /* this call now finishes out the nodemap
2202 * even if one or more nodes die */
2203 mlog(0, "worker about to master %.*s here, this=%u\n",
2204 res->lockname.len, res->lockname.name, dlm->node_num);
2205 ret = dlm_do_assert_master(dlm, res, nodemap, flags);
2206 if (ret < 0) {
2207 /* no need to restart, we are done */
2208 if (!dlm_is_host_down(ret))
2209 mlog_errno(ret);
2212 /* Ok, we've asserted ourselves. Let's let migration start. */
2213 dlm_lockres_release_ast(dlm, res);
2215 put:
2216 dlm_lockres_put(res);
2218 mlog(0, "finished with dlm_assert_master_worker\n");
2221 /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2222 * We cannot wait for node recovery to complete to begin mastering this
2223 * lockres because this lockres is used to kick off recovery! ;-)
2224 * So, do a pre-check on all living nodes to see if any of those nodes
2225 * think that $RECOVERY is currently mastered by a dead node. If so,
2226 * we wait a short time to allow that node to get notified by its own
2227 * heartbeat stack, then check again. All $RECOVERY lock resources
2228 * mastered by dead nodes are purged when the hearbeat callback is
2229 * fired, so we can know for sure that it is safe to continue once
2230 * the node returns a live node or no node. */
2231 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
2232 struct dlm_lock_resource *res)
2234 struct dlm_node_iter iter;
2235 int nodenum;
2236 int ret = 0;
2237 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
2239 spin_lock(&dlm->spinlock);
2240 dlm_node_iter_init(dlm->domain_map, &iter);
2241 spin_unlock(&dlm->spinlock);
2243 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2244 /* do not send to self */
2245 if (nodenum == dlm->node_num)
2246 continue;
2247 ret = dlm_do_master_requery(dlm, res, nodenum, &master);
2248 if (ret < 0) {
2249 mlog_errno(ret);
2250 if (!dlm_is_host_down(ret))
2251 BUG();
2252 /* host is down, so answer for that node would be
2253 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
2254 ret = 0;
2257 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
2258 /* check to see if this master is in the recovery map */
2259 spin_lock(&dlm->spinlock);
2260 if (test_bit(master, dlm->recovery_map)) {
2261 mlog(ML_NOTICE, "%s: node %u has not seen "
2262 "node %u go down yet, and thinks the "
2263 "dead node is mastering the recovery "
2264 "lock. must wait.\n", dlm->name,
2265 nodenum, master);
2266 ret = -EAGAIN;
2268 spin_unlock(&dlm->spinlock);
2269 mlog(0, "%s: reco lock master is %u\n", dlm->name,
2270 master);
2271 break;
2274 return ret;
2278 * DLM_DEREF_LOCKRES_MSG
2281 int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2283 struct dlm_deref_lockres deref;
2284 int ret = 0, r;
2285 const char *lockname;
2286 unsigned int namelen;
2288 lockname = res->lockname.name;
2289 namelen = res->lockname.len;
2290 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
2292 mlog(0, "%s:%.*s: sending deref to %d\n",
2293 dlm->name, namelen, lockname, res->owner);
2294 memset(&deref, 0, sizeof(deref));
2295 deref.node_idx = dlm->node_num;
2296 deref.namelen = namelen;
2297 memcpy(deref.name, lockname, namelen);
2299 ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
2300 &deref, sizeof(deref), res->owner, &r);
2301 if (ret < 0)
2302 mlog_errno(ret);
2303 else if (r < 0) {
2304 /* BAD. other node says I did not have a ref. */
2305 mlog(ML_ERROR,"while dropping ref on %s:%.*s "
2306 "(master=%u) got %d.\n", dlm->name, namelen,
2307 lockname, res->owner, r);
2308 dlm_print_one_lock_resource(res);
2309 BUG();
2311 return ret;
2314 int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
2315 void **ret_data)
2317 struct dlm_ctxt *dlm = data;
2318 struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
2319 struct dlm_lock_resource *res = NULL;
2320 char *name;
2321 unsigned int namelen;
2322 int ret = -EINVAL;
2323 u8 node;
2324 unsigned int hash;
2325 struct dlm_work_item *item;
2326 int cleared = 0;
2327 int dispatch = 0;
2329 if (!dlm_grab(dlm))
2330 return 0;
2332 name = deref->name;
2333 namelen = deref->namelen;
2334 node = deref->node_idx;
2336 if (namelen > DLM_LOCKID_NAME_MAX) {
2337 mlog(ML_ERROR, "Invalid name length!");
2338 goto done;
2340 if (deref->node_idx >= O2NM_MAX_NODES) {
2341 mlog(ML_ERROR, "Invalid node number: %u\n", node);
2342 goto done;
2345 hash = dlm_lockid_hash(name, namelen);
2347 spin_lock(&dlm->spinlock);
2348 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
2349 if (!res) {
2350 spin_unlock(&dlm->spinlock);
2351 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
2352 dlm->name, namelen, name);
2353 goto done;
2355 spin_unlock(&dlm->spinlock);
2357 spin_lock(&res->spinlock);
2358 if (res->state & DLM_LOCK_RES_SETREF_INPROG)
2359 dispatch = 1;
2360 else {
2361 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2362 if (test_bit(node, res->refmap)) {
2363 dlm_lockres_clear_refmap_bit(node, res);
2364 cleared = 1;
2367 spin_unlock(&res->spinlock);
2369 if (!dispatch) {
2370 if (cleared)
2371 dlm_lockres_calc_usage(dlm, res);
2372 else {
2373 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2374 "but it is already dropped!\n", dlm->name,
2375 res->lockname.len, res->lockname.name, node);
2376 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
2377 __dlm_print_one_lock_resource(res);
2378 =======
2379 dlm_print_one_lock_resource(res);
2380 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
2382 ret = 0;
2383 goto done;
2386 item = kzalloc(sizeof(*item), GFP_NOFS);
2387 if (!item) {
2388 ret = -ENOMEM;
2389 mlog_errno(ret);
2390 goto done;
2393 dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
2394 item->u.dl.deref_res = res;
2395 item->u.dl.deref_node = node;
2397 spin_lock(&dlm->work_lock);
2398 list_add_tail(&item->list, &dlm->work_list);
2399 spin_unlock(&dlm->work_lock);
2401 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2402 return 0;
2404 done:
2405 if (res)
2406 dlm_lockres_put(res);
2407 dlm_put(dlm);
2409 return ret;
2412 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
2414 struct dlm_ctxt *dlm;
2415 struct dlm_lock_resource *res;
2416 u8 node;
2417 u8 cleared = 0;
2419 dlm = item->dlm;
2420 res = item->u.dl.deref_res;
2421 node = item->u.dl.deref_node;
2423 spin_lock(&res->spinlock);
2424 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2425 if (test_bit(node, res->refmap)) {
2426 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2427 dlm_lockres_clear_refmap_bit(node, res);
2428 cleared = 1;
2430 spin_unlock(&res->spinlock);
2432 if (cleared) {
2433 mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
2434 dlm->name, res->lockname.len, res->lockname.name, node);
2435 dlm_lockres_calc_usage(dlm, res);
2436 } else {
2437 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2438 "but it is already dropped!\n", dlm->name,
2439 res->lockname.len, res->lockname.name, node);
2440 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
2441 __dlm_print_one_lock_resource(res);
2442 =======
2443 dlm_print_one_lock_resource(res);
2444 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
2447 dlm_lockres_put(res);
2450 /* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
2451 * if not. If 0, numlocks is set to the number of locks in the lockres.
2453 static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
2454 struct dlm_lock_resource *res,
2455 int *numlocks)
2457 int ret;
2458 int i;
2459 int count = 0;
2460 struct list_head *queue;
2461 struct dlm_lock *lock;
2463 assert_spin_locked(&res->spinlock);
2465 ret = -EINVAL;
2466 if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2467 mlog(0, "cannot migrate lockres with unknown owner!\n");
2468 goto leave;
2471 if (res->owner != dlm->node_num) {
2472 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2473 goto leave;
2476 ret = 0;
2477 queue = &res->granted;
2478 for (i = 0; i < 3; i++) {
2479 list_for_each_entry(lock, queue, list) {
2480 ++count;
2481 if (lock->ml.node == dlm->node_num) {
2482 mlog(0, "found a lock owned by this node still "
2483 "on the %s queue! will not migrate this "
2484 "lockres\n", (i == 0 ? "granted" :
2485 (i == 1 ? "converting" :
2486 "blocked")));
2487 ret = -ENOTEMPTY;
2488 goto leave;
2491 queue++;
2494 *numlocks = count;
2495 mlog(0, "migrateable lockres having %d locks\n", *numlocks);
2497 leave:
2498 return ret;
2502 * DLM_MIGRATE_LOCKRES
2506 static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
2507 struct dlm_lock_resource *res,
2508 u8 target)
2510 struct dlm_master_list_entry *mle = NULL;
2511 struct dlm_master_list_entry *oldmle = NULL;
2512 struct dlm_migratable_lockres *mres = NULL;
2513 int ret = 0;
2514 const char *name;
2515 unsigned int namelen;
2516 int mle_added = 0;
2517 int numlocks;
2518 int wake = 0;
2520 if (!dlm_grab(dlm))
2521 return -EINVAL;
2523 name = res->lockname.name;
2524 namelen = res->lockname.len;
2526 mlog(0, "migrating %.*s to %u\n", namelen, name, target);
2529 * ensure this lockres is a proper candidate for migration
2531 spin_lock(&res->spinlock);
2532 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2533 if (ret < 0) {
2534 spin_unlock(&res->spinlock);
2535 goto leave;
2537 spin_unlock(&res->spinlock);
2539 /* no work to do */
2540 if (numlocks == 0) {
2541 mlog(0, "no locks were found on this lockres! done!\n");
2542 goto leave;
2546 * preallocate up front
2547 * if this fails, abort
2550 ret = -ENOMEM;
2551 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
2552 if (!mres) {
2553 mlog_errno(ret);
2554 goto leave;
2557 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2558 GFP_NOFS);
2559 if (!mle) {
2560 mlog_errno(ret);
2561 goto leave;
2563 ret = 0;
2566 * find a node to migrate the lockres to
2569 mlog(0, "picking a migration node\n");
2570 spin_lock(&dlm->spinlock);
2571 /* pick a new node */
2572 if (!test_bit(target, dlm->domain_map) ||
2573 target >= O2NM_MAX_NODES) {
2574 target = dlm_pick_migration_target(dlm, res);
2576 mlog(0, "node %u chosen for migration\n", target);
2578 if (target >= O2NM_MAX_NODES ||
2579 !test_bit(target, dlm->domain_map)) {
2580 /* target chosen is not alive */
2581 ret = -EINVAL;
2584 if (ret) {
2585 spin_unlock(&dlm->spinlock);
2586 goto fail;
2589 mlog(0, "continuing with target = %u\n", target);
2592 * clear any existing master requests and
2593 * add the migration mle to the list
2595 spin_lock(&dlm->master_lock);
2596 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2597 namelen, target, dlm->node_num);
2598 spin_unlock(&dlm->master_lock);
2599 spin_unlock(&dlm->spinlock);
2601 if (ret == -EEXIST) {
2602 mlog(0, "another process is already migrating it\n");
2603 goto fail;
2605 mle_added = 1;
2608 * set the MIGRATING flag and flush asts
2609 * if we fail after this we need to re-dirty the lockres
2611 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2612 mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2613 "the target went down.\n", res->lockname.len,
2614 res->lockname.name, target);
2615 spin_lock(&res->spinlock);
2616 res->state &= ~DLM_LOCK_RES_MIGRATING;
2617 wake = 1;
2618 spin_unlock(&res->spinlock);
2619 ret = -EINVAL;
2622 fail:
2623 if (oldmle) {
2624 /* master is known, detach if not already detached */
2625 dlm_mle_detach_hb_events(dlm, oldmle);
2626 dlm_put_mle(oldmle);
2629 if (ret < 0) {
2630 if (mle_added) {
2631 dlm_mle_detach_hb_events(dlm, mle);
2632 dlm_put_mle(mle);
2633 } else if (mle) {
2634 kmem_cache_free(dlm_mle_cache, mle);
2636 goto leave;
2640 * at this point, we have a migration target, an mle
2641 * in the master list, and the MIGRATING flag set on
2642 * the lockres
2645 /* now that remote nodes are spinning on the MIGRATING flag,
2646 * ensure that all assert_master work is flushed. */
2647 flush_workqueue(dlm->dlm_worker);
2649 /* get an extra reference on the mle.
2650 * otherwise the assert_master from the new
2651 * master will destroy this.
2652 * also, make sure that all callers of dlm_get_mle
2653 * take both dlm->spinlock and dlm->master_lock */
2654 spin_lock(&dlm->spinlock);
2655 spin_lock(&dlm->master_lock);
2656 dlm_get_mle_inuse(mle);
2657 spin_unlock(&dlm->master_lock);
2658 spin_unlock(&dlm->spinlock);
2660 /* notify new node and send all lock state */
2661 /* call send_one_lockres with migration flag.
2662 * this serves as notice to the target node that a
2663 * migration is starting. */
2664 ret = dlm_send_one_lockres(dlm, res, mres, target,
2665 DLM_MRES_MIGRATION);
2667 if (ret < 0) {
2668 mlog(0, "migration to node %u failed with %d\n",
2669 target, ret);
2670 /* migration failed, detach and clean up mle */
2671 dlm_mle_detach_hb_events(dlm, mle);
2672 dlm_put_mle(mle);
2673 dlm_put_mle_inuse(mle);
2674 spin_lock(&res->spinlock);
2675 res->state &= ~DLM_LOCK_RES_MIGRATING;
2676 wake = 1;
2677 spin_unlock(&res->spinlock);
2678 goto leave;
2681 /* at this point, the target sends a message to all nodes,
2682 * (using dlm_do_migrate_request). this node is skipped since
2683 * we had to put an mle in the list to begin the process. this
2684 * node now waits for target to do an assert master. this node
2685 * will be the last one notified, ensuring that the migration
2686 * is complete everywhere. if the target dies while this is
2687 * going on, some nodes could potentially see the target as the
2688 * master, so it is important that my recovery finds the migration
2689 * mle and sets the master to UNKNONWN. */
2692 /* wait for new node to assert master */
2693 while (1) {
2694 ret = wait_event_interruptible_timeout(mle->wq,
2695 (atomic_read(&mle->woken) == 1),
2696 msecs_to_jiffies(5000));
2698 if (ret >= 0) {
2699 if (atomic_read(&mle->woken) == 1 ||
2700 res->owner == target)
2701 break;
2703 mlog(0, "%s:%.*s: timed out during migration\n",
2704 dlm->name, res->lockname.len, res->lockname.name);
2705 /* avoid hang during shutdown when migrating lockres
2706 * to a node which also goes down */
2707 if (dlm_is_node_dead(dlm, target)) {
2708 mlog(0, "%s:%.*s: expected migration "
2709 "target %u is no longer up, restarting\n",
2710 dlm->name, res->lockname.len,
2711 res->lockname.name, target);
2712 ret = -EINVAL;
2713 /* migration failed, detach and clean up mle */
2714 dlm_mle_detach_hb_events(dlm, mle);
2715 dlm_put_mle(mle);
2716 dlm_put_mle_inuse(mle);
2717 spin_lock(&res->spinlock);
2718 res->state &= ~DLM_LOCK_RES_MIGRATING;
2719 wake = 1;
2720 spin_unlock(&res->spinlock);
2721 goto leave;
2723 } else
2724 mlog(0, "%s:%.*s: caught signal during migration\n",
2725 dlm->name, res->lockname.len, res->lockname.name);
2728 /* all done, set the owner, clear the flag */
2729 spin_lock(&res->spinlock);
2730 dlm_set_lockres_owner(dlm, res, target);
2731 res->state &= ~DLM_LOCK_RES_MIGRATING;
2732 dlm_remove_nonlocal_locks(dlm, res);
2733 spin_unlock(&res->spinlock);
2734 wake_up(&res->wq);
2736 /* master is known, detach if not already detached */
2737 dlm_mle_detach_hb_events(dlm, mle);
2738 dlm_put_mle_inuse(mle);
2739 ret = 0;
2741 dlm_lockres_calc_usage(dlm, res);
2743 leave:
2744 /* re-dirty the lockres if we failed */
2745 if (ret < 0)
2746 dlm_kick_thread(dlm, res);
2748 /* wake up waiters if the MIGRATING flag got set
2749 * but migration failed */
2750 if (wake)
2751 wake_up(&res->wq);
2753 /* TODO: cleanup */
2754 if (mres)
2755 free_page((unsigned long)mres);
2757 dlm_put(dlm);
2759 mlog(0, "returning %d\n", ret);
2760 return ret;
2763 #define DLM_MIGRATION_RETRY_MS 100
2765 /* Should be called only after beginning the domain leave process.
2766 * There should not be any remaining locks on nonlocal lock resources,
2767 * and there should be no local locks left on locally mastered resources.
2769 * Called with the dlm spinlock held, may drop it to do migration, but
2770 * will re-acquire before exit.
2772 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2773 int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2775 int ret;
2776 int lock_dropped = 0;
2777 int numlocks;
2779 spin_lock(&res->spinlock);
2780 if (res->owner != dlm->node_num) {
2781 if (!__dlm_lockres_unused(res)) {
2782 mlog(ML_ERROR, "%s:%.*s: this node is not master, "
2783 "trying to free this but locks remain\n",
2784 dlm->name, res->lockname.len, res->lockname.name);
2786 spin_unlock(&res->spinlock);
2787 goto leave;
2790 /* No need to migrate a lockres having no locks */
2791 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2792 if (ret >= 0 && numlocks == 0) {
2793 spin_unlock(&res->spinlock);
2794 goto leave;
2796 spin_unlock(&res->spinlock);
2798 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2799 spin_unlock(&dlm->spinlock);
2800 lock_dropped = 1;
2801 while (1) {
2802 ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
2803 if (ret >= 0)
2804 break;
2805 if (ret == -ENOTEMPTY) {
2806 mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
2807 res->lockname.len, res->lockname.name);
2808 BUG();
2811 mlog(0, "lockres %.*s: migrate failed, "
2812 "retrying\n", res->lockname.len,
2813 res->lockname.name);
2814 msleep(DLM_MIGRATION_RETRY_MS);
2816 spin_lock(&dlm->spinlock);
2817 leave:
2818 return lock_dropped;
2821 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2823 int ret;
2824 spin_lock(&dlm->ast_lock);
2825 spin_lock(&lock->spinlock);
2826 ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2827 spin_unlock(&lock->spinlock);
2828 spin_unlock(&dlm->ast_lock);
2829 return ret;
2832 static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2833 struct dlm_lock_resource *res,
2834 u8 mig_target)
2836 int can_proceed;
2837 spin_lock(&res->spinlock);
2838 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2839 spin_unlock(&res->spinlock);
2841 /* target has died, so make the caller break out of the
2842 * wait_event, but caller must recheck the domain_map */
2843 spin_lock(&dlm->spinlock);
2844 if (!test_bit(mig_target, dlm->domain_map))
2845 can_proceed = 1;
2846 spin_unlock(&dlm->spinlock);
2847 return can_proceed;
2850 static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
2851 struct dlm_lock_resource *res)
2853 int ret;
2854 spin_lock(&res->spinlock);
2855 ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2856 spin_unlock(&res->spinlock);
2857 return ret;
2861 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2862 struct dlm_lock_resource *res,
2863 u8 target)
2865 int ret = 0;
2867 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2868 res->lockname.len, res->lockname.name, dlm->node_num,
2869 target);
2870 /* need to set MIGRATING flag on lockres. this is done by
2871 * ensuring that all asts have been flushed for this lockres. */
2872 spin_lock(&res->spinlock);
2873 BUG_ON(res->migration_pending);
2874 res->migration_pending = 1;
2875 /* strategy is to reserve an extra ast then release
2876 * it below, letting the release do all of the work */
2877 __dlm_lockres_reserve_ast(res);
2878 spin_unlock(&res->spinlock);
2880 /* now flush all the pending asts */
2881 dlm_kick_thread(dlm, res);
2882 /* before waiting on DIRTY, block processes which may
2883 * try to dirty the lockres before MIGRATING is set */
2884 spin_lock(&res->spinlock);
2885 BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
2886 res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
2887 spin_unlock(&res->spinlock);
2888 /* now wait on any pending asts and the DIRTY state */
2889 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2890 dlm_lockres_release_ast(dlm, res);
2892 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2893 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2894 /* if the extra ref we just put was the final one, this
2895 * will pass thru immediately. otherwise, we need to wait
2896 * for the last ast to finish. */
2897 again:
2898 ret = wait_event_interruptible_timeout(dlm->migration_wq,
2899 dlm_migration_can_proceed(dlm, res, target),
2900 msecs_to_jiffies(1000));
2901 if (ret < 0) {
2902 mlog(0, "woken again: migrating? %s, dead? %s\n",
2903 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2904 test_bit(target, dlm->domain_map) ? "no":"yes");
2905 } else {
2906 mlog(0, "all is well: migrating? %s, dead? %s\n",
2907 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2908 test_bit(target, dlm->domain_map) ? "no":"yes");
2910 if (!dlm_migration_can_proceed(dlm, res, target)) {
2911 mlog(0, "trying again...\n");
2912 goto again;
2914 /* now that we are sure the MIGRATING state is there, drop
2915 * the unneded state which blocked threads trying to DIRTY */
2916 spin_lock(&res->spinlock);
2917 BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
2918 BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
2919 res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
2920 spin_unlock(&res->spinlock);
2922 /* did the target go down or die? */
2923 spin_lock(&dlm->spinlock);
2924 if (!test_bit(target, dlm->domain_map)) {
2925 mlog(ML_ERROR, "aha. migration target %u just went down\n",
2926 target);
2927 ret = -EHOSTDOWN;
2929 spin_unlock(&dlm->spinlock);
2932 * at this point:
2934 * o the DLM_LOCK_RES_MIGRATING flag is set
2935 * o there are no pending asts on this lockres
2936 * o all processes trying to reserve an ast on this
2937 * lockres must wait for the MIGRATING flag to clear
2939 return ret;
2942 /* last step in the migration process.
2943 * original master calls this to free all of the dlm_lock
2944 * structures that used to be for other nodes. */
2945 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2946 struct dlm_lock_resource *res)
2948 struct list_head *queue = &res->granted;
2949 int i, bit;
2950 struct dlm_lock *lock, *next;
2952 assert_spin_locked(&res->spinlock);
2954 BUG_ON(res->owner == dlm->node_num);
2956 for (i=0; i<3; i++) {
2957 list_for_each_entry_safe(lock, next, queue, list) {
2958 if (lock->ml.node != dlm->node_num) {
2959 mlog(0, "putting lock for node %u\n",
2960 lock->ml.node);
2961 /* be extra careful */
2962 BUG_ON(!list_empty(&lock->ast_list));
2963 BUG_ON(!list_empty(&lock->bast_list));
2964 BUG_ON(lock->ast_pending);
2965 BUG_ON(lock->bast_pending);
2966 dlm_lockres_clear_refmap_bit(lock->ml.node, res);
2967 list_del_init(&lock->list);
2968 dlm_lock_put(lock);
2969 <<<<<<< HEAD:fs/ocfs2/dlm/dlmmaster.c
2970 =======
2971 /* In a normal unlock, we would have added a
2972 * DLM_UNLOCK_FREE_LOCK action. Force it. */
2973 dlm_lock_put(lock);
2974 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ocfs2/dlm/dlmmaster.c
2977 queue++;
2979 bit = 0;
2980 while (1) {
2981 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
2982 if (bit >= O2NM_MAX_NODES)
2983 break;
2984 /* do not clear the local node reference, if there is a
2985 * process holding this, let it drop the ref itself */
2986 if (bit != dlm->node_num) {
2987 mlog(0, "%s:%.*s: node %u had a ref to this "
2988 "migrating lockres, clearing\n", dlm->name,
2989 res->lockname.len, res->lockname.name, bit);
2990 dlm_lockres_clear_refmap_bit(bit, res);
2992 bit++;
2996 /* for now this is not too intelligent. we will
2997 * need stats to make this do the right thing.
2998 * this just finds the first lock on one of the
2999 * queues and uses that node as the target. */
3000 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
3001 struct dlm_lock_resource *res)
3003 int i;
3004 struct list_head *queue = &res->granted;
3005 struct dlm_lock *lock;
3006 int nodenum;
3008 assert_spin_locked(&dlm->spinlock);
3010 spin_lock(&res->spinlock);
3011 for (i=0; i<3; i++) {
3012 list_for_each_entry(lock, queue, list) {
3013 /* up to the caller to make sure this node
3014 * is alive */
3015 if (lock->ml.node != dlm->node_num) {
3016 spin_unlock(&res->spinlock);
3017 return lock->ml.node;
3020 queue++;
3022 spin_unlock(&res->spinlock);
3023 mlog(0, "have not found a suitable target yet! checking domain map\n");
3025 /* ok now we're getting desperate. pick anyone alive. */
3026 nodenum = -1;
3027 while (1) {
3028 nodenum = find_next_bit(dlm->domain_map,
3029 O2NM_MAX_NODES, nodenum+1);
3030 mlog(0, "found %d in domain map\n", nodenum);
3031 if (nodenum >= O2NM_MAX_NODES)
3032 break;
3033 if (nodenum != dlm->node_num) {
3034 mlog(0, "picking %d\n", nodenum);
3035 return nodenum;
3039 mlog(0, "giving up. no master to migrate to\n");
3040 return DLM_LOCK_RES_OWNER_UNKNOWN;
3045 /* this is called by the new master once all lockres
3046 * data has been received */
3047 static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
3048 struct dlm_lock_resource *res,
3049 u8 master, u8 new_master,
3050 struct dlm_node_iter *iter)
3052 struct dlm_migrate_request migrate;
3053 int ret, status = 0;
3054 int nodenum;
3056 memset(&migrate, 0, sizeof(migrate));
3057 migrate.namelen = res->lockname.len;
3058 memcpy(migrate.name, res->lockname.name, migrate.namelen);
3059 migrate.new_master = new_master;
3060 migrate.master = master;
3062 ret = 0;
3064 /* send message to all nodes, except the master and myself */
3065 while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
3066 if (nodenum == master ||
3067 nodenum == new_master)
3068 continue;
3070 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
3071 &migrate, sizeof(migrate), nodenum,
3072 &status);
3073 if (ret < 0)
3074 mlog_errno(ret);
3075 else if (status < 0) {
3076 mlog(0, "migrate request (node %u) returned %d!\n",
3077 nodenum, status);
3078 ret = status;
3079 } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
3080 /* during the migration request we short-circuited
3081 * the mastery of the lockres. make sure we have
3082 * a mastery ref for nodenum */
3083 mlog(0, "%s:%.*s: need ref for node %u\n",
3084 dlm->name, res->lockname.len, res->lockname.name,
3085 nodenum);
3086 spin_lock(&res->spinlock);
3087 dlm_lockres_set_refmap_bit(nodenum, res);
3088 spin_unlock(&res->spinlock);
3092 if (ret < 0)
3093 mlog_errno(ret);
3095 mlog(0, "returning ret=%d\n", ret);
3096 return ret;
3100 /* if there is an existing mle for this lockres, we now know who the master is.
3101 * (the one who sent us *this* message) we can clear it up right away.
3102 * since the process that put the mle on the list still has a reference to it,
3103 * we can unhash it now, set the master and wake the process. as a result,
3104 * we will have no mle in the list to start with. now we can add an mle for
3105 * the migration and this should be the only one found for those scanning the
3106 * list. */
3107 int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
3108 void **ret_data)
3110 struct dlm_ctxt *dlm = data;
3111 struct dlm_lock_resource *res = NULL;
3112 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
3113 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
3114 const char *name;
3115 unsigned int namelen, hash;
3116 int ret = 0;
3118 if (!dlm_grab(dlm))
3119 return -EINVAL;
3121 name = migrate->name;
3122 namelen = migrate->namelen;
3123 hash = dlm_lockid_hash(name, namelen);
3125 /* preallocate.. if this fails, abort */
3126 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
3127 GFP_NOFS);
3129 if (!mle) {
3130 ret = -ENOMEM;
3131 goto leave;
3134 /* check for pre-existing lock */
3135 spin_lock(&dlm->spinlock);
3136 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
3137 spin_lock(&dlm->master_lock);
3139 if (res) {
3140 spin_lock(&res->spinlock);
3141 if (res->state & DLM_LOCK_RES_RECOVERING) {
3142 /* if all is working ok, this can only mean that we got
3143 * a migrate request from a node that we now see as
3144 * dead. what can we do here? drop it to the floor? */
3145 spin_unlock(&res->spinlock);
3146 mlog(ML_ERROR, "Got a migrate request, but the "
3147 "lockres is marked as recovering!");
3148 kmem_cache_free(dlm_mle_cache, mle);
3149 ret = -EINVAL; /* need a better solution */
3150 goto unlock;
3152 res->state |= DLM_LOCK_RES_MIGRATING;
3153 spin_unlock(&res->spinlock);
3156 /* ignore status. only nonzero status would BUG. */
3157 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
3158 name, namelen,
3159 migrate->new_master,
3160 migrate->master);
3162 unlock:
3163 spin_unlock(&dlm->master_lock);
3164 spin_unlock(&dlm->spinlock);
3166 if (oldmle) {
3167 /* master is known, detach if not already detached */
3168 dlm_mle_detach_hb_events(dlm, oldmle);
3169 dlm_put_mle(oldmle);
3172 if (res)
3173 dlm_lockres_put(res);
3174 leave:
3175 dlm_put(dlm);
3176 return ret;
3179 /* must be holding dlm->spinlock and dlm->master_lock
3180 * when adding a migration mle, we can clear any other mles
3181 * in the master list because we know with certainty that
3182 * the master is "master". so we remove any old mle from
3183 * the list after setting it's master field, and then add
3184 * the new migration mle. this way we can hold with the rule
3185 * of having only one mle for a given lock name at all times. */
3186 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
3187 struct dlm_lock_resource *res,
3188 struct dlm_master_list_entry *mle,
3189 struct dlm_master_list_entry **oldmle,
3190 const char *name, unsigned int namelen,
3191 u8 new_master, u8 master)
3193 int found;
3194 int ret = 0;
3196 *oldmle = NULL;
3198 mlog_entry_void();
3200 assert_spin_locked(&dlm->spinlock);
3201 assert_spin_locked(&dlm->master_lock);
3203 /* caller is responsible for any ref taken here on oldmle */
3204 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
3205 if (found) {
3206 struct dlm_master_list_entry *tmp = *oldmle;
3207 spin_lock(&tmp->spinlock);
3208 if (tmp->type == DLM_MLE_MIGRATION) {
3209 if (master == dlm->node_num) {
3210 /* ah another process raced me to it */
3211 mlog(0, "tried to migrate %.*s, but some "
3212 "process beat me to it\n",
3213 namelen, name);
3214 ret = -EEXIST;
3215 } else {
3216 /* bad. 2 NODES are trying to migrate! */
3217 mlog(ML_ERROR, "migration error mle: "
3218 "master=%u new_master=%u // request: "
3219 "master=%u new_master=%u // "
3220 "lockres=%.*s\n",
3221 tmp->master, tmp->new_master,
3222 master, new_master,
3223 namelen, name);
3224 BUG();
3226 } else {
3227 /* this is essentially what assert_master does */
3228 tmp->master = master;
3229 atomic_set(&tmp->woken, 1);
3230 wake_up(&tmp->wq);
3231 /* remove it from the list so that only one
3232 * mle will be found */
3233 list_del_init(&tmp->list);
3234 /* this was obviously WRONG. mle is uninited here. should be tmp. */
3235 __dlm_mle_detach_hb_events(dlm, tmp);
3236 ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
3237 mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3238 "telling master to get ref for cleared out mle "
3239 "during migration\n", dlm->name, namelen, name,
3240 master, new_master);
3242 spin_unlock(&tmp->spinlock);
3245 /* now add a migration mle to the tail of the list */
3246 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
3247 mle->new_master = new_master;
3248 /* the new master will be sending an assert master for this.
3249 * at that point we will get the refmap reference */
3250 mle->master = master;
3251 /* do this for consistency with other mle types */
3252 set_bit(new_master, mle->maybe_map);
3253 list_add(&mle->list, &dlm->master_list);
3255 return ret;
3259 void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
3261 struct dlm_master_list_entry *mle, *next;
3262 struct dlm_lock_resource *res;
3263 unsigned int hash;
3265 mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
3266 top:
3267 assert_spin_locked(&dlm->spinlock);
3269 /* clean the master list */
3270 spin_lock(&dlm->master_lock);
3271 list_for_each_entry_safe(mle, next, &dlm->master_list, list) {
3272 BUG_ON(mle->type != DLM_MLE_BLOCK &&
3273 mle->type != DLM_MLE_MASTER &&
3274 mle->type != DLM_MLE_MIGRATION);
3276 /* MASTER mles are initiated locally. the waiting
3277 * process will notice the node map change
3278 * shortly. let that happen as normal. */
3279 if (mle->type == DLM_MLE_MASTER)
3280 continue;
3283 /* BLOCK mles are initiated by other nodes.
3284 * need to clean up if the dead node would have
3285 * been the master. */
3286 if (mle->type == DLM_MLE_BLOCK) {
3287 int bit;
3289 spin_lock(&mle->spinlock);
3290 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
3291 if (bit != dead_node) {
3292 mlog(0, "mle found, but dead node %u would "
3293 "not have been master\n", dead_node);
3294 spin_unlock(&mle->spinlock);
3295 } else {
3296 /* must drop the refcount by one since the
3297 * assert_master will never arrive. this
3298 * may result in the mle being unlinked and
3299 * freed, but there may still be a process
3300 * waiting in the dlmlock path which is fine. */
3301 mlog(0, "node %u was expected master\n",
3302 dead_node);
3303 atomic_set(&mle->woken, 1);
3304 spin_unlock(&mle->spinlock);
3305 wake_up(&mle->wq);
3306 /* do not need events any longer, so detach
3307 * from heartbeat */
3308 __dlm_mle_detach_hb_events(dlm, mle);
3309 __dlm_put_mle(mle);
3311 continue;
3314 /* everything else is a MIGRATION mle */
3316 /* the rule for MIGRATION mles is that the master
3317 * becomes UNKNOWN if *either* the original or
3318 * the new master dies. all UNKNOWN lockreses
3319 * are sent to whichever node becomes the recovery
3320 * master. the new master is responsible for
3321 * determining if there is still a master for
3322 * this lockres, or if he needs to take over
3323 * mastery. either way, this node should expect
3324 * another message to resolve this. */
3325 if (mle->master != dead_node &&
3326 mle->new_master != dead_node)
3327 continue;
3329 /* if we have reached this point, this mle needs to
3330 * be removed from the list and freed. */
3332 /* remove from the list early. NOTE: unlinking
3333 * list_head while in list_for_each_safe */
3334 __dlm_mle_detach_hb_events(dlm, mle);
3335 spin_lock(&mle->spinlock);
3336 list_del_init(&mle->list);
3337 atomic_set(&mle->woken, 1);
3338 spin_unlock(&mle->spinlock);
3339 wake_up(&mle->wq);
3341 mlog(0, "%s: node %u died during migration from "
3342 "%u to %u!\n", dlm->name, dead_node,
3343 mle->master, mle->new_master);
3344 /* if there is a lockres associated with this
3345 * mle, find it and set its owner to UNKNOWN */
3346 hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
3347 res = __dlm_lookup_lockres(dlm, mle->u.name.name,
3348 mle->u.name.len, hash);
3349 if (res) {
3350 /* unfortunately if we hit this rare case, our
3351 * lock ordering is messed. we need to drop
3352 * the master lock so that we can take the
3353 * lockres lock, meaning that we will have to
3354 * restart from the head of list. */
3355 spin_unlock(&dlm->master_lock);
3357 /* move lockres onto recovery list */
3358 spin_lock(&res->spinlock);
3359 dlm_set_lockres_owner(dlm, res,
3360 DLM_LOCK_RES_OWNER_UNKNOWN);
3361 dlm_move_lockres_to_recovery_list(dlm, res);
3362 spin_unlock(&res->spinlock);
3363 dlm_lockres_put(res);
3365 /* about to get rid of mle, detach from heartbeat */
3366 __dlm_mle_detach_hb_events(dlm, mle);
3368 /* dump the mle */
3369 spin_lock(&dlm->master_lock);
3370 __dlm_put_mle(mle);
3371 spin_unlock(&dlm->master_lock);
3373 /* restart */
3374 goto top;
3377 /* this may be the last reference */
3378 __dlm_put_mle(mle);
3380 spin_unlock(&dlm->master_lock);
3384 int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
3385 u8 old_master)
3387 struct dlm_node_iter iter;
3388 int ret = 0;
3390 spin_lock(&dlm->spinlock);
3391 dlm_node_iter_init(dlm->domain_map, &iter);
3392 clear_bit(old_master, iter.node_map);
3393 clear_bit(dlm->node_num, iter.node_map);
3394 spin_unlock(&dlm->spinlock);
3396 /* ownership of the lockres is changing. account for the
3397 * mastery reference here since old_master will briefly have
3398 * a reference after the migration completes */
3399 spin_lock(&res->spinlock);
3400 dlm_lockres_set_refmap_bit(old_master, res);
3401 spin_unlock(&res->spinlock);
3403 mlog(0, "now time to do a migrate request to other nodes\n");
3404 ret = dlm_do_migrate_request(dlm, res, old_master,
3405 dlm->node_num, &iter);
3406 if (ret < 0) {
3407 mlog_errno(ret);
3408 goto leave;
3411 mlog(0, "doing assert master of %.*s to all except the original node\n",
3412 res->lockname.len, res->lockname.name);
3413 /* this call now finishes out the nodemap
3414 * even if one or more nodes die */
3415 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3416 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3417 if (ret < 0) {
3418 /* no longer need to retry. all living nodes contacted. */
3419 mlog_errno(ret);
3420 ret = 0;
3423 memset(iter.node_map, 0, sizeof(iter.node_map));
3424 set_bit(old_master, iter.node_map);
3425 mlog(0, "doing assert master of %.*s back to %u\n",
3426 res->lockname.len, res->lockname.name, old_master);
3427 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3428 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3429 if (ret < 0) {
3430 mlog(0, "assert master to original master failed "
3431 "with %d.\n", ret);
3432 /* the only nonzero status here would be because of
3433 * a dead original node. we're done. */
3434 ret = 0;
3437 /* all done, set the owner, clear the flag */
3438 spin_lock(&res->spinlock);
3439 dlm_set_lockres_owner(dlm, res, dlm->node_num);
3440 res->state &= ~DLM_LOCK_RES_MIGRATING;
3441 spin_unlock(&res->spinlock);
3442 /* re-dirty it on the new master */
3443 dlm_kick_thread(dlm, res);
3444 wake_up(&res->wq);
3445 leave:
3446 return ret;
3450 * LOCKRES AST REFCOUNT
3451 * this is integral to migration
3454 /* for future intent to call an ast, reserve one ahead of time.
3455 * this should be called only after waiting on the lockres
3456 * with dlm_wait_on_lockres, and while still holding the
3457 * spinlock after the call. */
3458 void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
3460 assert_spin_locked(&res->spinlock);
3461 if (res->state & DLM_LOCK_RES_MIGRATING) {
3462 __dlm_print_one_lock_resource(res);
3464 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3466 atomic_inc(&res->asts_reserved);
3470 * used to drop the reserved ast, either because it went unused,
3471 * or because the ast/bast was actually called.
3473 * also, if there is a pending migration on this lockres,
3474 * and this was the last pending ast on the lockres,
3475 * atomically set the MIGRATING flag before we drop the lock.
3476 * this is how we ensure that migration can proceed with no
3477 * asts in progress. note that it is ok if the state of the
3478 * queues is such that a lock should be granted in the future
3479 * or that a bast should be fired, because the new master will
3480 * shuffle the lists on this lockres as soon as it is migrated.
3482 void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
3483 struct dlm_lock_resource *res)
3485 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
3486 return;
3488 if (!res->migration_pending) {
3489 spin_unlock(&res->spinlock);
3490 return;
3493 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3494 res->migration_pending = 0;
3495 res->state |= DLM_LOCK_RES_MIGRATING;
3496 spin_unlock(&res->spinlock);
3497 wake_up(&res->wq);
3498 wake_up(&dlm->migration_wq);