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