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