Whamcloud - gitweb
300a3dad93540084b739bfbb51717a48a620c458
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25
26 #ifdef __KERNEL__
27 # include <linux/slab.h>
28 # include <linux/dcache.h>
29 # include <linux/namei.h>
30 # include <linux/module.h>
31 # include <linux/lustre_dlm.h>
32 #else
33 # include <liblustre.h>
34 # include <libcfs/kp30.h>
35 #endif
36
37 #include <linux/obd_class.h>
38 #include "ldlm_internal.h"
39
40 //struct lustre_lock ldlm_everything_lock;
41
42 /* lock types */
43 char *ldlm_lockname[] = {
44         [0] "--",
45         [LCK_EX] "EX",
46         [LCK_PW] "PW",
47         [LCK_PR] "PR",
48         [LCK_CW] "CW",
49         [LCK_CR] "CR",
50         [LCK_NL] "NL",
51         [LCK_GROUP] "GROUP"
52 };
53 char *ldlm_typename[] = {
54         [LDLM_PLAIN] "PLN",
55         [LDLM_EXTENT] "EXT",
56         [LDLM_FLOCK] "FLK",
57         [LDLM_IBITS] "IBT",
58 };
59
60 char *ldlm_it2str(int it)
61 {
62         switch (it) {
63         case IT_OPEN:
64                 return "open";
65         case IT_CREAT:
66                 return "creat";
67         case (IT_OPEN | IT_CREAT):
68                 return "open|creat";
69         case IT_READDIR:
70                 return "readdir";
71         case IT_GETATTR:
72                 return "getattr";
73         case IT_LOOKUP:
74                 return "lookup";
75         case IT_UNLINK:
76                 return "unlink";
77         case IT_GETXATTR:
78                 return "getxattr";
79         case IT_CHDIR:
80                 return "chdir";
81         default:
82                 CERROR("Unknown intent %d\n", it);
83                 return "UNKNOWN";
84         }
85 }
86
87 extern kmem_cache_t *ldlm_lock_slab;
88
89 static ldlm_processing_policy ldlm_processing_policy_table[] = {
90         [LDLM_PLAIN] ldlm_process_plain_lock,
91         [LDLM_EXTENT] ldlm_process_extent_lock,
92 #ifdef __KERNEL__
93         [LDLM_FLOCK] ldlm_process_flock_lock,
94 #endif
95         [LDLM_IBITS] ldlm_process_inodebits_lock,
96 };
97
98 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
99 {
100         return ldlm_processing_policy_table[res->lr_type];
101 }
102
103 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
104 {
105         ns->ns_policy = arg;
106 }
107
108 /*
109  * REFCOUNTED LOCK OBJECTS
110  */
111
112
113 /*
114  * Lock refcounts, during creation:
115  *   - one special one for allocation, dec'd only once in destroy
116  *   - one for being a lock that's in-use
117  *   - one for the addref associated with a new lock
118  */
119 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
120 {
121         atomic_inc(&lock->l_refc);
122         return lock;
123 }
124
125 void ldlm_lock_put(struct ldlm_lock *lock)
126 {
127         ENTRY;
128
129         LASSERT(lock->l_resource != LP_POISON);
130         LASSERT(atomic_read(&lock->l_refc) > 0);
131         if (atomic_dec_and_test(&lock->l_refc)) {
132                 struct ldlm_resource *res;
133
134                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
135
136                 lock_res_and_lock(lock);
137                 res = lock->l_resource;
138                 LASSERT(lock->l_destroyed);
139                 LASSERT(list_empty(&lock->l_res_link));
140
141                 if (lock->l_parent)
142                         LDLM_LOCK_PUT(lock->l_parent);
143                 unlock_res_and_lock(lock);
144
145                 atomic_dec(&res->lr_namespace->ns_locks);
146                 ldlm_resource_putref(res);
147                 lock->l_resource = NULL;
148                 if (lock->l_export)
149                         class_export_put(lock->l_export);
150
151                 if (lock->l_lvb_data != NULL)
152                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
153
154                 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
155         }
156
157         EXIT;
158 }
159
160 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
161 {
162         ENTRY;
163         spin_lock(&lock->l_resource->lr_namespace->ns_unused_lock);
164         if (!list_empty(&lock->l_lru)) {
165                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
166                 list_del_init(&lock->l_lru);
167                 lock->l_resource->lr_namespace->ns_nr_unused--;
168                 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
169         }
170         spin_unlock(&lock->l_resource->lr_namespace->ns_unused_lock);
171         EXIT;
172 }
173
174 /* This used to have a 'strict' flact, which recovery would use to mark an
175  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
176  * shall explain why it's gone: with the new hash table scheme, once you call
177  * ldlm_lock_destroy, you can never drop your final references on this lock.
178  * Because it's not in the hash table anymore.  -phil */
179 void ldlm_lock_destroy(struct ldlm_lock *lock)
180 {
181         ENTRY;
182
183         lock_res_and_lock(lock);
184
185         if (!list_empty(&lock->l_children)) {
186                 LDLM_ERROR(lock, "still has children (%p)!",
187                            lock->l_children.next);
188                 ldlm_lock_dump(D_ERROR, lock, 0);
189                 LBUG();
190         }
191         if (lock->l_readers || lock->l_writers) {
192                 LDLM_ERROR(lock, "lock still has references");
193                 ldlm_lock_dump(D_ERROR, lock, 0);
194                 LBUG();
195         }
196
197         if (!list_empty(&lock->l_res_link)) {
198                 LDLM_ERROR(lock, "lock still on resource");
199                 ldlm_lock_dump(D_ERROR, lock, 0);
200                 LBUG();
201         }
202
203         if (lock->l_destroyed) {
204                 LASSERT(list_empty(&lock->l_lru));
205                 unlock_res_and_lock(lock);
206                 EXIT;
207                 return;
208         }
209         lock->l_destroyed = 1;
210
211         if (lock->l_export) {
212                 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
213                 if (!list_empty(&lock->l_export_chain))
214                         list_del_init(&lock->l_export_chain);
215                 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
216         } else {
217                 LASSERT(list_empty(&lock->l_export_chain));
218         }       
219
220         ldlm_lock_remove_from_lru(lock);
221         class_handle_unhash(&lock->l_handle);
222
223 #if 0
224         /* Wake anyone waiting for this lock */
225         /* FIXME: I should probably add yet another flag, instead of using
226          * l_export to only call this on clients */
227         if (lock->l_export)
228                 class_export_put(lock->l_export);
229         lock->l_export = NULL;
230         if (lock->l_export && lock->l_completion_ast)
231                 lock->l_completion_ast(lock, 0);
232 #endif
233
234         unlock_res_and_lock(lock);
235         LDLM_LOCK_PUT(lock);
236         EXIT;
237 }
238
239 /* this is called by portals_handle2object with the handle lock taken */
240 static void lock_handle_addref(void *lock)
241 {
242         LDLM_LOCK_GET((struct ldlm_lock *)lock);
243 }
244
245 /*
246  * usage: pass in a resource on which you have done ldlm_resource_get
247  *        pass in a parent lock on which you have done a ldlm_lock_get
248  *        after return, ldlm_*_put the resource and parent
249  * returns: lock with refcount 2 - one for current caller and one for remote
250  */
251 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
252                                        struct ldlm_resource *resource)
253 {
254         struct ldlm_lock *lock;
255         ENTRY;
256
257         if (resource == NULL)
258                 LBUG();
259
260         OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_NOFS, sizeof(*lock));
261         if (lock == NULL)
262                 RETURN(NULL);
263
264         lock->l_resource = ldlm_resource_getref(resource);
265
266         atomic_set(&lock->l_refc, 2);
267         INIT_LIST_HEAD(&lock->l_children);
268         INIT_LIST_HEAD(&lock->l_res_link);
269         INIT_LIST_HEAD(&lock->l_lru);
270         INIT_LIST_HEAD(&lock->l_export_chain);
271         INIT_LIST_HEAD(&lock->l_pending_chain);
272         INIT_LIST_HEAD(&lock->l_tmp);
273         INIT_LIST_HEAD(&lock->l_bl_ast);
274         INIT_LIST_HEAD(&lock->l_cp_ast);
275         init_waitqueue_head(&lock->l_waitq);
276         lock->l_blocking_lock = NULL;
277         lock->l_pidb = 0;
278
279         atomic_inc(&resource->lr_namespace->ns_locks);
280
281         if (parent != NULL) {
282                 spin_lock(&resource->lr_namespace->ns_hash_lock);
283                 lock->l_parent = LDLM_LOCK_GET(parent);
284                 list_add(&lock->l_childof, &parent->l_children);
285                 spin_unlock(&resource->lr_namespace->ns_hash_lock);
286         }
287
288         INIT_LIST_HEAD(&lock->l_handle.h_link);
289         class_handle_hash(&lock->l_handle, lock_handle_addref);
290
291         RETURN(lock);
292 }
293
294 void unlock_bitlock(struct ldlm_lock *lock);
295
296 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
297                               struct ldlm_res_id new_resid)
298 {
299         struct ldlm_resource *oldres = lock->l_resource;
300         struct ldlm_resource *newres;
301         int type;
302         ENTRY;
303
304         LASSERT(ns->ns_client != 0);
305
306         lock_res_and_lock(lock);
307         if (memcmp(&new_resid, &lock->l_resource->lr_name,
308                    sizeof(lock->l_resource->lr_name)) == 0) {
309                 /* Nothing to do */
310                 unlock_res_and_lock(lock);
311                 RETURN(0);
312         }
313
314         LASSERT(new_resid.name[0] != 0);
315
316         /* This function assumes that the lock isn't on any lists */
317         LASSERT(list_empty(&lock->l_res_link));
318
319         type = oldres->lr_type;
320         newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
321         if (newres == NULL) {
322                 LBUG();
323                 RETURN(-ENOMEM);
324         }
325
326         lock_res(newres);
327         lock->l_resource = newres;
328         unlock_res(newres);
329         unlock_res(oldres);
330         unlock_bitlock(lock);
331
332         /* ...and the flowers are still standing! */
333         ldlm_resource_putref(oldres);
334
335         RETURN(0);
336 }
337
338 /*
339  *  HANDLES
340  */
341
342 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
343 {
344         lockh->cookie = lock->l_handle.h_cookie;
345 }
346
347 /* if flags: atomically get the lock and set the flags.
348  *           Return NULL if flag already set
349  */
350
351 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
352 {
353         struct ldlm_namespace *ns;
354         struct ldlm_lock *lock = NULL, *retval = NULL;
355         ENTRY;
356
357         LASSERT(handle);
358
359         lock = class_handle2object(handle->cookie);
360         if (lock == NULL)
361                 RETURN(NULL);
362
363         LASSERT(lock->l_resource != NULL);
364         ns = lock->l_resource->lr_namespace;
365         LASSERT(ns != NULL);
366
367         lock_res_and_lock(lock);
368
369         /* It's unlikely but possible that someone marked the lock as
370          * destroyed after we did handle2object on it */
371         if (lock->l_destroyed) {
372                 unlock_res_and_lock(lock);
373                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
374                 LDLM_LOCK_PUT(lock);
375                 GOTO(out, retval);
376         }
377
378         if (flags && (lock->l_flags & flags)) {
379                 unlock_res_and_lock(lock);
380                 LDLM_LOCK_PUT(lock);
381                 GOTO(out, retval);
382         }
383
384         if (flags)
385                 lock->l_flags |= flags;
386
387         unlock_res_and_lock(lock);
388         retval = lock;
389         EXIT;
390  out:
391         return retval;
392 }
393
394 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
395                                       struct lustre_handle *handle)
396 {
397         struct ldlm_lock *retval = NULL;
398         retval = __ldlm_handle2lock(handle, 0);
399         return retval;
400 }
401
402 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
403 {
404         ldlm_res2desc(lock->l_resource, &desc->l_resource);
405         desc->l_req_mode = lock->l_req_mode;
406         desc->l_granted_mode = lock->l_granted_mode;
407         memcpy(&desc->l_policy_data, &lock->l_policy_data,
408                sizeof(desc->l_policy_data));
409 }
410
411 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
412                            struct list_head *work_list)
413 {
414         if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
415                 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
416                 lock->l_flags |= LDLM_FL_AST_SENT;
417                 /* If the enqueuing client said so, tell the AST recipient to
418                  * discard dirty data, rather than writing back. */
419                 if (new->l_flags & LDLM_AST_DISCARD_DATA)
420                         lock->l_flags |= LDLM_FL_DISCARD_DATA;
421                 LASSERT(list_empty(&lock->l_bl_ast));
422                 list_add(&lock->l_bl_ast, work_list);
423                 LDLM_LOCK_GET(lock);
424                 LASSERT(lock->l_blocking_lock == NULL);
425                 lock->l_blocking_lock = LDLM_LOCK_GET(new);
426         }
427 }
428
429 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
430 {
431         if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
432                 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
433                 lock->l_flags |= LDLM_FL_CP_REQD;
434                 LASSERT(list_empty(&lock->l_cp_ast));
435                 list_add(&lock->l_cp_ast, work_list);
436                 LDLM_LOCK_GET(lock);
437         }
438 }
439
440 /* must be called with lr_lock held */
441 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
442                                 struct list_head *work_list)
443 {
444         ENTRY;
445         check_res_locked(lock->l_resource);
446         if (new)
447                 ldlm_add_bl_work_item(lock, new, work_list);
448         else 
449                 ldlm_add_cp_work_item(lock, work_list);
450         EXIT;
451 }
452
453 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
454 {
455         struct ldlm_lock *lock;
456
457         lock = ldlm_handle2lock(lockh);
458         ldlm_lock_addref_internal(lock, mode);
459         LDLM_LOCK_PUT(lock);
460 }
461
462 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
463 {
464         ldlm_lock_remove_from_lru(lock);
465         if (mode & (LCK_NL | LCK_CR | LCK_PR))
466                 lock->l_readers++;
467         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
468                 lock->l_writers++;
469         lock->l_last_used = jiffies;
470         LDLM_LOCK_GET(lock);
471         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
472 }
473
474 /* only called for local locks */
475 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
476 {
477         lock_res_and_lock(lock);
478         ldlm_lock_addref_internal_nolock(lock, mode);
479         unlock_res_and_lock(lock);
480 }
481
482 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
483 {
484         struct ldlm_namespace *ns;
485         ENTRY;
486
487         lock_res_and_lock(lock);
488
489         ns = lock->l_resource->lr_namespace;
490
491         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
492         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
493                 LASSERT(lock->l_readers > 0);
494                 lock->l_readers--;
495         }
496         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
497                 LASSERT(lock->l_writers > 0);
498                 lock->l_writers--;
499         }
500
501         if (lock->l_flags & LDLM_FL_LOCAL &&
502             !lock->l_readers && !lock->l_writers) {
503                 /* If this is a local lock on a server namespace and this was
504                  * the last reference, cancel the lock. */
505                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
506                 lock->l_flags |= LDLM_FL_CBPENDING;
507         }
508
509         if (!lock->l_readers && !lock->l_writers &&
510             (lock->l_flags & LDLM_FL_CBPENDING)) {
511                 /* If we received a blocked AST and this was the last reference,
512                  * run the callback. */
513                 if (ns->ns_client == LDLM_NAMESPACE_SERVER && lock->l_export)
514                         CERROR("FL_CBPENDING set on non-local lock--just a "
515                                "warning\n");
516
517                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
518
519                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
520                 ldlm_lock_remove_from_lru(lock);
521                 unlock_res_and_lock(lock);
522                 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
523                                 ldlm_bl_to_thread(ns, NULL, lock) != 0)
524                         ldlm_handle_bl_callback(ns, NULL, lock);
525         } else if (ns->ns_client == LDLM_NAMESPACE_CLIENT &&
526                    !lock->l_readers && !lock->l_writers) {
527                 /* If this is a client-side namespace and this was the last
528                  * reference, put it on the LRU. */
529                 LASSERT(list_empty(&lock->l_lru));
530                 LASSERT(ns->ns_nr_unused >= 0);
531                 spin_lock(&ns->ns_unused_lock);
532                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
533                 ns->ns_nr_unused++;
534                 spin_unlock(&ns->ns_unused_lock);
535                 unlock_res_and_lock(lock);
536                 ldlm_cancel_lru(ns, LDLM_ASYNC);
537         } else {
538                 unlock_res_and_lock(lock);
539         }
540
541         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
542
543         EXIT;
544 }
545
546 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
547 {
548         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
549         LASSERTF(lock != NULL, "can't find lockh "LPX64"\n", lockh->cookie);
550         ldlm_lock_decref_internal(lock, mode);
551         LDLM_LOCK_PUT(lock);
552 }
553
554 /* This will drop a lock reference and mark it for destruction, but will not
555  * necessarily cancel the lock before returning. */
556 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
557 {
558         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
559         ENTRY;
560
561         LASSERT(lock != NULL);
562
563         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
564         lock_res_and_lock(lock);
565         lock->l_flags |= LDLM_FL_CBPENDING;
566         unlock_res_and_lock(lock);
567         ldlm_lock_decref_internal(lock, mode);
568         LDLM_LOCK_PUT(lock);
569 }
570
571 /* NOTE: called by
572  *  - ldlm_lock_enqueue
573  *  - ldlm_reprocess_queue
574  *  - ldlm_lock_convert
575  *
576  * must be called with lr_lock held
577  */
578 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
579 {
580         struct ldlm_resource *res = lock->l_resource;
581         ENTRY;
582
583         check_res_locked(res);
584
585         lock->l_granted_mode = lock->l_req_mode;
586         ldlm_resource_add_lock(res, &res->lr_granted, lock);
587
588         if (lock->l_granted_mode < res->lr_most_restr)
589                 res->lr_most_restr = lock->l_granted_mode;
590
591         if (work_list && lock->l_completion_ast != NULL)
592                 ldlm_add_ast_work_item(lock, NULL, work_list);
593
594         EXIT;
595 }
596
597 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
598  * comment above ldlm_lock_match */
599 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
600                                       ldlm_policy_data_t *policy,
601                                       struct ldlm_lock *old_lock, int flags)
602 {
603         struct ldlm_lock *lock;
604         struct list_head *tmp;
605
606         list_for_each(tmp, queue) {
607                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
608
609                 if (lock == old_lock)
610                         break;
611
612                 /* llite sometimes wants to match locks that will be
613                  * canceled when their users drop, but we allow it to match
614                  * if it passes in CBPENDING and the lock still has users.
615                  * this is generally only going to be used by children 
616                  * whose parents already hold a lock so forward progress
617                  * can still happen. */
618                 if (lock->l_flags & LDLM_FL_CBPENDING &&
619                     !(flags & LDLM_FL_CBPENDING))
620                         continue;
621                 if (lock->l_flags & LDLM_FL_CBPENDING &&
622                     lock->l_readers == 0 && lock->l_writers == 0)
623                         continue;
624
625                 if (!(lock->l_req_mode & mode))
626                         continue;
627
628                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
629                     (lock->l_policy_data.l_extent.start >
630                      policy->l_extent.start ||
631                      lock->l_policy_data.l_extent.end < policy->l_extent.end))
632                         continue;
633
634                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
635                     mode == LCK_GROUP &&
636                     lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
637                         continue;
638
639                 /* We match if we have existing lock with same or wider set
640                    of bits. */
641                 if (lock->l_resource->lr_type == LDLM_IBITS &&
642                      ((lock->l_policy_data.l_inodebits.bits &
643                       policy->l_inodebits.bits) !=
644                       policy->l_inodebits.bits))
645                         continue;
646
647                 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
648                         continue;
649
650                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
651                     !(lock->l_flags & LDLM_FL_LOCAL))
652                         continue;
653
654                 if (flags & LDLM_FL_TEST_LOCK)
655                         LDLM_LOCK_GET(lock);
656                 else
657                         ldlm_lock_addref_internal_nolock(lock, mode);
658                 return lock;
659         }
660
661         return NULL;
662 }
663
664 void ldlm_lock_allow_match(struct ldlm_lock *lock)
665 {
666         lock_res_and_lock(lock);
667         lock->l_flags |= LDLM_FL_CAN_MATCH;
668         wake_up(&lock->l_waitq);
669         unlock_res_and_lock(lock);
670 }
671
672 /* Can be called in two ways:
673  *
674  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
675  * for a duplicate of.
676  *
677  * Otherwise, all of the fields must be filled in, to match against.
678  *
679  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
680  *     server (ie, connh is NULL)
681  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
682  *     list will be considered
683  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
684  *     to be canceled can still be matched as long as they still have reader
685  *     or writer refernces
686  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
687  *     just tell us if we would have matched.
688  *
689  * Returns 1 if it finds an already-existing lock that is compatible; in this
690  * case, lockh is filled in with a addref()ed lock
691  */
692 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
693                     struct ldlm_res_id *res_id, __u32 type,
694                     ldlm_policy_data_t *policy, ldlm_mode_t mode,
695                     struct lustre_handle *lockh)
696 {
697         struct ldlm_resource *res;
698         struct ldlm_lock *lock, *old_lock = NULL;
699         int rc = 0;
700         ENTRY;
701
702         if (ns == NULL) {
703                 old_lock = ldlm_handle2lock(lockh);
704                 LASSERT(old_lock);
705
706                 ns = old_lock->l_resource->lr_namespace;
707                 res_id = &old_lock->l_resource->lr_name;
708                 type = old_lock->l_resource->lr_type;
709                 mode = old_lock->l_req_mode;
710         }
711
712         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
713         if (res == NULL) {
714                 LASSERT(old_lock == NULL);
715                 RETURN(0);
716         }
717
718         lock_res(res);
719
720         lock = search_queue(&res->lr_granted, mode, policy, old_lock, flags);
721         if (lock != NULL)
722                 GOTO(out, rc = 1);
723         if (flags & LDLM_FL_BLOCK_GRANTED)
724                 GOTO(out, rc = 0);
725         lock = search_queue(&res->lr_converting, mode, policy, old_lock, flags);
726         if (lock != NULL)
727                 GOTO(out, rc = 1);
728         lock = search_queue(&res->lr_waiting, mode, policy, old_lock, flags);
729         if (lock != NULL)
730                 GOTO(out, rc = 1);
731
732         EXIT;
733  out:
734         unlock_res(res);
735         ldlm_resource_putref(res);
736
737         if (lock) {
738                 ldlm_lock2handle(lock, lockh);
739                 if (!(lock->l_flags & LDLM_FL_CAN_MATCH)) {
740                         struct l_wait_info lwi;
741                         if (lock->l_completion_ast) {
742                                 int err = lock->l_completion_ast(lock,
743                                                                  LDLM_FL_WAIT_NOREPROC,
744                                                                  NULL);
745                                 if (err) {
746                                         rc = 0;
747                                         goto out2;
748                                 }
749                         }
750
751                         lwi = LWI_TIMEOUT_INTR(obd_timeout*HZ, NULL,NULL,NULL);
752
753                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
754                         l_wait_event(lock->l_waitq,
755                                      (lock->l_flags & LDLM_FL_CAN_MATCH), &lwi);
756                 }
757         }
758
759 out2:
760         if (rc) {
761                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
762                            type == LDLM_PLAIN ? res_id->name[2] :
763                                 policy->l_extent.start,
764                            type == LDLM_PLAIN ? res_id->name[3] :
765                            policy->l_extent.end);
766         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/* less verbose for test-only */
767                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
768                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
769                                   type, mode, res_id->name[0], res_id->name[1],
770                                   type == LDLM_PLAIN ? res_id->name[2] :
771                                   policy->l_extent.start,
772                                   type == LDLM_PLAIN ? res_id->name[3] :
773                                   policy->l_extent.end);
774         }
775
776         if (old_lock)
777                 LDLM_LOCK_PUT(old_lock);
778         if (flags & LDLM_FL_TEST_LOCK && rc)
779                 LDLM_LOCK_PUT(lock);
780
781         return rc;
782 }
783
784 /* Returns a referenced lock */
785 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
786                                    struct lustre_handle *parent_lock_handle,
787                                    struct ldlm_res_id res_id, __u32 type,
788                                    ldlm_mode_t mode,
789                                    ldlm_blocking_callback blocking,
790                                    ldlm_completion_callback completion,
791                                    ldlm_glimpse_callback glimpse,
792                                    void *data, __u32 lvb_len)
793 {
794         struct ldlm_resource *res, *parent_res = NULL;
795         struct ldlm_lock *lock, *parent_lock = NULL;
796         ENTRY;
797
798         if (parent_lock_handle) {
799                 parent_lock = ldlm_handle2lock(parent_lock_handle);
800                 if (parent_lock)
801                         parent_res = parent_lock->l_resource;
802         }
803
804         res = ldlm_resource_get(ns, parent_res, res_id,
805                                 type, 1);
806         if (res == NULL)
807                 RETURN(NULL);
808
809         lock = ldlm_lock_new(parent_lock, res);
810         ldlm_resource_putref(res);
811         if (parent_lock != NULL)
812                 LDLM_LOCK_PUT(parent_lock);
813
814         if (lock == NULL)
815                 RETURN(NULL);
816
817         lock->l_req_mode = mode;
818         lock->l_ast_data = data;
819         lock->l_blocking_ast = blocking;
820         lock->l_completion_ast = completion;
821         lock->l_glimpse_ast = glimpse;
822         lock->l_pid = current->pid;
823
824         if (lvb_len) {
825                 lock->l_lvb_len = lvb_len;
826                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
827                 if (lock->l_lvb_data == NULL) {
828                         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
829                         RETURN(NULL);
830                 }
831         }
832
833         RETURN(lock);
834 }
835
836 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
837                                struct ldlm_lock **lockp,
838                                void *cookie, int *flags)
839 {
840         struct ldlm_lock *lock = *lockp;
841         struct ldlm_resource *res = lock->l_resource;
842         int local = res->lr_namespace->ns_client;
843         ldlm_processing_policy policy;
844         ldlm_error_t rc = ELDLM_OK;
845         ENTRY;
846
847         /* policies are not executed on the client or during replay */
848         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
849             && !local && ns->ns_policy) {
850                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
851                                    NULL);
852                 if (rc == ELDLM_LOCK_REPLACED) {
853                         /* The lock that was returned has already been granted,
854                          * and placed into lockp.  If it's not the same as the
855                          * one we passed in, then destroy the old one and our
856                          * work here is done. */
857                         if (lock != *lockp) {
858                                 ldlm_lock_destroy(lock);
859                                 LDLM_LOCK_PUT(lock);
860                         }
861                         *flags |= LDLM_FL_LOCK_CHANGED;
862                         RETURN(0);
863                 } else if (rc == ELDLM_LOCK_ABORTED ||
864                            (rc == 0 && (*flags & LDLM_FL_INTENT_ONLY))) {
865                         ldlm_lock_destroy(lock);
866                         RETURN(rc);
867                 }
868                 LASSERT(rc == ELDLM_OK);
869         }
870
871         lock_res_and_lock(lock);
872         if (local && lock->l_req_mode == lock->l_granted_mode) {
873                 /* The server returned a blocked lock, but it was granted before
874                  * we got a chance to actually enqueue it.  We don't need to do
875                  * anything else. */
876                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
877                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
878                 GOTO(out, ELDLM_OK);
879         }
880
881         /* Some flags from the enqueue want to make it into the AST, via the
882          * lock's l_flags. */
883         lock->l_flags |= (*flags & LDLM_AST_DISCARD_DATA);
884
885         /* This distinction between local lock trees is very important; a client
886          * namespace only has information about locks taken by that client, and
887          * thus doesn't have enough information to decide for itself if it can
888          * be granted (below).  In this case, we do exactly what the server
889          * tells us to do, as dictated by the 'flags'.
890          *
891          * We do exactly the same thing during recovery, when the server is
892          * more or less trusting the clients not to lie.
893          *
894          * FIXME (bug 268): Detect obvious lies by checking compatibility in
895          * granted/converting queues. */
896         ldlm_resource_unlink_lock(lock);
897         if (local) {
898                 if (*flags & LDLM_FL_BLOCK_CONV)
899                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
900                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
901                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
902                 else
903                         ldlm_grant_lock(lock, NULL);
904                 GOTO(out, ELDLM_OK);
905         } else if (*flags & LDLM_FL_REPLAY) {
906                 if (*flags & LDLM_FL_BLOCK_CONV) {
907                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
908                         GOTO(out, ELDLM_OK);
909                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
910                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
911                         GOTO(out, ELDLM_OK);
912                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
913                         ldlm_grant_lock(lock, NULL);
914                         GOTO(out, ELDLM_OK);
915                 }
916                 /* If no flags, fall through to normal enqueue path. */
917         }
918
919         policy = ldlm_processing_policy_table[res->lr_type];
920         policy(lock, flags, 1, &rc, NULL);
921         EXIT;
922 out:
923         unlock_res_and_lock(lock);
924         return rc;
925 }
926
927 /* Must be called with namespace taken: queue is waiting or converting. */
928 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
929                          struct list_head *work_list)
930 {
931         struct list_head *tmp, *pos;
932         ldlm_processing_policy policy;
933         int flags;
934         int rc = LDLM_ITER_CONTINUE;
935         ldlm_error_t err;
936         ENTRY;
937
938         check_res_locked(res);
939
940         policy = ldlm_processing_policy_table[res->lr_type];
941         LASSERT(policy);
942
943         list_for_each_safe(tmp, pos, queue) {
944                 struct ldlm_lock *pending;
945                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
946
947                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
948
949                 flags = 0;
950                 rc = policy(pending, &flags, 0, &err, work_list);
951                 if (rc != LDLM_ITER_CONTINUE)
952                         break;
953         }
954
955         RETURN(rc);
956 }
957
958 int ldlm_run_bl_ast_work(struct list_head *rpc_list)
959 {
960         struct list_head *tmp, *pos;
961         struct ldlm_lock_desc d;
962         int rc = 0, retval = 0;
963         ENTRY;
964
965         list_for_each_safe(tmp, pos, rpc_list) {
966                 struct ldlm_lock *lock =
967                         list_entry(tmp, struct ldlm_lock, l_bl_ast);
968
969                 /* nobody should touch l_bl_ast */
970                 lock_res_and_lock(lock);
971                 list_del_init(&lock->l_bl_ast);
972
973                 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
974                 LASSERT(lock->l_bl_ast_run == 0);
975                 LASSERT(lock->l_blocking_lock);
976                 lock->l_bl_ast_run++;
977                 unlock_res_and_lock(lock);
978
979                 ldlm_lock2desc(lock->l_blocking_lock, &d);
980
981                 LDLM_LOCK_PUT(lock->l_blocking_lock);
982                 lock->l_blocking_lock = NULL;
983                 rc = lock->l_blocking_ast(lock, &d, NULL, LDLM_CB_BLOCKING);
984
985                 if (rc == -ERESTART)
986                         retval = rc;
987                 else if (rc)
988                         CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
989                                "disconnect client\n");
990                 LDLM_LOCK_PUT(lock);
991         }
992         RETURN(retval);
993 }
994
995 int ldlm_run_cp_ast_work(struct list_head *rpc_list)
996 {
997         struct list_head *tmp, *pos;
998         int rc = 0, retval = 0;
999         ENTRY;
1000
1001         /* It's possible to receive a completion AST before we've set
1002          * the l_completion_ast pointer: either because the AST arrived
1003          * before the reply, or simply because there's a small race
1004          * window between receiving the reply and finishing the local
1005          * enqueue. (bug 842)
1006          *
1007          * This can't happen with the blocking_ast, however, because we
1008          * will never call the local blocking_ast until we drop our
1009          * reader/writer reference, which we won't do until we get the
1010          * reply and finish enqueueing. */
1011
1012         list_for_each_safe(tmp, pos, rpc_list) {
1013                 struct ldlm_lock *lock =
1014                         list_entry(tmp, struct ldlm_lock, l_cp_ast);
1015
1016                 /* nobody should touch l_cp_ast */
1017                 lock_res_and_lock(lock);
1018                 list_del_init(&lock->l_cp_ast);
1019                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1020                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1021                 unlock_res_and_lock(lock);
1022
1023                 if (lock->l_completion_ast != NULL)
1024                         rc = lock->l_completion_ast(lock, 0, 0);
1025
1026                 if (rc == -ERESTART)
1027                         retval = rc;
1028                 else if (rc)
1029                         CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
1030                                "disconnect client\n");
1031                 LDLM_LOCK_PUT(lock);
1032         }
1033         RETURN(retval);
1034 }
1035
1036 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1037 {
1038         ldlm_reprocess_all(res);
1039         return LDLM_ITER_CONTINUE;
1040 }
1041
1042 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1043 {
1044         struct list_head *tmp;
1045         int i, rc;
1046
1047         spin_lock(&ns->ns_hash_lock);
1048         for (i = 0; i < RES_HASH_SIZE; i++) {
1049                 tmp = ns->ns_hash[i].next;
1050                 while (tmp != &(ns->ns_hash[i])) {
1051                         struct ldlm_resource *res =
1052                                 list_entry(tmp, struct ldlm_resource, lr_hash);
1053
1054                         ldlm_resource_getref(res);
1055                         spin_unlock(&ns->ns_hash_lock);
1056
1057                         rc = reprocess_one_queue(res, NULL);
1058
1059                         spin_lock(&ns->ns_hash_lock);
1060                         tmp = tmp->next;
1061                         ldlm_resource_putref_locked(res);
1062
1063                         if (rc == LDLM_ITER_STOP)
1064                                 GOTO(out, rc);
1065                 }
1066         }
1067  out:
1068         spin_unlock(&ns->ns_hash_lock);
1069         EXIT;
1070 }
1071
1072 void ldlm_reprocess_all(struct ldlm_resource *res)
1073 {
1074         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1075         int rc;
1076         ENTRY;
1077
1078         /* Local lock trees don't get reprocessed. */
1079         if (res->lr_namespace->ns_client) {
1080                 EXIT;
1081                 return;
1082         }
1083
1084  restart:
1085         lock_res(res);
1086         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1087         if (rc == LDLM_ITER_CONTINUE)
1088                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1089         unlock_res(res);
1090
1091         rc = ldlm_run_cp_ast_work(&rpc_list);
1092         if (rc == -ERESTART) {
1093                 LASSERT(list_empty(&rpc_list));
1094                 goto restart;
1095         }
1096         EXIT;
1097 }
1098
1099 void ldlm_cancel_callback(struct ldlm_lock *lock)
1100 {
1101         check_res_locked(lock->l_resource);
1102
1103         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1104                 lock->l_flags |= LDLM_FL_CANCEL;
1105                 if (lock->l_blocking_ast) {
1106                         unlock_res_and_lock(lock);
1107                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1108                                              LDLM_CB_CANCELING);
1109                         lock_res_and_lock(lock);
1110                 } else {
1111                         LDLM_DEBUG(lock, "no blocking ast");
1112                 }
1113         }
1114 }
1115
1116 void ldlm_lock_cancel(struct ldlm_lock *lock)
1117 {
1118         struct ldlm_resource *res;
1119         struct ldlm_namespace *ns;
1120         ENTRY;
1121
1122         ldlm_del_waiting_lock(lock);
1123         lock_res_and_lock(lock);
1124         
1125         res = lock->l_resource;
1126         ns = res->lr_namespace;
1127
1128         /* Please do not, no matter how tempting, remove this LBUG without
1129          * talking to me first. -phik */
1130         if (lock->l_readers || lock->l_writers) {
1131                 LDLM_ERROR(lock, "lock still has references");
1132                 LBUG();
1133         }
1134
1135         ldlm_cancel_callback(lock);
1136
1137         ldlm_resource_unlink_lock(lock);
1138         unlock_res_and_lock(lock);
1139         
1140         ldlm_lock_destroy(lock);
1141
1142         EXIT;
1143 }
1144
1145 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1146 {
1147         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1148         ENTRY;
1149
1150         if (lock == NULL)
1151                 RETURN(-EINVAL);
1152
1153         lock->l_ast_data = data;
1154         LDLM_LOCK_PUT(lock);
1155         RETURN(0);
1156 }
1157
1158 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1159 {
1160         struct ldlm_lock *lock;
1161         struct ldlm_resource *res;
1162
1163         spin_lock(&exp->exp_ldlm_data.led_lock);
1164         while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) { 
1165                 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1166                                   struct ldlm_lock, l_export_chain);
1167                 res = ldlm_resource_getref(lock->l_resource);
1168                 LDLM_LOCK_GET(lock);
1169                 spin_unlock(&exp->exp_ldlm_data.led_lock);
1170
1171                 LDLM_DEBUG(lock, "export %p", exp);
1172                 ldlm_lock_cancel(lock);
1173                 ldlm_reprocess_all(res);
1174
1175                 ldlm_resource_putref(res);
1176                 LDLM_LOCK_PUT(lock);
1177                 spin_lock(&exp->exp_ldlm_data.led_lock);
1178         }
1179         spin_unlock(&exp->exp_ldlm_data.led_lock);
1180 }
1181
1182 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1183                                         int *flags)
1184 {
1185         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1186         struct ldlm_resource *res;
1187         struct ldlm_namespace *ns;
1188         int granted = 0;
1189         int old_mode, rc;
1190         ldlm_error_t err;
1191         ENTRY;
1192
1193         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1194                 *flags |= LDLM_FL_BLOCK_GRANTED;
1195                 RETURN(lock->l_resource);
1196         }
1197
1198         LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1199                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1200
1201         lock_res_and_lock(lock);
1202
1203         res = lock->l_resource;
1204         ns = res->lr_namespace;
1205
1206         old_mode = lock->l_req_mode;
1207         lock->l_req_mode = new_mode;
1208         ldlm_resource_unlink_lock(lock);
1209
1210         /* If this is a local resource, put it on the appropriate list. */
1211         if (res->lr_namespace->ns_client) {
1212                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1213                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1214                 } else {
1215                         /* This should never happen, because of the way the
1216                          * server handles conversions. */
1217                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1218                                    *flags);
1219                         LBUG();
1220
1221                         ldlm_grant_lock(lock, &rpc_list);
1222                         granted = 1;
1223                         /* FIXME: completion handling not with ns_lock held ! */
1224                         if (lock->l_completion_ast)
1225                                 lock->l_completion_ast(lock, 0, NULL);
1226                 }
1227         } else {
1228                 int pflags = 0;
1229                 ldlm_processing_policy policy;
1230                 policy = ldlm_processing_policy_table[res->lr_type];
1231                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1232                 if (rc == LDLM_ITER_STOP) {
1233                         lock->l_req_mode = old_mode;
1234                         ldlm_resource_add_lock(res, &res->lr_granted, lock);
1235                         res = NULL;
1236                 } else {
1237                         *flags |= LDLM_FL_BLOCK_GRANTED;
1238                         granted = 1;
1239                 }
1240         }
1241         unlock_res_and_lock(lock);
1242
1243         if (granted)
1244                 ldlm_run_cp_ast_work(&rpc_list);
1245         RETURN(res);
1246 }
1247
1248 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1249 {
1250         char str[PTL_NALFMT_SIZE];
1251         struct obd_device *obd = NULL;
1252
1253         if (!((portal_debug | D_ERROR) & level))
1254                 return;
1255
1256         if (!lock) {
1257                 CDEBUG(level, "  NULL LDLM lock\n");
1258                 return;
1259         }
1260
1261         CDEBUG(level, "  -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1262                lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1263                pos, lock->l_pid);
1264         if (lock->l_conn_export != NULL)
1265                 obd = lock->l_conn_export->exp_obd;
1266         if (lock->l_export && lock->l_export->exp_connection) {
1267                 CDEBUG(level, "  Node: NID %s on %s (rhandle: "LPX64")\n",
1268                        ptlrpc_peernid2str(&lock->l_export->exp_connection->c_peer, str),
1269                        lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1270                        lock->l_remote_handle.cookie);
1271         } else if (obd == NULL) {
1272                 CDEBUG(level, "  Node: local\n");
1273         } else {
1274                 struct obd_import *imp = obd->u.cli.cl_import;
1275                 CDEBUG(level, "  Node: NID %s on %s (rhandle: "LPX64")\n",
1276                        ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
1277                        imp->imp_connection->c_peer.peer_ni->pni_name,
1278                        lock->l_remote_handle.cookie);
1279         }
1280         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1281                lock->l_resource->lr_name.name[0],
1282                lock->l_resource->lr_name.name[1]);
1283         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1284                "write: %d\n", ldlm_lockname[lock->l_req_mode],
1285                ldlm_lockname[lock->l_granted_mode],
1286                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers);
1287         if (lock->l_resource->lr_type == LDLM_EXTENT)
1288                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1289                        " (req "LPU64"-"LPU64")\n",
1290                        lock->l_policy_data.l_extent.start,
1291                        lock->l_policy_data.l_extent.end,
1292                        lock->l_req_extent.start, lock->l_req_extent.end);
1293         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1294                 CDEBUG(level, "  Pid: "LPU64" Extent: "LPU64" -> "LPU64"\n",
1295                        lock->l_policy_data.l_flock.pid,
1296                        lock->l_policy_data.l_flock.start,
1297                        lock->l_policy_data.l_flock.end);
1298         else if (lock->l_resource->lr_type == LDLM_IBITS)
1299                 CDEBUG(level, " Bits: "LPX64"\n",
1300                        lock->l_policy_data.l_inodebits.bits);
1301 }
1302
1303 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1304 {
1305         struct ldlm_lock *lock;
1306
1307         lock = ldlm_handle2lock(lockh);
1308         if (lock == NULL)
1309                 return;
1310
1311         ldlm_lock_dump(D_OTHER, lock, 0);
1312
1313         LDLM_LOCK_PUT(lock);
1314 }