Whamcloud - gitweb
b_release_1_4_6-patchless is now using lustre-build from HEAD. Happy hacking.
[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 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
295                               struct ldlm_res_id new_resid)
296 {
297         struct ldlm_resource *oldres = lock->l_resource;
298         struct ldlm_resource *newres;
299         int type;
300         ENTRY;
301
302         LASSERT(ns->ns_client != 0);
303
304         lock_res_and_lock(lock);
305         if (memcmp(&new_resid, &lock->l_resource->lr_name,
306                    sizeof(lock->l_resource->lr_name)) == 0) {
307                 /* Nothing to do */
308                 unlock_res_and_lock(lock);
309                 RETURN(0);
310         }
311
312         LASSERT(new_resid.name[0] != 0);
313
314         /* This function assumes that the lock isn't on any lists */
315         LASSERT(list_empty(&lock->l_res_link));
316
317         type = oldres->lr_type;
318         newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
319         if (newres == NULL) {
320                 LBUG();
321                 RETURN(-ENOMEM);
322         }
323
324         lock_res(newres);
325         lock->l_resource = newres;
326         unlock_res(newres);
327         unlock_res(oldres);
328         unlock_bitlock(lock);
329
330         /* ...and the flowers are still standing! */
331         ldlm_resource_putref(oldres);
332
333         RETURN(0);
334 }
335
336 /*
337  *  HANDLES
338  */
339
340 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
341 {
342         lockh->cookie = lock->l_handle.h_cookie;
343 }
344
345 /* if flags: atomically get the lock and set the flags.
346  *           Return NULL if flag already set
347  */
348
349 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
350 {
351         struct ldlm_namespace *ns;
352         struct ldlm_lock *lock = NULL, *retval = NULL;
353         ENTRY;
354
355         LASSERT(handle);
356
357         lock = class_handle2object(handle->cookie);
358         if (lock == NULL)
359                 RETURN(NULL);
360
361         LASSERT(lock->l_resource != NULL);
362         ns = lock->l_resource->lr_namespace;
363         LASSERT(ns != NULL);
364
365         lock_res_and_lock(lock);
366
367         /* It's unlikely but possible that someone marked the lock as
368          * destroyed after we did handle2object on it */
369         if (lock->l_destroyed) {
370                 unlock_res_and_lock(lock);
371                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
372                 LDLM_LOCK_PUT(lock);
373                 GOTO(out, retval);
374         }
375
376         if (flags && (lock->l_flags & flags)) {
377                 unlock_res_and_lock(lock);
378                 LDLM_LOCK_PUT(lock);
379                 GOTO(out, retval);
380         }
381
382         if (flags)
383                 lock->l_flags |= flags;
384
385         unlock_res_and_lock(lock);
386         retval = lock;
387         EXIT;
388  out:
389         return retval;
390 }
391
392 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
393                                       struct lustre_handle *handle)
394 {
395         struct ldlm_lock *retval = NULL;
396         retval = __ldlm_handle2lock(handle, 0);
397         return retval;
398 }
399
400 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
401 {
402         ldlm_res2desc(lock->l_resource, &desc->l_resource);
403         desc->l_req_mode = lock->l_req_mode;
404         desc->l_granted_mode = lock->l_granted_mode;
405         memcpy(&desc->l_policy_data, &lock->l_policy_data,
406                sizeof(desc->l_policy_data));
407 }
408
409 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
410                            struct list_head *work_list)
411 {
412         if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
413                 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
414                 lock->l_flags |= LDLM_FL_AST_SENT;
415                 /* If the enqueuing client said so, tell the AST recipient to
416                  * discard dirty data, rather than writing back. */
417                 if (new->l_flags & LDLM_AST_DISCARD_DATA)
418                         lock->l_flags |= LDLM_FL_DISCARD_DATA;
419                 LASSERT(list_empty(&lock->l_bl_ast));
420                 list_add(&lock->l_bl_ast, work_list);
421                 LDLM_LOCK_GET(lock);
422                 LASSERT(lock->l_blocking_lock == NULL);
423                 lock->l_blocking_lock = LDLM_LOCK_GET(new);
424         }
425 }
426
427 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
428 {
429         if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
430                 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
431                 lock->l_flags |= LDLM_FL_CP_REQD;
432                 LASSERT(list_empty(&lock->l_cp_ast));
433                 list_add(&lock->l_cp_ast, work_list);
434                 LDLM_LOCK_GET(lock);
435         }
436 }
437
438 /* must be called with lr_lock held */
439 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
440                                 struct list_head *work_list)
441 {
442         ENTRY;
443         check_res_locked(lock->l_resource);
444         if (new)
445                 ldlm_add_bl_work_item(lock, new, work_list);
446         else 
447                 ldlm_add_cp_work_item(lock, work_list);
448         EXIT;
449 }
450
451 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
452 {
453         struct ldlm_lock *lock;
454
455         lock = ldlm_handle2lock(lockh);
456         ldlm_lock_addref_internal(lock, mode);
457         LDLM_LOCK_PUT(lock);
458 }
459
460 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
461 {
462         ldlm_lock_remove_from_lru(lock);
463         if (mode & (LCK_NL | LCK_CR | LCK_PR))
464                 lock->l_readers++;
465         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
466                 lock->l_writers++;
467         lock->l_last_used = jiffies;
468         LDLM_LOCK_GET(lock);
469         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
470 }
471
472 /* only called for local locks */
473 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
474 {
475         lock_res_and_lock(lock);
476         ldlm_lock_addref_internal_nolock(lock, mode);
477         unlock_res_and_lock(lock);
478 }
479
480 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
481 {
482         struct ldlm_namespace *ns;
483         ENTRY;
484
485         lock_res_and_lock(lock);
486
487         ns = lock->l_resource->lr_namespace;
488
489         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
490         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
491                 LASSERT(lock->l_readers > 0);
492                 lock->l_readers--;
493         }
494         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
495                 LASSERT(lock->l_writers > 0);
496                 lock->l_writers--;
497         }
498
499         if (((lock->l_flags & LDLM_FL_LOCAL) || (mode == LCK_GROUP)) &&
500             !lock->l_readers && !lock->l_writers) {
501                 /* If this is a local lock on a server namespace or a group
502                  * lock and this was the last reference, cancel the lock. */
503                 CDEBUG(D_INFO, "forcing cancel of local or group lock\n");
504                 lock->l_flags |= LDLM_FL_CBPENDING;
505         }
506
507         if (!lock->l_readers && !lock->l_writers &&
508             (lock->l_flags & LDLM_FL_CBPENDING)) {
509                 /* If we received a blocked AST and this was the last reference,
510                  * run the callback. */
511                 if (ns->ns_client == LDLM_NAMESPACE_SERVER && lock->l_export)
512                         CERROR("FL_CBPENDING set on non-local lock--just a "
513                                "warning\n");
514
515                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
516
517                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
518                 ldlm_lock_remove_from_lru(lock);
519                 unlock_res_and_lock(lock);
520                 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
521                                 ldlm_bl_to_thread(ns, NULL, lock) != 0)
522                         ldlm_handle_bl_callback(ns, NULL, lock);
523         } else if (ns->ns_client == LDLM_NAMESPACE_CLIENT &&
524                    !lock->l_readers && !lock->l_writers) {
525                 /* If this is a client-side namespace and this was the last
526                  * reference, put it on the LRU. */
527                 LASSERT(list_empty(&lock->l_lru));
528                 LASSERT(ns->ns_nr_unused >= 0);
529                 spin_lock(&ns->ns_unused_lock);
530                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
531                 ns->ns_nr_unused++;
532                 spin_unlock(&ns->ns_unused_lock);
533                 unlock_res_and_lock(lock);
534                 ldlm_cancel_lru(ns, LDLM_ASYNC);
535         } else {
536                 unlock_res_and_lock(lock);
537         }
538
539         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
540
541         EXIT;
542 }
543
544 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
545 {
546         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
547         LASSERTF(lock != NULL, "can't find lockh "LPX64"\n", lockh->cookie);
548         ldlm_lock_decref_internal(lock, mode);
549         LDLM_LOCK_PUT(lock);
550 }
551
552 /* This will drop a lock reference and mark it for destruction, but will not
553  * necessarily cancel the lock before returning. */
554 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
555 {
556         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
557         ENTRY;
558
559         LASSERT(lock != NULL);
560
561         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
562         lock_res_and_lock(lock);
563         lock->l_flags |= LDLM_FL_CBPENDING;
564         unlock_res_and_lock(lock);
565         ldlm_lock_decref_internal(lock, mode);
566         LDLM_LOCK_PUT(lock);
567 }
568
569 /* NOTE: called by
570  *  - ldlm_lock_enqueue
571  *  - ldlm_reprocess_queue
572  *  - ldlm_lock_convert
573  *
574  * must be called with lr_lock held
575  */
576 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
577 {
578         struct ldlm_resource *res = lock->l_resource;
579         ENTRY;
580
581         check_res_locked(res);
582
583         lock->l_granted_mode = lock->l_req_mode;
584         ldlm_resource_add_lock(res, &res->lr_granted, lock);
585
586         if (lock->l_granted_mode < res->lr_most_restr)
587                 res->lr_most_restr = lock->l_granted_mode;
588
589         if (work_list && lock->l_completion_ast != NULL)
590                 ldlm_add_ast_work_item(lock, NULL, work_list);
591
592         EXIT;
593 }
594
595 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
596  * comment above ldlm_lock_match */
597 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
598                                       ldlm_policy_data_t *policy,
599                                       struct ldlm_lock *old_lock, int flags)
600 {
601         struct ldlm_lock *lock;
602         struct list_head *tmp;
603
604         list_for_each(tmp, queue) {
605                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
606
607                 if (lock == old_lock)
608                         break;
609
610                 /* llite sometimes wants to match locks that will be
611                  * canceled when their users drop, but we allow it to match
612                  * if it passes in CBPENDING and the lock still has users.
613                  * this is generally only going to be used by children 
614                  * whose parents already hold a lock so forward progress
615                  * can still happen. */
616                 if (lock->l_flags & LDLM_FL_CBPENDING &&
617                     !(flags & LDLM_FL_CBPENDING))
618                         continue;
619                 if (lock->l_flags & LDLM_FL_CBPENDING &&
620                     lock->l_readers == 0 && lock->l_writers == 0)
621                         continue;
622
623                 if (!(lock->l_req_mode & mode))
624                         continue;
625
626                 if (lock->l_resource->lr_type == LDLM_EXTENT) {
627                         if (mode == LCK_GROUP) {
628                                 if (lock->l_policy_data.l_extent.gid !=
629                                     policy->l_extent.gid)
630                                         continue;
631                         } else {
632                                 if ((lock->l_policy_data.l_extent.start >
633                                      policy->l_extent.start) ||
634                                     (lock->l_policy_data.l_extent.end <
635                                      policy->l_extent.end))
636                                         continue;
637                         }
638                 }
639
640                 /* We match if we have existing lock with same or wider set
641                    of bits. */
642                 if (lock->l_resource->lr_type == LDLM_IBITS &&
643                      ((lock->l_policy_data.l_inodebits.bits &
644                       policy->l_inodebits.bits) !=
645                       policy->l_inodebits.bits))
646                         continue;
647
648                 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
649                         continue;
650
651                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
652                     !(lock->l_flags & LDLM_FL_LOCAL))
653                         continue;
654
655                 if (flags & LDLM_FL_TEST_LOCK)
656                         LDLM_LOCK_GET(lock);
657                 else
658                         ldlm_lock_addref_internal_nolock(lock, mode);
659                 return lock;
660         }
661
662         return NULL;
663 }
664
665 void ldlm_lock_allow_match(struct ldlm_lock *lock)
666 {
667         lock_res_and_lock(lock);
668         lock->l_flags |= LDLM_FL_CAN_MATCH;
669         wake_up(&lock->l_waitq);
670         unlock_res_and_lock(lock);
671 }
672
673 /* Can be called in two ways:
674  *
675  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
676  * for a duplicate of.
677  *
678  * Otherwise, all of the fields must be filled in, to match against.
679  *
680  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
681  *     server (ie, connh is NULL)
682  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
683  *     list will be considered
684  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
685  *     to be canceled can still be matched as long as they still have reader
686  *     or writer refernces
687  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
688  *     just tell us if we would have matched.
689  *
690  * Returns 1 if it finds an already-existing lock that is compatible; in this
691  * case, lockh is filled in with a addref()ed lock
692  */
693 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
694                     struct ldlm_res_id *res_id, __u32 type,
695                     ldlm_policy_data_t *policy, ldlm_mode_t mode,
696                     struct lustre_handle *lockh)
697 {
698         struct ldlm_resource *res;
699         struct ldlm_lock *lock, *old_lock = NULL;
700         int rc = 0;
701         ENTRY;
702
703         if (ns == NULL) {
704                 old_lock = ldlm_handle2lock(lockh);
705                 LASSERT(old_lock);
706
707                 ns = old_lock->l_resource->lr_namespace;
708                 res_id = &old_lock->l_resource->lr_name;
709                 type = old_lock->l_resource->lr_type;
710                 mode = old_lock->l_req_mode;
711         }
712
713         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
714         if (res == NULL) {
715                 LASSERT(old_lock == NULL);
716                 RETURN(0);
717         }
718
719         lock_res(res);
720
721         lock = search_queue(&res->lr_granted, mode, policy, old_lock, flags);
722         if (lock != NULL)
723                 GOTO(out, rc = 1);
724         if (flags & LDLM_FL_BLOCK_GRANTED)
725                 GOTO(out, rc = 0);
726         lock = search_queue(&res->lr_converting, mode, policy, old_lock, flags);
727         if (lock != NULL)
728                 GOTO(out, rc = 1);
729         lock = search_queue(&res->lr_waiting, mode, policy, old_lock, flags);
730         if (lock != NULL)
731                 GOTO(out, rc = 1);
732
733         EXIT;
734  out:
735         unlock_res(res);
736         ldlm_resource_putref(res);
737
738         if (lock) {
739                 ldlm_lock2handle(lock, lockh);
740                 if (!(lock->l_flags & LDLM_FL_CAN_MATCH)) {
741                         struct l_wait_info lwi;
742                         if (lock->l_completion_ast) {
743                                 int err = lock->l_completion_ast(lock,
744                                                                  LDLM_FL_WAIT_NOREPROC,
745                                                                  NULL);
746                                 if (err) {
747                                         rc = 0;
748                                         goto out2;
749                                 }
750                         }
751
752                         lwi = LWI_TIMEOUT_INTR(obd_timeout*HZ, NULL,NULL,NULL);
753
754                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
755                         l_wait_event(lock->l_waitq,
756                                      (lock->l_flags & LDLM_FL_CAN_MATCH), &lwi);
757                 }
758         }
759
760 out2:
761         if (rc) {
762                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
763                            type == LDLM_PLAIN ? res_id->name[2] :
764                                 policy->l_extent.start,
765                            type == LDLM_PLAIN ? res_id->name[3] :
766                            policy->l_extent.end);
767         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/* less verbose for test-only */
768                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
769                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
770                                   type, mode, res_id->name[0], res_id->name[1],
771                                   type == LDLM_PLAIN ? res_id->name[2] :
772                                   policy->l_extent.start,
773                                   type == LDLM_PLAIN ? res_id->name[3] :
774                                   policy->l_extent.end);
775         }
776
777         if (old_lock)
778                 LDLM_LOCK_PUT(old_lock);
779         if (flags & LDLM_FL_TEST_LOCK && rc)
780                 LDLM_LOCK_PUT(lock);
781
782         return rc;
783 }
784
785 /* Returns a referenced lock */
786 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
787                                    struct lustre_handle *parent_lock_handle,
788                                    struct ldlm_res_id res_id, __u32 type,
789                                    ldlm_mode_t mode,
790                                    ldlm_blocking_callback blocking,
791                                    ldlm_completion_callback completion,
792                                    ldlm_glimpse_callback glimpse,
793                                    void *data, __u32 lvb_len)
794 {
795         struct ldlm_resource *res, *parent_res = NULL;
796         struct ldlm_lock *lock, *parent_lock = NULL;
797         ENTRY;
798
799         if (parent_lock_handle) {
800                 parent_lock = ldlm_handle2lock(parent_lock_handle);
801                 if (parent_lock)
802                         parent_res = parent_lock->l_resource;
803         }
804
805         res = ldlm_resource_get(ns, parent_res, res_id,
806                                 type, 1);
807         if (res == NULL)
808                 RETURN(NULL);
809
810         lock = ldlm_lock_new(parent_lock, res);
811         ldlm_resource_putref(res);
812         if (parent_lock != NULL)
813                 LDLM_LOCK_PUT(parent_lock);
814
815         if (lock == NULL)
816                 RETURN(NULL);
817
818         lock->l_req_mode = mode;
819         lock->l_ast_data = data;
820         lock->l_blocking_ast = blocking;
821         lock->l_completion_ast = completion;
822         lock->l_glimpse_ast = glimpse;
823         lock->l_pid = current->pid;
824
825         if (lvb_len) {
826                 lock->l_lvb_len = lvb_len;
827                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
828                 if (lock->l_lvb_data == NULL) {
829                         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
830                         RETURN(NULL);
831                 }
832         }
833
834         RETURN(lock);
835 }
836
837 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
838                                struct ldlm_lock **lockp,
839                                void *cookie, int *flags)
840 {
841         struct ldlm_lock *lock = *lockp;
842         struct ldlm_resource *res = lock->l_resource;
843         int local = res->lr_namespace->ns_client;
844         ldlm_processing_policy policy;
845         ldlm_error_t rc = ELDLM_OK;
846         ENTRY;
847
848         /* policies are not executed on the client or during replay */
849         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
850             && !local && ns->ns_policy) {
851                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
852                                    NULL);
853                 if (rc == ELDLM_LOCK_REPLACED) {
854                         /* The lock that was returned has already been granted,
855                          * and placed into lockp.  If it's not the same as the
856                          * one we passed in, then destroy the old one and our
857                          * work here is done. */
858                         if (lock != *lockp) {
859                                 ldlm_lock_destroy(lock);
860                                 LDLM_LOCK_PUT(lock);
861                         }
862                         *flags |= LDLM_FL_LOCK_CHANGED;
863                         RETURN(0);
864                 } else if (rc == ELDLM_LOCK_ABORTED ||
865                            (rc == 0 && (*flags & LDLM_FL_INTENT_ONLY))) {
866                         ldlm_lock_destroy(lock);
867                         RETURN(rc);
868                 }
869                 LASSERT(rc == ELDLM_OK);
870         }
871
872         lock_res_and_lock(lock);
873         if (local && lock->l_req_mode == lock->l_granted_mode) {
874                 /* The server returned a blocked lock, but it was granted before
875                  * we got a chance to actually enqueue it.  We don't need to do
876                  * anything else. */
877                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
878                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
879                 GOTO(out, ELDLM_OK);
880         }
881
882         /* Some flags from the enqueue want to make it into the AST, via the
883          * lock's l_flags. */
884         lock->l_flags |= (*flags & LDLM_AST_DISCARD_DATA);
885
886         /* This distinction between local lock trees is very important; a client
887          * namespace only has information about locks taken by that client, and
888          * thus doesn't have enough information to decide for itself if it can
889          * be granted (below).  In this case, we do exactly what the server
890          * tells us to do, as dictated by the 'flags'.
891          *
892          * We do exactly the same thing during recovery, when the server is
893          * more or less trusting the clients not to lie.
894          *
895          * FIXME (bug 268): Detect obvious lies by checking compatibility in
896          * granted/converting queues. */
897         ldlm_resource_unlink_lock(lock);
898         if (local) {
899                 if (*flags & LDLM_FL_BLOCK_CONV)
900                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
901                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
902                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
903                 else
904                         ldlm_grant_lock(lock, NULL);
905                 GOTO(out, ELDLM_OK);
906         } else if (*flags & LDLM_FL_REPLAY) {
907                 if (*flags & LDLM_FL_BLOCK_CONV) {
908                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
909                         GOTO(out, ELDLM_OK);
910                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
911                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
912                         GOTO(out, ELDLM_OK);
913                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
914                         ldlm_grant_lock(lock, NULL);
915                         GOTO(out, ELDLM_OK);
916                 }
917                 /* If no flags, fall through to normal enqueue path. */
918         }
919
920         policy = ldlm_processing_policy_table[res->lr_type];
921         policy(lock, flags, 1, &rc, NULL);
922         EXIT;
923 out:
924         unlock_res_and_lock(lock);
925         return rc;
926 }
927
928 /* Must be called with namespace taken: queue is waiting or converting. */
929 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
930                          struct list_head *work_list)
931 {
932         struct list_head *tmp, *pos;
933         ldlm_processing_policy policy;
934         int flags;
935         int rc = LDLM_ITER_CONTINUE;
936         ldlm_error_t err;
937         ENTRY;
938
939         check_res_locked(res);
940
941         policy = ldlm_processing_policy_table[res->lr_type];
942         LASSERT(policy);
943
944         list_for_each_safe(tmp, pos, queue) {
945                 struct ldlm_lock *pending;
946                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
947
948                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
949
950                 flags = 0;
951                 rc = policy(pending, &flags, 0, &err, work_list);
952                 if (rc != LDLM_ITER_CONTINUE)
953                         break;
954         }
955
956         RETURN(rc);
957 }
958
959 int ldlm_run_bl_ast_work(struct list_head *rpc_list)
960 {
961         struct list_head *tmp, *pos;
962         struct ldlm_lock_desc d;
963         int rc = 0, retval = 0;
964         ENTRY;
965
966         list_for_each_safe(tmp, pos, rpc_list) {
967                 struct ldlm_lock *lock =
968                         list_entry(tmp, struct ldlm_lock, l_bl_ast);
969
970                 /* nobody should touch l_bl_ast */
971                 lock_res_and_lock(lock);
972                 list_del_init(&lock->l_bl_ast);
973
974                 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
975                 LASSERT(lock->l_bl_ast_run == 0);
976                 LASSERT(lock->l_blocking_lock);
977                 lock->l_bl_ast_run++;
978                 unlock_res_and_lock(lock);
979
980                 ldlm_lock2desc(lock->l_blocking_lock, &d);
981
982                 LDLM_LOCK_PUT(lock->l_blocking_lock);
983                 lock->l_blocking_lock = NULL;
984                 rc = lock->l_blocking_ast(lock, &d, NULL, LDLM_CB_BLOCKING);
985
986                 if (rc == -ERESTART)
987                         retval = rc;
988                 else if (rc)
989                         CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
990                                "disconnect client\n");
991                 LDLM_LOCK_PUT(lock);
992         }
993         RETURN(retval);
994 }
995
996 int ldlm_run_cp_ast_work(struct list_head *rpc_list)
997 {
998         struct list_head *tmp, *pos;
999         int rc = 0, retval = 0;
1000         ENTRY;
1001
1002         /* It's possible to receive a completion AST before we've set
1003          * the l_completion_ast pointer: either because the AST arrived
1004          * before the reply, or simply because there's a small race
1005          * window between receiving the reply and finishing the local
1006          * enqueue. (bug 842)
1007          *
1008          * This can't happen with the blocking_ast, however, because we
1009          * will never call the local blocking_ast until we drop our
1010          * reader/writer reference, which we won't do until we get the
1011          * reply and finish enqueueing. */
1012         
1013         list_for_each_safe(tmp, pos, rpc_list) {
1014                 struct ldlm_lock *lock =
1015                         list_entry(tmp, struct ldlm_lock, l_cp_ast);
1016
1017                 /* nobody should touch l_cp_ast */
1018                 lock_res_and_lock(lock);
1019                 list_del_init(&lock->l_cp_ast);
1020                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1021                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1022                 unlock_res_and_lock(lock);
1023
1024                 if (lock->l_completion_ast != NULL)
1025                         rc = lock->l_completion_ast(lock, 0, 0);
1026
1027                 if (rc == -ERESTART)
1028                         retval = rc;
1029                 else if (rc)
1030                         CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
1031                                "disconnect client\n");
1032                 LDLM_LOCK_PUT(lock);
1033         }
1034         RETURN(retval);
1035 }
1036
1037 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1038 {
1039         ldlm_reprocess_all(res);
1040         return LDLM_ITER_CONTINUE;
1041 }
1042
1043 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1044 {
1045         struct list_head *tmp;
1046         int i, rc;
1047
1048         spin_lock(&ns->ns_hash_lock);
1049         for (i = 0; i < RES_HASH_SIZE; i++) {
1050                 tmp = ns->ns_hash[i].next;
1051                 while (tmp != &(ns->ns_hash[i])) {
1052                         struct ldlm_resource *res =
1053                                 list_entry(tmp, struct ldlm_resource, lr_hash);
1054
1055                         ldlm_resource_getref(res);
1056                         spin_unlock(&ns->ns_hash_lock);
1057
1058                         rc = reprocess_one_queue(res, NULL);
1059
1060                         spin_lock(&ns->ns_hash_lock);
1061                         tmp = tmp->next;
1062                         ldlm_resource_putref_locked(res);
1063
1064                         if (rc == LDLM_ITER_STOP)
1065                                 GOTO(out, rc);
1066                 }
1067         }
1068  out:
1069         spin_unlock(&ns->ns_hash_lock);
1070         EXIT;
1071 }
1072
1073 void ldlm_reprocess_all(struct ldlm_resource *res)
1074 {
1075         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1076         int rc;
1077         ENTRY;
1078
1079         /* Local lock trees don't get reprocessed. */
1080         if (res->lr_namespace->ns_client) {
1081                 EXIT;
1082                 return;
1083         }
1084
1085  restart:
1086         lock_res(res);
1087         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1088         if (rc == LDLM_ITER_CONTINUE)
1089                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1090         unlock_res(res);
1091
1092         rc = ldlm_run_cp_ast_work(&rpc_list);
1093         if (rc == -ERESTART) {
1094                 LASSERT(list_empty(&rpc_list));
1095                 goto restart;
1096         }
1097         EXIT;
1098 }
1099
1100 void ldlm_cancel_callback(struct ldlm_lock *lock)
1101 {
1102         check_res_locked(lock->l_resource);
1103
1104         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1105                 lock->l_flags |= LDLM_FL_CANCEL;
1106                 if (lock->l_blocking_ast) {
1107                         unlock_res_and_lock(lock);
1108                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1109                                              LDLM_CB_CANCELING);
1110                         lock_res_and_lock(lock);
1111                 } else {
1112                         LDLM_DEBUG(lock, "no blocking ast");
1113                 }
1114         }
1115 }
1116
1117 void ldlm_lock_cancel(struct ldlm_lock *lock)
1118 {
1119         struct ldlm_resource *res;
1120         struct ldlm_namespace *ns;
1121         ENTRY;
1122
1123         ldlm_del_waiting_lock(lock);
1124         lock_res_and_lock(lock);
1125         
1126         res = lock->l_resource;
1127         ns = res->lr_namespace;
1128
1129         /* Please do not, no matter how tempting, remove this LBUG without
1130          * talking to me first. -phik */
1131         if (lock->l_readers || lock->l_writers) {
1132                 LDLM_ERROR(lock, "lock still has references");
1133                 LBUG();
1134         }
1135
1136         ldlm_cancel_callback(lock);
1137
1138         ldlm_resource_unlink_lock(lock);
1139         unlock_res_and_lock(lock);
1140         
1141         ldlm_lock_destroy(lock);
1142
1143         EXIT;
1144 }
1145
1146 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1147 {
1148         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1149         ENTRY;
1150
1151         if (lock == NULL)
1152                 RETURN(-EINVAL);
1153
1154         lock->l_ast_data = data;
1155         LDLM_LOCK_PUT(lock);
1156         RETURN(0);
1157 }
1158
1159 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1160 {
1161         struct ldlm_lock *lock;
1162         struct ldlm_resource *res;
1163
1164         spin_lock(&exp->exp_ldlm_data.led_lock);
1165         while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) { 
1166                 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1167                                   struct ldlm_lock, l_export_chain);
1168                 res = ldlm_resource_getref(lock->l_resource);
1169                 LDLM_LOCK_GET(lock);
1170                 spin_unlock(&exp->exp_ldlm_data.led_lock);
1171
1172                 LDLM_DEBUG(lock, "export %p", exp);
1173                 ldlm_lock_cancel(lock);
1174                 ldlm_reprocess_all(res);
1175
1176                 ldlm_resource_putref(res);
1177                 LDLM_LOCK_PUT(lock);
1178                 spin_lock(&exp->exp_ldlm_data.led_lock);
1179         }
1180         spin_unlock(&exp->exp_ldlm_data.led_lock);
1181 }
1182
1183 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1184                                         int *flags)
1185 {
1186         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1187         struct ldlm_resource *res;
1188         struct ldlm_namespace *ns;
1189         int granted = 0;
1190         int old_mode, rc;
1191         ldlm_error_t err;
1192         ENTRY;
1193
1194         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1195                 *flags |= LDLM_FL_BLOCK_GRANTED;
1196                 RETURN(lock->l_resource);
1197         }
1198
1199         LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1200                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1201
1202         lock_res_and_lock(lock);
1203
1204         res = lock->l_resource;
1205         ns = res->lr_namespace;
1206
1207         old_mode = lock->l_req_mode;
1208         lock->l_req_mode = new_mode;
1209         ldlm_resource_unlink_lock(lock);
1210
1211         /* If this is a local resource, put it on the appropriate list. */
1212         if (res->lr_namespace->ns_client) {
1213                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1214                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1215                 } else {
1216                         /* This should never happen, because of the way the
1217                          * server handles conversions. */
1218                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1219                                    *flags);
1220                         LBUG();
1221
1222                         ldlm_grant_lock(lock, &rpc_list);
1223                         granted = 1;
1224                         /* FIXME: completion handling not with ns_lock held ! */
1225                         if (lock->l_completion_ast)
1226                                 lock->l_completion_ast(lock, 0, NULL);
1227                 }
1228         } else {
1229                 int pflags = 0;
1230                 ldlm_processing_policy policy;
1231                 policy = ldlm_processing_policy_table[res->lr_type];
1232                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1233                 if (rc == LDLM_ITER_STOP) {
1234                         lock->l_req_mode = old_mode;
1235                         ldlm_resource_add_lock(res, &res->lr_granted, lock);
1236                         res = NULL;
1237                 } else {
1238                         *flags |= LDLM_FL_BLOCK_GRANTED;
1239                         granted = 1;
1240                 }
1241         }
1242         unlock_res_and_lock(lock);
1243
1244         if (granted)
1245                 ldlm_run_cp_ast_work(&rpc_list);
1246         RETURN(res);
1247 }
1248
1249 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1250 {
1251         char str[PTL_NALFMT_SIZE];
1252         struct obd_device *obd = NULL;
1253
1254         if (!((portal_debug | D_ERROR) & level))
1255                 return;
1256
1257         if (!lock) {
1258                 CDEBUG(level, "  NULL LDLM lock\n");
1259                 return;
1260         }
1261
1262         CDEBUG(level, "  -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1263                lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1264                pos, lock->l_pid);
1265         if (lock->l_conn_export != NULL)
1266                 obd = lock->l_conn_export->exp_obd;
1267         if (lock->l_export && lock->l_export->exp_connection) {
1268                 CDEBUG(level, "  Node: NID %s on %s (rhandle: "LPX64")\n",
1269                        ptlrpc_peernid2str(&lock->l_export->exp_connection->c_peer, str),
1270                        lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1271                        lock->l_remote_handle.cookie);
1272         } else if (obd == NULL) {
1273                 CDEBUG(level, "  Node: local\n");
1274         } else {
1275                 struct obd_import *imp = obd->u.cli.cl_import;
1276                 CDEBUG(level, "  Node: NID %s on %s (rhandle: "LPX64")\n",
1277                        ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
1278                        imp->imp_connection->c_peer.peer_ni->pni_name,
1279                        lock->l_remote_handle.cookie);
1280         }
1281         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1282                lock->l_resource->lr_name.name[0],
1283                lock->l_resource->lr_name.name[1]);
1284         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1285                "write: %d\n", ldlm_lockname[lock->l_req_mode],
1286                ldlm_lockname[lock->l_granted_mode],
1287                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers);
1288         if (lock->l_resource->lr_type == LDLM_EXTENT)
1289                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64" gid: "LPU64
1290                        " (req "LPU64"-"LPU64" gid: "LPU64")\n",
1291                        lock->l_policy_data.l_extent.start,
1292                        lock->l_policy_data.l_extent.end,
1293                        lock->l_policy_data.l_extent.gid,
1294                        lock->l_req_extent.start, lock->l_req_extent.end,
1295                        lock->l_req_extent.gid);
1296         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1297                 CDEBUG(level, "  Pid: "LPU64" Extent: "LPU64" -> "LPU64"\n",
1298                        lock->l_policy_data.l_flock.pid,
1299                        lock->l_policy_data.l_flock.start,
1300                        lock->l_policy_data.l_flock.end);
1301         else if (lock->l_resource->lr_type == LDLM_IBITS)
1302                 CDEBUG(level, " Bits: "LPX64"\n",
1303                        lock->l_policy_data.l_inodebits.bits);
1304 }
1305
1306 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1307 {
1308         struct ldlm_lock *lock;
1309
1310         lock = ldlm_handle2lock(lockh);
1311         if (lock == NULL)
1312                 return;
1313
1314         ldlm_lock_dump(D_OTHER, lock, 0);
1315
1316         LDLM_LOCK_PUT(lock);
1317 }