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