Merge git://git.infradead.org/~dwmw2/khdrs-2.6
[pandora-kernel.git] / fs / ocfs2 / dlm / dlmlock.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dlmlock.c
5  *
6  * underlying calls for lock creation
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
44 #include "cluster/heartbeat.h"
45 #include "cluster/nodemanager.h"
46 #include "cluster/tcp.h"
47
48 #include "dlmapi.h"
49 #include "dlmcommon.h"
50
51 #include "dlmconvert.h"
52
53 #define MLOG_MASK_PREFIX ML_DLM
54 #include "cluster/masklog.h"
55
56 static DEFINE_SPINLOCK(dlm_cookie_lock);
57 static u64 dlm_next_cookie = 1;
58
59 static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
60                                                struct dlm_lock_resource *res,
61                                                struct dlm_lock *lock, int flags);
62 static void dlm_init_lock(struct dlm_lock *newlock, int type,
63                           u8 node, u64 cookie);
64 static void dlm_lock_release(struct kref *kref);
65 static void dlm_lock_detach_lockres(struct dlm_lock *lock);
66
67 /* Tell us whether we can grant a new lock request.
68  * locking:
69  *   caller needs:  res->spinlock
70  *   taken:         none
71  *   held on exit:  none
72  * returns: 1 if the lock can be granted, 0 otherwise.
73  */
74 static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
75                                   struct dlm_lock *lock)
76 {
77         struct list_head *iter;
78         struct dlm_lock *tmplock;
79
80         list_for_each(iter, &res->granted) {
81                 tmplock = list_entry(iter, struct dlm_lock, list);
82
83                 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
84                         return 0;
85         }
86
87         list_for_each(iter, &res->converting) {
88                 tmplock = list_entry(iter, struct dlm_lock, list);
89
90                 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
91                         return 0;
92         }
93
94         return 1;
95 }
96
97 /* performs lock creation at the lockres master site
98  * locking:
99  *   caller needs:  none
100  *   taken:         takes and drops res->spinlock
101  *   held on exit:  none
102  * returns: DLM_NORMAL, DLM_NOTQUEUED
103  */
104 static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
105                                       struct dlm_lock_resource *res,
106                                       struct dlm_lock *lock, int flags)
107 {
108         int call_ast = 0, kick_thread = 0;
109         enum dlm_status status = DLM_NORMAL;
110
111         mlog_entry("type=%d\n", lock->ml.type);
112
113         spin_lock(&res->spinlock);
114         /* if called from dlm_create_lock_handler, need to
115          * ensure it will not sleep in dlm_wait_on_lockres */
116         status = __dlm_lockres_state_to_status(res);
117         if (status != DLM_NORMAL &&
118             lock->ml.node != dlm->node_num) {
119                 /* erf.  state changed after lock was dropped. */
120                 spin_unlock(&res->spinlock);
121                 dlm_error(status);
122                 return status;
123         }
124         __dlm_wait_on_lockres(res);
125         __dlm_lockres_reserve_ast(res);
126
127         if (dlm_can_grant_new_lock(res, lock)) {
128                 mlog(0, "I can grant this lock right away\n");
129                 /* got it right away */
130                 lock->lksb->status = DLM_NORMAL;
131                 status = DLM_NORMAL;
132                 dlm_lock_get(lock);
133                 list_add_tail(&lock->list, &res->granted);
134
135                 /* for the recovery lock, we can't allow the ast
136                  * to be queued since the dlmthread is already
137                  * frozen.  but the recovery lock is always locked
138                  * with LKM_NOQUEUE so we do not need the ast in
139                  * this special case */
140                 if (!dlm_is_recovery_lock(res->lockname.name,
141                                           res->lockname.len)) {
142                         kick_thread = 1;
143                         call_ast = 1;
144                 } else {
145                         mlog(0, "%s: returning DLM_NORMAL to "
146                              "node %u for reco lock\n", dlm->name,
147                              lock->ml.node);
148                 }
149         } else {
150                 /* for NOQUEUE request, unless we get the
151                  * lock right away, return DLM_NOTQUEUED */
152                 if (flags & LKM_NOQUEUE) {
153                         status = DLM_NOTQUEUED;
154                         if (dlm_is_recovery_lock(res->lockname.name,
155                                                  res->lockname.len)) {
156                                 mlog(0, "%s: returning NOTQUEUED to "
157                                      "node %u for reco lock\n", dlm->name,
158                                      lock->ml.node);
159                         }
160                 } else {
161                         dlm_lock_get(lock);
162                         list_add_tail(&lock->list, &res->blocked);
163                         kick_thread = 1;
164                 }
165         }
166
167         spin_unlock(&res->spinlock);
168         wake_up(&res->wq);
169
170         /* either queue the ast or release it */
171         if (call_ast)
172                 dlm_queue_ast(dlm, lock);
173         else
174                 dlm_lockres_release_ast(dlm, res);
175
176         dlm_lockres_calc_usage(dlm, res);
177         if (kick_thread)
178                 dlm_kick_thread(dlm, res);
179
180         return status;
181 }
182
183 void dlm_revert_pending_lock(struct dlm_lock_resource *res,
184                              struct dlm_lock *lock)
185 {
186         /* remove from local queue if it failed */
187         list_del_init(&lock->list);
188         lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
189 }
190
191
192 /*
193  * locking:
194  *   caller needs:  none
195  *   taken:         takes and drops res->spinlock
196  *   held on exit:  none
197  * returns: DLM_DENIED, DLM_RECOVERING, or net status
198  */
199 static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
200                                       struct dlm_lock_resource *res,
201                                       struct dlm_lock *lock, int flags)
202 {
203         enum dlm_status status = DLM_DENIED;
204         int lockres_changed = 1;
205
206         mlog_entry("type=%d\n", lock->ml.type);
207         mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
208              res->lockname.name, flags);
209
210         spin_lock(&res->spinlock);
211
212         /* will exit this call with spinlock held */
213         __dlm_wait_on_lockres(res);
214         res->state |= DLM_LOCK_RES_IN_PROGRESS;
215
216         /* add lock to local (secondary) queue */
217         dlm_lock_get(lock);
218         list_add_tail(&lock->list, &res->blocked);
219         lock->lock_pending = 1;
220         spin_unlock(&res->spinlock);
221
222         /* spec seems to say that you will get DLM_NORMAL when the lock
223          * has been queued, meaning we need to wait for a reply here. */
224         status = dlm_send_remote_lock_request(dlm, res, lock, flags);
225
226         spin_lock(&res->spinlock);
227         res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
228         lock->lock_pending = 0;
229         if (status != DLM_NORMAL) {
230                 if (status == DLM_RECOVERING &&
231                     dlm_is_recovery_lock(res->lockname.name,
232                                          res->lockname.len)) {
233                         /* recovery lock was mastered by dead node.
234                          * we need to have calc_usage shoot down this
235                          * lockres and completely remaster it. */
236                         mlog(0, "%s: recovery lock was owned by "
237                              "dead node %u, remaster it now.\n",
238                              dlm->name, res->owner);
239                 } else if (status != DLM_NOTQUEUED) {
240                         /*
241                          * DO NOT call calc_usage, as this would unhash
242                          * the remote lockres before we ever get to use
243                          * it.  treat as if we never made any change to
244                          * the lockres.
245                          */
246                         lockres_changed = 0;
247                         dlm_error(status);
248                 }
249                 dlm_revert_pending_lock(res, lock);
250                 dlm_lock_put(lock);
251         } else if (dlm_is_recovery_lock(res->lockname.name, 
252                                         res->lockname.len)) {
253                 /* special case for the $RECOVERY lock.
254                  * there will never be an AST delivered to put
255                  * this lock on the proper secondary queue
256                  * (granted), so do it manually. */
257                 mlog(0, "%s: $RECOVERY lock for this node (%u) is "
258                      "mastered by %u; got lock, manually granting (no ast)\n",
259                      dlm->name, dlm->node_num, res->owner);
260                 list_move_tail(&lock->list, &res->granted);
261         }
262         spin_unlock(&res->spinlock);
263
264         if (lockres_changed)
265                 dlm_lockres_calc_usage(dlm, res);
266
267         wake_up(&res->wq);
268         return status;
269 }
270
271
272 /* for remote lock creation.
273  * locking:
274  *   caller needs:  none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
275  *   taken:         none
276  *   held on exit:  none
277  * returns: DLM_NOLOCKMGR, or net status
278  */
279 static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
280                                                struct dlm_lock_resource *res,
281                                                struct dlm_lock *lock, int flags)
282 {
283         struct dlm_create_lock create;
284         int tmpret, status = 0;
285         enum dlm_status ret;
286
287         mlog_entry_void();
288
289         memset(&create, 0, sizeof(create));
290         create.node_idx = dlm->node_num;
291         create.requested_type = lock->ml.type;
292         create.cookie = lock->ml.cookie;
293         create.namelen = res->lockname.len;
294         create.flags = cpu_to_be32(flags);
295         memcpy(create.name, res->lockname.name, create.namelen);
296
297         tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
298                                     sizeof(create), res->owner, &status);
299         if (tmpret >= 0) {
300                 // successfully sent and received
301                 ret = status;  // this is already a dlm_status
302                 if (ret == DLM_REJECTED) {
303                         mlog(ML_ERROR, "%s:%.*s: BUG.  this is a stale lockres "
304                              "no longer owned by %u.  that node is coming back "
305                              "up currently.\n", dlm->name, create.namelen,
306                              create.name, res->owner);
307                         dlm_print_one_lock_resource(res);
308                         BUG();
309                 }
310         } else {
311                 mlog_errno(tmpret);
312                 if (dlm_is_host_down(tmpret)) {
313                         ret = DLM_RECOVERING;
314                         mlog(0, "node %u died so returning DLM_RECOVERING "
315                              "from lock message!\n", res->owner);
316                 } else {
317                         ret = dlm_err_to_dlm_status(tmpret);
318                 }
319         }
320
321         return ret;
322 }
323
324 void dlm_lock_get(struct dlm_lock *lock)
325 {
326         kref_get(&lock->lock_refs);
327 }
328
329 void dlm_lock_put(struct dlm_lock *lock)
330 {
331         kref_put(&lock->lock_refs, dlm_lock_release);
332 }
333
334 static void dlm_lock_release(struct kref *kref)
335 {
336         struct dlm_lock *lock;
337
338         lock = container_of(kref, struct dlm_lock, lock_refs);
339
340         BUG_ON(!list_empty(&lock->list));
341         BUG_ON(!list_empty(&lock->ast_list));
342         BUG_ON(!list_empty(&lock->bast_list));
343         BUG_ON(lock->ast_pending);
344         BUG_ON(lock->bast_pending);
345
346         dlm_lock_detach_lockres(lock);
347
348         if (lock->lksb_kernel_allocated) {
349                 mlog(0, "freeing kernel-allocated lksb\n");
350                 kfree(lock->lksb);
351         }
352         kfree(lock);
353 }
354
355 /* associate a lock with it's lockres, getting a ref on the lockres */
356 void dlm_lock_attach_lockres(struct dlm_lock *lock,
357                              struct dlm_lock_resource *res)
358 {
359         dlm_lockres_get(res);
360         lock->lockres = res;
361 }
362
363 /* drop ref on lockres, if there is still one associated with lock */
364 static void dlm_lock_detach_lockres(struct dlm_lock *lock)
365 {
366         struct dlm_lock_resource *res;
367
368         res = lock->lockres;
369         if (res) {
370                 lock->lockres = NULL;
371                 mlog(0, "removing lock's lockres reference\n");
372                 dlm_lockres_put(res);
373         }
374 }
375
376 static void dlm_init_lock(struct dlm_lock *newlock, int type,
377                           u8 node, u64 cookie)
378 {
379         INIT_LIST_HEAD(&newlock->list);
380         INIT_LIST_HEAD(&newlock->ast_list);
381         INIT_LIST_HEAD(&newlock->bast_list);
382         spin_lock_init(&newlock->spinlock);
383         newlock->ml.type = type;
384         newlock->ml.convert_type = LKM_IVMODE;
385         newlock->ml.highest_blocked = LKM_IVMODE;
386         newlock->ml.node = node;
387         newlock->ml.pad1 = 0;
388         newlock->ml.list = 0;
389         newlock->ml.flags = 0;
390         newlock->ast = NULL;
391         newlock->bast = NULL;
392         newlock->astdata = NULL;
393         newlock->ml.cookie = cpu_to_be64(cookie);
394         newlock->ast_pending = 0;
395         newlock->bast_pending = 0;
396         newlock->convert_pending = 0;
397         newlock->lock_pending = 0;
398         newlock->unlock_pending = 0;
399         newlock->cancel_pending = 0;
400         newlock->lksb_kernel_allocated = 0;
401
402         kref_init(&newlock->lock_refs);
403 }
404
405 struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
406                                struct dlm_lockstatus *lksb)
407 {
408         struct dlm_lock *lock;
409         int kernel_allocated = 0;
410
411         lock = kcalloc(1, sizeof(*lock), GFP_NOFS);
412         if (!lock)
413                 return NULL;
414
415         if (!lksb) {
416                 /* zero memory only if kernel-allocated */
417                 lksb = kcalloc(1, sizeof(*lksb), GFP_NOFS);
418                 if (!lksb) {
419                         kfree(lock);
420                         return NULL;
421                 }
422                 kernel_allocated = 1;
423         }
424
425         dlm_init_lock(lock, type, node, cookie);
426         if (kernel_allocated)
427                 lock->lksb_kernel_allocated = 1;
428         lock->lksb = lksb;
429         lksb->lockid = lock;
430         return lock;
431 }
432
433 /* handler for lock creation net message
434  * locking:
435  *   caller needs:  none
436  *   taken:         takes and drops res->spinlock
437  *   held on exit:  none
438  * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
439  */
440 int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data)
441 {
442         struct dlm_ctxt *dlm = data;
443         struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
444         struct dlm_lock_resource *res = NULL;
445         struct dlm_lock *newlock = NULL;
446         struct dlm_lockstatus *lksb = NULL;
447         enum dlm_status status = DLM_NORMAL;
448         char *name;
449         unsigned int namelen;
450
451         BUG_ON(!dlm);
452
453         mlog_entry_void();
454
455         if (!dlm_grab(dlm))
456                 return DLM_REJECTED;
457
458         name = create->name;
459         namelen = create->namelen;
460         status = DLM_REJECTED;
461         if (!dlm_domain_fully_joined(dlm)) {
462                 mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
463                      "sending a create_lock message for lock %.*s!\n",
464                      dlm->name, create->node_idx, namelen, name);
465                 dlm_error(status);
466                 goto leave;
467         }
468
469         status = DLM_IVBUFLEN;
470         if (namelen > DLM_LOCKID_NAME_MAX) {
471                 dlm_error(status);
472                 goto leave;
473         }
474
475         status = DLM_SYSERR;
476         newlock = dlm_new_lock(create->requested_type,
477                                create->node_idx,
478                                be64_to_cpu(create->cookie), NULL);
479         if (!newlock) {
480                 dlm_error(status);
481                 goto leave;
482         }
483
484         lksb = newlock->lksb;
485
486         if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
487                 lksb->flags |= DLM_LKSB_GET_LVB;
488                 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
489         }
490
491         status = DLM_IVLOCKID;
492         res = dlm_lookup_lockres(dlm, name, namelen);
493         if (!res) {
494                 dlm_error(status);
495                 goto leave;
496         }
497
498         spin_lock(&res->spinlock);
499         status = __dlm_lockres_state_to_status(res);
500         spin_unlock(&res->spinlock);
501
502         if (status != DLM_NORMAL) {
503                 mlog(0, "lockres recovering/migrating/in-progress\n");
504                 goto leave;
505         }
506
507         dlm_lock_attach_lockres(newlock, res);
508
509         status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
510 leave:
511         if (status != DLM_NORMAL)
512                 if (newlock)
513                         dlm_lock_put(newlock);
514
515         if (res)
516                 dlm_lockres_put(res);
517
518         dlm_put(dlm);
519
520         return status;
521 }
522
523
524 /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
525 static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
526 {
527         u64 tmpnode = node_num;
528
529         /* shift single byte of node num into top 8 bits */
530         tmpnode <<= 56;
531
532         spin_lock(&dlm_cookie_lock);
533         *cookie = (dlm_next_cookie | tmpnode);
534         if (++dlm_next_cookie & 0xff00000000000000ull) {
535                 mlog(0, "This node's cookie will now wrap!\n");
536                 dlm_next_cookie = 1;
537         }
538         spin_unlock(&dlm_cookie_lock);
539 }
540
541 enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
542                         struct dlm_lockstatus *lksb, int flags,
543                         const char *name, dlm_astlockfunc_t *ast, void *data,
544                         dlm_bastlockfunc_t *bast)
545 {
546         enum dlm_status status;
547         struct dlm_lock_resource *res = NULL;
548         struct dlm_lock *lock = NULL;
549         int convert = 0, recovery = 0;
550
551         /* yes this function is a mess.
552          * TODO: clean this up.  lots of common code in the
553          *       lock and convert paths, especially in the retry blocks */
554         if (!lksb) {
555                 dlm_error(DLM_BADARGS);
556                 return DLM_BADARGS;
557         }
558
559         status = DLM_BADPARAM;
560         if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
561                 dlm_error(status);
562                 goto error;
563         }
564
565         if (flags & ~LKM_VALID_FLAGS) {
566                 dlm_error(status);
567                 goto error;
568         }
569
570         convert = (flags & LKM_CONVERT);
571         recovery = (flags & LKM_RECOVERY);
572
573         if (recovery &&
574             (!dlm_is_recovery_lock(name, strlen(name)) || convert) ) {
575                 dlm_error(status);
576                 goto error;
577         }
578         if (convert && (flags & LKM_LOCAL)) {
579                 mlog(ML_ERROR, "strange LOCAL convert request!\n");
580                 goto error;
581         }
582
583         if (convert) {
584                 /* CONVERT request */
585
586                 /* if converting, must pass in a valid dlm_lock */
587                 lock = lksb->lockid;
588                 if (!lock) {
589                         mlog(ML_ERROR, "NULL lock pointer in convert "
590                              "request\n");
591                         goto error;
592                 }
593
594                 res = lock->lockres;
595                 if (!res) {
596                         mlog(ML_ERROR, "NULL lockres pointer in convert "
597                              "request\n");
598                         goto error;
599                 }
600                 dlm_lockres_get(res);
601
602                 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
603                  * static after the original lock call.  convert requests will
604                  * ensure that everything is the same, or return DLM_BADARGS.
605                  * this means that DLM_DENIED_NOASTS will never be returned.
606                  */
607                 if (lock->lksb != lksb || lock->ast != ast ||
608                     lock->bast != bast || lock->astdata != data) {
609                         status = DLM_BADARGS;
610                         mlog(ML_ERROR, "new args:  lksb=%p, ast=%p, bast=%p, "
611                              "astdata=%p\n", lksb, ast, bast, data);
612                         mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
613                              "astdata=%p\n", lock->lksb, lock->ast,
614                              lock->bast, lock->astdata);
615                         goto error;
616                 }
617 retry_convert:
618                 dlm_wait_for_recovery(dlm);
619
620                 if (res->owner == dlm->node_num)
621                         status = dlmconvert_master(dlm, res, lock, flags, mode);
622                 else
623                         status = dlmconvert_remote(dlm, res, lock, flags, mode);
624                 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
625                     status == DLM_FORWARD) {
626                         /* for now, see how this works without sleeping
627                          * and just retry right away.  I suspect the reco
628                          * or migration will complete fast enough that
629                          * no waiting will be necessary */
630                         mlog(0, "retrying convert with migration/recovery/"
631                              "in-progress\n");
632                         msleep(100);
633                         goto retry_convert;
634                 }
635         } else {
636                 u64 tmpcookie;
637
638                 /* LOCK request */
639                 status = DLM_BADARGS;
640                 if (!name) {
641                         dlm_error(status);
642                         goto error;
643                 }
644
645                 status = DLM_IVBUFLEN;
646                 if (strlen(name) > DLM_LOCKID_NAME_MAX || strlen(name) < 1) {
647                         dlm_error(status);
648                         goto error;
649                 }
650
651                 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
652                 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
653                 if (!lock) {
654                         dlm_error(status);
655                         goto error;
656                 }
657
658                 if (!recovery)
659                         dlm_wait_for_recovery(dlm);
660
661                 /* find or create the lock resource */
662                 res = dlm_get_lock_resource(dlm, name, flags);
663                 if (!res) {
664                         status = DLM_IVLOCKID;
665                         dlm_error(status);
666                         goto error;
667                 }
668
669                 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
670                 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
671
672                 dlm_lock_attach_lockres(lock, res);
673                 lock->ast = ast;
674                 lock->bast = bast;
675                 lock->astdata = data;
676
677 retry_lock:
678                 if (flags & LKM_VALBLK) {
679                         mlog(0, "LKM_VALBLK passed by caller\n");
680
681                         /* LVB requests for non PR, PW or EX locks are
682                          * ignored. */
683                         if (mode < LKM_PRMODE)
684                                 flags &= ~LKM_VALBLK;
685                         else {
686                                 flags |= LKM_GET_LVB;
687                                 lock->lksb->flags |= DLM_LKSB_GET_LVB;
688                         }
689                 }
690
691                 if (res->owner == dlm->node_num)
692                         status = dlmlock_master(dlm, res, lock, flags);
693                 else
694                         status = dlmlock_remote(dlm, res, lock, flags);
695
696                 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
697                     status == DLM_FORWARD) {
698                         mlog(0, "retrying lock with migration/"
699                              "recovery/in progress\n");
700                         msleep(100);
701                         /* no waiting for dlm_reco_thread */
702                         if (recovery) {
703                                 if (status != DLM_RECOVERING)
704                                         goto retry_lock;
705
706                                 mlog(0, "%s: got RECOVERING "
707                                      "for $RECOVERY lock, master "
708                                      "was %u\n", dlm->name,
709                                      res->owner);
710                                 /* wait to see the node go down, then
711                                  * drop down and allow the lockres to
712                                  * get cleaned up.  need to remaster. */
713                                 dlm_wait_for_node_death(dlm, res->owner,
714                                                 DLM_NODE_DEATH_WAIT_MAX);
715                         } else {
716                                 dlm_wait_for_recovery(dlm);
717                                 goto retry_lock;
718                         }
719                 }
720
721                 if (status != DLM_NORMAL) {
722                         lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
723                         if (status != DLM_NOTQUEUED)
724                                 dlm_error(status);
725                         goto error;
726                 }
727         }
728
729 error:
730         if (status != DLM_NORMAL) {
731                 if (lock && !convert)
732                         dlm_lock_put(lock);
733                 // this is kind of unnecessary
734                 lksb->status = status;
735         }
736
737         /* put lockres ref from the convert path
738          * or from dlm_get_lock_resource */
739         if (res)
740                 dlm_lockres_put(res);
741
742         return status;
743 }
744 EXPORT_SYMBOL_GPL(dlmlock);