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