xref: /OK3568_Linux_fs/kernel/fs/ocfs2/dlm/dlmunlock.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  * dlmunlock.c
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * underlying calls for unlocking locks
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/highmem.h>
17*4882a593Smuzhiyun #include <linux/init.h>
18*4882a593Smuzhiyun #include <linux/sysctl.h>
19*4882a593Smuzhiyun #include <linux/random.h>
20*4882a593Smuzhiyun #include <linux/blkdev.h>
21*4882a593Smuzhiyun #include <linux/socket.h>
22*4882a593Smuzhiyun #include <linux/inet.h>
23*4882a593Smuzhiyun #include <linux/spinlock.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "../cluster/heartbeat.h"
27*4882a593Smuzhiyun #include "../cluster/nodemanager.h"
28*4882a593Smuzhiyun #include "../cluster/tcp.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include "dlmapi.h"
31*4882a593Smuzhiyun #include "dlmcommon.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define MLOG_MASK_PREFIX ML_DLM
34*4882a593Smuzhiyun #include "../cluster/masklog.h"
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define DLM_UNLOCK_FREE_LOCK           0x00000001
37*4882a593Smuzhiyun #define DLM_UNLOCK_CALL_AST            0x00000002
38*4882a593Smuzhiyun #define DLM_UNLOCK_REMOVE_LOCK         0x00000004
39*4882a593Smuzhiyun #define DLM_UNLOCK_REGRANT_LOCK        0x00000008
40*4882a593Smuzhiyun #define DLM_UNLOCK_CLEAR_CONVERT_TYPE  0x00000010
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
44*4882a593Smuzhiyun 					      struct dlm_lock_resource *res,
45*4882a593Smuzhiyun 					      struct dlm_lock *lock,
46*4882a593Smuzhiyun 					      struct dlm_lockstatus *lksb,
47*4882a593Smuzhiyun 					      int *actions);
48*4882a593Smuzhiyun static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
49*4882a593Smuzhiyun 					      struct dlm_lock_resource *res,
50*4882a593Smuzhiyun 					      struct dlm_lock *lock,
51*4882a593Smuzhiyun 					      struct dlm_lockstatus *lksb,
52*4882a593Smuzhiyun 					      int *actions);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
55*4882a593Smuzhiyun 						 struct dlm_lock_resource *res,
56*4882a593Smuzhiyun 						 struct dlm_lock *lock,
57*4882a593Smuzhiyun 						 struct dlm_lockstatus *lksb,
58*4882a593Smuzhiyun 						 int flags,
59*4882a593Smuzhiyun 						 u8 owner);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun  * according to the spec:
64*4882a593Smuzhiyun  * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  *  flags & LKM_CANCEL != 0: must be converting or blocked
67*4882a593Smuzhiyun  *  flags & LKM_CANCEL == 0: must be granted
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * So to unlock a converting lock, you must first cancel the
70*4882a593Smuzhiyun  * convert (passing LKM_CANCEL in flags), then call the unlock
71*4882a593Smuzhiyun  * again (with no LKM_CANCEL in flags).
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun  * locking:
77*4882a593Smuzhiyun  *   caller needs:  none
78*4882a593Smuzhiyun  *   taken:         res->spinlock and lock->spinlock taken and dropped
79*4882a593Smuzhiyun  *   held on exit:  none
80*4882a593Smuzhiyun  * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
81*4882a593Smuzhiyun  * all callers should have taken an extra ref on lock coming in
82*4882a593Smuzhiyun  */
dlmunlock_common(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int flags,int * call_ast,int master_node)83*4882a593Smuzhiyun static enum dlm_status dlmunlock_common(struct dlm_ctxt *dlm,
84*4882a593Smuzhiyun 					struct dlm_lock_resource *res,
85*4882a593Smuzhiyun 					struct dlm_lock *lock,
86*4882a593Smuzhiyun 					struct dlm_lockstatus *lksb,
87*4882a593Smuzhiyun 					int flags, int *call_ast,
88*4882a593Smuzhiyun 					int master_node)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	enum dlm_status status;
91*4882a593Smuzhiyun 	int actions = 0;
92*4882a593Smuzhiyun 	int in_use;
93*4882a593Smuzhiyun 	u8 owner;
94*4882a593Smuzhiyun 	int recovery_wait = 0;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	mlog(0, "master_node = %d, valblk = %d\n", master_node,
97*4882a593Smuzhiyun 	     flags & LKM_VALBLK);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (master_node)
100*4882a593Smuzhiyun 		BUG_ON(res->owner != dlm->node_num);
101*4882a593Smuzhiyun 	else
102*4882a593Smuzhiyun 		BUG_ON(res->owner == dlm->node_num);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	spin_lock(&dlm->ast_lock);
105*4882a593Smuzhiyun 	/* We want to be sure that we're not freeing a lock
106*4882a593Smuzhiyun 	 * that still has AST's pending... */
107*4882a593Smuzhiyun 	in_use = !list_empty(&lock->ast_list);
108*4882a593Smuzhiyun 	spin_unlock(&dlm->ast_lock);
109*4882a593Smuzhiyun 	if (in_use && !(flags & LKM_CANCEL)) {
110*4882a593Smuzhiyun 	       mlog(ML_ERROR, "lockres %.*s: Someone is calling dlmunlock "
111*4882a593Smuzhiyun 		    "while waiting for an ast!", res->lockname.len,
112*4882a593Smuzhiyun 		    res->lockname.name);
113*4882a593Smuzhiyun 		return DLM_BADPARAM;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	spin_lock(&res->spinlock);
117*4882a593Smuzhiyun 	if (res->state & DLM_LOCK_RES_IN_PROGRESS) {
118*4882a593Smuzhiyun 		if (master_node && !(flags & LKM_CANCEL)) {
119*4882a593Smuzhiyun 			mlog(ML_ERROR, "lockres in progress!\n");
120*4882a593Smuzhiyun 			spin_unlock(&res->spinlock);
121*4882a593Smuzhiyun 			return DLM_FORWARD;
122*4882a593Smuzhiyun 		}
123*4882a593Smuzhiyun 		/* ok for this to sleep if not in a network handler */
124*4882a593Smuzhiyun 		__dlm_wait_on_lockres(res);
125*4882a593Smuzhiyun 		res->state |= DLM_LOCK_RES_IN_PROGRESS;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	spin_lock(&lock->spinlock);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (res->state & DLM_LOCK_RES_RECOVERING) {
130*4882a593Smuzhiyun 		status = DLM_RECOVERING;
131*4882a593Smuzhiyun 		goto leave;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	if (res->state & DLM_LOCK_RES_MIGRATING) {
135*4882a593Smuzhiyun 		status = DLM_MIGRATING;
136*4882a593Smuzhiyun 		goto leave;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* see above for what the spec says about
140*4882a593Smuzhiyun 	 * LKM_CANCEL and the lock queue state */
141*4882a593Smuzhiyun 	if (flags & LKM_CANCEL)
142*4882a593Smuzhiyun 		status = dlm_get_cancel_actions(dlm, res, lock, lksb, &actions);
143*4882a593Smuzhiyun 	else
144*4882a593Smuzhiyun 		status = dlm_get_unlock_actions(dlm, res, lock, lksb, &actions);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (status != DLM_NORMAL && (status != DLM_CANCELGRANT || !master_node))
147*4882a593Smuzhiyun 		goto leave;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* By now this has been masked out of cancel requests. */
150*4882a593Smuzhiyun 	if (flags & LKM_VALBLK) {
151*4882a593Smuzhiyun 		/* make the final update to the lvb */
152*4882a593Smuzhiyun 		if (master_node)
153*4882a593Smuzhiyun 			memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
154*4882a593Smuzhiyun 		else
155*4882a593Smuzhiyun 			flags |= LKM_PUT_LVB; /* let the send function
156*4882a593Smuzhiyun 					       * handle it. */
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (!master_node) {
160*4882a593Smuzhiyun 		owner = res->owner;
161*4882a593Smuzhiyun 		/* drop locks and send message */
162*4882a593Smuzhiyun 		if (flags & LKM_CANCEL)
163*4882a593Smuzhiyun 			lock->cancel_pending = 1;
164*4882a593Smuzhiyun 		else
165*4882a593Smuzhiyun 			lock->unlock_pending = 1;
166*4882a593Smuzhiyun 		spin_unlock(&lock->spinlock);
167*4882a593Smuzhiyun 		spin_unlock(&res->spinlock);
168*4882a593Smuzhiyun 		status = dlm_send_remote_unlock_request(dlm, res, lock, lksb,
169*4882a593Smuzhiyun 							flags, owner);
170*4882a593Smuzhiyun 		spin_lock(&res->spinlock);
171*4882a593Smuzhiyun 		spin_lock(&lock->spinlock);
172*4882a593Smuzhiyun 		/* if the master told us the lock was already granted,
173*4882a593Smuzhiyun 		 * let the ast handle all of these actions */
174*4882a593Smuzhiyun 		if (status == DLM_CANCELGRANT) {
175*4882a593Smuzhiyun 			actions &= ~(DLM_UNLOCK_REMOVE_LOCK|
176*4882a593Smuzhiyun 				     DLM_UNLOCK_REGRANT_LOCK|
177*4882a593Smuzhiyun 				     DLM_UNLOCK_CLEAR_CONVERT_TYPE);
178*4882a593Smuzhiyun 		} else if (status == DLM_RECOVERING ||
179*4882a593Smuzhiyun 			   status == DLM_MIGRATING ||
180*4882a593Smuzhiyun 			   status == DLM_FORWARD ||
181*4882a593Smuzhiyun 			   status == DLM_NOLOCKMGR
182*4882a593Smuzhiyun 			   ) {
183*4882a593Smuzhiyun 			/* must clear the actions because this unlock
184*4882a593Smuzhiyun 			 * is about to be retried.  cannot free or do
185*4882a593Smuzhiyun 			 * any list manipulation. */
186*4882a593Smuzhiyun 			mlog(0, "%s:%.*s: clearing actions, %s\n",
187*4882a593Smuzhiyun 			     dlm->name, res->lockname.len,
188*4882a593Smuzhiyun 			     res->lockname.name,
189*4882a593Smuzhiyun 			     status==DLM_RECOVERING?"recovering":
190*4882a593Smuzhiyun 			     (status==DLM_MIGRATING?"migrating":
191*4882a593Smuzhiyun 				(status == DLM_FORWARD ? "forward" :
192*4882a593Smuzhiyun 						"nolockmanager")));
193*4882a593Smuzhiyun 			actions = 0;
194*4882a593Smuzhiyun 		}
195*4882a593Smuzhiyun 		if (flags & LKM_CANCEL)
196*4882a593Smuzhiyun 			lock->cancel_pending = 0;
197*4882a593Smuzhiyun 		else {
198*4882a593Smuzhiyun 			if (!lock->unlock_pending)
199*4882a593Smuzhiyun 				recovery_wait = 1;
200*4882a593Smuzhiyun 			else
201*4882a593Smuzhiyun 				lock->unlock_pending = 0;
202*4882a593Smuzhiyun 		}
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* get an extra ref on lock.  if we are just switching
206*4882a593Smuzhiyun 	 * lists here, we dont want the lock to go away. */
207*4882a593Smuzhiyun 	dlm_lock_get(lock);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (actions & DLM_UNLOCK_REMOVE_LOCK) {
210*4882a593Smuzhiyun 		list_del_init(&lock->list);
211*4882a593Smuzhiyun 		dlm_lock_put(lock);
212*4882a593Smuzhiyun 	}
213*4882a593Smuzhiyun 	if (actions & DLM_UNLOCK_REGRANT_LOCK) {
214*4882a593Smuzhiyun 		dlm_lock_get(lock);
215*4882a593Smuzhiyun 		list_add_tail(&lock->list, &res->granted);
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 	if (actions & DLM_UNLOCK_CLEAR_CONVERT_TYPE) {
218*4882a593Smuzhiyun 		mlog(0, "clearing convert_type at %smaster node\n",
219*4882a593Smuzhiyun 		     master_node ? "" : "non-");
220*4882a593Smuzhiyun 		lock->ml.convert_type = LKM_IVMODE;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* remove the extra ref on lock */
224*4882a593Smuzhiyun 	dlm_lock_put(lock);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun leave:
227*4882a593Smuzhiyun 	res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
228*4882a593Smuzhiyun 	if (!dlm_lock_on_list(&res->converting, lock))
229*4882a593Smuzhiyun 		BUG_ON(lock->ml.convert_type != LKM_IVMODE);
230*4882a593Smuzhiyun 	else
231*4882a593Smuzhiyun 		BUG_ON(lock->ml.convert_type == LKM_IVMODE);
232*4882a593Smuzhiyun 	spin_unlock(&lock->spinlock);
233*4882a593Smuzhiyun 	spin_unlock(&res->spinlock);
234*4882a593Smuzhiyun 	wake_up(&res->wq);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (recovery_wait) {
237*4882a593Smuzhiyun 		spin_lock(&res->spinlock);
238*4882a593Smuzhiyun 		/* Unlock request will directly succeed after owner dies,
239*4882a593Smuzhiyun 		 * and the lock is already removed from grant list. We have to
240*4882a593Smuzhiyun 		 * wait for RECOVERING done or we miss the chance to purge it
241*4882a593Smuzhiyun 		 * since the removement is much faster than RECOVERING proc.
242*4882a593Smuzhiyun 		 */
243*4882a593Smuzhiyun 		__dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_RECOVERING);
244*4882a593Smuzhiyun 		spin_unlock(&res->spinlock);
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* let the caller's final dlm_lock_put handle the actual kfree */
248*4882a593Smuzhiyun 	if (actions & DLM_UNLOCK_FREE_LOCK) {
249*4882a593Smuzhiyun 		/* this should always be coupled with list removal */
250*4882a593Smuzhiyun 		BUG_ON(!(actions & DLM_UNLOCK_REMOVE_LOCK));
251*4882a593Smuzhiyun 		mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
252*4882a593Smuzhiyun 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
253*4882a593Smuzhiyun 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
254*4882a593Smuzhiyun 		     kref_read(&lock->lock_refs)-1);
255*4882a593Smuzhiyun 		dlm_lock_put(lock);
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 	if (actions & DLM_UNLOCK_CALL_AST)
258*4882a593Smuzhiyun 		*call_ast = 1;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* if cancel or unlock succeeded, lvb work is done */
261*4882a593Smuzhiyun 	if (status == DLM_NORMAL)
262*4882a593Smuzhiyun 		lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	return status;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
dlm_commit_pending_unlock(struct dlm_lock_resource * res,struct dlm_lock * lock)267*4882a593Smuzhiyun void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
268*4882a593Smuzhiyun 			       struct dlm_lock *lock)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	/* leave DLM_LKSB_PUT_LVB on the lksb so any final
271*4882a593Smuzhiyun 	 * update of the lvb will be sent to the new master */
272*4882a593Smuzhiyun 	list_del_init(&lock->list);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
dlm_commit_pending_cancel(struct dlm_lock_resource * res,struct dlm_lock * lock)275*4882a593Smuzhiyun void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
276*4882a593Smuzhiyun 			       struct dlm_lock *lock)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	list_move_tail(&lock->list, &res->granted);
279*4882a593Smuzhiyun 	lock->ml.convert_type = LKM_IVMODE;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 
dlmunlock_master(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int flags,int * call_ast)283*4882a593Smuzhiyun static inline enum dlm_status dlmunlock_master(struct dlm_ctxt *dlm,
284*4882a593Smuzhiyun 					  struct dlm_lock_resource *res,
285*4882a593Smuzhiyun 					  struct dlm_lock *lock,
286*4882a593Smuzhiyun 					  struct dlm_lockstatus *lksb,
287*4882a593Smuzhiyun 					  int flags,
288*4882a593Smuzhiyun 					  int *call_ast)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 1);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
dlmunlock_remote(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int flags,int * call_ast)293*4882a593Smuzhiyun static inline enum dlm_status dlmunlock_remote(struct dlm_ctxt *dlm,
294*4882a593Smuzhiyun 					  struct dlm_lock_resource *res,
295*4882a593Smuzhiyun 					  struct dlm_lock *lock,
296*4882a593Smuzhiyun 					  struct dlm_lockstatus *lksb,
297*4882a593Smuzhiyun 					  int flags, int *call_ast)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 0);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun  * locking:
304*4882a593Smuzhiyun  *   caller needs:  none
305*4882a593Smuzhiyun  *   taken:         none
306*4882a593Smuzhiyun  *   held on exit:  none
307*4882a593Smuzhiyun  * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
308*4882a593Smuzhiyun  */
dlm_send_remote_unlock_request(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int flags,u8 owner)309*4882a593Smuzhiyun static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
310*4882a593Smuzhiyun 						 struct dlm_lock_resource *res,
311*4882a593Smuzhiyun 						 struct dlm_lock *lock,
312*4882a593Smuzhiyun 						 struct dlm_lockstatus *lksb,
313*4882a593Smuzhiyun 						 int flags,
314*4882a593Smuzhiyun 						 u8 owner)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct dlm_unlock_lock unlock;
317*4882a593Smuzhiyun 	int tmpret;
318*4882a593Smuzhiyun 	enum dlm_status ret;
319*4882a593Smuzhiyun 	int status = 0;
320*4882a593Smuzhiyun 	struct kvec vec[2];
321*4882a593Smuzhiyun 	size_t veclen = 1;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	mlog(0, "%.*s\n", res->lockname.len, res->lockname.name);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (owner == dlm->node_num) {
326*4882a593Smuzhiyun 		/* ended up trying to contact ourself.  this means
327*4882a593Smuzhiyun 		 * that the lockres had been remote but became local
328*4882a593Smuzhiyun 		 * via a migration.  just retry it, now as local */
329*4882a593Smuzhiyun 		mlog(0, "%s:%.*s: this node became the master due to a "
330*4882a593Smuzhiyun 		     "migration, re-evaluate now\n", dlm->name,
331*4882a593Smuzhiyun 		     res->lockname.len, res->lockname.name);
332*4882a593Smuzhiyun 		return DLM_FORWARD;
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	memset(&unlock, 0, sizeof(unlock));
336*4882a593Smuzhiyun 	unlock.node_idx = dlm->node_num;
337*4882a593Smuzhiyun 	unlock.flags = cpu_to_be32(flags);
338*4882a593Smuzhiyun 	unlock.cookie = lock->ml.cookie;
339*4882a593Smuzhiyun 	unlock.namelen = res->lockname.len;
340*4882a593Smuzhiyun 	memcpy(unlock.name, res->lockname.name, unlock.namelen);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	vec[0].iov_len = sizeof(struct dlm_unlock_lock);
343*4882a593Smuzhiyun 	vec[0].iov_base = &unlock;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (flags & LKM_PUT_LVB) {
346*4882a593Smuzhiyun 		/* extra data to send if we are updating lvb */
347*4882a593Smuzhiyun 		vec[1].iov_len = DLM_LVB_LEN;
348*4882a593Smuzhiyun 		vec[1].iov_base = lock->lksb->lvb;
349*4882a593Smuzhiyun 		veclen++;
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	tmpret = o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG, dlm->key,
353*4882a593Smuzhiyun 					vec, veclen, owner, &status);
354*4882a593Smuzhiyun 	if (tmpret >= 0) {
355*4882a593Smuzhiyun 		// successfully sent and received
356*4882a593Smuzhiyun 		if (status == DLM_FORWARD)
357*4882a593Smuzhiyun 			mlog(0, "master was in-progress.  retry\n");
358*4882a593Smuzhiyun 		ret = status;
359*4882a593Smuzhiyun 	} else {
360*4882a593Smuzhiyun 		mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
361*4882a593Smuzhiyun 		     "node %u\n", tmpret, DLM_UNLOCK_LOCK_MSG, dlm->key, owner);
362*4882a593Smuzhiyun 		if (dlm_is_host_down(tmpret)) {
363*4882a593Smuzhiyun 			/* NOTE: this seems strange, but it is what we want.
364*4882a593Smuzhiyun 			 * when the master goes down during a cancel or
365*4882a593Smuzhiyun 			 * unlock, the recovery code completes the operation
366*4882a593Smuzhiyun 			 * as if the master had not died, then passes the
367*4882a593Smuzhiyun 			 * updated state to the recovery master.  this thread
368*4882a593Smuzhiyun 			 * just needs to finish out the operation and call
369*4882a593Smuzhiyun 			 * the unlockast. */
370*4882a593Smuzhiyun 			if (dlm_is_node_dead(dlm, owner))
371*4882a593Smuzhiyun 				ret = DLM_NORMAL;
372*4882a593Smuzhiyun 			else
373*4882a593Smuzhiyun 				ret = DLM_NOLOCKMGR;
374*4882a593Smuzhiyun 		} else {
375*4882a593Smuzhiyun 			/* something bad.  this will BUG in ocfs2 */
376*4882a593Smuzhiyun 			ret = dlm_err_to_dlm_status(tmpret);
377*4882a593Smuzhiyun 		}
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	return ret;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun  * locking:
385*4882a593Smuzhiyun  *   caller needs:  none
386*4882a593Smuzhiyun  *   taken:         takes and drops res->spinlock
387*4882a593Smuzhiyun  *   held on exit:  none
388*4882a593Smuzhiyun  * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
389*4882a593Smuzhiyun  *          return value from dlmunlock_master
390*4882a593Smuzhiyun  */
dlm_unlock_lock_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)391*4882a593Smuzhiyun int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
392*4882a593Smuzhiyun 			    void **ret_data)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	struct dlm_ctxt *dlm = data;
395*4882a593Smuzhiyun 	struct dlm_unlock_lock *unlock = (struct dlm_unlock_lock *)msg->buf;
396*4882a593Smuzhiyun 	struct dlm_lock_resource *res = NULL;
397*4882a593Smuzhiyun 	struct dlm_lock *lock = NULL;
398*4882a593Smuzhiyun 	enum dlm_status status = DLM_NORMAL;
399*4882a593Smuzhiyun 	int found = 0, i;
400*4882a593Smuzhiyun 	struct dlm_lockstatus *lksb = NULL;
401*4882a593Smuzhiyun 	int ignore;
402*4882a593Smuzhiyun 	u32 flags;
403*4882a593Smuzhiyun 	struct list_head *queue;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	flags = be32_to_cpu(unlock->flags);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	if (flags & LKM_GET_LVB) {
408*4882a593Smuzhiyun 		mlog(ML_ERROR, "bad args!  GET_LVB specified on unlock!\n");
409*4882a593Smuzhiyun 		return DLM_BADARGS;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	if ((flags & (LKM_PUT_LVB|LKM_CANCEL)) == (LKM_PUT_LVB|LKM_CANCEL)) {
413*4882a593Smuzhiyun 		mlog(ML_ERROR, "bad args!  cannot modify lvb on a CANCEL "
414*4882a593Smuzhiyun 		     "request!\n");
415*4882a593Smuzhiyun 		return DLM_BADARGS;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	if (unlock->namelen > DLM_LOCKID_NAME_MAX) {
419*4882a593Smuzhiyun 		mlog(ML_ERROR, "Invalid name length in unlock handler!\n");
420*4882a593Smuzhiyun 		return DLM_IVBUFLEN;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (!dlm_grab(dlm))
424*4882a593Smuzhiyun 		return DLM_FORWARD;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
427*4882a593Smuzhiyun 			"Domain %s not fully joined!\n", dlm->name);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" : "none");
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	res = dlm_lookup_lockres(dlm, unlock->name, unlock->namelen);
432*4882a593Smuzhiyun 	if (!res) {
433*4882a593Smuzhiyun 		/* We assume here that a no lock resource simply means
434*4882a593Smuzhiyun 		 * it was migrated away and destroyed before the other
435*4882a593Smuzhiyun 		 * node could detect it. */
436*4882a593Smuzhiyun 		mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
437*4882a593Smuzhiyun 		status = DLM_FORWARD;
438*4882a593Smuzhiyun 		goto not_found;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	queue=&res->granted;
442*4882a593Smuzhiyun 	found = 0;
443*4882a593Smuzhiyun 	spin_lock(&res->spinlock);
444*4882a593Smuzhiyun 	if (res->state & DLM_LOCK_RES_RECOVERING) {
445*4882a593Smuzhiyun 		spin_unlock(&res->spinlock);
446*4882a593Smuzhiyun 		mlog(0, "returning DLM_RECOVERING\n");
447*4882a593Smuzhiyun 		status = DLM_RECOVERING;
448*4882a593Smuzhiyun 		goto leave;
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (res->state & DLM_LOCK_RES_MIGRATING) {
452*4882a593Smuzhiyun 		spin_unlock(&res->spinlock);
453*4882a593Smuzhiyun 		mlog(0, "returning DLM_MIGRATING\n");
454*4882a593Smuzhiyun 		status = DLM_MIGRATING;
455*4882a593Smuzhiyun 		goto leave;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	if (res->owner != dlm->node_num) {
459*4882a593Smuzhiyun 		spin_unlock(&res->spinlock);
460*4882a593Smuzhiyun 		mlog(0, "returning DLM_FORWARD -- not master\n");
461*4882a593Smuzhiyun 		status = DLM_FORWARD;
462*4882a593Smuzhiyun 		goto leave;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	for (i=0; i<3; i++) {
466*4882a593Smuzhiyun 		list_for_each_entry(lock, queue, list) {
467*4882a593Smuzhiyun 			if (lock->ml.cookie == unlock->cookie &&
468*4882a593Smuzhiyun 		    	    lock->ml.node == unlock->node_idx) {
469*4882a593Smuzhiyun 				dlm_lock_get(lock);
470*4882a593Smuzhiyun 				found = 1;
471*4882a593Smuzhiyun 				break;
472*4882a593Smuzhiyun 			}
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 		if (found)
475*4882a593Smuzhiyun 			break;
476*4882a593Smuzhiyun 		/* scan granted -> converting -> blocked queues */
477*4882a593Smuzhiyun 		queue++;
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 	spin_unlock(&res->spinlock);
480*4882a593Smuzhiyun 	if (!found) {
481*4882a593Smuzhiyun 		status = DLM_IVLOCKID;
482*4882a593Smuzhiyun 		goto not_found;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* lock was found on queue */
486*4882a593Smuzhiyun 	lksb = lock->lksb;
487*4882a593Smuzhiyun 	if (flags & (LKM_VALBLK|LKM_PUT_LVB) &&
488*4882a593Smuzhiyun 	    lock->ml.type != LKM_EXMODE)
489*4882a593Smuzhiyun 		flags &= ~(LKM_VALBLK|LKM_PUT_LVB);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* unlockast only called on originating node */
492*4882a593Smuzhiyun 	if (flags & LKM_PUT_LVB) {
493*4882a593Smuzhiyun 		lksb->flags |= DLM_LKSB_PUT_LVB;
494*4882a593Smuzhiyun 		memcpy(&lksb->lvb[0], &unlock->lvb[0], DLM_LVB_LEN);
495*4882a593Smuzhiyun 	}
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	/* if this is in-progress, propagate the DLM_FORWARD
498*4882a593Smuzhiyun 	 * all the way back out */
499*4882a593Smuzhiyun 	status = dlmunlock_master(dlm, res, lock, lksb, flags, &ignore);
500*4882a593Smuzhiyun 	if (status == DLM_FORWARD)
501*4882a593Smuzhiyun 		mlog(0, "lockres is in progress\n");
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (flags & LKM_PUT_LVB)
504*4882a593Smuzhiyun 		lksb->flags &= ~DLM_LKSB_PUT_LVB;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	dlm_lockres_calc_usage(dlm, res);
507*4882a593Smuzhiyun 	dlm_kick_thread(dlm, res);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun not_found:
510*4882a593Smuzhiyun 	if (!found)
511*4882a593Smuzhiyun 		mlog(ML_ERROR, "failed to find lock to unlock! "
512*4882a593Smuzhiyun 			       "cookie=%u:%llu\n",
513*4882a593Smuzhiyun 		     dlm_get_lock_cookie_node(be64_to_cpu(unlock->cookie)),
514*4882a593Smuzhiyun 		     dlm_get_lock_cookie_seq(be64_to_cpu(unlock->cookie)));
515*4882a593Smuzhiyun 	else
516*4882a593Smuzhiyun 		dlm_lock_put(lock);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun leave:
519*4882a593Smuzhiyun 	if (res)
520*4882a593Smuzhiyun 		dlm_lockres_put(res);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	dlm_put(dlm);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	return status;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 
dlm_get_cancel_actions(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int * actions)528*4882a593Smuzhiyun static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
529*4882a593Smuzhiyun 					      struct dlm_lock_resource *res,
530*4882a593Smuzhiyun 					      struct dlm_lock *lock,
531*4882a593Smuzhiyun 					      struct dlm_lockstatus *lksb,
532*4882a593Smuzhiyun 					      int *actions)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	enum dlm_status status;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (dlm_lock_on_list(&res->blocked, lock)) {
537*4882a593Smuzhiyun 		/* cancel this outright */
538*4882a593Smuzhiyun 		status = DLM_NORMAL;
539*4882a593Smuzhiyun 		*actions = (DLM_UNLOCK_CALL_AST |
540*4882a593Smuzhiyun 			    DLM_UNLOCK_REMOVE_LOCK);
541*4882a593Smuzhiyun 	} else if (dlm_lock_on_list(&res->converting, lock)) {
542*4882a593Smuzhiyun 		/* cancel the request, put back on granted */
543*4882a593Smuzhiyun 		status = DLM_NORMAL;
544*4882a593Smuzhiyun 		*actions = (DLM_UNLOCK_CALL_AST |
545*4882a593Smuzhiyun 			    DLM_UNLOCK_REMOVE_LOCK |
546*4882a593Smuzhiyun 			    DLM_UNLOCK_REGRANT_LOCK |
547*4882a593Smuzhiyun 			    DLM_UNLOCK_CLEAR_CONVERT_TYPE);
548*4882a593Smuzhiyun 	} else if (dlm_lock_on_list(&res->granted, lock)) {
549*4882a593Smuzhiyun 		/* too late, already granted. */
550*4882a593Smuzhiyun 		status = DLM_CANCELGRANT;
551*4882a593Smuzhiyun 		*actions = DLM_UNLOCK_CALL_AST;
552*4882a593Smuzhiyun 	} else {
553*4882a593Smuzhiyun 		mlog(ML_ERROR, "lock to cancel is not on any list!\n");
554*4882a593Smuzhiyun 		status = DLM_IVLOCKID;
555*4882a593Smuzhiyun 		*actions = 0;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 	return status;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
dlm_get_unlock_actions(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_lock * lock,struct dlm_lockstatus * lksb,int * actions)560*4882a593Smuzhiyun static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
561*4882a593Smuzhiyun 					      struct dlm_lock_resource *res,
562*4882a593Smuzhiyun 					      struct dlm_lock *lock,
563*4882a593Smuzhiyun 					      struct dlm_lockstatus *lksb,
564*4882a593Smuzhiyun 					      int *actions)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	enum dlm_status status;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	/* unlock request */
569*4882a593Smuzhiyun 	if (!dlm_lock_on_list(&res->granted, lock)) {
570*4882a593Smuzhiyun 		status = DLM_DENIED;
571*4882a593Smuzhiyun 		dlm_error(status);
572*4882a593Smuzhiyun 		*actions = 0;
573*4882a593Smuzhiyun 	} else {
574*4882a593Smuzhiyun 		/* unlock granted lock */
575*4882a593Smuzhiyun 		status = DLM_NORMAL;
576*4882a593Smuzhiyun 		*actions = (DLM_UNLOCK_FREE_LOCK |
577*4882a593Smuzhiyun 			    DLM_UNLOCK_CALL_AST |
578*4882a593Smuzhiyun 			    DLM_UNLOCK_REMOVE_LOCK);
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun 	return status;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /* there seems to be no point in doing this async
584*4882a593Smuzhiyun  * since (even for the remote case) there is really
585*4882a593Smuzhiyun  * no work to queue up... so just do it and fire the
586*4882a593Smuzhiyun  * unlockast by hand when done... */
dlmunlock(struct dlm_ctxt * dlm,struct dlm_lockstatus * lksb,int flags,dlm_astunlockfunc_t * unlockast,void * data)587*4882a593Smuzhiyun enum dlm_status dlmunlock(struct dlm_ctxt *dlm, struct dlm_lockstatus *lksb,
588*4882a593Smuzhiyun 			  int flags, dlm_astunlockfunc_t *unlockast, void *data)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	enum dlm_status status;
591*4882a593Smuzhiyun 	struct dlm_lock_resource *res;
592*4882a593Smuzhiyun 	struct dlm_lock *lock = NULL;
593*4882a593Smuzhiyun 	int call_ast, is_master;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (!lksb) {
596*4882a593Smuzhiyun 		dlm_error(DLM_BADARGS);
597*4882a593Smuzhiyun 		return DLM_BADARGS;
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (flags & ~(LKM_CANCEL | LKM_VALBLK | LKM_INVVALBLK)) {
601*4882a593Smuzhiyun 		dlm_error(DLM_BADPARAM);
602*4882a593Smuzhiyun 		return DLM_BADPARAM;
603*4882a593Smuzhiyun 	}
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if ((flags & (LKM_VALBLK | LKM_CANCEL)) == (LKM_VALBLK | LKM_CANCEL)) {
606*4882a593Smuzhiyun 		mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
607*4882a593Smuzhiyun 		flags &= ~LKM_VALBLK;
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	if (!lksb->lockid || !lksb->lockid->lockres) {
611*4882a593Smuzhiyun 		dlm_error(DLM_BADPARAM);
612*4882a593Smuzhiyun 		return DLM_BADPARAM;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	lock = lksb->lockid;
616*4882a593Smuzhiyun 	BUG_ON(!lock);
617*4882a593Smuzhiyun 	dlm_lock_get(lock);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	res = lock->lockres;
620*4882a593Smuzhiyun 	BUG_ON(!res);
621*4882a593Smuzhiyun 	dlm_lockres_get(res);
622*4882a593Smuzhiyun retry:
623*4882a593Smuzhiyun 	call_ast = 0;
624*4882a593Smuzhiyun 	/* need to retry up here because owner may have changed */
625*4882a593Smuzhiyun 	mlog(0, "lock=%p res=%p\n", lock, res);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	spin_lock(&res->spinlock);
628*4882a593Smuzhiyun 	is_master = (res->owner == dlm->node_num);
629*4882a593Smuzhiyun 	if (flags & LKM_VALBLK && lock->ml.type != LKM_EXMODE)
630*4882a593Smuzhiyun 		flags &= ~LKM_VALBLK;
631*4882a593Smuzhiyun 	spin_unlock(&res->spinlock);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	if (is_master) {
634*4882a593Smuzhiyun 		status = dlmunlock_master(dlm, res, lock, lksb, flags,
635*4882a593Smuzhiyun 					  &call_ast);
636*4882a593Smuzhiyun 		mlog(0, "done calling dlmunlock_master: returned %d, "
637*4882a593Smuzhiyun 		     "call_ast is %d\n", status, call_ast);
638*4882a593Smuzhiyun 	} else {
639*4882a593Smuzhiyun 		status = dlmunlock_remote(dlm, res, lock, lksb, flags,
640*4882a593Smuzhiyun 					  &call_ast);
641*4882a593Smuzhiyun 		mlog(0, "done calling dlmunlock_remote: returned %d, "
642*4882a593Smuzhiyun 		     "call_ast is %d\n", status, call_ast);
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	if (status == DLM_RECOVERING ||
646*4882a593Smuzhiyun 	    status == DLM_MIGRATING ||
647*4882a593Smuzhiyun 	    status == DLM_FORWARD ||
648*4882a593Smuzhiyun 	    status == DLM_NOLOCKMGR) {
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 		/* We want to go away for a tiny bit to allow recovery
651*4882a593Smuzhiyun 		 * / migration to complete on this resource. I don't
652*4882a593Smuzhiyun 		 * know of any wait queue we could sleep on as this
653*4882a593Smuzhiyun 		 * may be happening on another node. Perhaps the
654*4882a593Smuzhiyun 		 * proper solution is to queue up requests on the
655*4882a593Smuzhiyun 		 * other end? */
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		/* do we want to yield(); ?? */
658*4882a593Smuzhiyun 		msleep(50);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 		mlog(0, "retrying unlock due to pending recovery/"
661*4882a593Smuzhiyun 		     "migration/in-progress/reconnect\n");
662*4882a593Smuzhiyun 		goto retry;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	if (call_ast) {
666*4882a593Smuzhiyun 		mlog(0, "calling unlockast(%p, %d)\n", data, status);
667*4882a593Smuzhiyun 		if (is_master) {
668*4882a593Smuzhiyun 			/* it is possible that there is one last bast
669*4882a593Smuzhiyun 			 * pending.  make sure it is flushed, then
670*4882a593Smuzhiyun 			 * call the unlockast.
671*4882a593Smuzhiyun 			 * not an issue if this is a mastered remotely,
672*4882a593Smuzhiyun 			 * since this lock has been removed from the
673*4882a593Smuzhiyun 			 * lockres queues and cannot be found. */
674*4882a593Smuzhiyun 			dlm_kick_thread(dlm, NULL);
675*4882a593Smuzhiyun 			wait_event(dlm->ast_wq,
676*4882a593Smuzhiyun 				   dlm_lock_basts_flushed(dlm, lock));
677*4882a593Smuzhiyun 		}
678*4882a593Smuzhiyun 		(*unlockast)(data, status);
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	if (status == DLM_CANCELGRANT)
682*4882a593Smuzhiyun 		status = DLM_NORMAL;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	if (status == DLM_NORMAL) {
685*4882a593Smuzhiyun 		mlog(0, "kicking the thread\n");
686*4882a593Smuzhiyun 		dlm_kick_thread(dlm, res);
687*4882a593Smuzhiyun 	} else
688*4882a593Smuzhiyun 		dlm_error(status);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	dlm_lockres_calc_usage(dlm, res);
691*4882a593Smuzhiyun 	dlm_lockres_put(res);
692*4882a593Smuzhiyun 	dlm_lock_put(lock);
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	mlog(0, "returning status=%d!\n", status);
695*4882a593Smuzhiyun 	return status;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dlmunlock);
698*4882a593Smuzhiyun 
699