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