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