• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/fs/ocfs2/dlm/
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmmod.c
5 *
6 * standalone DLM module
7 *
8 * Copyright (C) 2004 Oracle.  All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 *
25 */
26
27
28#include <linux/module.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/highmem.h>
33#include <linux/init.h>
34#include <linux/sysctl.h>
35#include <linux/random.h>
36#include <linux/blkdev.h>
37#include <linux/socket.h>
38#include <linux/inet.h>
39#include <linux/spinlock.h>
40#include <linux/delay.h>
41
42
43#include "cluster/heartbeat.h"
44#include "cluster/nodemanager.h"
45#include "cluster/tcp.h"
46
47#include "dlmapi.h"
48#include "dlmcommon.h"
49#include "dlmdomain.h"
50#include "dlmdebug.h"
51
52#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
53#include "cluster/masklog.h"
54
55static void dlm_mle_node_down(struct dlm_ctxt *dlm,
56			      struct dlm_master_list_entry *mle,
57			      struct o2nm_node *node,
58			      int idx);
59static void dlm_mle_node_up(struct dlm_ctxt *dlm,
60			    struct dlm_master_list_entry *mle,
61			    struct o2nm_node *node,
62			    int idx);
63
64static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
65static int dlm_do_assert_master(struct dlm_ctxt *dlm,
66				struct dlm_lock_resource *res,
67				void *nodemap, u32 flags);
68static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
69
70static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
71				struct dlm_master_list_entry *mle,
72				const char *name,
73				unsigned int namelen)
74{
75	if (dlm != mle->dlm)
76		return 0;
77
78	if (namelen != mle->mnamelen ||
79	    memcmp(name, mle->mname, namelen) != 0)
80		return 0;
81
82	return 1;
83}
84
85static struct kmem_cache *dlm_lockres_cache = NULL;
86static struct kmem_cache *dlm_lockname_cache = NULL;
87static struct kmem_cache *dlm_mle_cache = NULL;
88
89static void dlm_mle_release(struct kref *kref);
90static void dlm_init_mle(struct dlm_master_list_entry *mle,
91			enum dlm_mle_type type,
92			struct dlm_ctxt *dlm,
93			struct dlm_lock_resource *res,
94			const char *name,
95			unsigned int namelen);
96static void dlm_put_mle(struct dlm_master_list_entry *mle);
97static void __dlm_put_mle(struct dlm_master_list_entry *mle);
98static int dlm_find_mle(struct dlm_ctxt *dlm,
99			struct dlm_master_list_entry **mle,
100			char *name, unsigned int namelen);
101
102static int dlm_do_master_request(struct dlm_lock_resource *res,
103				 struct dlm_master_list_entry *mle, int to);
104
105
106static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
107				     struct dlm_lock_resource *res,
108				     struct dlm_master_list_entry *mle,
109				     int *blocked);
110static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
111				    struct dlm_lock_resource *res,
112				    struct dlm_master_list_entry *mle,
113				    int blocked);
114static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
115				 struct dlm_lock_resource *res,
116				 struct dlm_master_list_entry *mle,
117				 struct dlm_master_list_entry **oldmle,
118				 const char *name, unsigned int namelen,
119				 u8 new_master, u8 master);
120
121static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
122				    struct dlm_lock_resource *res);
123static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
124				      struct dlm_lock_resource *res);
125static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
126				       struct dlm_lock_resource *res,
127				       u8 target);
128static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
129				       struct dlm_lock_resource *res);
130
131
132int dlm_is_host_down(int errno)
133{
134	switch (errno) {
135		case -EBADF:
136		case -ECONNREFUSED:
137		case -ENOTCONN:
138		case -ECONNRESET:
139		case -EPIPE:
140		case -EHOSTDOWN:
141		case -EHOSTUNREACH:
142		case -ETIMEDOUT:
143		case -ECONNABORTED:
144		case -ENETDOWN:
145		case -ENETUNREACH:
146		case -ENETRESET:
147		case -ESHUTDOWN:
148		case -ENOPROTOOPT:
149		case -EINVAL:   /* if returned from our tcp code,
150				   this means there is no socket */
151			return 1;
152	}
153	return 0;
154}
155
156
157/*
158 * MASTER LIST FUNCTIONS
159 */
160
161
162/*
163 * regarding master list entries and heartbeat callbacks:
164 *
165 * in order to avoid sleeping and allocation that occurs in
166 * heartbeat, master list entries are simply attached to the
167 * dlm's established heartbeat callbacks.  the mle is attached
168 * when it is created, and since the dlm->spinlock is held at
169 * that time, any heartbeat event will be properly discovered
170 * by the mle.  the mle needs to be detached from the
171 * dlm->mle_hb_events list as soon as heartbeat events are no
172 * longer useful to the mle, and before the mle is freed.
173 *
174 * as a general rule, heartbeat events are no longer needed by
175 * the mle once an "answer" regarding the lock master has been
176 * received.
177 */
178static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
179					      struct dlm_master_list_entry *mle)
180{
181	assert_spin_locked(&dlm->spinlock);
182
183	list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
184}
185
186
187static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
188					      struct dlm_master_list_entry *mle)
189{
190	if (!list_empty(&mle->hb_events))
191		list_del_init(&mle->hb_events);
192}
193
194
195static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
196					    struct dlm_master_list_entry *mle)
197{
198	spin_lock(&dlm->spinlock);
199	__dlm_mle_detach_hb_events(dlm, mle);
200	spin_unlock(&dlm->spinlock);
201}
202
203static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
204{
205	struct dlm_ctxt *dlm;
206	dlm = mle->dlm;
207
208	assert_spin_locked(&dlm->spinlock);
209	assert_spin_locked(&dlm->master_lock);
210	mle->inuse++;
211	kref_get(&mle->mle_refs);
212}
213
214static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
215{
216	struct dlm_ctxt *dlm;
217	dlm = mle->dlm;
218
219	spin_lock(&dlm->spinlock);
220	spin_lock(&dlm->master_lock);
221	mle->inuse--;
222	__dlm_put_mle(mle);
223	spin_unlock(&dlm->master_lock);
224	spin_unlock(&dlm->spinlock);
225
226}
227
228/* remove from list and free */
229static void __dlm_put_mle(struct dlm_master_list_entry *mle)
230{
231	struct dlm_ctxt *dlm;
232	dlm = mle->dlm;
233
234	assert_spin_locked(&dlm->spinlock);
235	assert_spin_locked(&dlm->master_lock);
236	if (!atomic_read(&mle->mle_refs.refcount)) {
237		/* this may or may not crash, but who cares.
238		 * it's a BUG. */
239		mlog(ML_ERROR, "bad mle: %p\n", mle);
240		dlm_print_one_mle(mle);
241		BUG();
242	} else
243		kref_put(&mle->mle_refs, dlm_mle_release);
244}
245
246
247/* must not have any spinlocks coming in */
248static void dlm_put_mle(struct dlm_master_list_entry *mle)
249{
250	struct dlm_ctxt *dlm;
251	dlm = mle->dlm;
252
253	spin_lock(&dlm->spinlock);
254	spin_lock(&dlm->master_lock);
255	__dlm_put_mle(mle);
256	spin_unlock(&dlm->master_lock);
257	spin_unlock(&dlm->spinlock);
258}
259
260static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
261{
262	kref_get(&mle->mle_refs);
263}
264
265static void dlm_init_mle(struct dlm_master_list_entry *mle,
266			enum dlm_mle_type type,
267			struct dlm_ctxt *dlm,
268			struct dlm_lock_resource *res,
269			const char *name,
270			unsigned int namelen)
271{
272	assert_spin_locked(&dlm->spinlock);
273
274	mle->dlm = dlm;
275	mle->type = type;
276	INIT_HLIST_NODE(&mle->master_hash_node);
277	INIT_LIST_HEAD(&mle->hb_events);
278	memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
279	spin_lock_init(&mle->spinlock);
280	init_waitqueue_head(&mle->wq);
281	atomic_set(&mle->woken, 0);
282	kref_init(&mle->mle_refs);
283	memset(mle->response_map, 0, sizeof(mle->response_map));
284	mle->master = O2NM_MAX_NODES;
285	mle->new_master = O2NM_MAX_NODES;
286	mle->inuse = 0;
287
288	BUG_ON(mle->type != DLM_MLE_BLOCK &&
289	       mle->type != DLM_MLE_MASTER &&
290	       mle->type != DLM_MLE_MIGRATION);
291
292	if (mle->type == DLM_MLE_MASTER) {
293		BUG_ON(!res);
294		mle->mleres = res;
295		memcpy(mle->mname, res->lockname.name, res->lockname.len);
296		mle->mnamelen = res->lockname.len;
297		mle->mnamehash = res->lockname.hash;
298	} else {
299		BUG_ON(!name);
300		mle->mleres = NULL;
301		memcpy(mle->mname, name, namelen);
302		mle->mnamelen = namelen;
303		mle->mnamehash = dlm_lockid_hash(name, namelen);
304	}
305
306	atomic_inc(&dlm->mle_tot_count[mle->type]);
307	atomic_inc(&dlm->mle_cur_count[mle->type]);
308
309	/* copy off the node_map and register hb callbacks on our copy */
310	memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
311	memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
312	clear_bit(dlm->node_num, mle->vote_map);
313	clear_bit(dlm->node_num, mle->node_map);
314
315	/* attach the mle to the domain node up/down events */
316	__dlm_mle_attach_hb_events(dlm, mle);
317}
318
319void __dlm_unlink_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
320{
321	assert_spin_locked(&dlm->spinlock);
322	assert_spin_locked(&dlm->master_lock);
323
324	if (!hlist_unhashed(&mle->master_hash_node))
325		hlist_del_init(&mle->master_hash_node);
326}
327
328void __dlm_insert_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
329{
330	struct hlist_head *bucket;
331
332	assert_spin_locked(&dlm->master_lock);
333
334	bucket = dlm_master_hash(dlm, mle->mnamehash);
335	hlist_add_head(&mle->master_hash_node, bucket);
336}
337
338/* returns 1 if found, 0 if not */
339static int dlm_find_mle(struct dlm_ctxt *dlm,
340			struct dlm_master_list_entry **mle,
341			char *name, unsigned int namelen)
342{
343	struct dlm_master_list_entry *tmpmle;
344	struct hlist_head *bucket;
345	struct hlist_node *list;
346	unsigned int hash;
347
348	assert_spin_locked(&dlm->master_lock);
349
350	hash = dlm_lockid_hash(name, namelen);
351	bucket = dlm_master_hash(dlm, hash);
352	hlist_for_each(list, bucket) {
353		tmpmle = hlist_entry(list, struct dlm_master_list_entry,
354				     master_hash_node);
355		if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
356			continue;
357		dlm_get_mle(tmpmle);
358		*mle = tmpmle;
359		return 1;
360	}
361	return 0;
362}
363
364void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
365{
366	struct dlm_master_list_entry *mle;
367
368	assert_spin_locked(&dlm->spinlock);
369
370	list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) {
371		if (node_up)
372			dlm_mle_node_up(dlm, mle, NULL, idx);
373		else
374			dlm_mle_node_down(dlm, mle, NULL, idx);
375	}
376}
377
378static void dlm_mle_node_down(struct dlm_ctxt *dlm,
379			      struct dlm_master_list_entry *mle,
380			      struct o2nm_node *node, int idx)
381{
382	spin_lock(&mle->spinlock);
383
384	if (!test_bit(idx, mle->node_map))
385		mlog(0, "node %u already removed from nodemap!\n", idx);
386	else
387		clear_bit(idx, mle->node_map);
388
389	spin_unlock(&mle->spinlock);
390}
391
392static void dlm_mle_node_up(struct dlm_ctxt *dlm,
393			    struct dlm_master_list_entry *mle,
394			    struct o2nm_node *node, int idx)
395{
396	spin_lock(&mle->spinlock);
397
398	if (test_bit(idx, mle->node_map))
399		mlog(0, "node %u already in node map!\n", idx);
400	else
401		set_bit(idx, mle->node_map);
402
403	spin_unlock(&mle->spinlock);
404}
405
406
407int dlm_init_mle_cache(void)
408{
409	dlm_mle_cache = kmem_cache_create("o2dlm_mle",
410					  sizeof(struct dlm_master_list_entry),
411					  0, SLAB_HWCACHE_ALIGN,
412					  NULL);
413	if (dlm_mle_cache == NULL)
414		return -ENOMEM;
415	return 0;
416}
417
418void dlm_destroy_mle_cache(void)
419{
420	if (dlm_mle_cache)
421		kmem_cache_destroy(dlm_mle_cache);
422}
423
424static void dlm_mle_release(struct kref *kref)
425{
426	struct dlm_master_list_entry *mle;
427	struct dlm_ctxt *dlm;
428
429	mlog_entry_void();
430
431	mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
432	dlm = mle->dlm;
433
434	assert_spin_locked(&dlm->spinlock);
435	assert_spin_locked(&dlm->master_lock);
436
437	mlog(0, "Releasing mle for %.*s, type %d\n", mle->mnamelen, mle->mname,
438	     mle->type);
439
440	/* remove from list if not already */
441	__dlm_unlink_mle(dlm, mle);
442
443	/* detach the mle from the domain node up/down events */
444	__dlm_mle_detach_hb_events(dlm, mle);
445
446	atomic_dec(&dlm->mle_cur_count[mle->type]);
447
448	/* NOTE: kfree under spinlock here.
449	 * if this is bad, we can move this to a freelist. */
450	kmem_cache_free(dlm_mle_cache, mle);
451}
452
453
454/*
455 * LOCK RESOURCE FUNCTIONS
456 */
457
458int dlm_init_master_caches(void)
459{
460	dlm_lockres_cache = kmem_cache_create("o2dlm_lockres",
461					      sizeof(struct dlm_lock_resource),
462					      0, SLAB_HWCACHE_ALIGN, NULL);
463	if (!dlm_lockres_cache)
464		goto bail;
465
466	dlm_lockname_cache = kmem_cache_create("o2dlm_lockname",
467					       DLM_LOCKID_NAME_MAX, 0,
468					       SLAB_HWCACHE_ALIGN, NULL);
469	if (!dlm_lockname_cache)
470		goto bail;
471
472	return 0;
473bail:
474	dlm_destroy_master_caches();
475	return -ENOMEM;
476}
477
478void dlm_destroy_master_caches(void)
479{
480	if (dlm_lockname_cache)
481		kmem_cache_destroy(dlm_lockname_cache);
482
483	if (dlm_lockres_cache)
484		kmem_cache_destroy(dlm_lockres_cache);
485}
486
487static void dlm_lockres_release(struct kref *kref)
488{
489	struct dlm_lock_resource *res;
490	struct dlm_ctxt *dlm;
491
492	res = container_of(kref, struct dlm_lock_resource, refs);
493	dlm = res->dlm;
494
495	/* This should not happen -- all lockres' have a name
496	 * associated with them at init time. */
497	BUG_ON(!res->lockname.name);
498
499	mlog(0, "destroying lockres %.*s\n", res->lockname.len,
500	     res->lockname.name);
501
502	spin_lock(&dlm->track_lock);
503	if (!list_empty(&res->tracking))
504		list_del_init(&res->tracking);
505	else {
506		mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
507		     res->lockname.len, res->lockname.name);
508		dlm_print_one_lock_resource(res);
509	}
510	spin_unlock(&dlm->track_lock);
511
512	atomic_dec(&dlm->res_cur_count);
513
514	if (!hlist_unhashed(&res->hash_node) ||
515	    !list_empty(&res->granted) ||
516	    !list_empty(&res->converting) ||
517	    !list_empty(&res->blocked) ||
518	    !list_empty(&res->dirty) ||
519	    !list_empty(&res->recovering) ||
520	    !list_empty(&res->purge)) {
521		mlog(ML_ERROR,
522		     "Going to BUG for resource %.*s."
523		     "  We're on a list! [%c%c%c%c%c%c%c]\n",
524		     res->lockname.len, res->lockname.name,
525		     !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
526		     !list_empty(&res->granted) ? 'G' : ' ',
527		     !list_empty(&res->converting) ? 'C' : ' ',
528		     !list_empty(&res->blocked) ? 'B' : ' ',
529		     !list_empty(&res->dirty) ? 'D' : ' ',
530		     !list_empty(&res->recovering) ? 'R' : ' ',
531		     !list_empty(&res->purge) ? 'P' : ' ');
532
533		dlm_print_one_lock_resource(res);
534	}
535
536	/* By the time we're ready to blow this guy away, we shouldn't
537	 * be on any lists. */
538	BUG_ON(!hlist_unhashed(&res->hash_node));
539	BUG_ON(!list_empty(&res->granted));
540	BUG_ON(!list_empty(&res->converting));
541	BUG_ON(!list_empty(&res->blocked));
542	BUG_ON(!list_empty(&res->dirty));
543	BUG_ON(!list_empty(&res->recovering));
544	BUG_ON(!list_empty(&res->purge));
545
546	kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name);
547
548	kmem_cache_free(dlm_lockres_cache, res);
549}
550
551void dlm_lockres_put(struct dlm_lock_resource *res)
552{
553	kref_put(&res->refs, dlm_lockres_release);
554}
555
556static void dlm_init_lockres(struct dlm_ctxt *dlm,
557			     struct dlm_lock_resource *res,
558			     const char *name, unsigned int namelen)
559{
560	char *qname;
561
562	/* If we memset here, we lose our reference to the kmalloc'd
563	 * res->lockname.name, so be sure to init every field
564	 * correctly! */
565
566	qname = (char *) res->lockname.name;
567	memcpy(qname, name, namelen);
568
569	res->lockname.len = namelen;
570	res->lockname.hash = dlm_lockid_hash(name, namelen);
571
572	init_waitqueue_head(&res->wq);
573	spin_lock_init(&res->spinlock);
574	INIT_HLIST_NODE(&res->hash_node);
575	INIT_LIST_HEAD(&res->granted);
576	INIT_LIST_HEAD(&res->converting);
577	INIT_LIST_HEAD(&res->blocked);
578	INIT_LIST_HEAD(&res->dirty);
579	INIT_LIST_HEAD(&res->recovering);
580	INIT_LIST_HEAD(&res->purge);
581	INIT_LIST_HEAD(&res->tracking);
582	atomic_set(&res->asts_reserved, 0);
583	res->migration_pending = 0;
584	res->inflight_locks = 0;
585
586	res->dlm = dlm;
587
588	kref_init(&res->refs);
589
590	atomic_inc(&dlm->res_tot_count);
591	atomic_inc(&dlm->res_cur_count);
592
593	/* just for consistency */
594	spin_lock(&res->spinlock);
595	dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
596	spin_unlock(&res->spinlock);
597
598	res->state = DLM_LOCK_RES_IN_PROGRESS;
599
600	res->last_used = 0;
601
602	spin_lock(&dlm->spinlock);
603	list_add_tail(&res->tracking, &dlm->tracking_list);
604	spin_unlock(&dlm->spinlock);
605
606	memset(res->lvb, 0, DLM_LVB_LEN);
607	memset(res->refmap, 0, sizeof(res->refmap));
608}
609
610struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
611				   const char *name,
612				   unsigned int namelen)
613{
614	struct dlm_lock_resource *res = NULL;
615
616	res = kmem_cache_zalloc(dlm_lockres_cache, GFP_NOFS);
617	if (!res)
618		goto error;
619
620	res->lockname.name = kmem_cache_zalloc(dlm_lockname_cache, GFP_NOFS);
621	if (!res->lockname.name)
622		goto error;
623
624	dlm_init_lockres(dlm, res, name, namelen);
625	return res;
626
627error:
628	if (res && res->lockname.name)
629		kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name);
630
631	if (res)
632		kmem_cache_free(dlm_lockres_cache, res);
633	return NULL;
634}
635
636void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
637				   struct dlm_lock_resource *res,
638				   int new_lockres,
639				   const char *file,
640				   int line)
641{
642	if (!new_lockres)
643		assert_spin_locked(&res->spinlock);
644
645	if (!test_bit(dlm->node_num, res->refmap)) {
646		BUG_ON(res->inflight_locks != 0);
647		dlm_lockres_set_refmap_bit(dlm->node_num, res);
648	}
649	res->inflight_locks++;
650	mlog(0, "%s:%.*s: inflight++: now %u\n",
651	     dlm->name, res->lockname.len, res->lockname.name,
652	     res->inflight_locks);
653}
654
655void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
656				   struct dlm_lock_resource *res,
657				   const char *file,
658				   int line)
659{
660	assert_spin_locked(&res->spinlock);
661
662	BUG_ON(res->inflight_locks == 0);
663	res->inflight_locks--;
664	mlog(0, "%s:%.*s: inflight--: now %u\n",
665	     dlm->name, res->lockname.len, res->lockname.name,
666	     res->inflight_locks);
667	if (res->inflight_locks == 0)
668		dlm_lockres_clear_refmap_bit(dlm->node_num, res);
669	wake_up(&res->wq);
670}
671
672/*
673 * lookup a lock resource by name.
674 * may already exist in the hashtable.
675 * lockid is null terminated
676 *
677 * if not, allocate enough for the lockres and for
678 * the temporary structure used in doing the mastering.
679 *
680 * also, do a lookup in the dlm->master_list to see
681 * if another node has begun mastering the same lock.
682 * if so, there should be a block entry in there
683 * for this name, and we should *not* attempt to master
684 * the lock here.   need to wait around for that node
685 * to assert_master (or die).
686 *
687 */
688struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
689					  const char *lockid,
690					  int namelen,
691					  int flags)
692{
693	struct dlm_lock_resource *tmpres=NULL, *res=NULL;
694	struct dlm_master_list_entry *mle = NULL;
695	struct dlm_master_list_entry *alloc_mle = NULL;
696	int blocked = 0;
697	int ret, nodenum;
698	struct dlm_node_iter iter;
699	unsigned int hash;
700	int tries = 0;
701	int bit, wait_on_recovery = 0;
702	int drop_inflight_if_nonlocal = 0;
703
704	BUG_ON(!lockid);
705
706	hash = dlm_lockid_hash(lockid, namelen);
707
708	mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
709
710lookup:
711	spin_lock(&dlm->spinlock);
712	tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
713	if (tmpres) {
714		int dropping_ref = 0;
715
716		spin_unlock(&dlm->spinlock);
717
718		spin_lock(&tmpres->spinlock);
719		/* We wait for the other thread that is mastering the resource */
720		if (tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
721			__dlm_wait_on_lockres(tmpres);
722			BUG_ON(tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN);
723		}
724
725		if (tmpres->owner == dlm->node_num) {
726			BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
727			dlm_lockres_grab_inflight_ref(dlm, tmpres);
728		} else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
729			dropping_ref = 1;
730		spin_unlock(&tmpres->spinlock);
731
732		/* wait until done messaging the master, drop our ref to allow
733		 * the lockres to be purged, start over. */
734		if (dropping_ref) {
735			spin_lock(&tmpres->spinlock);
736			__dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
737			spin_unlock(&tmpres->spinlock);
738			dlm_lockres_put(tmpres);
739			tmpres = NULL;
740			goto lookup;
741		}
742
743		mlog(0, "found in hash!\n");
744		if (res)
745			dlm_lockres_put(res);
746		res = tmpres;
747		goto leave;
748	}
749
750	if (!res) {
751		spin_unlock(&dlm->spinlock);
752		mlog(0, "allocating a new resource\n");
753		/* nothing found and we need to allocate one. */
754		alloc_mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
755		if (!alloc_mle)
756			goto leave;
757		res = dlm_new_lockres(dlm, lockid, namelen);
758		if (!res)
759			goto leave;
760		goto lookup;
761	}
762
763	mlog(0, "no lockres found, allocated our own: %p\n", res);
764
765	if (flags & LKM_LOCAL) {
766		/* caller knows it's safe to assume it's not mastered elsewhere
767		 * DONE!  return right away */
768		spin_lock(&res->spinlock);
769		dlm_change_lockres_owner(dlm, res, dlm->node_num);
770		__dlm_insert_lockres(dlm, res);
771		dlm_lockres_grab_inflight_ref(dlm, res);
772		spin_unlock(&res->spinlock);
773		spin_unlock(&dlm->spinlock);
774		/* lockres still marked IN_PROGRESS */
775		goto wake_waiters;
776	}
777
778	/* check master list to see if another node has started mastering it */
779	spin_lock(&dlm->master_lock);
780
781	/* if we found a block, wait for lock to be mastered by another node */
782	blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
783	if (blocked) {
784		int mig;
785		if (mle->type == DLM_MLE_MASTER) {
786			mlog(ML_ERROR, "master entry for nonexistent lock!\n");
787			BUG();
788		}
789		mig = (mle->type == DLM_MLE_MIGRATION);
790		/* if there is a migration in progress, let the migration
791		 * finish before continuing.  we can wait for the absence
792		 * of the MIGRATION mle: either the migrate finished or
793		 * one of the nodes died and the mle was cleaned up.
794		 * if there is a BLOCK here, but it already has a master
795		 * set, we are too late.  the master does not have a ref
796		 * for us in the refmap.  detach the mle and drop it.
797		 * either way, go back to the top and start over. */
798		if (mig || mle->master != O2NM_MAX_NODES) {
799			BUG_ON(mig && mle->master == dlm->node_num);
800			/* we arrived too late.  the master does not
801			 * have a ref for us. retry. */
802			mlog(0, "%s:%.*s: late on %s\n",
803			     dlm->name, namelen, lockid,
804			     mig ?  "MIGRATION" : "BLOCK");
805			spin_unlock(&dlm->master_lock);
806			spin_unlock(&dlm->spinlock);
807
808			/* master is known, detach */
809			if (!mig)
810				dlm_mle_detach_hb_events(dlm, mle);
811			dlm_put_mle(mle);
812			mle = NULL;
813			/* this is lame, but we cant wait on either
814			 * the mle or lockres waitqueue here */
815			if (mig)
816				msleep(100);
817			goto lookup;
818		}
819	} else {
820		/* go ahead and try to master lock on this node */
821		mle = alloc_mle;
822		/* make sure this does not get freed below */
823		alloc_mle = NULL;
824		dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
825		set_bit(dlm->node_num, mle->maybe_map);
826		__dlm_insert_mle(dlm, mle);
827
828		/* still holding the dlm spinlock, check the recovery map
829		 * to see if there are any nodes that still need to be
830		 * considered.  these will not appear in the mle nodemap
831		 * but they might own this lockres.  wait on them. */
832		bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
833		if (bit < O2NM_MAX_NODES) {
834			mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
835			     "recover before lock mastery can begin\n",
836			     dlm->name, namelen, (char *)lockid, bit);
837			wait_on_recovery = 1;
838		}
839	}
840
841	/* at this point there is either a DLM_MLE_BLOCK or a
842	 * DLM_MLE_MASTER on the master list, so it's safe to add the
843	 * lockres to the hashtable.  anyone who finds the lock will
844	 * still have to wait on the IN_PROGRESS. */
845
846	/* finally add the lockres to its hash bucket */
847	__dlm_insert_lockres(dlm, res);
848	/* since this lockres is new it doesnt not require the spinlock */
849	dlm_lockres_grab_inflight_ref_new(dlm, res);
850
851	/* if this node does not become the master make sure to drop
852	 * this inflight reference below */
853	drop_inflight_if_nonlocal = 1;
854
855	/* get an extra ref on the mle in case this is a BLOCK
856	 * if so, the creator of the BLOCK may try to put the last
857	 * ref at this time in the assert master handler, so we
858	 * need an extra one to keep from a bad ptr deref. */
859	dlm_get_mle_inuse(mle);
860	spin_unlock(&dlm->master_lock);
861	spin_unlock(&dlm->spinlock);
862
863redo_request:
864	while (wait_on_recovery) {
865		/* any cluster changes that occurred after dropping the
866		 * dlm spinlock would be detectable be a change on the mle,
867		 * so we only need to clear out the recovery map once. */
868		if (dlm_is_recovery_lock(lockid, namelen)) {
869			mlog(ML_NOTICE, "%s: recovery map is not empty, but "
870			     "must master $RECOVERY lock now\n", dlm->name);
871			if (!dlm_pre_master_reco_lockres(dlm, res))
872				wait_on_recovery = 0;
873			else {
874				mlog(0, "%s: waiting 500ms for heartbeat state "
875				    "change\n", dlm->name);
876				msleep(500);
877			}
878			continue;
879		}
880
881		dlm_kick_recovery_thread(dlm);
882		msleep(1000);
883		dlm_wait_for_recovery(dlm);
884
885		spin_lock(&dlm->spinlock);
886		bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
887		if (bit < O2NM_MAX_NODES) {
888			mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
889			     "recover before lock mastery can begin\n",
890			     dlm->name, namelen, (char *)lockid, bit);
891			wait_on_recovery = 1;
892		} else
893			wait_on_recovery = 0;
894		spin_unlock(&dlm->spinlock);
895
896		if (wait_on_recovery)
897			dlm_wait_for_node_recovery(dlm, bit, 10000);
898	}
899
900	/* must wait for lock to be mastered elsewhere */
901	if (blocked)
902		goto wait;
903
904	ret = -EINVAL;
905	dlm_node_iter_init(mle->vote_map, &iter);
906	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
907		ret = dlm_do_master_request(res, mle, nodenum);
908		if (ret < 0)
909			mlog_errno(ret);
910		if (mle->master != O2NM_MAX_NODES) {
911			/* found a master ! */
912			if (mle->master <= nodenum)
913				break;
914			/* if our master request has not reached the master
915			 * yet, keep going until it does.  this is how the
916			 * master will know that asserts are needed back to
917			 * the lower nodes. */
918			mlog(0, "%s:%.*s: requests only up to %u but master "
919			     "is %u, keep going\n", dlm->name, namelen,
920			     lockid, nodenum, mle->master);
921		}
922	}
923
924wait:
925	/* keep going until the response map includes all nodes */
926	ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
927	if (ret < 0) {
928		wait_on_recovery = 1;
929		mlog(0, "%s:%.*s: node map changed, redo the "
930		     "master request now, blocked=%d\n",
931		     dlm->name, res->lockname.len,
932		     res->lockname.name, blocked);
933		if (++tries > 20) {
934			mlog(ML_ERROR, "%s:%.*s: spinning on "
935			     "dlm_wait_for_lock_mastery, blocked=%d\n",
936			     dlm->name, res->lockname.len,
937			     res->lockname.name, blocked);
938			dlm_print_one_lock_resource(res);
939			dlm_print_one_mle(mle);
940			tries = 0;
941		}
942		goto redo_request;
943	}
944
945	mlog(0, "lockres mastered by %u\n", res->owner);
946	/* make sure we never continue without this */
947	BUG_ON(res->owner == O2NM_MAX_NODES);
948
949	/* master is known, detach if not already detached */
950	dlm_mle_detach_hb_events(dlm, mle);
951	dlm_put_mle(mle);
952	/* put the extra ref */
953	dlm_put_mle_inuse(mle);
954
955wake_waiters:
956	spin_lock(&res->spinlock);
957	if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
958		dlm_lockres_drop_inflight_ref(dlm, res);
959	res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
960	spin_unlock(&res->spinlock);
961	wake_up(&res->wq);
962
963leave:
964	/* need to free the unused mle */
965	if (alloc_mle)
966		kmem_cache_free(dlm_mle_cache, alloc_mle);
967
968	return res;
969}
970
971
972#define DLM_MASTERY_TIMEOUT_MS   5000
973
974static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
975				     struct dlm_lock_resource *res,
976				     struct dlm_master_list_entry *mle,
977				     int *blocked)
978{
979	u8 m;
980	int ret, bit;
981	int map_changed, voting_done;
982	int assert, sleep;
983
984recheck:
985	ret = 0;
986	assert = 0;
987
988	/* check if another node has already become the owner */
989	spin_lock(&res->spinlock);
990	if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
991		mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
992		     res->lockname.len, res->lockname.name, res->owner);
993		spin_unlock(&res->spinlock);
994		/* this will cause the master to re-assert across
995		 * the whole cluster, freeing up mles */
996		if (res->owner != dlm->node_num) {
997			ret = dlm_do_master_request(res, mle, res->owner);
998			if (ret < 0) {
999				/* give recovery a chance to run */
1000				mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
1001				msleep(500);
1002				goto recheck;
1003			}
1004		}
1005		ret = 0;
1006		goto leave;
1007	}
1008	spin_unlock(&res->spinlock);
1009
1010	spin_lock(&mle->spinlock);
1011	m = mle->master;
1012	map_changed = (memcmp(mle->vote_map, mle->node_map,
1013			      sizeof(mle->vote_map)) != 0);
1014	voting_done = (memcmp(mle->vote_map, mle->response_map,
1015			     sizeof(mle->vote_map)) == 0);
1016
1017	/* restart if we hit any errors */
1018	if (map_changed) {
1019		int b;
1020		mlog(0, "%s: %.*s: node map changed, restarting\n",
1021		     dlm->name, res->lockname.len, res->lockname.name);
1022		ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
1023		b = (mle->type == DLM_MLE_BLOCK);
1024		if ((*blocked && !b) || (!*blocked && b)) {
1025			mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1026			     dlm->name, res->lockname.len, res->lockname.name,
1027			     *blocked, b);
1028			*blocked = b;
1029		}
1030		spin_unlock(&mle->spinlock);
1031		if (ret < 0) {
1032			mlog_errno(ret);
1033			goto leave;
1034		}
1035		mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1036		     "rechecking now\n", dlm->name, res->lockname.len,
1037		     res->lockname.name);
1038		goto recheck;
1039	} else {
1040		if (!voting_done) {
1041			mlog(0, "map not changed and voting not done "
1042			     "for %s:%.*s\n", dlm->name, res->lockname.len,
1043			     res->lockname.name);
1044		}
1045	}
1046
1047	if (m != O2NM_MAX_NODES) {
1048		/* another node has done an assert!
1049		 * all done! */
1050		sleep = 0;
1051	} else {
1052		sleep = 1;
1053		/* have all nodes responded? */
1054		if (voting_done && !*blocked) {
1055			bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
1056			if (dlm->node_num <= bit) {
1057				/* my node number is lowest.
1058			 	 * now tell other nodes that I am
1059				 * mastering this. */
1060				mle->master = dlm->node_num;
1061				/* ref was grabbed in get_lock_resource
1062				 * will be dropped in dlmlock_master */
1063				assert = 1;
1064				sleep = 0;
1065			}
1066			/* if voting is done, but we have not received
1067			 * an assert master yet, we must sleep */
1068		}
1069	}
1070
1071	spin_unlock(&mle->spinlock);
1072
1073	/* sleep if we haven't finished voting yet */
1074	if (sleep) {
1075		unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1076
1077		/*
1078		if (atomic_read(&mle->mle_refs.refcount) < 2)
1079			mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1080			atomic_read(&mle->mle_refs.refcount),
1081			res->lockname.len, res->lockname.name);
1082		*/
1083		atomic_set(&mle->woken, 0);
1084		(void)wait_event_timeout(mle->wq,
1085					 (atomic_read(&mle->woken) == 1),
1086					 timeo);
1087		if (res->owner == O2NM_MAX_NODES) {
1088			mlog(0, "%s:%.*s: waiting again\n", dlm->name,
1089			     res->lockname.len, res->lockname.name);
1090			goto recheck;
1091		}
1092		mlog(0, "done waiting, master is %u\n", res->owner);
1093		ret = 0;
1094		goto leave;
1095	}
1096
1097	ret = 0;   /* done */
1098	if (assert) {
1099		m = dlm->node_num;
1100		mlog(0, "about to master %.*s here, this=%u\n",
1101		     res->lockname.len, res->lockname.name, m);
1102		ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
1103		if (ret) {
1104			/* This is a failure in the network path,
1105			 * not in the response to the assert_master
1106			 * (any nonzero response is a BUG on this node).
1107			 * Most likely a socket just got disconnected
1108			 * due to node death. */
1109			mlog_errno(ret);
1110		}
1111		/* no longer need to restart lock mastery.
1112		 * all living nodes have been contacted. */
1113		ret = 0;
1114	}
1115
1116	/* set the lockres owner */
1117	spin_lock(&res->spinlock);
1118	/* mastery reference obtained either during
1119	 * assert_master_handler or in get_lock_resource */
1120	dlm_change_lockres_owner(dlm, res, m);
1121	spin_unlock(&res->spinlock);
1122
1123leave:
1124	return ret;
1125}
1126
1127struct dlm_bitmap_diff_iter
1128{
1129	int curnode;
1130	unsigned long *orig_bm;
1131	unsigned long *cur_bm;
1132	unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1133};
1134
1135enum dlm_node_state_change
1136{
1137	NODE_DOWN = -1,
1138	NODE_NO_CHANGE = 0,
1139	NODE_UP
1140};
1141
1142static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1143				      unsigned long *orig_bm,
1144				      unsigned long *cur_bm)
1145{
1146	unsigned long p1, p2;
1147	int i;
1148
1149	iter->curnode = -1;
1150	iter->orig_bm = orig_bm;
1151	iter->cur_bm = cur_bm;
1152
1153	for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1154       		p1 = *(iter->orig_bm + i);
1155	       	p2 = *(iter->cur_bm + i);
1156		iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1157	}
1158}
1159
1160static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1161				     enum dlm_node_state_change *state)
1162{
1163	int bit;
1164
1165	if (iter->curnode >= O2NM_MAX_NODES)
1166		return -ENOENT;
1167
1168	bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1169			    iter->curnode+1);
1170	if (bit >= O2NM_MAX_NODES) {
1171		iter->curnode = O2NM_MAX_NODES;
1172		return -ENOENT;
1173	}
1174
1175	/* if it was there in the original then this node died */
1176	if (test_bit(bit, iter->orig_bm))
1177		*state = NODE_DOWN;
1178	else
1179		*state = NODE_UP;
1180
1181	iter->curnode = bit;
1182	return bit;
1183}
1184
1185
1186static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1187				    struct dlm_lock_resource *res,
1188				    struct dlm_master_list_entry *mle,
1189				    int blocked)
1190{
1191	struct dlm_bitmap_diff_iter bdi;
1192	enum dlm_node_state_change sc;
1193	int node;
1194	int ret = 0;
1195
1196	mlog(0, "something happened such that the "
1197	     "master process may need to be restarted!\n");
1198
1199	assert_spin_locked(&mle->spinlock);
1200
1201	dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1202	node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1203	while (node >= 0) {
1204		if (sc == NODE_UP) {
1205			/* a node came up.  clear any old vote from
1206			 * the response map and set it in the vote map
1207			 * then restart the mastery. */
1208			mlog(ML_NOTICE, "node %d up while restarting\n", node);
1209
1210			/* redo the master request, but only for the new node */
1211			mlog(0, "sending request to new node\n");
1212			clear_bit(node, mle->response_map);
1213			set_bit(node, mle->vote_map);
1214		} else {
1215			mlog(ML_ERROR, "node down! %d\n", node);
1216			if (blocked) {
1217				int lowest = find_next_bit(mle->maybe_map,
1218						       O2NM_MAX_NODES, 0);
1219
1220				/* act like it was never there */
1221				clear_bit(node, mle->maybe_map);
1222
1223			       	if (node == lowest) {
1224					mlog(0, "expected master %u died"
1225					    " while this node was blocked "
1226					    "waiting on it!\n", node);
1227					lowest = find_next_bit(mle->maybe_map,
1228						       	O2NM_MAX_NODES,
1229						       	lowest+1);
1230					if (lowest < O2NM_MAX_NODES) {
1231						mlog(0, "%s:%.*s:still "
1232						     "blocked. waiting on %u "
1233						     "now\n", dlm->name,
1234						     res->lockname.len,
1235						     res->lockname.name,
1236						     lowest);
1237					} else {
1238						/* mle is an MLE_BLOCK, but
1239						 * there is now nothing left to
1240						 * block on.  we need to return
1241						 * all the way back out and try
1242						 * again with an MLE_MASTER.
1243						 * dlm_do_local_recovery_cleanup
1244						 * has already run, so the mle
1245						 * refcount is ok */
1246						mlog(0, "%s:%.*s: no "
1247						     "longer blocking. try to "
1248						     "master this here\n",
1249						     dlm->name,
1250						     res->lockname.len,
1251						     res->lockname.name);
1252						mle->type = DLM_MLE_MASTER;
1253						mle->mleres = res;
1254					}
1255				}
1256			}
1257
1258			/* now blank out everything, as if we had never
1259			 * contacted anyone */
1260			memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
1261			memset(mle->response_map, 0, sizeof(mle->response_map));
1262			/* reset the vote_map to the current node_map */
1263			memcpy(mle->vote_map, mle->node_map,
1264			       sizeof(mle->node_map));
1265			/* put myself into the maybe map */
1266			if (mle->type != DLM_MLE_BLOCK)
1267				set_bit(dlm->node_num, mle->maybe_map);
1268		}
1269		ret = -EAGAIN;
1270		node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1271	}
1272	return ret;
1273}
1274
1275
1276/*
1277 * DLM_MASTER_REQUEST_MSG
1278 *
1279 * returns: 0 on success,
1280 *          -errno on a network error
1281 *
1282 * on error, the caller should assume the target node is "dead"
1283 *
1284 */
1285
1286static int dlm_do_master_request(struct dlm_lock_resource *res,
1287				 struct dlm_master_list_entry *mle, int to)
1288{
1289	struct dlm_ctxt *dlm = mle->dlm;
1290	struct dlm_master_request request;
1291	int ret, response=0, resend;
1292
1293	memset(&request, 0, sizeof(request));
1294	request.node_idx = dlm->node_num;
1295
1296	BUG_ON(mle->type == DLM_MLE_MIGRATION);
1297
1298	request.namelen = (u8)mle->mnamelen;
1299	memcpy(request.name, mle->mname, request.namelen);
1300
1301again:
1302	ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1303				 sizeof(request), to, &response);
1304	if (ret < 0)  {
1305		if (ret == -ESRCH) {
1306			/* should never happen */
1307			mlog(ML_ERROR, "TCP stack not ready!\n");
1308			BUG();
1309		} else if (ret == -EINVAL) {
1310			mlog(ML_ERROR, "bad args passed to o2net!\n");
1311			BUG();
1312		} else if (ret == -ENOMEM) {
1313			mlog(ML_ERROR, "out of memory while trying to send "
1314			     "network message!  retrying\n");
1315			/* this is totally crude */
1316			msleep(50);
1317			goto again;
1318		} else if (!dlm_is_host_down(ret)) {
1319			/* not a network error. bad. */
1320			mlog_errno(ret);
1321			mlog(ML_ERROR, "unhandled error!");
1322			BUG();
1323		}
1324		/* all other errors should be network errors,
1325		 * and likely indicate node death */
1326		mlog(ML_ERROR, "link to %d went down!\n", to);
1327		goto out;
1328	}
1329
1330	ret = 0;
1331	resend = 0;
1332	spin_lock(&mle->spinlock);
1333	switch (response) {
1334		case DLM_MASTER_RESP_YES:
1335			set_bit(to, mle->response_map);
1336			mlog(0, "node %u is the master, response=YES\n", to);
1337			mlog(0, "%s:%.*s: master node %u now knows I have a "
1338			     "reference\n", dlm->name, res->lockname.len,
1339			     res->lockname.name, to);
1340			mle->master = to;
1341			break;
1342		case DLM_MASTER_RESP_NO:
1343			mlog(0, "node %u not master, response=NO\n", to);
1344			set_bit(to, mle->response_map);
1345			break;
1346		case DLM_MASTER_RESP_MAYBE:
1347			mlog(0, "node %u not master, response=MAYBE\n", to);
1348			set_bit(to, mle->response_map);
1349			set_bit(to, mle->maybe_map);
1350			break;
1351		case DLM_MASTER_RESP_ERROR:
1352			mlog(0, "node %u hit an error, resending\n", to);
1353			resend = 1;
1354			response = 0;
1355			break;
1356		default:
1357			mlog(ML_ERROR, "bad response! %u\n", response);
1358			BUG();
1359	}
1360	spin_unlock(&mle->spinlock);
1361	if (resend) {
1362		/* this is also totally crude */
1363		msleep(50);
1364		goto again;
1365	}
1366
1367out:
1368	return ret;
1369}
1370
1371/*
1372 * locks that can be taken here:
1373 * dlm->spinlock
1374 * res->spinlock
1375 * mle->spinlock
1376 * dlm->master_list
1377 *
1378 * if possible, TRIM THIS DOWN!!!
1379 */
1380int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
1381			       void **ret_data)
1382{
1383	u8 response = DLM_MASTER_RESP_MAYBE;
1384	struct dlm_ctxt *dlm = data;
1385	struct dlm_lock_resource *res = NULL;
1386	struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1387	struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1388	char *name;
1389	unsigned int namelen, hash;
1390	int found, ret;
1391	int set_maybe;
1392	int dispatch_assert = 0;
1393
1394	if (!dlm_grab(dlm))
1395		return DLM_MASTER_RESP_NO;
1396
1397	if (!dlm_domain_fully_joined(dlm)) {
1398		response = DLM_MASTER_RESP_NO;
1399		goto send_response;
1400	}
1401
1402	name = request->name;
1403	namelen = request->namelen;
1404	hash = dlm_lockid_hash(name, namelen);
1405
1406	if (namelen > DLM_LOCKID_NAME_MAX) {
1407		response = DLM_IVBUFLEN;
1408		goto send_response;
1409	}
1410
1411way_up_top:
1412	spin_lock(&dlm->spinlock);
1413	res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1414	if (res) {
1415		spin_unlock(&dlm->spinlock);
1416
1417		/* take care of the easy cases up front */
1418		spin_lock(&res->spinlock);
1419		if (res->state & (DLM_LOCK_RES_RECOVERING|
1420				  DLM_LOCK_RES_MIGRATING)) {
1421			spin_unlock(&res->spinlock);
1422			mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1423			     "being recovered/migrated\n");
1424			response = DLM_MASTER_RESP_ERROR;
1425			if (mle)
1426				kmem_cache_free(dlm_mle_cache, mle);
1427			goto send_response;
1428		}
1429
1430		if (res->owner == dlm->node_num) {
1431			mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1432			     dlm->name, namelen, name, request->node_idx);
1433			dlm_lockres_set_refmap_bit(request->node_idx, res);
1434			spin_unlock(&res->spinlock);
1435			response = DLM_MASTER_RESP_YES;
1436			if (mle)
1437				kmem_cache_free(dlm_mle_cache, mle);
1438
1439			/* this node is the owner.
1440			 * there is some extra work that needs to
1441			 * happen now.  the requesting node has
1442			 * caused all nodes up to this one to
1443			 * create mles.  this node now needs to
1444			 * go back and clean those up. */
1445			dispatch_assert = 1;
1446			goto send_response;
1447		} else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1448			spin_unlock(&res->spinlock);
1449			// mlog(0, "node %u is the master\n", res->owner);
1450			response = DLM_MASTER_RESP_NO;
1451			if (mle)
1452				kmem_cache_free(dlm_mle_cache, mle);
1453			goto send_response;
1454		}
1455
1456		/* ok, there is no owner.  either this node is
1457		 * being blocked, or it is actively trying to
1458		 * master this lock. */
1459		if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1460			mlog(ML_ERROR, "lock with no owner should be "
1461			     "in-progress!\n");
1462			BUG();
1463		}
1464
1465		// mlog(0, "lockres is in progress...\n");
1466		spin_lock(&dlm->master_lock);
1467		found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1468		if (!found) {
1469			mlog(ML_ERROR, "no mle found for this lock!\n");
1470			BUG();
1471		}
1472		set_maybe = 1;
1473		spin_lock(&tmpmle->spinlock);
1474		if (tmpmle->type == DLM_MLE_BLOCK) {
1475			// mlog(0, "this node is waiting for "
1476			// "lockres to be mastered\n");
1477			response = DLM_MASTER_RESP_NO;
1478		} else if (tmpmle->type == DLM_MLE_MIGRATION) {
1479			mlog(0, "node %u is master, but trying to migrate to "
1480			     "node %u.\n", tmpmle->master, tmpmle->new_master);
1481			if (tmpmle->master == dlm->node_num) {
1482				mlog(ML_ERROR, "no owner on lockres, but this "
1483				     "node is trying to migrate it to %u?!\n",
1484				     tmpmle->new_master);
1485				BUG();
1486			} else {
1487				/* the real master can respond on its own */
1488				response = DLM_MASTER_RESP_NO;
1489			}
1490		} else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1491			set_maybe = 0;
1492			if (tmpmle->master == dlm->node_num) {
1493				response = DLM_MASTER_RESP_YES;
1494				/* this node will be the owner.
1495				 * go back and clean the mles on any
1496				 * other nodes */
1497				dispatch_assert = 1;
1498				dlm_lockres_set_refmap_bit(request->node_idx, res);
1499				mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1500				     dlm->name, namelen, name,
1501				     request->node_idx);
1502			} else
1503				response = DLM_MASTER_RESP_NO;
1504		} else {
1505			// mlog(0, "this node is attempting to "
1506			// "master lockres\n");
1507			response = DLM_MASTER_RESP_MAYBE;
1508		}
1509		if (set_maybe)
1510			set_bit(request->node_idx, tmpmle->maybe_map);
1511		spin_unlock(&tmpmle->spinlock);
1512
1513		spin_unlock(&dlm->master_lock);
1514		spin_unlock(&res->spinlock);
1515
1516		/* keep the mle attached to heartbeat events */
1517		dlm_put_mle(tmpmle);
1518		if (mle)
1519			kmem_cache_free(dlm_mle_cache, mle);
1520		goto send_response;
1521	}
1522
1523	/*
1524	 * lockres doesn't exist on this node
1525	 * if there is an MLE_BLOCK, return NO
1526	 * if there is an MLE_MASTER, return MAYBE
1527	 * otherwise, add an MLE_BLOCK, return NO
1528	 */
1529	spin_lock(&dlm->master_lock);
1530	found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1531	if (!found) {
1532		/* this lockid has never been seen on this node yet */
1533		// mlog(0, "no mle found\n");
1534		if (!mle) {
1535			spin_unlock(&dlm->master_lock);
1536			spin_unlock(&dlm->spinlock);
1537
1538			mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
1539			if (!mle) {
1540				response = DLM_MASTER_RESP_ERROR;
1541				mlog_errno(-ENOMEM);
1542				goto send_response;
1543			}
1544			goto way_up_top;
1545		}
1546
1547		// mlog(0, "this is second time thru, already allocated, "
1548		// "add the block.\n");
1549		dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
1550		set_bit(request->node_idx, mle->maybe_map);
1551		__dlm_insert_mle(dlm, mle);
1552		response = DLM_MASTER_RESP_NO;
1553	} else {
1554		// mlog(0, "mle was found\n");
1555		set_maybe = 1;
1556		spin_lock(&tmpmle->spinlock);
1557		if (tmpmle->master == dlm->node_num) {
1558			mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1559			BUG();
1560		}
1561		if (tmpmle->type == DLM_MLE_BLOCK)
1562			response = DLM_MASTER_RESP_NO;
1563		else if (tmpmle->type == DLM_MLE_MIGRATION) {
1564			mlog(0, "migration mle was found (%u->%u)\n",
1565			     tmpmle->master, tmpmle->new_master);
1566			/* real master can respond on its own */
1567			response = DLM_MASTER_RESP_NO;
1568		} else
1569			response = DLM_MASTER_RESP_MAYBE;
1570		if (set_maybe)
1571			set_bit(request->node_idx, tmpmle->maybe_map);
1572		spin_unlock(&tmpmle->spinlock);
1573	}
1574	spin_unlock(&dlm->master_lock);
1575	spin_unlock(&dlm->spinlock);
1576
1577	if (found) {
1578		/* keep the mle attached to heartbeat events */
1579		dlm_put_mle(tmpmle);
1580	}
1581send_response:
1582	/*
1583	 * __dlm_lookup_lockres() grabbed a reference to this lockres.
1584	 * The reference is released by dlm_assert_master_worker() under
1585	 * the call to dlm_dispatch_assert_master().  If
1586	 * dlm_assert_master_worker() isn't called, we drop it here.
1587	 */
1588	if (dispatch_assert) {
1589		if (response != DLM_MASTER_RESP_YES)
1590			mlog(ML_ERROR, "invalid response %d\n", response);
1591		if (!res) {
1592			mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1593			BUG();
1594		}
1595		mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1596			     dlm->node_num, res->lockname.len, res->lockname.name);
1597		ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1598						 DLM_ASSERT_MASTER_MLE_CLEANUP);
1599		if (ret < 0) {
1600			mlog(ML_ERROR, "failed to dispatch assert master work\n");
1601			response = DLM_MASTER_RESP_ERROR;
1602			dlm_lockres_put(res);
1603		}
1604	} else {
1605		if (res)
1606			dlm_lockres_put(res);
1607	}
1608
1609	dlm_put(dlm);
1610	return response;
1611}
1612
1613/*
1614 * DLM_ASSERT_MASTER_MSG
1615 */
1616
1617
1618/*
1619 * NOTE: this can be used for debugging
1620 * can periodically run all locks owned by this node
1621 * and re-assert across the cluster...
1622 */
1623static int dlm_do_assert_master(struct dlm_ctxt *dlm,
1624				struct dlm_lock_resource *res,
1625				void *nodemap, u32 flags)
1626{
1627	struct dlm_assert_master assert;
1628	int to, tmpret;
1629	struct dlm_node_iter iter;
1630	int ret = 0;
1631	int reassert;
1632	const char *lockname = res->lockname.name;
1633	unsigned int namelen = res->lockname.len;
1634
1635	BUG_ON(namelen > O2NM_MAX_NAME_LEN);
1636
1637	spin_lock(&res->spinlock);
1638	res->state |= DLM_LOCK_RES_SETREF_INPROG;
1639	spin_unlock(&res->spinlock);
1640
1641again:
1642	reassert = 0;
1643
1644	/* note that if this nodemap is empty, it returns 0 */
1645	dlm_node_iter_init(nodemap, &iter);
1646	while ((to = dlm_node_iter_next(&iter)) >= 0) {
1647		int r = 0;
1648		struct dlm_master_list_entry *mle = NULL;
1649
1650		mlog(0, "sending assert master to %d (%.*s)\n", to,
1651		     namelen, lockname);
1652		memset(&assert, 0, sizeof(assert));
1653		assert.node_idx = dlm->node_num;
1654		assert.namelen = namelen;
1655		memcpy(assert.name, lockname, namelen);
1656		assert.flags = cpu_to_be32(flags);
1657
1658		tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1659					    &assert, sizeof(assert), to, &r);
1660		if (tmpret < 0) {
1661			mlog(ML_ERROR, "Error %d when sending message %u (key "
1662			     "0x%x) to node %u\n", tmpret,
1663			     DLM_ASSERT_MASTER_MSG, dlm->key, to);
1664			if (!dlm_is_host_down(tmpret)) {
1665				mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
1666				BUG();
1667			}
1668			/* a node died.  finish out the rest of the nodes. */
1669			mlog(0, "link to %d went down!\n", to);
1670			/* any nonzero status return will do */
1671			ret = tmpret;
1672			r = 0;
1673		} else if (r < 0) {
1674			/* ok, something horribly messed.  kill thyself. */
1675			mlog(ML_ERROR,"during assert master of %.*s to %u, "
1676			     "got %d.\n", namelen, lockname, to, r);
1677			spin_lock(&dlm->spinlock);
1678			spin_lock(&dlm->master_lock);
1679			if (dlm_find_mle(dlm, &mle, (char *)lockname,
1680					 namelen)) {
1681				dlm_print_one_mle(mle);
1682				__dlm_put_mle(mle);
1683			}
1684			spin_unlock(&dlm->master_lock);
1685			spin_unlock(&dlm->spinlock);
1686			BUG();
1687		}
1688
1689		if (r & DLM_ASSERT_RESPONSE_REASSERT &&
1690		    !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
1691				mlog(ML_ERROR, "%.*s: very strange, "
1692				     "master MLE but no lockres on %u\n",
1693				     namelen, lockname, to);
1694		}
1695
1696		if (r & DLM_ASSERT_RESPONSE_REASSERT) {
1697			mlog(0, "%.*s: node %u create mles on other "
1698			     "nodes and requests a re-assert\n",
1699			     namelen, lockname, to);
1700			reassert = 1;
1701		}
1702		if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
1703			mlog(0, "%.*s: node %u has a reference to this "
1704			     "lockres, set the bit in the refmap\n",
1705			     namelen, lockname, to);
1706			spin_lock(&res->spinlock);
1707			dlm_lockres_set_refmap_bit(to, res);
1708			spin_unlock(&res->spinlock);
1709		}
1710	}
1711
1712	if (reassert)
1713		goto again;
1714
1715	spin_lock(&res->spinlock);
1716	res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
1717	spin_unlock(&res->spinlock);
1718	wake_up(&res->wq);
1719
1720	return ret;
1721}
1722
1723/*
1724 * locks that can be taken here:
1725 * dlm->spinlock
1726 * res->spinlock
1727 * mle->spinlock
1728 * dlm->master_list
1729 *
1730 * if possible, TRIM THIS DOWN!!!
1731 */
1732int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
1733			      void **ret_data)
1734{
1735	struct dlm_ctxt *dlm = data;
1736	struct dlm_master_list_entry *mle = NULL;
1737	struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1738	struct dlm_lock_resource *res = NULL;
1739	char *name;
1740	unsigned int namelen, hash;
1741	u32 flags;
1742	int master_request = 0, have_lockres_ref = 0;
1743	int ret = 0;
1744
1745	if (!dlm_grab(dlm))
1746		return 0;
1747
1748	name = assert->name;
1749	namelen = assert->namelen;
1750	hash = dlm_lockid_hash(name, namelen);
1751	flags = be32_to_cpu(assert->flags);
1752
1753	if (namelen > DLM_LOCKID_NAME_MAX) {
1754		mlog(ML_ERROR, "Invalid name length!");
1755		goto done;
1756	}
1757
1758	spin_lock(&dlm->spinlock);
1759
1760	if (flags)
1761		mlog(0, "assert_master with flags: %u\n", flags);
1762
1763	/* find the MLE */
1764	spin_lock(&dlm->master_lock);
1765	if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1766		/* not an error, could be master just re-asserting */
1767		mlog(0, "just got an assert_master from %u, but no "
1768		     "MLE for it! (%.*s)\n", assert->node_idx,
1769		     namelen, name);
1770	} else {
1771		int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1772		if (bit >= O2NM_MAX_NODES) {
1773			/* not necessarily an error, though less likely.
1774			 * could be master just re-asserting. */
1775			mlog(0, "no bits set in the maybe_map, but %u "
1776			     "is asserting! (%.*s)\n", assert->node_idx,
1777			     namelen, name);
1778		} else if (bit != assert->node_idx) {
1779			if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1780				mlog(0, "master %u was found, %u should "
1781				     "back off\n", assert->node_idx, bit);
1782			} else {
1783				/* with the fix for bug 569, a higher node
1784				 * number winning the mastery will respond
1785				 * YES to mastery requests, but this node
1786				 * had no way of knowing.  let it pass. */
1787				mlog(0, "%u is the lowest node, "
1788				     "%u is asserting. (%.*s)  %u must "
1789				     "have begun after %u won.\n", bit,
1790				     assert->node_idx, namelen, name, bit,
1791				     assert->node_idx);
1792			}
1793		}
1794		if (mle->type == DLM_MLE_MIGRATION) {
1795			if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1796				mlog(0, "%s:%.*s: got cleanup assert"
1797				     " from %u for migration\n",
1798				     dlm->name, namelen, name,
1799				     assert->node_idx);
1800			} else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
1801				mlog(0, "%s:%.*s: got unrelated assert"
1802				     " from %u for migration, ignoring\n",
1803				     dlm->name, namelen, name,
1804				     assert->node_idx);
1805				__dlm_put_mle(mle);
1806				spin_unlock(&dlm->master_lock);
1807				spin_unlock(&dlm->spinlock);
1808				goto done;
1809			}
1810		}
1811	}
1812	spin_unlock(&dlm->master_lock);
1813
1814	/* ok everything checks out with the MLE
1815	 * now check to see if there is a lockres */
1816	res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1817	if (res) {
1818		spin_lock(&res->spinlock);
1819		if (res->state & DLM_LOCK_RES_RECOVERING)  {
1820			mlog(ML_ERROR, "%u asserting but %.*s is "
1821			     "RECOVERING!\n", assert->node_idx, namelen, name);
1822			goto kill;
1823		}
1824		if (!mle) {
1825			if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
1826			    res->owner != assert->node_idx) {
1827				mlog(ML_ERROR, "DIE! Mastery assert from %u, "
1828				     "but current owner is %u! (%.*s)\n",
1829				     assert->node_idx, res->owner, namelen,
1830				     name);
1831				__dlm_print_one_lock_resource(res);
1832				BUG();
1833			}
1834		} else if (mle->type != DLM_MLE_MIGRATION) {
1835			if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1836				/* owner is just re-asserting */
1837				if (res->owner == assert->node_idx) {
1838					mlog(0, "owner %u re-asserting on "
1839					     "lock %.*s\n", assert->node_idx,
1840					     namelen, name);
1841					goto ok;
1842				}
1843				mlog(ML_ERROR, "got assert_master from "
1844				     "node %u, but %u is the owner! "
1845				     "(%.*s)\n", assert->node_idx,
1846				     res->owner, namelen, name);
1847				goto kill;
1848			}
1849			if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1850				mlog(ML_ERROR, "got assert from %u, but lock "
1851				     "with no owner should be "
1852				     "in-progress! (%.*s)\n",
1853				     assert->node_idx,
1854				     namelen, name);
1855				goto kill;
1856			}
1857		} else /* mle->type == DLM_MLE_MIGRATION */ {
1858			/* should only be getting an assert from new master */
1859			if (assert->node_idx != mle->new_master) {
1860				mlog(ML_ERROR, "got assert from %u, but "
1861				     "new master is %u, and old master "
1862				     "was %u (%.*s)\n",
1863				     assert->node_idx, mle->new_master,
1864				     mle->master, namelen, name);
1865				goto kill;
1866			}
1867
1868		}
1869ok:
1870		spin_unlock(&res->spinlock);
1871	}
1872
1873	// mlog(0, "woo!  got an assert_master from node %u!\n",
1874	// 	     assert->node_idx);
1875	if (mle) {
1876		int extra_ref = 0;
1877		int nn = -1;
1878		int rr, err = 0;
1879
1880		spin_lock(&mle->spinlock);
1881		if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1882			extra_ref = 1;
1883		else {
1884			/* MASTER mle: if any bits set in the response map
1885			 * then the calling node needs to re-assert to clear
1886			 * up nodes that this node contacted */
1887			while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1888						    nn+1)) < O2NM_MAX_NODES) {
1889				if (nn != dlm->node_num && nn != assert->node_idx)
1890					master_request = 1;
1891			}
1892		}
1893		mle->master = assert->node_idx;
1894		atomic_set(&mle->woken, 1);
1895		wake_up(&mle->wq);
1896		spin_unlock(&mle->spinlock);
1897
1898		if (res) {
1899			int wake = 0;
1900			spin_lock(&res->spinlock);
1901			if (mle->type == DLM_MLE_MIGRATION) {
1902				mlog(0, "finishing off migration of lockres %.*s, "
1903			     		"from %u to %u\n",
1904			       		res->lockname.len, res->lockname.name,
1905			       		dlm->node_num, mle->new_master);
1906				res->state &= ~DLM_LOCK_RES_MIGRATING;
1907				wake = 1;
1908				dlm_change_lockres_owner(dlm, res, mle->new_master);
1909				BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
1910			} else {
1911				dlm_change_lockres_owner(dlm, res, mle->master);
1912			}
1913			spin_unlock(&res->spinlock);
1914			have_lockres_ref = 1;
1915			if (wake)
1916				wake_up(&res->wq);
1917		}
1918
1919		/* master is known, detach if not already detached.
1920		 * ensures that only one assert_master call will happen
1921		 * on this mle. */
1922		spin_lock(&dlm->master_lock);
1923
1924		rr = atomic_read(&mle->mle_refs.refcount);
1925		if (mle->inuse > 0) {
1926			if (extra_ref && rr < 3)
1927				err = 1;
1928			else if (!extra_ref && rr < 2)
1929				err = 1;
1930		} else {
1931			if (extra_ref && rr < 2)
1932				err = 1;
1933			else if (!extra_ref && rr < 1)
1934				err = 1;
1935		}
1936		if (err) {
1937			mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
1938			     "that will mess up this node, refs=%d, extra=%d, "
1939			     "inuse=%d\n", dlm->name, namelen, name,
1940			     assert->node_idx, rr, extra_ref, mle->inuse);
1941			dlm_print_one_mle(mle);
1942		}
1943		__dlm_unlink_mle(dlm, mle);
1944		__dlm_mle_detach_hb_events(dlm, mle);
1945		__dlm_put_mle(mle);
1946		if (extra_ref) {
1947			/* the assert master message now balances the extra
1948		 	 * ref given by the master / migration request message.
1949		 	 * if this is the last put, it will be removed
1950		 	 * from the list. */
1951			__dlm_put_mle(mle);
1952		}
1953		spin_unlock(&dlm->master_lock);
1954	} else if (res) {
1955		if (res->owner != assert->node_idx) {
1956			mlog(0, "assert_master from %u, but current "
1957			     "owner is %u (%.*s), no mle\n", assert->node_idx,
1958			     res->owner, namelen, name);
1959		}
1960	}
1961	spin_unlock(&dlm->spinlock);
1962
1963done:
1964	ret = 0;
1965	if (res) {
1966		spin_lock(&res->spinlock);
1967		res->state |= DLM_LOCK_RES_SETREF_INPROG;
1968		spin_unlock(&res->spinlock);
1969		*ret_data = (void *)res;
1970	}
1971	dlm_put(dlm);
1972	if (master_request) {
1973		mlog(0, "need to tell master to reassert\n");
1974		/* positive. negative would shoot down the node. */
1975		ret |= DLM_ASSERT_RESPONSE_REASSERT;
1976		if (!have_lockres_ref) {
1977			mlog(ML_ERROR, "strange, got assert from %u, MASTER "
1978			     "mle present here for %s:%.*s, but no lockres!\n",
1979			     assert->node_idx, dlm->name, namelen, name);
1980		}
1981	}
1982	if (have_lockres_ref) {
1983		/* let the master know we have a reference to the lockres */
1984		ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
1985		mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
1986		     dlm->name, namelen, name, assert->node_idx);
1987	}
1988	return ret;
1989
1990kill:
1991	/* kill the caller! */
1992	mlog(ML_ERROR, "Bad message received from another node.  Dumping state "
1993	     "and killing the other node now!  This node is OK and can continue.\n");
1994	__dlm_print_one_lock_resource(res);
1995	spin_unlock(&res->spinlock);
1996	spin_unlock(&dlm->spinlock);
1997	*ret_data = (void *)res;
1998	dlm_put(dlm);
1999	return -EINVAL;
2000}
2001
2002void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
2003{
2004	struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
2005
2006	if (ret_data) {
2007		spin_lock(&res->spinlock);
2008		res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
2009		spin_unlock(&res->spinlock);
2010		wake_up(&res->wq);
2011		dlm_lockres_put(res);
2012	}
2013	return;
2014}
2015
2016int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
2017			       struct dlm_lock_resource *res,
2018			       int ignore_higher, u8 request_from, u32 flags)
2019{
2020	struct dlm_work_item *item;
2021	item = kzalloc(sizeof(*item), GFP_NOFS);
2022	if (!item)
2023		return -ENOMEM;
2024
2025
2026	/* queue up work for dlm_assert_master_worker */
2027	dlm_grab(dlm);  /* get an extra ref for the work item */
2028	dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
2029	item->u.am.lockres = res; /* already have a ref */
2030	/* can optionally ignore node numbers higher than this node */
2031	item->u.am.ignore_higher = ignore_higher;
2032	item->u.am.request_from = request_from;
2033	item->u.am.flags = flags;
2034
2035	if (ignore_higher)
2036		mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
2037		     res->lockname.name);
2038
2039	spin_lock(&dlm->work_lock);
2040	list_add_tail(&item->list, &dlm->work_list);
2041	spin_unlock(&dlm->work_lock);
2042
2043	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2044	return 0;
2045}
2046
2047static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
2048{
2049	struct dlm_ctxt *dlm = data;
2050	int ret = 0;
2051	struct dlm_lock_resource *res;
2052	unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
2053	int ignore_higher;
2054	int bit;
2055	u8 request_from;
2056	u32 flags;
2057
2058	dlm = item->dlm;
2059	res = item->u.am.lockres;
2060	ignore_higher = item->u.am.ignore_higher;
2061	request_from = item->u.am.request_from;
2062	flags = item->u.am.flags;
2063
2064	spin_lock(&dlm->spinlock);
2065	memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
2066	spin_unlock(&dlm->spinlock);
2067
2068	clear_bit(dlm->node_num, nodemap);
2069	if (ignore_higher) {
2070		/* if is this just to clear up mles for nodes below
2071		 * this node, do not send the message to the original
2072		 * caller or any node number higher than this */
2073		clear_bit(request_from, nodemap);
2074		bit = dlm->node_num;
2075		while (1) {
2076			bit = find_next_bit(nodemap, O2NM_MAX_NODES,
2077					    bit+1);
2078		       	if (bit >= O2NM_MAX_NODES)
2079				break;
2080			clear_bit(bit, nodemap);
2081		}
2082	}
2083
2084	/*
2085	 * If we're migrating this lock to someone else, we are no
2086	 * longer allowed to assert out own mastery.  OTOH, we need to
2087	 * prevent migration from starting while we're still asserting
2088	 * our dominance.  The reserved ast delays migration.
2089	 */
2090	spin_lock(&res->spinlock);
2091	if (res->state & DLM_LOCK_RES_MIGRATING) {
2092		mlog(0, "Someone asked us to assert mastery, but we're "
2093		     "in the middle of migration.  Skipping assert, "
2094		     "the new master will handle that.\n");
2095		spin_unlock(&res->spinlock);
2096		goto put;
2097	} else
2098		__dlm_lockres_reserve_ast(res);
2099	spin_unlock(&res->spinlock);
2100
2101	/* this call now finishes out the nodemap
2102	 * even if one or more nodes die */
2103	mlog(0, "worker about to master %.*s here, this=%u\n",
2104		     res->lockname.len, res->lockname.name, dlm->node_num);
2105	ret = dlm_do_assert_master(dlm, res, nodemap, flags);
2106	if (ret < 0) {
2107		/* no need to restart, we are done */
2108		if (!dlm_is_host_down(ret))
2109			mlog_errno(ret);
2110	}
2111
2112	/* Ok, we've asserted ourselves.  Let's let migration start. */
2113	dlm_lockres_release_ast(dlm, res);
2114
2115put:
2116	dlm_lockres_put(res);
2117
2118	mlog(0, "finished with dlm_assert_master_worker\n");
2119}
2120
2121/* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2122 * We cannot wait for node recovery to complete to begin mastering this
2123 * lockres because this lockres is used to kick off recovery! ;-)
2124 * So, do a pre-check on all living nodes to see if any of those nodes
2125 * think that $RECOVERY is currently mastered by a dead node.  If so,
2126 * we wait a short time to allow that node to get notified by its own
2127 * heartbeat stack, then check again.  All $RECOVERY lock resources
2128 * mastered by dead nodes are purged when the hearbeat callback is
2129 * fired, so we can know for sure that it is safe to continue once
2130 * the node returns a live node or no node.  */
2131static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
2132				       struct dlm_lock_resource *res)
2133{
2134	struct dlm_node_iter iter;
2135	int nodenum;
2136	int ret = 0;
2137	u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
2138
2139	spin_lock(&dlm->spinlock);
2140	dlm_node_iter_init(dlm->domain_map, &iter);
2141	spin_unlock(&dlm->spinlock);
2142
2143	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2144		/* do not send to self */
2145		if (nodenum == dlm->node_num)
2146			continue;
2147		ret = dlm_do_master_requery(dlm, res, nodenum, &master);
2148		if (ret < 0) {
2149			mlog_errno(ret);
2150			if (!dlm_is_host_down(ret))
2151				BUG();
2152			/* host is down, so answer for that node would be
2153			 * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
2154			ret = 0;
2155		}
2156
2157		if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
2158			/* check to see if this master is in the recovery map */
2159			spin_lock(&dlm->spinlock);
2160			if (test_bit(master, dlm->recovery_map)) {
2161				mlog(ML_NOTICE, "%s: node %u has not seen "
2162				     "node %u go down yet, and thinks the "
2163				     "dead node is mastering the recovery "
2164				     "lock.  must wait.\n", dlm->name,
2165				     nodenum, master);
2166				ret = -EAGAIN;
2167			}
2168			spin_unlock(&dlm->spinlock);
2169			mlog(0, "%s: reco lock master is %u\n", dlm->name,
2170			     master);
2171			break;
2172		}
2173	}
2174	return ret;
2175}
2176
2177/*
2178 * DLM_DEREF_LOCKRES_MSG
2179 */
2180
2181int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2182{
2183	struct dlm_deref_lockres deref;
2184	int ret = 0, r;
2185	const char *lockname;
2186	unsigned int namelen;
2187
2188	lockname = res->lockname.name;
2189	namelen = res->lockname.len;
2190	BUG_ON(namelen > O2NM_MAX_NAME_LEN);
2191
2192	mlog(0, "%s:%.*s: sending deref to %d\n",
2193	     dlm->name, namelen, lockname, res->owner);
2194	memset(&deref, 0, sizeof(deref));
2195	deref.node_idx = dlm->node_num;
2196	deref.namelen = namelen;
2197	memcpy(deref.name, lockname, namelen);
2198
2199	ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
2200				 &deref, sizeof(deref), res->owner, &r);
2201	if (ret < 0)
2202		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
2203		     "node %u\n", ret, DLM_DEREF_LOCKRES_MSG, dlm->key,
2204		     res->owner);
2205	else if (r < 0) {
2206		/* BAD.  other node says I did not have a ref. */
2207		mlog(ML_ERROR,"while dropping ref on %s:%.*s "
2208		    "(master=%u) got %d.\n", dlm->name, namelen,
2209		    lockname, res->owner, r);
2210		dlm_print_one_lock_resource(res);
2211		BUG();
2212	}
2213	return ret;
2214}
2215
2216int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
2217			      void **ret_data)
2218{
2219	struct dlm_ctxt *dlm = data;
2220	struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
2221	struct dlm_lock_resource *res = NULL;
2222	char *name;
2223	unsigned int namelen;
2224	int ret = -EINVAL;
2225	u8 node;
2226	unsigned int hash;
2227	struct dlm_work_item *item;
2228	int cleared = 0;
2229	int dispatch = 0;
2230
2231	if (!dlm_grab(dlm))
2232		return 0;
2233
2234	name = deref->name;
2235	namelen = deref->namelen;
2236	node = deref->node_idx;
2237
2238	if (namelen > DLM_LOCKID_NAME_MAX) {
2239		mlog(ML_ERROR, "Invalid name length!");
2240		goto done;
2241	}
2242	if (deref->node_idx >= O2NM_MAX_NODES) {
2243		mlog(ML_ERROR, "Invalid node number: %u\n", node);
2244		goto done;
2245	}
2246
2247	hash = dlm_lockid_hash(name, namelen);
2248
2249	spin_lock(&dlm->spinlock);
2250	res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
2251	if (!res) {
2252		spin_unlock(&dlm->spinlock);
2253		mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
2254		     dlm->name, namelen, name);
2255		goto done;
2256	}
2257	spin_unlock(&dlm->spinlock);
2258
2259	spin_lock(&res->spinlock);
2260	if (res->state & DLM_LOCK_RES_SETREF_INPROG)
2261		dispatch = 1;
2262	else {
2263		BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2264		if (test_bit(node, res->refmap)) {
2265			dlm_lockres_clear_refmap_bit(node, res);
2266			cleared = 1;
2267		}
2268	}
2269	spin_unlock(&res->spinlock);
2270
2271	if (!dispatch) {
2272		if (cleared)
2273			dlm_lockres_calc_usage(dlm, res);
2274		else {
2275			mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2276		     	"but it is already dropped!\n", dlm->name,
2277		     	res->lockname.len, res->lockname.name, node);
2278			dlm_print_one_lock_resource(res);
2279		}
2280		ret = 0;
2281		goto done;
2282	}
2283
2284	item = kzalloc(sizeof(*item), GFP_NOFS);
2285	if (!item) {
2286		ret = -ENOMEM;
2287		mlog_errno(ret);
2288		goto done;
2289	}
2290
2291	dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
2292	item->u.dl.deref_res = res;
2293	item->u.dl.deref_node = node;
2294
2295	spin_lock(&dlm->work_lock);
2296	list_add_tail(&item->list, &dlm->work_list);
2297	spin_unlock(&dlm->work_lock);
2298
2299	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2300	return 0;
2301
2302done:
2303	if (res)
2304		dlm_lockres_put(res);
2305	dlm_put(dlm);
2306
2307	return ret;
2308}
2309
2310static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
2311{
2312	struct dlm_ctxt *dlm;
2313	struct dlm_lock_resource *res;
2314	u8 node;
2315	u8 cleared = 0;
2316
2317	dlm = item->dlm;
2318	res = item->u.dl.deref_res;
2319	node = item->u.dl.deref_node;
2320
2321	spin_lock(&res->spinlock);
2322	BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2323	if (test_bit(node, res->refmap)) {
2324		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2325		dlm_lockres_clear_refmap_bit(node, res);
2326		cleared = 1;
2327	}
2328	spin_unlock(&res->spinlock);
2329
2330	if (cleared) {
2331		mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
2332		     dlm->name, res->lockname.len, res->lockname.name, node);
2333		dlm_lockres_calc_usage(dlm, res);
2334	} else {
2335		mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2336		     "but it is already dropped!\n", dlm->name,
2337		     res->lockname.len, res->lockname.name, node);
2338		dlm_print_one_lock_resource(res);
2339	}
2340
2341	dlm_lockres_put(res);
2342}
2343
2344/* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
2345 * if not. If 0, numlocks is set to the number of locks in the lockres.
2346 */
2347static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
2348				      struct dlm_lock_resource *res,
2349				      int *numlocks)
2350{
2351	int ret;
2352	int i;
2353	int count = 0;
2354	struct list_head *queue;
2355	struct dlm_lock *lock;
2356
2357	assert_spin_locked(&res->spinlock);
2358
2359	ret = -EINVAL;
2360	if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2361		mlog(0, "cannot migrate lockres with unknown owner!\n");
2362		goto leave;
2363	}
2364
2365	if (res->owner != dlm->node_num) {
2366		mlog(0, "cannot migrate lockres this node doesn't own!\n");
2367		goto leave;
2368	}
2369
2370	ret = 0;
2371	queue = &res->granted;
2372	for (i = 0; i < 3; i++) {
2373		list_for_each_entry(lock, queue, list) {
2374			++count;
2375			if (lock->ml.node == dlm->node_num) {
2376				mlog(0, "found a lock owned by this node still "
2377				     "on the %s queue!  will not migrate this "
2378				     "lockres\n", (i == 0 ? "granted" :
2379						   (i == 1 ? "converting" :
2380						    "blocked")));
2381				ret = -ENOTEMPTY;
2382				goto leave;
2383			}
2384		}
2385		queue++;
2386	}
2387
2388	*numlocks = count;
2389	mlog(0, "migrateable lockres having %d locks\n", *numlocks);
2390
2391leave:
2392	return ret;
2393}
2394
2395/*
2396 * DLM_MIGRATE_LOCKRES
2397 */
2398
2399
2400static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
2401			       struct dlm_lock_resource *res,
2402			       u8 target)
2403{
2404	struct dlm_master_list_entry *mle = NULL;
2405	struct dlm_master_list_entry *oldmle = NULL;
2406 	struct dlm_migratable_lockres *mres = NULL;
2407	int ret = 0;
2408	const char *name;
2409	unsigned int namelen;
2410	int mle_added = 0;
2411	int numlocks;
2412	int wake = 0;
2413
2414	if (!dlm_grab(dlm))
2415		return -EINVAL;
2416
2417	name = res->lockname.name;
2418	namelen = res->lockname.len;
2419
2420	mlog(0, "migrating %.*s to %u\n", namelen, name, target);
2421
2422	/*
2423	 * ensure this lockres is a proper candidate for migration
2424	 */
2425	spin_lock(&res->spinlock);
2426	ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2427	if (ret < 0) {
2428		spin_unlock(&res->spinlock);
2429		goto leave;
2430	}
2431	spin_unlock(&res->spinlock);
2432
2433	/* no work to do */
2434	if (numlocks == 0) {
2435		mlog(0, "no locks were found on this lockres! done!\n");
2436		goto leave;
2437	}
2438
2439	/*
2440	 * preallocate up front
2441	 * if this fails, abort
2442	 */
2443
2444	ret = -ENOMEM;
2445	mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
2446	if (!mres) {
2447		mlog_errno(ret);
2448		goto leave;
2449	}
2450
2451	mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
2452	if (!mle) {
2453		mlog_errno(ret);
2454		goto leave;
2455	}
2456	ret = 0;
2457
2458	/*
2459	 * find a node to migrate the lockres to
2460	 */
2461
2462	mlog(0, "picking a migration node\n");
2463	spin_lock(&dlm->spinlock);
2464	/* pick a new node */
2465	if (!test_bit(target, dlm->domain_map) ||
2466	    target >= O2NM_MAX_NODES) {
2467		target = dlm_pick_migration_target(dlm, res);
2468	}
2469	mlog(0, "node %u chosen for migration\n", target);
2470
2471	if (target >= O2NM_MAX_NODES ||
2472	    !test_bit(target, dlm->domain_map)) {
2473		/* target chosen is not alive */
2474		ret = -EINVAL;
2475	}
2476
2477	if (ret) {
2478		spin_unlock(&dlm->spinlock);
2479		goto fail;
2480	}
2481
2482	mlog(0, "continuing with target = %u\n", target);
2483
2484	/*
2485	 * clear any existing master requests and
2486	 * add the migration mle to the list
2487	 */
2488	spin_lock(&dlm->master_lock);
2489	ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2490				    namelen, target, dlm->node_num);
2491	spin_unlock(&dlm->master_lock);
2492	spin_unlock(&dlm->spinlock);
2493
2494	if (ret == -EEXIST) {
2495		mlog(0, "another process is already migrating it\n");
2496		goto fail;
2497	}
2498	mle_added = 1;
2499
2500	/*
2501	 * set the MIGRATING flag and flush asts
2502	 * if we fail after this we need to re-dirty the lockres
2503	 */
2504	if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2505		mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2506		     "the target went down.\n", res->lockname.len,
2507		     res->lockname.name, target);
2508		spin_lock(&res->spinlock);
2509		res->state &= ~DLM_LOCK_RES_MIGRATING;
2510		wake = 1;
2511		spin_unlock(&res->spinlock);
2512		ret = -EINVAL;
2513	}
2514
2515fail:
2516	if (oldmle) {
2517		/* master is known, detach if not already detached */
2518		dlm_mle_detach_hb_events(dlm, oldmle);
2519		dlm_put_mle(oldmle);
2520	}
2521
2522	if (ret < 0) {
2523		if (mle_added) {
2524			dlm_mle_detach_hb_events(dlm, mle);
2525			dlm_put_mle(mle);
2526		} else if (mle) {
2527			kmem_cache_free(dlm_mle_cache, mle);
2528		}
2529		goto leave;
2530	}
2531
2532	/*
2533	 * at this point, we have a migration target, an mle
2534	 * in the master list, and the MIGRATING flag set on
2535	 * the lockres
2536	 */
2537
2538	/* now that remote nodes are spinning on the MIGRATING flag,
2539	 * ensure that all assert_master work is flushed. */
2540	flush_workqueue(dlm->dlm_worker);
2541
2542	/* get an extra reference on the mle.
2543	 * otherwise the assert_master from the new
2544	 * master will destroy this.
2545	 * also, make sure that all callers of dlm_get_mle
2546	 * take both dlm->spinlock and dlm->master_lock */
2547	spin_lock(&dlm->spinlock);
2548	spin_lock(&dlm->master_lock);
2549	dlm_get_mle_inuse(mle);
2550	spin_unlock(&dlm->master_lock);
2551	spin_unlock(&dlm->spinlock);
2552
2553	/* notify new node and send all lock state */
2554	/* call send_one_lockres with migration flag.
2555	 * this serves as notice to the target node that a
2556	 * migration is starting. */
2557	ret = dlm_send_one_lockres(dlm, res, mres, target,
2558				   DLM_MRES_MIGRATION);
2559
2560	if (ret < 0) {
2561		mlog(0, "migration to node %u failed with %d\n",
2562		     target, ret);
2563		/* migration failed, detach and clean up mle */
2564		dlm_mle_detach_hb_events(dlm, mle);
2565		dlm_put_mle(mle);
2566		dlm_put_mle_inuse(mle);
2567		spin_lock(&res->spinlock);
2568		res->state &= ~DLM_LOCK_RES_MIGRATING;
2569		wake = 1;
2570		spin_unlock(&res->spinlock);
2571		goto leave;
2572	}
2573
2574	/* at this point, the target sends a message to all nodes,
2575	 * (using dlm_do_migrate_request).  this node is skipped since
2576	 * we had to put an mle in the list to begin the process.  this
2577	 * node now waits for target to do an assert master.  this node
2578	 * will be the last one notified, ensuring that the migration
2579	 * is complete everywhere.  if the target dies while this is
2580	 * going on, some nodes could potentially see the target as the
2581	 * master, so it is important that my recovery finds the migration
2582	 * mle and sets the master to UNKNOWN. */
2583
2584
2585	/* wait for new node to assert master */
2586	while (1) {
2587		ret = wait_event_interruptible_timeout(mle->wq,
2588					(atomic_read(&mle->woken) == 1),
2589					msecs_to_jiffies(5000));
2590
2591		if (ret >= 0) {
2592		       	if (atomic_read(&mle->woken) == 1 ||
2593			    res->owner == target)
2594				break;
2595
2596			mlog(0, "%s:%.*s: timed out during migration\n",
2597			     dlm->name, res->lockname.len, res->lockname.name);
2598			/* avoid hang during shutdown when migrating lockres
2599			 * to a node which also goes down */
2600			if (dlm_is_node_dead(dlm, target)) {
2601				mlog(0, "%s:%.*s: expected migration "
2602				     "target %u is no longer up, restarting\n",
2603				     dlm->name, res->lockname.len,
2604				     res->lockname.name, target);
2605				ret = -EINVAL;
2606				/* migration failed, detach and clean up mle */
2607				dlm_mle_detach_hb_events(dlm, mle);
2608				dlm_put_mle(mle);
2609				dlm_put_mle_inuse(mle);
2610				spin_lock(&res->spinlock);
2611				res->state &= ~DLM_LOCK_RES_MIGRATING;
2612				wake = 1;
2613				spin_unlock(&res->spinlock);
2614				goto leave;
2615			}
2616		} else
2617			mlog(0, "%s:%.*s: caught signal during migration\n",
2618			     dlm->name, res->lockname.len, res->lockname.name);
2619	}
2620
2621	/* all done, set the owner, clear the flag */
2622	spin_lock(&res->spinlock);
2623	dlm_set_lockres_owner(dlm, res, target);
2624	res->state &= ~DLM_LOCK_RES_MIGRATING;
2625	dlm_remove_nonlocal_locks(dlm, res);
2626	spin_unlock(&res->spinlock);
2627	wake_up(&res->wq);
2628
2629	/* master is known, detach if not already detached */
2630	dlm_mle_detach_hb_events(dlm, mle);
2631	dlm_put_mle_inuse(mle);
2632	ret = 0;
2633
2634	dlm_lockres_calc_usage(dlm, res);
2635
2636leave:
2637	/* re-dirty the lockres if we failed */
2638	if (ret < 0)
2639		dlm_kick_thread(dlm, res);
2640
2641	/* wake up waiters if the MIGRATING flag got set
2642	 * but migration failed */
2643	if (wake)
2644		wake_up(&res->wq);
2645
2646	/* TODO: cleanup */
2647	if (mres)
2648		free_page((unsigned long)mres);
2649
2650	dlm_put(dlm);
2651
2652	mlog(0, "returning %d\n", ret);
2653	return ret;
2654}
2655
2656#define DLM_MIGRATION_RETRY_MS  100
2657
2658/* Should be called only after beginning the domain leave process.
2659 * There should not be any remaining locks on nonlocal lock resources,
2660 * and there should be no local locks left on locally mastered resources.
2661 *
2662 * Called with the dlm spinlock held, may drop it to do migration, but
2663 * will re-acquire before exit.
2664 *
2665 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2666int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2667{
2668	int ret;
2669	int lock_dropped = 0;
2670	int numlocks;
2671
2672	spin_lock(&res->spinlock);
2673	if (res->owner != dlm->node_num) {
2674		if (!__dlm_lockres_unused(res)) {
2675			mlog(ML_ERROR, "%s:%.*s: this node is not master, "
2676			     "trying to free this but locks remain\n",
2677			     dlm->name, res->lockname.len, res->lockname.name);
2678		}
2679		spin_unlock(&res->spinlock);
2680		goto leave;
2681	}
2682
2683	/* No need to migrate a lockres having no locks */
2684	ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2685	if (ret >= 0 && numlocks == 0) {
2686		spin_unlock(&res->spinlock);
2687		goto leave;
2688	}
2689	spin_unlock(&res->spinlock);
2690
2691	/* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2692	spin_unlock(&dlm->spinlock);
2693	lock_dropped = 1;
2694	while (1) {
2695		ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
2696		if (ret >= 0)
2697			break;
2698		if (ret == -ENOTEMPTY) {
2699			mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
2700		     		res->lockname.len, res->lockname.name);
2701			BUG();
2702		}
2703
2704		mlog(0, "lockres %.*s: migrate failed, "
2705		     "retrying\n", res->lockname.len,
2706		     res->lockname.name);
2707		msleep(DLM_MIGRATION_RETRY_MS);
2708	}
2709	spin_lock(&dlm->spinlock);
2710leave:
2711	return lock_dropped;
2712}
2713
2714int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2715{
2716	int ret;
2717	spin_lock(&dlm->ast_lock);
2718	spin_lock(&lock->spinlock);
2719	ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2720	spin_unlock(&lock->spinlock);
2721	spin_unlock(&dlm->ast_lock);
2722	return ret;
2723}
2724
2725static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2726				     struct dlm_lock_resource *res,
2727				     u8 mig_target)
2728{
2729	int can_proceed;
2730	spin_lock(&res->spinlock);
2731	can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2732	spin_unlock(&res->spinlock);
2733
2734	/* target has died, so make the caller break out of the
2735	 * wait_event, but caller must recheck the domain_map */
2736	spin_lock(&dlm->spinlock);
2737	if (!test_bit(mig_target, dlm->domain_map))
2738		can_proceed = 1;
2739	spin_unlock(&dlm->spinlock);
2740	return can_proceed;
2741}
2742
2743static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
2744				struct dlm_lock_resource *res)
2745{
2746	int ret;
2747	spin_lock(&res->spinlock);
2748	ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2749	spin_unlock(&res->spinlock);
2750	return ret;
2751}
2752
2753
2754static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2755				       struct dlm_lock_resource *res,
2756				       u8 target)
2757{
2758	int ret = 0;
2759
2760	mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2761	       res->lockname.len, res->lockname.name, dlm->node_num,
2762	       target);
2763	/* need to set MIGRATING flag on lockres.  this is done by
2764	 * ensuring that all asts have been flushed for this lockres. */
2765	spin_lock(&res->spinlock);
2766	BUG_ON(res->migration_pending);
2767	res->migration_pending = 1;
2768	/* strategy is to reserve an extra ast then release
2769	 * it below, letting the release do all of the work */
2770	__dlm_lockres_reserve_ast(res);
2771	spin_unlock(&res->spinlock);
2772
2773	/* now flush all the pending asts */
2774	dlm_kick_thread(dlm, res);
2775	/* before waiting on DIRTY, block processes which may
2776	 * try to dirty the lockres before MIGRATING is set */
2777	spin_lock(&res->spinlock);
2778	BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
2779	res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
2780	spin_unlock(&res->spinlock);
2781	/* now wait on any pending asts and the DIRTY state */
2782	wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2783	dlm_lockres_release_ast(dlm, res);
2784
2785	mlog(0, "about to wait on migration_wq, dirty=%s\n",
2786	       res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2787	/* if the extra ref we just put was the final one, this
2788	 * will pass thru immediately.  otherwise, we need to wait
2789	 * for the last ast to finish. */
2790again:
2791	ret = wait_event_interruptible_timeout(dlm->migration_wq,
2792		   dlm_migration_can_proceed(dlm, res, target),
2793		   msecs_to_jiffies(1000));
2794	if (ret < 0) {
2795		mlog(0, "woken again: migrating? %s, dead? %s\n",
2796		       res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2797		       test_bit(target, dlm->domain_map) ? "no":"yes");
2798	} else {
2799		mlog(0, "all is well: migrating? %s, dead? %s\n",
2800		       res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2801		       test_bit(target, dlm->domain_map) ? "no":"yes");
2802	}
2803	if (!dlm_migration_can_proceed(dlm, res, target)) {
2804		mlog(0, "trying again...\n");
2805		goto again;
2806	}
2807
2808	ret = 0;
2809	/* did the target go down or die? */
2810	spin_lock(&dlm->spinlock);
2811	if (!test_bit(target, dlm->domain_map)) {
2812		mlog(ML_ERROR, "aha. migration target %u just went down\n",
2813		     target);
2814		ret = -EHOSTDOWN;
2815	}
2816	spin_unlock(&dlm->spinlock);
2817
2818	/*
2819	 * if target is down, we need to clear DLM_LOCK_RES_BLOCK_DIRTY for
2820	 * another try; otherwise, we are sure the MIGRATING state is there,
2821	 * drop the unneded state which blocked threads trying to DIRTY
2822	 */
2823	spin_lock(&res->spinlock);
2824	BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
2825	res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
2826	if (!ret)
2827		BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
2828	spin_unlock(&res->spinlock);
2829
2830	/*
2831	 * at this point:
2832	 *
2833	 *   o the DLM_LOCK_RES_MIGRATING flag is set if target not down
2834	 *   o there are no pending asts on this lockres
2835	 *   o all processes trying to reserve an ast on this
2836	 *     lockres must wait for the MIGRATING flag to clear
2837	 */
2838	return ret;
2839}
2840
2841/* last step in the migration process.
2842 * original master calls this to free all of the dlm_lock
2843 * structures that used to be for other nodes. */
2844static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2845				      struct dlm_lock_resource *res)
2846{
2847	struct list_head *queue = &res->granted;
2848	int i, bit;
2849	struct dlm_lock *lock, *next;
2850
2851	assert_spin_locked(&res->spinlock);
2852
2853	BUG_ON(res->owner == dlm->node_num);
2854
2855	for (i=0; i<3; i++) {
2856		list_for_each_entry_safe(lock, next, queue, list) {
2857			if (lock->ml.node != dlm->node_num) {
2858				mlog(0, "putting lock for node %u\n",
2859				     lock->ml.node);
2860				/* be extra careful */
2861				BUG_ON(!list_empty(&lock->ast_list));
2862				BUG_ON(!list_empty(&lock->bast_list));
2863				BUG_ON(lock->ast_pending);
2864				BUG_ON(lock->bast_pending);
2865				dlm_lockres_clear_refmap_bit(lock->ml.node, res);
2866				list_del_init(&lock->list);
2867				dlm_lock_put(lock);
2868				/* In a normal unlock, we would have added a
2869				 * DLM_UNLOCK_FREE_LOCK action. Force it. */
2870				dlm_lock_put(lock);
2871			}
2872		}
2873		queue++;
2874	}
2875	bit = 0;
2876	while (1) {
2877		bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
2878		if (bit >= O2NM_MAX_NODES)
2879			break;
2880		/* do not clear the local node reference, if there is a
2881		 * process holding this, let it drop the ref itself */
2882		if (bit != dlm->node_num) {
2883			mlog(0, "%s:%.*s: node %u had a ref to this "
2884			     "migrating lockres, clearing\n", dlm->name,
2885			     res->lockname.len, res->lockname.name, bit);
2886			dlm_lockres_clear_refmap_bit(bit, res);
2887		}
2888		bit++;
2889	}
2890}
2891
2892/* for now this is not too intelligent.  we will
2893 * need stats to make this do the right thing.
2894 * this just finds the first lock on one of the
2895 * queues and uses that node as the target. */
2896static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
2897				    struct dlm_lock_resource *res)
2898{
2899	int i;
2900	struct list_head *queue = &res->granted;
2901	struct dlm_lock *lock;
2902	int nodenum;
2903
2904	assert_spin_locked(&dlm->spinlock);
2905
2906	spin_lock(&res->spinlock);
2907	for (i=0; i<3; i++) {
2908		list_for_each_entry(lock, queue, list) {
2909			/* up to the caller to make sure this node
2910			 * is alive */
2911			if (lock->ml.node != dlm->node_num) {
2912				spin_unlock(&res->spinlock);
2913				return lock->ml.node;
2914			}
2915		}
2916		queue++;
2917	}
2918	spin_unlock(&res->spinlock);
2919	mlog(0, "have not found a suitable target yet! checking domain map\n");
2920
2921	/* ok now we're getting desperate.  pick anyone alive. */
2922	nodenum = -1;
2923	while (1) {
2924		nodenum = find_next_bit(dlm->domain_map,
2925					O2NM_MAX_NODES, nodenum+1);
2926		mlog(0, "found %d in domain map\n", nodenum);
2927		if (nodenum >= O2NM_MAX_NODES)
2928			break;
2929		if (nodenum != dlm->node_num) {
2930			mlog(0, "picking %d\n", nodenum);
2931			return nodenum;
2932		}
2933	}
2934
2935	mlog(0, "giving up.  no master to migrate to\n");
2936	return DLM_LOCK_RES_OWNER_UNKNOWN;
2937}
2938
2939
2940
2941/* this is called by the new master once all lockres
2942 * data has been received */
2943static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
2944				  struct dlm_lock_resource *res,
2945				  u8 master, u8 new_master,
2946				  struct dlm_node_iter *iter)
2947{
2948	struct dlm_migrate_request migrate;
2949	int ret, skip, status = 0;
2950	int nodenum;
2951
2952	memset(&migrate, 0, sizeof(migrate));
2953	migrate.namelen = res->lockname.len;
2954	memcpy(migrate.name, res->lockname.name, migrate.namelen);
2955	migrate.new_master = new_master;
2956	migrate.master = master;
2957
2958	ret = 0;
2959
2960	/* send message to all nodes, except the master and myself */
2961	while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
2962		if (nodenum == master ||
2963		    nodenum == new_master)
2964			continue;
2965
2966		/* We could race exit domain. If exited, skip. */
2967		spin_lock(&dlm->spinlock);
2968		skip = (!test_bit(nodenum, dlm->domain_map));
2969		spin_unlock(&dlm->spinlock);
2970		if (skip) {
2971			clear_bit(nodenum, iter->node_map);
2972			continue;
2973		}
2974
2975		ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
2976					 &migrate, sizeof(migrate), nodenum,
2977					 &status);
2978		if (ret < 0) {
2979			mlog(ML_ERROR, "Error %d when sending message %u (key "
2980			     "0x%x) to node %u\n", ret, DLM_MIGRATE_REQUEST_MSG,
2981			     dlm->key, nodenum);
2982			if (!dlm_is_host_down(ret)) {
2983				mlog(ML_ERROR, "unhandled error=%d!\n", ret);
2984				BUG();
2985			}
2986			clear_bit(nodenum, iter->node_map);
2987			ret = 0;
2988		} else if (status < 0) {
2989			mlog(0, "migrate request (node %u) returned %d!\n",
2990			     nodenum, status);
2991			ret = status;
2992		} else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
2993			/* during the migration request we short-circuited
2994			 * the mastery of the lockres.  make sure we have
2995			 * a mastery ref for nodenum */
2996			mlog(0, "%s:%.*s: need ref for node %u\n",
2997			     dlm->name, res->lockname.len, res->lockname.name,
2998			     nodenum);
2999			spin_lock(&res->spinlock);
3000			dlm_lockres_set_refmap_bit(nodenum, res);
3001			spin_unlock(&res->spinlock);
3002		}
3003	}
3004
3005	if (ret < 0)
3006		mlog_errno(ret);
3007
3008	mlog(0, "returning ret=%d\n", ret);
3009	return ret;
3010}
3011
3012
3013/* if there is an existing mle for this lockres, we now know who the master is.
3014 * (the one who sent us *this* message) we can clear it up right away.
3015 * since the process that put the mle on the list still has a reference to it,
3016 * we can unhash it now, set the master and wake the process.  as a result,
3017 * we will have no mle in the list to start with.  now we can add an mle for
3018 * the migration and this should be the only one found for those scanning the
3019 * list.  */
3020int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
3021				void **ret_data)
3022{
3023	struct dlm_ctxt *dlm = data;
3024	struct dlm_lock_resource *res = NULL;
3025	struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
3026	struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
3027	const char *name;
3028	unsigned int namelen, hash;
3029	int ret = 0;
3030
3031	if (!dlm_grab(dlm))
3032		return -EINVAL;
3033
3034	name = migrate->name;
3035	namelen = migrate->namelen;
3036	hash = dlm_lockid_hash(name, namelen);
3037
3038	/* preallocate.. if this fails, abort */
3039	mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
3040
3041	if (!mle) {
3042		ret = -ENOMEM;
3043		goto leave;
3044	}
3045
3046	/* check for pre-existing lock */
3047	spin_lock(&dlm->spinlock);
3048	res = __dlm_lookup_lockres(dlm, name, namelen, hash);
3049	if (res) {
3050		spin_lock(&res->spinlock);
3051		if (res->state & DLM_LOCK_RES_RECOVERING) {
3052			/* if all is working ok, this can only mean that we got
3053		 	* a migrate request from a node that we now see as
3054		 	* dead.  what can we do here?  drop it to the floor? */
3055			spin_unlock(&res->spinlock);
3056			mlog(ML_ERROR, "Got a migrate request, but the "
3057			     "lockres is marked as recovering!");
3058			kmem_cache_free(dlm_mle_cache, mle);
3059			ret = -EINVAL; /* need a better solution */
3060			goto unlock;
3061		}
3062		res->state |= DLM_LOCK_RES_MIGRATING;
3063		spin_unlock(&res->spinlock);
3064	}
3065
3066	spin_lock(&dlm->master_lock);
3067	/* ignore status.  only nonzero status would BUG. */
3068	ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
3069				    name, namelen,
3070				    migrate->new_master,
3071				    migrate->master);
3072
3073	spin_unlock(&dlm->master_lock);
3074unlock:
3075	spin_unlock(&dlm->spinlock);
3076
3077	if (oldmle) {
3078		/* master is known, detach if not already detached */
3079		dlm_mle_detach_hb_events(dlm, oldmle);
3080		dlm_put_mle(oldmle);
3081	}
3082
3083	if (res)
3084		dlm_lockres_put(res);
3085leave:
3086	dlm_put(dlm);
3087	return ret;
3088}
3089
3090/* must be holding dlm->spinlock and dlm->master_lock
3091 * when adding a migration mle, we can clear any other mles
3092 * in the master list because we know with certainty that
3093 * the master is "master".  so we remove any old mle from
3094 * the list after setting it's master field, and then add
3095 * the new migration mle.  this way we can hold with the rule
3096 * of having only one mle for a given lock name at all times. */
3097static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
3098				 struct dlm_lock_resource *res,
3099				 struct dlm_master_list_entry *mle,
3100				 struct dlm_master_list_entry **oldmle,
3101				 const char *name, unsigned int namelen,
3102				 u8 new_master, u8 master)
3103{
3104	int found;
3105	int ret = 0;
3106
3107	*oldmle = NULL;
3108
3109	mlog_entry_void();
3110
3111	assert_spin_locked(&dlm->spinlock);
3112	assert_spin_locked(&dlm->master_lock);
3113
3114	/* caller is responsible for any ref taken here on oldmle */
3115	found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
3116	if (found) {
3117		struct dlm_master_list_entry *tmp = *oldmle;
3118		spin_lock(&tmp->spinlock);
3119		if (tmp->type == DLM_MLE_MIGRATION) {
3120			if (master == dlm->node_num) {
3121				/* ah another process raced me to it */
3122				mlog(0, "tried to migrate %.*s, but some "
3123				     "process beat me to it\n",
3124				     namelen, name);
3125				ret = -EEXIST;
3126			} else {
3127				/* bad.  2 NODES are trying to migrate! */
3128				mlog(ML_ERROR, "migration error  mle: "
3129				     "master=%u new_master=%u // request: "
3130				     "master=%u new_master=%u // "
3131				     "lockres=%.*s\n",
3132				     tmp->master, tmp->new_master,
3133				     master, new_master,
3134				     namelen, name);
3135				BUG();
3136			}
3137		} else {
3138			/* this is essentially what assert_master does */
3139			tmp->master = master;
3140			atomic_set(&tmp->woken, 1);
3141			wake_up(&tmp->wq);
3142			/* remove it so that only one mle will be found */
3143			__dlm_unlink_mle(dlm, tmp);
3144			__dlm_mle_detach_hb_events(dlm, tmp);
3145			ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
3146			mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3147			    "telling master to get ref for cleared out mle "
3148			    "during migration\n", dlm->name, namelen, name,
3149			    master, new_master);
3150		}
3151		spin_unlock(&tmp->spinlock);
3152	}
3153
3154	/* now add a migration mle to the tail of the list */
3155	dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
3156	mle->new_master = new_master;
3157	/* the new master will be sending an assert master for this.
3158	 * at that point we will get the refmap reference */
3159	mle->master = master;
3160	/* do this for consistency with other mle types */
3161	set_bit(new_master, mle->maybe_map);
3162	__dlm_insert_mle(dlm, mle);
3163
3164	return ret;
3165}
3166
3167/*
3168 * Sets the owner of the lockres, associated to the mle, to UNKNOWN
3169 */
3170static struct dlm_lock_resource *dlm_reset_mleres_owner(struct dlm_ctxt *dlm,
3171					struct dlm_master_list_entry *mle)
3172{
3173	struct dlm_lock_resource *res;
3174
3175	/* Find the lockres associated to the mle and set its owner to UNK */
3176	res = __dlm_lookup_lockres(dlm, mle->mname, mle->mnamelen,
3177				   mle->mnamehash);
3178	if (res) {
3179		spin_unlock(&dlm->master_lock);
3180
3181		/* move lockres onto recovery list */
3182		spin_lock(&res->spinlock);
3183		dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
3184		dlm_move_lockres_to_recovery_list(dlm, res);
3185		spin_unlock(&res->spinlock);
3186		dlm_lockres_put(res);
3187
3188		/* about to get rid of mle, detach from heartbeat */
3189		__dlm_mle_detach_hb_events(dlm, mle);
3190
3191		/* dump the mle */
3192		spin_lock(&dlm->master_lock);
3193		__dlm_put_mle(mle);
3194		spin_unlock(&dlm->master_lock);
3195	}
3196
3197	return res;
3198}
3199
3200static void dlm_clean_migration_mle(struct dlm_ctxt *dlm,
3201				    struct dlm_master_list_entry *mle)
3202{
3203	__dlm_mle_detach_hb_events(dlm, mle);
3204
3205	spin_lock(&mle->spinlock);
3206	__dlm_unlink_mle(dlm, mle);
3207	atomic_set(&mle->woken, 1);
3208	spin_unlock(&mle->spinlock);
3209
3210	wake_up(&mle->wq);
3211}
3212
3213static void dlm_clean_block_mle(struct dlm_ctxt *dlm,
3214				struct dlm_master_list_entry *mle, u8 dead_node)
3215{
3216	int bit;
3217
3218	BUG_ON(mle->type != DLM_MLE_BLOCK);
3219
3220	spin_lock(&mle->spinlock);
3221	bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
3222	if (bit != dead_node) {
3223		mlog(0, "mle found, but dead node %u would not have been "
3224		     "master\n", dead_node);
3225		spin_unlock(&mle->spinlock);
3226	} else {
3227		/* Must drop the refcount by one since the assert_master will
3228		 * never arrive. This may result in the mle being unlinked and
3229		 * freed, but there may still be a process waiting in the
3230		 * dlmlock path which is fine. */
3231		mlog(0, "node %u was expected master\n", dead_node);
3232		atomic_set(&mle->woken, 1);
3233		spin_unlock(&mle->spinlock);
3234		wake_up(&mle->wq);
3235
3236		/* Do not need events any longer, so detach from heartbeat */
3237		__dlm_mle_detach_hb_events(dlm, mle);
3238		__dlm_put_mle(mle);
3239	}
3240}
3241
3242void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
3243{
3244	struct dlm_master_list_entry *mle;
3245	struct dlm_lock_resource *res;
3246	struct hlist_head *bucket;
3247	struct hlist_node *list;
3248	unsigned int i;
3249
3250	mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
3251top:
3252	assert_spin_locked(&dlm->spinlock);
3253
3254	/* clean the master list */
3255	spin_lock(&dlm->master_lock);
3256	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
3257		bucket = dlm_master_hash(dlm, i);
3258		hlist_for_each(list, bucket) {
3259			mle = hlist_entry(list, struct dlm_master_list_entry,
3260					  master_hash_node);
3261
3262			BUG_ON(mle->type != DLM_MLE_BLOCK &&
3263			       mle->type != DLM_MLE_MASTER &&
3264			       mle->type != DLM_MLE_MIGRATION);
3265
3266			/* MASTER mles are initiated locally. The waiting
3267			 * process will notice the node map change shortly.
3268			 * Let that happen as normal. */
3269			if (mle->type == DLM_MLE_MASTER)
3270				continue;
3271
3272			/* BLOCK mles are initiated by other nodes. Need to
3273			 * clean up if the dead node would have been the
3274			 * master. */
3275			if (mle->type == DLM_MLE_BLOCK) {
3276				dlm_clean_block_mle(dlm, mle, dead_node);
3277				continue;
3278			}
3279
3280			/* Everything else is a MIGRATION mle */
3281
3282			/* The rule for MIGRATION mles is that the master
3283			 * becomes UNKNOWN if *either* the original or the new
3284			 * master dies. All UNKNOWN lockres' are sent to
3285			 * whichever node becomes the recovery master. The new
3286			 * master is responsible for determining if there is
3287			 * still a master for this lockres, or if he needs to
3288			 * take over mastery. Either way, this node should
3289			 * expect another message to resolve this. */
3290
3291			if (mle->master != dead_node &&
3292			    mle->new_master != dead_node)
3293				continue;
3294
3295			/* If we have reached this point, this mle needs to be
3296			 * removed from the list and freed. */
3297			dlm_clean_migration_mle(dlm, mle);
3298
3299			mlog(0, "%s: node %u died during migration from "
3300			     "%u to %u!\n", dlm->name, dead_node, mle->master,
3301			     mle->new_master);
3302
3303			/* If we find a lockres associated with the mle, we've
3304			 * hit this rare case that messes up our lock ordering.
3305			 * If so, we need to drop the master lock so that we can
3306			 * take the lockres lock, meaning that we will have to
3307			 * restart from the head of list. */
3308			res = dlm_reset_mleres_owner(dlm, mle);
3309			if (res)
3310				/* restart */
3311				goto top;
3312
3313			/* This may be the last reference */
3314			__dlm_put_mle(mle);
3315		}
3316	}
3317	spin_unlock(&dlm->master_lock);
3318}
3319
3320int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
3321			 u8 old_master)
3322{
3323	struct dlm_node_iter iter;
3324	int ret = 0;
3325
3326	spin_lock(&dlm->spinlock);
3327	dlm_node_iter_init(dlm->domain_map, &iter);
3328	clear_bit(old_master, iter.node_map);
3329	clear_bit(dlm->node_num, iter.node_map);
3330	spin_unlock(&dlm->spinlock);
3331
3332	/* ownership of the lockres is changing.  account for the
3333	 * mastery reference here since old_master will briefly have
3334	 * a reference after the migration completes */
3335	spin_lock(&res->spinlock);
3336	dlm_lockres_set_refmap_bit(old_master, res);
3337	spin_unlock(&res->spinlock);
3338
3339	mlog(0, "now time to do a migrate request to other nodes\n");
3340	ret = dlm_do_migrate_request(dlm, res, old_master,
3341				     dlm->node_num, &iter);
3342	if (ret < 0) {
3343		mlog_errno(ret);
3344		goto leave;
3345	}
3346
3347	mlog(0, "doing assert master of %.*s to all except the original node\n",
3348	     res->lockname.len, res->lockname.name);
3349	/* this call now finishes out the nodemap
3350	 * even if one or more nodes die */
3351	ret = dlm_do_assert_master(dlm, res, iter.node_map,
3352				   DLM_ASSERT_MASTER_FINISH_MIGRATION);
3353	if (ret < 0) {
3354		/* no longer need to retry.  all living nodes contacted. */
3355		mlog_errno(ret);
3356		ret = 0;
3357	}
3358
3359	memset(iter.node_map, 0, sizeof(iter.node_map));
3360	set_bit(old_master, iter.node_map);
3361	mlog(0, "doing assert master of %.*s back to %u\n",
3362	     res->lockname.len, res->lockname.name, old_master);
3363	ret = dlm_do_assert_master(dlm, res, iter.node_map,
3364				   DLM_ASSERT_MASTER_FINISH_MIGRATION);
3365	if (ret < 0) {
3366		mlog(0, "assert master to original master failed "
3367		     "with %d.\n", ret);
3368		/* the only nonzero status here would be because of
3369		 * a dead original node.  we're done. */
3370		ret = 0;
3371	}
3372
3373	/* all done, set the owner, clear the flag */
3374	spin_lock(&res->spinlock);
3375	dlm_set_lockres_owner(dlm, res, dlm->node_num);
3376	res->state &= ~DLM_LOCK_RES_MIGRATING;
3377	spin_unlock(&res->spinlock);
3378	/* re-dirty it on the new master */
3379	dlm_kick_thread(dlm, res);
3380	wake_up(&res->wq);
3381leave:
3382	return ret;
3383}
3384
3385/*
3386 * LOCKRES AST REFCOUNT
3387 * this is integral to migration
3388 */
3389
3390/* for future intent to call an ast, reserve one ahead of time.
3391 * this should be called only after waiting on the lockres
3392 * with dlm_wait_on_lockres, and while still holding the
3393 * spinlock after the call. */
3394void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
3395{
3396	assert_spin_locked(&res->spinlock);
3397	if (res->state & DLM_LOCK_RES_MIGRATING) {
3398		__dlm_print_one_lock_resource(res);
3399	}
3400	BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3401
3402	atomic_inc(&res->asts_reserved);
3403}
3404
3405/*
3406 * used to drop the reserved ast, either because it went unused,
3407 * or because the ast/bast was actually called.
3408 *
3409 * also, if there is a pending migration on this lockres,
3410 * and this was the last pending ast on the lockres,
3411 * atomically set the MIGRATING flag before we drop the lock.
3412 * this is how we ensure that migration can proceed with no
3413 * asts in progress.  note that it is ok if the state of the
3414 * queues is such that a lock should be granted in the future
3415 * or that a bast should be fired, because the new master will
3416 * shuffle the lists on this lockres as soon as it is migrated.
3417 */
3418void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
3419			     struct dlm_lock_resource *res)
3420{
3421	if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
3422		return;
3423
3424	if (!res->migration_pending) {
3425		spin_unlock(&res->spinlock);
3426		return;
3427	}
3428
3429	BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3430	res->migration_pending = 0;
3431	res->state |= DLM_LOCK_RES_MIGRATING;
3432	spin_unlock(&res->spinlock);
3433	wake_up(&res->wq);
3434	wake_up(&dlm->migration_wq);
3435}
3436
3437void dlm_force_free_mles(struct dlm_ctxt *dlm)
3438{
3439	int i;
3440	struct hlist_head *bucket;
3441	struct dlm_master_list_entry *mle;
3442	struct hlist_node *tmp, *list;
3443
3444	/*
3445	 * We notified all other nodes that we are exiting the domain and
3446	 * marked the dlm state to DLM_CTXT_LEAVING. If any mles are still
3447	 * around we force free them and wake any processes that are waiting
3448	 * on the mles
3449	 */
3450	spin_lock(&dlm->spinlock);
3451	spin_lock(&dlm->master_lock);
3452
3453	BUG_ON(dlm->dlm_state != DLM_CTXT_LEAVING);
3454	BUG_ON((find_next_bit(dlm->domain_map, O2NM_MAX_NODES, 0) < O2NM_MAX_NODES));
3455
3456	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
3457		bucket = dlm_master_hash(dlm, i);
3458		hlist_for_each_safe(list, tmp, bucket) {
3459			mle = hlist_entry(list, struct dlm_master_list_entry,
3460					  master_hash_node);
3461			if (mle->type != DLM_MLE_BLOCK) {
3462				mlog(ML_ERROR, "bad mle: %p\n", mle);
3463				dlm_print_one_mle(mle);
3464			}
3465			atomic_set(&mle->woken, 1);
3466			wake_up(&mle->wq);
3467
3468			__dlm_unlink_mle(dlm, mle);
3469			__dlm_mle_detach_hb_events(dlm, mle);
3470			__dlm_put_mle(mle);
3471		}
3472	}
3473	spin_unlock(&dlm->master_lock);
3474	spin_unlock(&dlm->spinlock);
3475}
3476