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