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