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