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