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