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