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