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