Whamcloud - gitweb
5678333ebd0bddfdb3f11babade0aa390b912926
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_lock.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LDLM
43
44 #ifdef __KERNEL__
45 # include <libcfs/libcfs.h>
46 # ifndef HAVE_VFS_INTENT_PATCHES
47 # include <linux/lustre_intent.h>
48 # endif
49 #else
50 # include <liblustre.h>
51 #endif
52
53 #include <obd_class.h>
54 #include "ldlm_internal.h"
55
56 /* lock types */
57 char *ldlm_lockname[] = {
58         [0] "--",
59         [LCK_EX] "EX",
60         [LCK_PW] "PW",
61         [LCK_PR] "PR",
62         [LCK_CW] "CW",
63         [LCK_CR] "CR",
64         [LCK_NL] "NL",
65         [LCK_GROUP] "GROUP"
66 };
67
68 char *ldlm_typename[] = {
69         [LDLM_PLAIN] "PLN",
70         [LDLM_EXTENT] "EXT",
71         [LDLM_FLOCK] "FLK",
72         [LDLM_IBITS] "IBT",
73 };
74
75 char *ldlm_it2str(int it)
76 {
77         switch (it) {
78         case IT_OPEN:
79                 return "open";
80         case IT_CREAT:
81                 return "creat";
82         case (IT_OPEN | IT_CREAT):
83                 return "open|creat";
84         case IT_READDIR:
85                 return "readdir";
86         case IT_GETATTR:
87                 return "getattr";
88         case IT_LOOKUP:
89                 return "lookup";
90         case IT_UNLINK:
91                 return "unlink";
92         case IT_GETXATTR:
93                 return "getxattr";
94         default:
95                 CERROR("Unknown intent %d\n", it);
96                 return "UNKNOWN";
97         }
98 }
99
100 extern cfs_mem_cache_t *ldlm_lock_slab;
101
102 static ldlm_processing_policy ldlm_processing_policy_table[] = {
103         [LDLM_PLAIN] ldlm_process_plain_lock,
104         [LDLM_EXTENT] ldlm_process_extent_lock,
105 #ifdef __KERNEL__
106         [LDLM_FLOCK] ldlm_process_flock_lock,
107 #endif
108         [LDLM_IBITS] ldlm_process_inodebits_lock,
109 };
110
111 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
112 {
113         return ldlm_processing_policy_table[res->lr_type];
114 }
115
116 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
117 {
118         ns->ns_policy = arg;
119 }
120
121 /*
122  * REFCOUNTED LOCK OBJECTS
123  */
124
125
126 /*
127  * Lock refcounts, during creation:
128  *   - one special one for allocation, dec'd only once in destroy
129  *   - one for being a lock that's in-use
130  *   - one for the addref associated with a new lock
131  */
132 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
133 {
134         atomic_inc(&lock->l_refc);
135         return lock;
136 }
137
138 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
139 {
140         LASSERT(size == sizeof(*lock));
141         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
142 }
143
144 void ldlm_lock_put(struct ldlm_lock *lock)
145 {
146         ENTRY;
147
148         LASSERT(lock->l_resource != LP_POISON);
149         LASSERT(atomic_read(&lock->l_refc) > 0);
150         if (atomic_dec_and_test(&lock->l_refc)) {
151                 struct ldlm_resource *res;
152
153                 LDLM_DEBUG(lock,
154                            "final lock_put on destroyed lock, freeing it.");
155
156                 res = lock->l_resource;
157                 LASSERT(lock->l_destroyed);
158                 LASSERT(list_empty(&lock->l_res_link));
159                 LASSERT(list_empty(&lock->l_pending_chain));
160
161                 atomic_dec(&res->lr_namespace->ns_locks);
162                 ldlm_resource_putref(res);
163                 lock->l_resource = NULL;
164                 if (lock->l_export) {
165                         class_export_put(lock->l_export);
166                         lock->l_export = NULL;
167                 }
168
169                 if (lock->l_lvb_data != NULL)
170                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
171
172                 ldlm_interval_free(ldlm_interval_detach(lock));
173                 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle, 
174                                 ldlm_lock_free);
175         }
176
177         EXIT;
178 }
179
180 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
181 {
182         int rc = 0;
183         if (!list_empty(&lock->l_lru)) {
184                 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
185                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
186                 list_del_init(&lock->l_lru);
187                 ns->ns_nr_unused--;
188                 LASSERT(ns->ns_nr_unused >= 0);
189                 rc = 1;
190         }
191         return rc;
192 }
193
194 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
195 {
196         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
197         int rc;
198         ENTRY;
199         spin_lock(&ns->ns_unused_lock);
200         rc = ldlm_lock_remove_from_lru_nolock(lock);
201         spin_unlock(&ns->ns_unused_lock);
202         EXIT;
203         return rc;
204 }
205
206 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
207 {
208         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
209         lock->l_last_used = cfs_time_current();
210         LASSERT(list_empty(&lock->l_lru));
211         LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
212         list_add_tail(&lock->l_lru, &ns->ns_unused_list);
213         LASSERT(ns->ns_nr_unused >= 0);
214         ns->ns_nr_unused++;
215 }
216
217 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
218 {
219         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
220         ENTRY;
221         spin_lock(&ns->ns_unused_lock);
222         ldlm_lock_add_to_lru_nolock(lock);
223         spin_unlock(&ns->ns_unused_lock);
224         EXIT;
225 }
226
227 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
228 {
229         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
230         ENTRY;
231         spin_lock(&ns->ns_unused_lock);
232         if (!list_empty(&lock->l_lru)) {
233                 ldlm_lock_remove_from_lru_nolock(lock);
234                 ldlm_lock_add_to_lru_nolock(lock);
235         }
236         spin_unlock(&ns->ns_unused_lock);
237         EXIT;
238 }
239
240 /* This used to have a 'strict' flag, which recovery would use to mark an
241  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
242  * shall explain why it's gone: with the new hash table scheme, once you call
243  * ldlm_lock_destroy, you can never drop your final references on this lock.
244  * Because it's not in the hash table anymore.  -phil */
245 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
246 {
247         ENTRY;
248
249         if (lock->l_readers || lock->l_writers) {
250                 LDLM_ERROR(lock, "lock still has references");
251                 ldlm_lock_dump(D_ERROR, lock, 0);
252                 LBUG();
253         }
254
255         if (!list_empty(&lock->l_res_link)) {
256                 LDLM_ERROR(lock, "lock still on resource");
257                 ldlm_lock_dump(D_ERROR, lock, 0);
258                 LBUG();
259         }
260
261         if (lock->l_destroyed) {
262                 LASSERT(list_empty(&lock->l_lru));
263                 EXIT;
264                 return 0;
265         }
266         lock->l_destroyed = 1;
267
268         if (lock->l_export)
269                 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
270         list_del_init(&lock->l_export_chain);
271         if (lock->l_export)
272                 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
273
274         ldlm_lock_remove_from_lru(lock);
275         class_handle_unhash(&lock->l_handle);
276
277 #if 0
278         /* Wake anyone waiting for this lock */
279         /* FIXME: I should probably add yet another flag, instead of using
280          * l_export to only call this on clients */
281         if (lock->l_export)
282                 class_export_put(lock->l_export);
283         lock->l_export = NULL;
284         if (lock->l_export && lock->l_completion_ast)
285                 lock->l_completion_ast(lock, 0);
286 #endif
287         EXIT;
288         return 1;
289 }
290
291 void ldlm_lock_destroy(struct ldlm_lock *lock)
292 {
293         int first;
294         ENTRY;
295         lock_res_and_lock(lock);
296         first = ldlm_lock_destroy_internal(lock);
297         unlock_res_and_lock(lock);
298
299         /* drop reference from hashtable only for first destroy */
300         if (first)
301                 LDLM_LOCK_PUT(lock);
302         EXIT;
303 }
304
305 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
306 {
307         int first;
308         ENTRY;
309         first = ldlm_lock_destroy_internal(lock);
310         /* drop reference from hashtable only for first destroy */
311         if (first)
312                 LDLM_LOCK_PUT(lock);
313         EXIT;
314 }
315
316 /* this is called by portals_handle2object with the handle lock taken */
317 static void lock_handle_addref(void *lock)
318 {
319         LDLM_LOCK_GET((struct ldlm_lock *)lock);
320 }
321
322 /*
323  * usage: pass in a resource on which you have done ldlm_resource_get
324  *        pass in a parent lock on which you have done a ldlm_lock_get
325  *        after return, ldlm_*_put the resource and parent
326  * returns: lock with refcount 2 - one for current caller and one for remote
327  */
328 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
329 {
330         struct ldlm_lock *lock;
331         ENTRY;
332
333         if (resource == NULL)
334                 LBUG();
335
336         OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
337         if (lock == NULL)
338                 RETURN(NULL);
339
340         spin_lock_init(&lock->l_lock);
341         lock->l_resource = ldlm_resource_getref(resource);
342
343         atomic_set(&lock->l_refc, 2);
344         CFS_INIT_LIST_HEAD(&lock->l_res_link);
345         CFS_INIT_LIST_HEAD(&lock->l_lru);
346         CFS_INIT_LIST_HEAD(&lock->l_export_chain);
347         CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
348         CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
349         CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
350         cfs_waitq_init(&lock->l_waitq);
351         lock->l_blocking_lock = NULL;
352         CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
353         CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
354
355         atomic_inc(&resource->lr_namespace->ns_locks);
356         CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
357         class_handle_hash(&lock->l_handle, lock_handle_addref);
358
359         CFS_INIT_LIST_HEAD(&lock->l_extents_list);
360         spin_lock_init(&lock->l_extents_list_lock);
361         CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
362
363         RETURN(lock);
364 }
365
366 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
367                               const struct ldlm_res_id *new_resid)
368 {
369         struct ldlm_resource *oldres = lock->l_resource;
370         struct ldlm_resource *newres;
371         int type;
372         ENTRY;
373
374         LASSERT(ns_is_client(ns));
375
376         lock_res_and_lock(lock);
377         if (memcmp(new_resid, &lock->l_resource->lr_name,
378                    sizeof(lock->l_resource->lr_name)) == 0) {
379                 /* Nothing to do */
380                 unlock_res_and_lock(lock);
381                 RETURN(0);
382         }
383
384         LASSERT(new_resid->name[0] != 0);
385
386         /* This function assumes that the lock isn't on any lists */
387         LASSERT(list_empty(&lock->l_res_link));
388
389         type = oldres->lr_type;
390         unlock_res_and_lock(lock);
391
392         newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
393         if (newres == NULL)
394                 RETURN(-ENOMEM);
395
396         lock_res_and_lock(lock);
397         LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
398                        sizeof(lock->l_resource->lr_name)) != 0);
399         lock_res(newres);
400         lock->l_resource = newres;
401         unlock_res(oldres);
402         unlock_res_and_lock(lock);
403
404         /* ...and the flowers are still standing! */
405         ldlm_resource_putref(oldres);
406
407         RETURN(0);
408 }
409
410 /*
411  *  HANDLES
412  */
413
414 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
415 {
416         lockh->cookie = lock->l_handle.h_cookie;
417 }
418
419 /* if flags: atomically get the lock and set the flags.
420  *           Return NULL if flag already set
421  */
422
423 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
424                                      int flags)
425 {
426         struct ldlm_namespace *ns;
427         struct ldlm_lock *lock, *retval = NULL;
428         ENTRY;
429
430         LASSERT(handle);
431
432         lock = class_handle2object(handle->cookie);
433         if (lock == NULL)
434                 RETURN(NULL);
435
436         LASSERT(lock->l_resource != NULL);
437         ns = lock->l_resource->lr_namespace;
438         LASSERT(ns != NULL);
439
440         lock_res_and_lock(lock);
441
442         /* It's unlikely but possible that someone marked the lock as
443          * destroyed after we did handle2object on it */
444         if (lock->l_destroyed) {
445                 unlock_res_and_lock(lock);
446                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
447                 LDLM_LOCK_PUT(lock);
448                 GOTO(out, retval);
449         }
450
451         if (flags && (lock->l_flags & flags)) {
452                 unlock_res_and_lock(lock);
453                 LDLM_LOCK_PUT(lock);
454                 GOTO(out, retval);
455         }
456
457         if (flags)
458                 lock->l_flags |= flags;
459
460         unlock_res_and_lock(lock);
461         retval = lock;
462         EXIT;
463  out:
464         return retval;
465 }
466
467 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
468                                       const struct lustre_handle *handle)
469 {
470         struct ldlm_lock *retval = NULL;
471         retval = __ldlm_handle2lock(handle, 0);
472         return retval;
473 }
474
475 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
476 {
477         struct obd_export *exp = lock->l_export?:lock->l_conn_export;
478         /* INODEBITS_INTEROP: If the other side does not support
479          * inodebits, reply with a plain lock descriptor.
480          */
481         if ((lock->l_resource->lr_type == LDLM_IBITS) &&
482             (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
483                 struct ldlm_resource res = *lock->l_resource;
484
485                 /* Make sure all the right bits are set in this lock we
486                    are going to pass to client */
487                 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
488                          (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
489                          "Inappropriate inode lock bits during "
490                          "conversion " LPU64 "\n",
491                          lock->l_policy_data.l_inodebits.bits);
492                 res.lr_type = LDLM_PLAIN;
493                 ldlm_res2desc(&res, &desc->l_resource);
494                 /* Convert "new" lock mode to something old client can
495                    understand */
496                 if ((lock->l_req_mode == LCK_CR) ||
497                     (lock->l_req_mode == LCK_CW))
498                         desc->l_req_mode = LCK_PR;
499                 else
500                         desc->l_req_mode = lock->l_req_mode;
501                 if ((lock->l_granted_mode == LCK_CR) ||
502                     (lock->l_granted_mode == LCK_CW)) {
503                         desc->l_granted_mode = LCK_PR;
504                 } else {
505                         /* We never grant PW/EX locks to clients */
506                         LASSERT((lock->l_granted_mode != LCK_PW) &&
507                                 (lock->l_granted_mode != LCK_EX));
508                         desc->l_granted_mode = lock->l_granted_mode;
509                 }
510
511                 /* We do not copy policy here, because there is no
512                    policy for plain locks */
513         } else {
514                 ldlm_res2desc(lock->l_resource, &desc->l_resource);
515                 desc->l_req_mode = lock->l_req_mode;
516                 desc->l_granted_mode = lock->l_granted_mode;
517                 desc->l_policy_data = lock->l_policy_data;
518         }
519 }
520
521 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
522                            struct list_head *work_list)
523 {
524         if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
525                 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
526                 lock->l_flags |= LDLM_FL_AST_SENT;
527                 /* If the enqueuing client said so, tell the AST recipient to
528                  * discard dirty data, rather than writing back. */
529                 if (new->l_flags & LDLM_AST_DISCARD_DATA)
530                         lock->l_flags |= LDLM_FL_DISCARD_DATA;
531                 LASSERT(list_empty(&lock->l_bl_ast));
532                 list_add(&lock->l_bl_ast, work_list);
533                 LDLM_LOCK_GET(lock);
534                 LASSERT(lock->l_blocking_lock == NULL);
535                 lock->l_blocking_lock = LDLM_LOCK_GET(new);
536         }
537 }
538
539 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
540 {
541         if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
542                 lock->l_flags |= LDLM_FL_CP_REQD;
543                 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
544                 LASSERT(list_empty(&lock->l_cp_ast));
545                 list_add(&lock->l_cp_ast, work_list);
546                 LDLM_LOCK_GET(lock);
547         }
548 }
549
550 /* must be called with lr_lock held */
551 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
552                                 struct list_head *work_list)
553 {
554         ENTRY;
555         check_res_locked(lock->l_resource);
556         if (new)
557                 ldlm_add_bl_work_item(lock, new, work_list);
558         else
559                 ldlm_add_cp_work_item(lock, work_list);
560         EXIT;
561 }
562
563 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
564 {
565         struct ldlm_lock *lock;
566
567         lock = ldlm_handle2lock(lockh);
568         LASSERT(lock != NULL);
569         ldlm_lock_addref_internal(lock, mode);
570         LDLM_LOCK_PUT(lock);
571 }
572
573 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
574 {
575         ldlm_lock_remove_from_lru(lock);
576         if (mode & (LCK_NL | LCK_CR | LCK_PR))
577                 lock->l_readers++;
578         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
579                 lock->l_writers++;
580         LDLM_LOCK_GET(lock);
581         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
582 }
583
584 /* only called for local locks */
585 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
586 {
587         lock_res_and_lock(lock);
588         ldlm_lock_addref_internal_nolock(lock, mode);
589         unlock_res_and_lock(lock);
590 }
591
592 /* only called in ldlm_flock_destroy and for local locks.
593  *  * for LDLM_FLOCK type locks, l_blocking_ast is null, and
594  *   * ldlm_lock_remove_from_lru() does nothing, it is safe 
595  *    * for ldlm_flock_destroy usage by dropping some code */
596 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
597 {
598         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
599         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
600                 LASSERT(lock->l_readers > 0);
601                 lock->l_readers--;
602         }
603         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
604                 LASSERT(lock->l_writers > 0);
605                 lock->l_writers--;
606         }
607
608         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
609 }
610
611 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
612 {
613         struct ldlm_namespace *ns;
614         ENTRY;
615
616         lock_res_and_lock(lock);
617
618         ns = lock->l_resource->lr_namespace;
619
620         ldlm_lock_decref_internal_nolock(lock, mode);
621
622         if (lock->l_flags & LDLM_FL_LOCAL &&
623             !lock->l_readers && !lock->l_writers) {
624                 /* If this is a local lock on a server namespace and this was
625                  * the last reference, cancel the lock. */
626                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
627                 lock->l_flags |= LDLM_FL_CBPENDING;
628         }
629
630         if (!lock->l_readers && !lock->l_writers &&
631             (lock->l_flags & LDLM_FL_CBPENDING)) {
632                 /* If we received a blocked AST and this was the last reference,
633                  * run the callback. */
634                 if (ns_is_server(ns) && lock->l_export)
635                         CERROR("FL_CBPENDING set on non-local lock--just a "
636                                "warning\n");
637
638                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
639
640                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
641                 ldlm_lock_remove_from_lru(lock);
642                 unlock_res_and_lock(lock);
643                 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
644                     ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
645                         ldlm_handle_bl_callback(ns, NULL, lock);
646         } else if (ns_is_client(ns) &&
647                    !lock->l_readers && !lock->l_writers &&
648                    !(lock->l_flags & LDLM_FL_NO_LRU) &&
649                    !(lock->l_flags & LDLM_FL_BL_AST)) {
650                 /* If this is a client-side namespace and this was the last
651                  * reference, put it on the LRU. */
652                 ldlm_lock_add_to_lru(lock);
653                 unlock_res_and_lock(lock);
654                 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE 
655                  * are not supported by the server, otherwise, it is done on 
656                  * enqueue. */
657                 if (!exp_connect_cancelset(lock->l_conn_export) && 
658                     !ns_connect_lru_resize(ns))
659                         ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
660         } else {
661                 unlock_res_and_lock(lock);
662         }
663
664         EXIT;
665 }
666
667 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
668 {
669         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
670         LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
671         ldlm_lock_decref_internal(lock, mode);
672         LDLM_LOCK_PUT(lock);
673 }
674
675 /* This will drop a lock reference and mark it for destruction, but will not
676  * necessarily cancel the lock before returning. */
677 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
678 {
679         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
680         ENTRY;
681
682         LASSERT(lock != NULL);
683
684         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
685         lock_res_and_lock(lock);
686         lock->l_flags |= LDLM_FL_CBPENDING;
687         unlock_res_and_lock(lock);
688         ldlm_lock_decref_internal(lock, mode);
689         LDLM_LOCK_PUT(lock);
690 }
691
692 struct sl_insert_point {
693         struct list_head *res_link;
694         struct list_head *mode_link;
695         struct list_head *policy_link;
696 };
697
698 /*
699  * search_granted_lock
700  *
701  * Description:
702  *      Finds a position to insert the new lock.
703  * Parameters:
704  *      queue [input]:  the granted list where search acts on;
705  *      req [input]:    the lock whose position to be located;
706  *      prev [output]:  positions within 3 lists to insert @req to
707  * Return Value:
708  *      filled @prev
709  * NOTE: called by
710  *  - ldlm_grant_lock_with_skiplist
711  */
712 static void search_granted_lock(struct list_head *queue,
713                                 struct ldlm_lock *req,
714                                 struct sl_insert_point *prev)
715 {
716         struct list_head *tmp;
717         struct ldlm_lock *lock, *mode_end, *policy_end;
718         ENTRY;
719
720         list_for_each(tmp, queue) {
721                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
722
723                 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
724                                       l_sl_mode);
725
726                 if (lock->l_req_mode != req->l_req_mode) {
727                         /* jump to last lock of mode group */
728                         tmp = &mode_end->l_res_link;
729                         continue;
730                 }
731
732                 /* suitable mode group is found */
733                 if (lock->l_resource->lr_type == LDLM_PLAIN) {
734                         /* insert point is last lock of the mode group */
735                         prev->res_link = &mode_end->l_res_link;
736                         prev->mode_link = &mode_end->l_sl_mode;
737                         prev->policy_link = &req->l_sl_policy;
738                         EXIT;
739                         return;
740                 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
741                         for (;;) {
742                                 policy_end = list_entry(lock->l_sl_policy.prev,
743                                                         struct ldlm_lock,
744                                                         l_sl_policy);
745
746                                 if (lock->l_policy_data.l_inodebits.bits ==
747                                     req->l_policy_data.l_inodebits.bits) {
748                                         /* insert point is last lock of
749                                          * the policy group */
750                                         prev->res_link =
751                                                 &policy_end->l_res_link;
752                                         prev->mode_link =
753                                                 &policy_end->l_sl_mode;
754                                         prev->policy_link =
755                                                 &policy_end->l_sl_policy;
756                                         EXIT;
757                                         return;
758                                 }
759
760                                 if (policy_end == mode_end)
761                                         /* done with mode group */
762                                         break;
763
764                                 /* jump to next policy group within the mode group */
765                                 tmp = policy_end->l_res_link.next;
766                                 lock = list_entry(tmp, struct ldlm_lock, 
767                                                   l_res_link);
768                         }  /* loop over policy groups within the mode group */
769
770                         /* insert point is last lock of the mode group,
771                          * new policy group is started */
772                         prev->res_link = &mode_end->l_res_link;
773                         prev->mode_link = &mode_end->l_sl_mode;
774                         prev->policy_link = &req->l_sl_policy;
775                         EXIT;
776                         return;
777                 } else {
778                         LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
779                         LBUG();
780                 }
781         }
782
783         /* insert point is last lock on the queue,
784          * new mode group and new policy group are started */
785         prev->res_link = queue->prev;
786         prev->mode_link = &req->l_sl_mode;
787         prev->policy_link = &req->l_sl_policy;
788         EXIT;
789         return;
790 }
791
792 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock, 
793                                        struct sl_insert_point *prev)
794 {
795         struct ldlm_resource *res = lock->l_resource;
796         ENTRY;
797
798         check_res_locked(res);
799
800         ldlm_resource_dump(D_INFO, res);
801         CDEBUG(D_OTHER, "About to add this lock:\n");
802         ldlm_lock_dump(D_OTHER, lock, 0);
803
804         if (lock->l_destroyed) {
805                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
806                 return;
807         }
808
809         LASSERT(list_empty(&lock->l_res_link));
810         LASSERT(list_empty(&lock->l_sl_mode));
811         LASSERT(list_empty(&lock->l_sl_policy));
812
813         list_add(&lock->l_res_link, prev->res_link);
814         list_add(&lock->l_sl_mode, prev->mode_link);
815         list_add(&lock->l_sl_policy, prev->policy_link);
816
817         EXIT;
818 }
819
820 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
821 {
822         struct sl_insert_point prev;
823         ENTRY;
824
825         LASSERT(lock->l_req_mode == lock->l_granted_mode);
826
827         search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
828         ldlm_granted_list_add_lock(lock, &prev);
829         EXIT;
830 }
831
832 /* NOTE: called by
833  *  - ldlm_lock_enqueue
834  *  - ldlm_reprocess_queue
835  *  - ldlm_lock_convert
836  *
837  * must be called with lr_lock held
838  */
839 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
840 {
841         struct ldlm_resource *res = lock->l_resource;
842         ENTRY;
843
844         check_res_locked(res);
845
846         lock->l_granted_mode = lock->l_req_mode;
847         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
848                 ldlm_grant_lock_with_skiplist(lock);
849         else if (res->lr_type == LDLM_EXTENT)
850                 ldlm_extent_add_lock(res, lock);
851         else
852                 ldlm_resource_add_lock(res, &res->lr_granted, lock);
853
854         if (lock->l_granted_mode < res->lr_most_restr)
855                 res->lr_most_restr = lock->l_granted_mode;
856
857         if (work_list && lock->l_completion_ast != NULL)
858                 ldlm_add_ast_work_item(lock, NULL, work_list);
859
860         ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
861         EXIT;
862 }
863
864 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
865  * comment above ldlm_lock_match */
866 static struct ldlm_lock *search_queue(struct list_head *queue,
867                                       ldlm_mode_t *mode,
868                                       ldlm_policy_data_t *policy,
869                                       struct ldlm_lock *old_lock, int flags)
870 {
871         struct ldlm_lock *lock;
872         struct list_head *tmp;
873
874         list_for_each(tmp, queue) {
875                 ldlm_mode_t match;
876
877                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
878
879                 if (lock == old_lock)
880                         break;
881
882                 /* llite sometimes wants to match locks that will be
883                  * canceled when their users drop, but we allow it to match
884                  * if it passes in CBPENDING and the lock still has users.
885                  * this is generally only going to be used by children
886                  * whose parents already hold a lock so forward progress
887                  * can still happen. */
888                 if (lock->l_flags & LDLM_FL_CBPENDING &&
889                     !(flags & LDLM_FL_CBPENDING))
890                         continue;
891                 if (lock->l_flags & LDLM_FL_CBPENDING &&
892                     lock->l_readers == 0 && lock->l_writers == 0)
893                         continue;
894
895                 if (!(lock->l_req_mode & *mode))
896                         continue;
897                 match = lock->l_req_mode;
898
899                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
900                     (lock->l_policy_data.l_extent.start >
901                      policy->l_extent.start ||
902                      lock->l_policy_data.l_extent.end < policy->l_extent.end))
903                         continue;
904
905                 if (unlikely(match == LCK_GROUP) &&
906                     lock->l_resource->lr_type == LDLM_EXTENT &&
907                     lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
908                         continue;
909
910                 /* We match if we have existing lock with same or wider set
911                    of bits. */
912                 if (lock->l_resource->lr_type == LDLM_IBITS &&
913                      ((lock->l_policy_data.l_inodebits.bits &
914                       policy->l_inodebits.bits) !=
915                       policy->l_inodebits.bits))
916                         continue;
917
918                 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
919                         continue;
920
921                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
922                     !(lock->l_flags & LDLM_FL_LOCAL))
923                         continue;
924
925                 if (flags & LDLM_FL_TEST_LOCK) {
926                         LDLM_LOCK_GET(lock);
927                         ldlm_lock_touch_in_lru(lock);
928                 } else {
929                         ldlm_lock_addref_internal_nolock(lock, match);
930                 }
931                 *mode = match;
932                 return lock;
933         }
934
935         return NULL;
936 }
937
938 void ldlm_lock_allow_match(struct ldlm_lock *lock)
939 {
940         lock_res_and_lock(lock);
941         lock->l_flags |= LDLM_FL_LVB_READY;
942         cfs_waitq_signal(&lock->l_waitq);
943         unlock_res_and_lock(lock);
944 }
945
946 /**
947  * Checks if requested extent lock is compatible with another owned lock.
948  *
949  * Checks if \a lock is compatible with a read or write lock
950  * (specified by \a rw) for an extent [\a start , \a end].
951  *
952  * \param lock the already owned lock
953  * \param rw OBD_BRW_READ if requested for reading,
954  *           OBD_BRW_WRITE if requested for writing
955  * \param start start of the requested extent
956  * \param end end of the requested extent
957  * \param cookie transparent parameter for passing locking context
958  *
959  * \post result == 1, *cookie == context, appropriate lock is referenced
960  *
961  * \retval 1 owned lock is reused for the request
962  * \retval 0 no lock reused for the request
963  *
964  * \see ldlm_lock_fast_release
965  */
966 int ldlm_lock_fast_match(struct ldlm_lock *lock, int rw,
967                          obd_off start, obd_off end,
968                          void **cookie)
969 {
970         LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
971
972         if (!lock)
973                 return 0;
974
975         lock_res_and_lock(lock);
976         /* check if granted mode is compatible */
977         if (rw == OBD_BRW_WRITE &&
978             !(lock->l_granted_mode & (LCK_PW|LCK_GROUP)))
979                 goto no_match;
980
981         /* does the lock cover the region we would like to access? */
982         if ((lock->l_policy_data.l_extent.start > start) ||
983             (lock->l_policy_data.l_extent.end < end))
984                 goto no_match;
985
986         /* if we received a blocking callback and the lock is no longer
987          * referenced, don't use it */
988         if ((lock->l_flags & LDLM_FL_CBPENDING) &&
989             !lock->l_writers && !lock->l_readers)
990                 goto no_match;
991
992         ldlm_lock_addref_internal_nolock(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
993         unlock_res_and_lock(lock);
994         *cookie = (void *)lock;
995         return 1; /* avoid using rc for stack relief */
996
997 no_match:
998         unlock_res_and_lock(lock);
999         return 0;
1000 }
1001
1002 /**
1003  * Releases a reference to a lock taken in a "fast" way.
1004  *
1005  * Releases a read or write (specified by \a rw) lock
1006  * referenced by \a cookie.
1007  *
1008  * \param rw OBD_BRW_READ if requested for reading,
1009  *           OBD_BRW_WRITE if requested for writing
1010  * \param cookie transparent parameter for passing locking context
1011  *
1012  * \post appropriate lock is dereferenced
1013  *
1014  * \see ldlm_lock_fast_lock
1015  */
1016 void ldlm_lock_fast_release(void *cookie, int rw)
1017 {
1018         struct ldlm_lock *lock = (struct ldlm_lock *)cookie;
1019
1020         LASSERT(lock != NULL);
1021         LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
1022         LASSERT(rw == OBD_BRW_READ ||
1023                 (lock->l_granted_mode & (LCK_PW | LCK_GROUP)));
1024         ldlm_lock_decref_internal(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
1025 }
1026
1027 /* Can be called in two ways:
1028  *
1029  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1030  * for a duplicate of.
1031  *
1032  * Otherwise, all of the fields must be filled in, to match against.
1033  *
1034  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1035  *     server (ie, connh is NULL)
1036  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1037  *     list will be considered
1038  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1039  *     to be canceled can still be matched as long as they still have reader
1040  *     or writer refernces
1041  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1042  *     just tell us if we would have matched.
1043  *
1044  * Returns 1 if it finds an already-existing lock that is compatible; in this
1045  * case, lockh is filled in with a addref()ed lock
1046  *
1047  * we also check security context, if that failed we simply return 0 (to keep
1048  * caller code unchanged), the context failure will be discovered by caller
1049  * sometime later.
1050  */
1051 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1052                             const struct ldlm_res_id *res_id, ldlm_type_t type,
1053                             ldlm_policy_data_t *policy, ldlm_mode_t mode,
1054                             struct lustre_handle *lockh)
1055 {
1056         struct ldlm_resource *res;
1057         struct ldlm_lock *lock, *old_lock = NULL;
1058         int rc = 0;
1059         ENTRY;
1060
1061         if (ns == NULL) {
1062                 old_lock = ldlm_handle2lock(lockh);
1063                 LASSERT(old_lock);
1064
1065                 ns = old_lock->l_resource->lr_namespace;
1066                 res_id = &old_lock->l_resource->lr_name;
1067                 type = old_lock->l_resource->lr_type;
1068                 mode = old_lock->l_req_mode;
1069         }
1070
1071         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1072         if (res == NULL) {
1073                 LASSERT(old_lock == NULL);
1074                 RETURN(0);
1075         }
1076
1077         lock_res(res);
1078
1079         lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1080         if (lock != NULL)
1081                 GOTO(out, rc = 1);
1082         if (flags & LDLM_FL_BLOCK_GRANTED)
1083                 GOTO(out, rc = 0);
1084         lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1085         if (lock != NULL)
1086                 GOTO(out, rc = 1);
1087         lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1088         if (lock != NULL)
1089                 GOTO(out, rc = 1);
1090
1091         EXIT;
1092  out:
1093         unlock_res(res);
1094         ldlm_resource_putref(res);
1095
1096         if (lock) {
1097                 ldlm_lock2handle(lock, lockh);
1098                 if ((flags & LDLM_FL_LVB_READY) &&
1099                     (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1100                         struct l_wait_info lwi;
1101                         if (lock->l_completion_ast) {
1102                                 int err = lock->l_completion_ast(lock,
1103                                                           LDLM_FL_WAIT_NOREPROC,
1104                                                                  NULL);
1105                                 if (err) {
1106                                         if (flags & LDLM_FL_TEST_LOCK)
1107                                                 LDLM_LOCK_PUT(lock);
1108                                         else
1109                                                 ldlm_lock_decref_internal(lock, mode);
1110                                         rc = 0;
1111                                         goto out2;
1112                                 }
1113                         }
1114
1115                         lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1116                                                LWI_ON_SIGNAL_NOOP, NULL);
1117
1118                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1119                         l_wait_event(lock->l_waitq,
1120                                      (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1121                 }
1122         }
1123  out2:
1124         if (rc) {
1125                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1126                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1127                                 res_id->name[2] : policy->l_extent.start,
1128                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1129                                 res_id->name[3] : policy->l_extent.end);
1130
1131                 /* check user's security context */
1132                 if (lock->l_conn_export &&
1133                     sptlrpc_import_check_ctx(
1134                                 class_exp2cliimp(lock->l_conn_export))) {
1135                         if (!(flags & LDLM_FL_TEST_LOCK))
1136                                 ldlm_lock_decref_internal(lock, mode);
1137                         rc = 0;
1138                 }
1139
1140                 if (flags & LDLM_FL_TEST_LOCK)
1141                         LDLM_LOCK_PUT(lock);
1142
1143         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1144                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1145                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1146                                   type, mode, res_id->name[0], res_id->name[1],
1147                                   (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1148                                         res_id->name[2] :policy->l_extent.start,
1149                                 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1150                                         res_id->name[3] : policy->l_extent.end);
1151         }
1152         if (old_lock)
1153                 LDLM_LOCK_PUT(old_lock);
1154
1155         return rc ? mode : 0;
1156 }
1157
1158 /* Returns a referenced lock */
1159 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1160                                    const struct ldlm_res_id *res_id,
1161                                    ldlm_type_t type,
1162                                    ldlm_mode_t mode,
1163                                    ldlm_blocking_callback blocking,
1164                                    ldlm_completion_callback completion,
1165                                    ldlm_glimpse_callback glimpse,
1166                                    void *data, __u32 lvb_len)
1167 {
1168         struct ldlm_lock *lock;
1169         struct ldlm_resource *res;
1170         ENTRY;
1171
1172         res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1173         if (res == NULL)
1174                 RETURN(NULL);
1175
1176         lock = ldlm_lock_new(res);
1177         ldlm_resource_putref(res);
1178
1179         if (lock == NULL)
1180                 RETURN(NULL);
1181
1182         lock->l_req_mode = mode;
1183         lock->l_ast_data = data;
1184         lock->l_blocking_ast = blocking;
1185         lock->l_completion_ast = completion;
1186         lock->l_glimpse_ast = glimpse;
1187         lock->l_pid = cfs_curproc_pid();
1188
1189         lock->l_tree_node = NULL;
1190         /* if this is the extent lock, allocate the interval tree node */
1191         if (type == LDLM_EXTENT) {
1192                 if (ldlm_interval_alloc(lock) == NULL)
1193                         GOTO(out, 0);
1194         }
1195
1196         if (lvb_len) {
1197                 lock->l_lvb_len = lvb_len;
1198                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1199                 if (lock->l_lvb_data == NULL)
1200                         GOTO(out, 0);
1201         }
1202
1203         RETURN(lock);
1204
1205 out:
1206         if (lock->l_lvb_data)
1207                 OBD_FREE(lock->l_lvb_data, lvb_len);
1208         ldlm_interval_free(ldlm_interval_detach(lock));
1209         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1210         return NULL;
1211 }
1212
1213 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1214                                struct ldlm_lock **lockp,
1215                                void *cookie, int *flags)
1216 {
1217         struct ldlm_lock *lock = *lockp;
1218         struct ldlm_resource *res = lock->l_resource;
1219         int local = ns_is_client(res->lr_namespace);
1220         ldlm_processing_policy policy;
1221         ldlm_error_t rc = ELDLM_OK;
1222         struct ldlm_interval *node = NULL;
1223         ENTRY;
1224
1225         do_gettimeofday(&lock->l_enqueued_time);
1226         /* policies are not executed on the client or during replay */
1227         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1228             && !local && ns->ns_policy) {
1229                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1230                                    NULL);
1231                 if (rc == ELDLM_LOCK_REPLACED) {
1232                         /* The lock that was returned has already been granted,
1233                          * and placed into lockp.  If it's not the same as the
1234                          * one we passed in, then destroy the old one and our
1235                          * work here is done. */
1236                         if (lock != *lockp) {
1237                                 ldlm_lock_destroy(lock);
1238                                 LDLM_LOCK_PUT(lock);
1239                         }
1240                         *flags |= LDLM_FL_LOCK_CHANGED;
1241                         RETURN(0);
1242                 } else if (rc != ELDLM_OK ||
1243                            (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1244                         ldlm_lock_destroy(lock);
1245                         RETURN(rc);
1246                 }
1247         }
1248
1249         /* For a replaying lock, it might be already in granted list. So
1250          * unlinking the lock will cause the interval node to be freed, we
1251          * have to allocate the interval node early otherwise we can't regrant
1252          * this lock in the future. - jay */
1253         if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1254                 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1255                                sizeof(*node));
1256
1257         lock_res_and_lock(lock);
1258         if (local && lock->l_req_mode == lock->l_granted_mode) {
1259                 /* The server returned a blocked lock, but it was granted
1260                  * before we got a chance to actually enqueue it.  We don't
1261                  * need to do anything else. */
1262                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1263                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1264                 GOTO(out, ELDLM_OK);
1265         }
1266
1267         ldlm_resource_unlink_lock(lock);
1268         if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1269                 if (node == NULL) {
1270                         ldlm_lock_destroy_nolock(lock);
1271                         GOTO(out, rc = -ENOMEM);
1272                 }
1273
1274                 CFS_INIT_LIST_HEAD(&node->li_group);
1275                 ldlm_interval_attach(node, lock);
1276                 node = NULL;
1277         }
1278
1279         /* Some flags from the enqueue want to make it into the AST, via the
1280          * lock's l_flags. */
1281         lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1282
1283         /* This distinction between local lock trees is very important; a client
1284          * namespace only has information about locks taken by that client, and
1285          * thus doesn't have enough information to decide for itself if it can
1286          * be granted (below).  In this case, we do exactly what the server
1287          * tells us to do, as dictated by the 'flags'.
1288          *
1289          * We do exactly the same thing during recovery, when the server is
1290          * more or less trusting the clients not to lie.
1291          *
1292          * FIXME (bug 268): Detect obvious lies by checking compatibility in
1293          * granted/converting queues. */
1294         if (local) {
1295                 if (*flags & LDLM_FL_BLOCK_CONV)
1296                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1297                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1298                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1299                 else
1300                         ldlm_grant_lock(lock, NULL);
1301                 GOTO(out, ELDLM_OK);
1302         } else if (*flags & LDLM_FL_REPLAY) {
1303                 if (*flags & LDLM_FL_BLOCK_CONV) {
1304                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1305                         GOTO(out, ELDLM_OK);
1306                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1307                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1308                         GOTO(out, ELDLM_OK);
1309                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1310                         ldlm_grant_lock(lock, NULL);
1311                         GOTO(out, ELDLM_OK);
1312                 }
1313                 /* If no flags, fall through to normal enqueue path. */
1314         }
1315
1316         policy = ldlm_processing_policy_table[res->lr_type];
1317         policy(lock, flags, 1, &rc, NULL);
1318         GOTO(out, rc);
1319 out:
1320         unlock_res_and_lock(lock);
1321         if (node)
1322                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1323         return rc;
1324 }
1325
1326 /* Must be called with namespace taken: queue is waiting or converting. */
1327 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1328                          struct list_head *work_list)
1329 {
1330         struct list_head *tmp, *pos;
1331         ldlm_processing_policy policy;
1332         int flags;
1333         int rc = LDLM_ITER_CONTINUE;
1334         ldlm_error_t err;
1335         ENTRY;
1336
1337         check_res_locked(res);
1338
1339         policy = ldlm_processing_policy_table[res->lr_type];
1340         LASSERT(policy);
1341
1342         list_for_each_safe(tmp, pos, queue) {
1343                 struct ldlm_lock *pending;
1344                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1345
1346                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1347
1348                 flags = 0;
1349                 rc = policy(pending, &flags, 0, &err, work_list);
1350                 if (rc != LDLM_ITER_CONTINUE)
1351                         break;
1352         }
1353
1354         RETURN(rc);
1355 }
1356
1357 /* Helper function for ldlm_run_ast_work().
1358  * 
1359  * Send an existing rpc set specified by @arg->set and then
1360  * destroy it. Create new one if @do_create flag is set. */
1361 static void
1362 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1363 {
1364         ENTRY;
1365
1366         ptlrpc_set_wait(arg->set);
1367         if (arg->type == LDLM_BL_CALLBACK)
1368                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1369         ptlrpc_set_destroy(arg->set);
1370
1371         if (do_create)
1372                 arg->set = ptlrpc_prep_set();
1373
1374         EXIT;
1375 }
1376
1377 static int
1378 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1379 {
1380         struct ldlm_lock_desc d;
1381         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1382         ENTRY;
1383
1384         /* nobody should touch l_bl_ast */
1385         lock_res_and_lock(lock);
1386         list_del_init(&lock->l_bl_ast);
1387
1388         LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1389         LASSERT(lock->l_bl_ast_run == 0);
1390         LASSERT(lock->l_blocking_lock);
1391         lock->l_bl_ast_run++;
1392         unlock_res_and_lock(lock);
1393
1394         ldlm_lock2desc(lock->l_blocking_lock, &d);
1395
1396         LDLM_LOCK_PUT(lock->l_blocking_lock);
1397         lock->l_blocking_lock = NULL;
1398         lock->l_blocking_ast(lock, &d, (void *)arg, 
1399                              LDLM_CB_BLOCKING);
1400         LDLM_LOCK_PUT(lock);
1401
1402         RETURN(1);
1403 }
1404
1405 static int
1406 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1407 {
1408         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1409         ldlm_completion_callback completion_callback;
1410         int rc = 0;
1411         ENTRY;
1412
1413         /* It's possible to receive a completion AST before we've set
1414          * the l_completion_ast pointer: either because the AST arrived
1415          * before the reply, or simply because there's a small race
1416          * window between receiving the reply and finishing the local
1417          * enqueue. (bug 842)
1418          *
1419          * This can't happen with the blocking_ast, however, because we
1420          * will never call the local blocking_ast until we drop our
1421          * reader/writer reference, which we won't do until we get the
1422          * reply and finish enqueueing. */
1423
1424         /* nobody should touch l_cp_ast */
1425         lock_res_and_lock(lock);
1426         list_del_init(&lock->l_cp_ast);
1427         LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1428         /* save l_completion_ast since it can be changed by
1429          * mds_intent_policy(), see bug 14225 */
1430         completion_callback = lock->l_completion_ast;
1431         lock->l_flags &= ~LDLM_FL_CP_REQD;
1432         unlock_res_and_lock(lock);
1433
1434         if (completion_callback != NULL) {
1435                 completion_callback(lock, 0, (void *)arg);
1436                 rc = 1;
1437         }
1438         LDLM_LOCK_PUT(lock);
1439
1440         RETURN(rc);
1441 }
1442
1443 static int
1444 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1445 {
1446         struct ldlm_lock_desc desc;
1447         struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
1448         ENTRY;
1449
1450         list_del_init(&lock->l_export_chain);
1451
1452         /* the desc just pretend to exclusive */
1453         ldlm_lock2desc(lock, &desc);
1454         desc.l_req_mode = LCK_EX;
1455         desc.l_granted_mode = 0;
1456
1457         lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1458         LDLM_LOCK_PUT(lock);
1459
1460         RETURN(1);
1461 }
1462
1463 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1464 {
1465         struct ldlm_cb_set_arg arg;
1466         struct list_head *tmp, *pos;
1467         int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1468         int ast_count;
1469         ENTRY;
1470
1471         arg.set = ptlrpc_prep_set();
1472         atomic_set(&arg.restart, 0);
1473         switch (ast_type) {
1474         case LDLM_WORK_BL_AST:
1475                 arg.type = LDLM_BL_CALLBACK;
1476                 work_ast_lock = ldlm_work_bl_ast_lock;
1477                 break;
1478         case LDLM_WORK_CP_AST:
1479                 arg.type = LDLM_CP_CALLBACK;
1480                 work_ast_lock = ldlm_work_cp_ast_lock;
1481                 break;
1482         case LDLM_WORK_REVOKE_AST:
1483                 arg.type = LDLM_BL_CALLBACK;
1484                 work_ast_lock = ldlm_work_revoke_ast_lock;
1485                 break;
1486         default:
1487                 LBUG();
1488         }
1489
1490         ast_count = 0;
1491         list_for_each_safe(tmp, pos, rpc_list) {
1492                 ast_count += work_ast_lock(tmp, &arg);
1493
1494                 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1495                  * and create a new set for requests that remained in
1496                  * @rpc_list */
1497                 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1498                         ldlm_send_and_maybe_create_set(&arg, 1);
1499                         ast_count = 0;
1500                 }
1501         }
1502
1503         if (ast_count > 0)
1504                 ldlm_send_and_maybe_create_set(&arg, 0);
1505         else
1506                 /* In case when number of ASTs is multiply of
1507                  * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1508                  * @arg.set must be destroyed here, otherwise we get 
1509                  * write memory leaking. */
1510                 ptlrpc_set_destroy(arg.set);
1511
1512         RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1513 }
1514
1515 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1516 {
1517         ldlm_reprocess_all(res);
1518         return LDLM_ITER_CONTINUE;
1519 }
1520
1521 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1522 {
1523         struct list_head *tmp;
1524         int i, rc;
1525
1526         if (ns == NULL)
1527                 return;
1528
1529         ENTRY;
1530         spin_lock(&ns->ns_hash_lock);
1531         for (i = 0; i < RES_HASH_SIZE; i++) {
1532                 tmp = ns->ns_hash[i].next;
1533                 while (tmp != &(ns->ns_hash[i])) {
1534                         struct ldlm_resource *res =
1535                                 list_entry(tmp, struct ldlm_resource, lr_hash);
1536
1537                         ldlm_resource_getref(res);
1538                         spin_unlock(&ns->ns_hash_lock);
1539
1540                         rc = reprocess_one_queue(res, NULL);
1541
1542                         spin_lock(&ns->ns_hash_lock);
1543                         tmp = tmp->next;
1544                         ldlm_resource_putref_locked(res);
1545
1546                         if (rc == LDLM_ITER_STOP)
1547                                 GOTO(out, rc);
1548                 }
1549         }
1550  out:
1551         spin_unlock(&ns->ns_hash_lock);
1552         EXIT;
1553 }
1554
1555 void ldlm_reprocess_all(struct ldlm_resource *res)
1556 {
1557         CFS_LIST_HEAD(rpc_list);
1558         int rc;
1559         ENTRY;
1560
1561         /* Local lock trees don't get reprocessed. */
1562         if (ns_is_client(res->lr_namespace)) {
1563                 EXIT;
1564                 return;
1565         }
1566
1567  restart:
1568         lock_res(res);
1569         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1570         if (rc == LDLM_ITER_CONTINUE)
1571                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1572         unlock_res(res);
1573
1574         rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1575         if (rc == -ERESTART) {
1576                 LASSERT(list_empty(&rpc_list));
1577                 goto restart;
1578         }
1579         EXIT;
1580 }
1581
1582 void ldlm_cancel_callback(struct ldlm_lock *lock)
1583 {
1584         check_res_locked(lock->l_resource);
1585         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1586                 lock->l_flags |= LDLM_FL_CANCEL;
1587                 if (lock->l_blocking_ast) {
1588                         // l_check_no_ns_lock(ns);
1589                         unlock_res_and_lock(lock);
1590                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1591                                              LDLM_CB_CANCELING);
1592                         lock_res_and_lock(lock);
1593                 } else {
1594                         LDLM_DEBUG(lock, "no blocking ast");
1595                 }
1596         }
1597         lock->l_flags |= LDLM_FL_BL_DONE;
1598 }
1599
1600 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1601 {
1602         if (req->l_resource->lr_type != LDLM_PLAIN &&
1603             req->l_resource->lr_type != LDLM_IBITS)
1604                 return;
1605
1606         list_del_init(&req->l_sl_policy);
1607         list_del_init(&req->l_sl_mode);
1608 }
1609
1610 void ldlm_lock_cancel(struct ldlm_lock *lock)
1611 {
1612         struct ldlm_resource *res;
1613         struct ldlm_namespace *ns;
1614         ENTRY;
1615
1616         lock_res_and_lock(lock);
1617
1618         res = lock->l_resource;
1619         ns = res->lr_namespace;
1620
1621         /* Please do not, no matter how tempting, remove this LBUG without
1622          * talking to me first. -phik */
1623         if (lock->l_readers || lock->l_writers) {
1624                 LDLM_ERROR(lock, "lock still has references");
1625                 LBUG();
1626         }
1627
1628         ldlm_del_waiting_lock(lock);
1629
1630         /* Releases cancel callback. */
1631         ldlm_cancel_callback(lock);
1632
1633         /* Yes, second time, just in case it was added again while we were
1634            running with no res lock in ldlm_cancel_callback */
1635         ldlm_del_waiting_lock(lock); 
1636         ldlm_resource_unlink_lock(lock);
1637         ldlm_lock_destroy_nolock(lock);
1638
1639         if (lock->l_granted_mode == lock->l_req_mode)
1640                 ldlm_pool_del(&ns->ns_pool, lock);
1641
1642         /* Make sure we will not be called again for same lock what is possible
1643          * if not to zero out lock->l_granted_mode */
1644         lock->l_granted_mode = 0;
1645         unlock_res_and_lock(lock);
1646
1647         EXIT;
1648 }
1649
1650 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1651 {
1652         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1653         ENTRY;
1654
1655         if (lock == NULL)
1656                 RETURN(-EINVAL);
1657
1658         lock->l_ast_data = data;
1659         LDLM_LOCK_PUT(lock);
1660         RETURN(0);
1661 }
1662
1663 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1664 {
1665         struct ldlm_lock *lock;
1666         struct ldlm_resource *res;
1667
1668         spin_lock(&exp->exp_ldlm_data.led_lock);
1669         while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1670                 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1671                                   struct ldlm_lock, l_export_chain);
1672                 res = ldlm_resource_getref(lock->l_resource);
1673                 LDLM_LOCK_GET(lock);
1674                 spin_unlock(&exp->exp_ldlm_data.led_lock);
1675
1676                 LDLM_DEBUG(lock, "export %p", exp);
1677                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1678
1679                 ldlm_lock_cancel(lock);
1680                 ldlm_reprocess_all(res);
1681
1682                 ldlm_resource_putref(res);
1683                 LDLM_LOCK_PUT(lock);
1684                 spin_lock(&exp->exp_ldlm_data.led_lock);
1685         }
1686         spin_unlock(&exp->exp_ldlm_data.led_lock);
1687 }
1688
1689 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1690                                         __u32 *flags)
1691 {
1692         CFS_LIST_HEAD(rpc_list);
1693         struct ldlm_resource *res;
1694         struct ldlm_namespace *ns;
1695         int granted = 0;
1696         int old_mode, rc;
1697         struct sl_insert_point prev;
1698         ldlm_error_t err;
1699         struct ldlm_interval *node;
1700         ENTRY;
1701
1702         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1703                 *flags |= LDLM_FL_BLOCK_GRANTED;
1704                 RETURN(lock->l_resource);
1705         }
1706
1707         /* I can't check the type of lock here because the bitlock of lock
1708          * is not held here, so do the allocation blindly. -jay */
1709         OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1710         if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
1711                 RETURN(NULL);
1712
1713         LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1714                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1715
1716         lock_res_and_lock(lock);
1717
1718         res = lock->l_resource;
1719         ns = res->lr_namespace;
1720
1721         old_mode = lock->l_req_mode;
1722         lock->l_req_mode = new_mode;
1723         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1724                 /* remember the lock position where the lock might be 
1725                  * added back to the granted list later and also 
1726                  * remember the join mode for skiplist fixing. */
1727                 prev.res_link = lock->l_res_link.prev;
1728                 prev.mode_link = lock->l_sl_mode.prev;
1729                 prev.policy_link = lock->l_sl_policy.prev;
1730                 ldlm_resource_unlink_lock(lock);
1731         } else {
1732                 ldlm_resource_unlink_lock(lock);
1733                 if (res->lr_type == LDLM_EXTENT) {
1734                         /* FIXME: ugly code, I have to attach the lock to a 
1735                          * interval node again since perhaps it will be granted
1736                          * soon */
1737                         CFS_INIT_LIST_HEAD(&node->li_group);
1738                         ldlm_interval_attach(node, lock);
1739                         node = NULL;
1740                 }
1741         }
1742
1743         /* If this is a local resource, put it on the appropriate list. */
1744         if (ns_is_client(res->lr_namespace)) {
1745                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1746                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1747                 } else {
1748                         /* This should never happen, because of the way the
1749                          * server handles conversions. */
1750                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1751                                    *flags);
1752                         LBUG();
1753
1754                         ldlm_grant_lock(lock, &rpc_list);
1755                         granted = 1;
1756                         /* FIXME: completion handling not with ns_lock held ! */
1757                         if (lock->l_completion_ast)
1758                                 lock->l_completion_ast(lock, 0, NULL);
1759                 }
1760         } else {
1761                 int pflags = 0;
1762                 ldlm_processing_policy policy;
1763                 policy = ldlm_processing_policy_table[res->lr_type];
1764                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1765                 if (rc == LDLM_ITER_STOP) {
1766                         lock->l_req_mode = old_mode;
1767                         if (res->lr_type == LDLM_EXTENT)
1768                                 ldlm_extent_add_lock(res, lock);
1769                         else
1770                                 ldlm_granted_list_add_lock(lock, &prev);
1771
1772                         res = NULL;
1773                 } else {
1774                         *flags |= LDLM_FL_BLOCK_GRANTED;
1775                         granted = 1;
1776                 }
1777         }
1778         unlock_res_and_lock(lock);
1779
1780         if (granted)
1781                 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1782         if (node)
1783                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1784         RETURN(res);
1785 }
1786
1787 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1788 {
1789         struct obd_device *obd = NULL;
1790
1791         if (!((libcfs_debug | D_ERROR) & level))
1792                 return;
1793
1794         if (!lock) {
1795                 CDEBUG(level, "  NULL LDLM lock\n");
1796                 return;
1797         }
1798
1799         CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1800                lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1801                pos, lock->l_pid);
1802         if (lock->l_conn_export != NULL)
1803                 obd = lock->l_conn_export->exp_obd;
1804         if (lock->l_export && lock->l_export->exp_connection) {
1805                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1806                      libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1807                      lock->l_remote_handle.cookie);
1808         } else if (obd == NULL) {
1809                 CDEBUG(level, "  Node: local\n");
1810         } else {
1811                 struct obd_import *imp = obd->u.cli.cl_import;
1812                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1813                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1814                        lock->l_remote_handle.cookie);
1815         }
1816         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1817                   lock->l_resource,
1818                   lock->l_resource->lr_name.name[0],
1819                   lock->l_resource->lr_name.name[1],
1820                   lock->l_resource->lr_name.name[2]);
1821         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1822                "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1823                ldlm_lockname[lock->l_granted_mode],
1824                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1825                lock->l_flags);
1826         if (lock->l_resource->lr_type == LDLM_EXTENT)
1827                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1828                        " (req "LPU64"-"LPU64")\n",
1829                        lock->l_policy_data.l_extent.start,
1830                        lock->l_policy_data.l_extent.end,
1831                        lock->l_req_extent.start, lock->l_req_extent.end);
1832         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1833                 CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
1834                        lock->l_policy_data.l_flock.pid,
1835                        lock->l_policy_data.l_flock.start,
1836                        lock->l_policy_data.l_flock.end);
1837        else if (lock->l_resource->lr_type == LDLM_IBITS)
1838                 CDEBUG(level, "  Bits: "LPX64"\n",
1839                        lock->l_policy_data.l_inodebits.bits);
1840 }
1841
1842 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1843 {
1844         struct ldlm_lock *lock;
1845
1846         if (!((libcfs_debug | D_ERROR) & level))
1847                 return;
1848
1849         lock = ldlm_handle2lock(lockh);
1850         if (lock == NULL)
1851                 return;
1852
1853         ldlm_lock_dump(D_OTHER, lock, 0);
1854
1855         LDLM_LOCK_PUT(lock);
1856 }
1857
1858 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1859                       struct libcfs_debug_msg_data *data, const char *fmt,
1860                       ...)
1861 {
1862         va_list args;
1863         cfs_debug_limit_state_t *cdls = data->msg_cdls;
1864         
1865         va_start(args, fmt);
1866
1867         if (lock->l_resource == NULL) {
1868                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1869                                    data->msg_fn, data->msg_line, fmt, args,
1870                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1871                        "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1872                        LPX64" expref: %d pid: %u\n", lock,
1873                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1874                        lock->l_readers, lock->l_writers,
1875                        ldlm_lockname[lock->l_granted_mode],
1876                        ldlm_lockname[lock->l_req_mode],
1877                        lock->l_flags, lock->l_remote_handle.cookie,
1878                        lock->l_export ?
1879                        atomic_read(&lock->l_export->exp_refcount) : -99,
1880                        lock->l_pid);
1881                 va_end(args);
1882                 return;
1883         }
1884
1885         switch (lock->l_resource->lr_type) {
1886         case LDLM_EXTENT:
1887                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1888                                    data->msg_fn, data->msg_line, fmt, args,
1889                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1890                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1891                        "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1892                        " expref: %d pid: %u\n",
1893                        lock->l_resource->lr_namespace->ns_name, lock,
1894                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1895                        lock->l_readers, lock->l_writers,
1896                        ldlm_lockname[lock->l_granted_mode],
1897                        ldlm_lockname[lock->l_req_mode],
1898                        lock->l_resource->lr_name.name[0],
1899                        lock->l_resource->lr_name.name[1],
1900                        atomic_read(&lock->l_resource->lr_refcount),
1901                        ldlm_typename[lock->l_resource->lr_type],
1902                        lock->l_policy_data.l_extent.start,
1903                        lock->l_policy_data.l_extent.end,
1904                        lock->l_req_extent.start, lock->l_req_extent.end,
1905                        lock->l_flags, lock->l_remote_handle.cookie,
1906                        lock->l_export ?
1907                        atomic_read(&lock->l_export->exp_refcount) : -99,
1908                        lock->l_pid);
1909                 break;
1910
1911         case LDLM_FLOCK:
1912                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1913                                    data->msg_fn, data->msg_line, fmt, args,
1914                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1915                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1916                        "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1917                        " expref: %d pid: %u\n",
1918                        lock->l_resource->lr_namespace->ns_name, lock,
1919                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1920                        lock->l_readers, lock->l_writers,
1921                        ldlm_lockname[lock->l_granted_mode],
1922                        ldlm_lockname[lock->l_req_mode],
1923                        lock->l_resource->lr_name.name[0],
1924                        lock->l_resource->lr_name.name[1],
1925                        atomic_read(&lock->l_resource->lr_refcount),
1926                        ldlm_typename[lock->l_resource->lr_type],
1927                        lock->l_policy_data.l_flock.pid,
1928                        lock->l_policy_data.l_flock.start,
1929                        lock->l_policy_data.l_flock.end,
1930                        lock->l_flags, lock->l_remote_handle.cookie,
1931                        lock->l_export ?
1932                        atomic_read(&lock->l_export->exp_refcount) : -99,
1933                        lock->l_pid);
1934                 break;
1935
1936         case LDLM_IBITS:
1937                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1938                                    data->msg_fn, data->msg_line, fmt, args,
1939                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1940                        "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1941                        "flags: %x remote: "LPX64" expref: %d "
1942                        "pid %u\n",
1943                        lock->l_resource->lr_namespace->ns_name,
1944                        lock, lock->l_handle.h_cookie,
1945                        atomic_read (&lock->l_refc),
1946                        lock->l_readers, lock->l_writers,
1947                        ldlm_lockname[lock->l_granted_mode],
1948                        ldlm_lockname[lock->l_req_mode],
1949                        lock->l_resource->lr_name.name[0],
1950                        lock->l_resource->lr_name.name[1],
1951                        lock->l_policy_data.l_inodebits.bits,
1952                        atomic_read(&lock->l_resource->lr_refcount),
1953                        ldlm_typename[lock->l_resource->lr_type],
1954                        lock->l_flags, lock->l_remote_handle.cookie,
1955                        lock->l_export ?
1956                        atomic_read(&lock->l_export->exp_refcount) : -99,
1957                        lock->l_pid);
1958                 break;
1959
1960         default:
1961                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1962                                    data->msg_fn, data->msg_line, fmt, args,
1963                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1964                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1965                        "remote: "LPX64" expref: %d pid: %u\n",
1966                        lock->l_resource->lr_namespace->ns_name,
1967                        lock, lock->l_handle.h_cookie,
1968                        atomic_read (&lock->l_refc),
1969                        lock->l_readers, lock->l_writers,
1970                        ldlm_lockname[lock->l_granted_mode],
1971                        ldlm_lockname[lock->l_req_mode],
1972                        lock->l_resource->lr_name.name[0],
1973                        lock->l_resource->lr_name.name[1],
1974                        atomic_read(&lock->l_resource->lr_refcount),
1975                        ldlm_typename[lock->l_resource->lr_type],
1976                        lock->l_flags, lock->l_remote_handle.cookie,
1977                        lock->l_export ?
1978                        atomic_read(&lock->l_export->exp_refcount) : -99,
1979                        lock->l_pid);
1980                 break;
1981         }
1982         va_end(args);
1983 }
1984 EXPORT_SYMBOL(_ldlm_lock_debug);