Whamcloud - gitweb
574b1ad9f7729d150ec47a0f83ed2d3005c69a7d
[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 # include <linux/lustre_intent.h>
47 #else
48 # include <liblustre.h>
49 #endif
50
51 #include <obd_class.h>
52 #include "ldlm_internal.h"
53
54 /* lock types */
55 char *ldlm_lockname[] = {
56         [0] "--",
57         [LCK_EX] "EX",
58         [LCK_PW] "PW",
59         [LCK_PR] "PR",
60         [LCK_CW] "CW",
61         [LCK_CR] "CR",
62         [LCK_NL] "NL",
63         [LCK_GROUP] "GROUP",
64         [LCK_COS] "COS"
65 };
66
67 char *ldlm_typename[] = {
68         [LDLM_PLAIN] "PLN",
69         [LDLM_EXTENT] "EXT",
70         [LDLM_FLOCK] "FLK",
71         [LDLM_IBITS] "IBT",
72 };
73
74 char *ldlm_it2str(int it)
75 {
76         switch (it) {
77         case IT_OPEN:
78                 return "open";
79         case IT_CREAT:
80                 return "creat";
81         case (IT_OPEN | IT_CREAT):
82                 return "open|creat";
83         case IT_READDIR:
84                 return "readdir";
85         case IT_GETATTR:
86                 return "getattr";
87         case IT_LOOKUP:
88                 return "lookup";
89         case IT_UNLINK:
90                 return "unlink";
91         case IT_GETXATTR:
92                 return "getxattr";
93         default:
94                 CERROR("Unknown intent %d\n", it);
95                 return "UNKNOWN";
96         }
97 }
98
99 extern cfs_mem_cache_t *ldlm_lock_slab;
100
101 static ldlm_processing_policy ldlm_processing_policy_table[] = {
102         [LDLM_PLAIN] ldlm_process_plain_lock,
103         [LDLM_EXTENT] ldlm_process_extent_lock,
104 #ifdef __KERNEL__
105         [LDLM_FLOCK] ldlm_process_flock_lock,
106 #endif
107         [LDLM_IBITS] ldlm_process_inodebits_lock,
108 };
109
110 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
111 {
112         return ldlm_processing_policy_table[res->lr_type];
113 }
114
115 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
116 {
117         ns->ns_policy = arg;
118 }
119
120 /*
121  * REFCOUNTED LOCK OBJECTS
122  */
123
124
125 /*
126  * Lock refcounts, during creation:
127  *   - one special one for allocation, dec'd only once in destroy
128  *   - one for being a lock that's in-use
129  *   - one for the addref associated with a new lock
130  */
131 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
132 {
133         cfs_atomic_inc(&lock->l_refc);
134         return lock;
135 }
136
137 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
138 {
139         LASSERT(size == sizeof(*lock));
140         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
141 }
142
143 void ldlm_lock_put(struct ldlm_lock *lock)
144 {
145         ENTRY;
146
147         LASSERT(lock->l_resource != LP_POISON);
148         LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
149         if (cfs_atomic_dec_and_test(&lock->l_refc)) {
150                 struct ldlm_resource *res;
151
152                 LDLM_DEBUG(lock,
153                            "final lock_put on destroyed lock, freeing it.");
154
155                 res = lock->l_resource;
156                 LASSERT(lock->l_destroyed);
157                 LASSERT(cfs_list_empty(&lock->l_res_link));
158                 LASSERT(cfs_list_empty(&lock->l_pending_chain));
159
160                 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
161                                      LDLM_NSS_LOCKS);
162                 lu_ref_del(&res->lr_reference, "lock", lock);
163                 ldlm_resource_putref(res);
164                 lock->l_resource = NULL;
165                 if (lock->l_export) {
166                         class_export_lock_put(lock->l_export, lock);
167                         lock->l_export = NULL;
168                 }
169
170                 if (lock->l_lvb_data != NULL)
171                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
172
173                 ldlm_interval_free(ldlm_interval_detach(lock));
174                 lu_ref_fini(&lock->l_reference);
175                 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
176                                 ldlm_lock_free);
177         }
178
179         EXIT;
180 }
181
182 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
183 {
184         int rc = 0;
185         if (!cfs_list_empty(&lock->l_lru)) {
186                 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
187
188                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
189                 cfs_list_del_init(&lock->l_lru);
190                 if (lock->l_flags & LDLM_FL_SKIPPED)
191                         lock->l_flags &= ~LDLM_FL_SKIPPED;
192                 LASSERT(ns->ns_nr_unused > 0);
193                 ns->ns_nr_unused--;
194                 rc = 1;
195         }
196         return rc;
197 }
198
199 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
200 {
201         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
202         int rc;
203
204         ENTRY;
205         if (lock->l_ns_srv) {
206                 LASSERT(cfs_list_empty(&lock->l_lru));
207                 RETURN(0);
208         }
209
210         cfs_spin_lock(&ns->ns_lock);
211         rc = ldlm_lock_remove_from_lru_nolock(lock);
212         cfs_spin_unlock(&ns->ns_lock);
213         EXIT;
214         return rc;
215 }
216
217 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
218 {
219         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
220
221         lock->l_last_used = cfs_time_current();
222         LASSERT(cfs_list_empty(&lock->l_lru));
223         LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
224         cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
225         LASSERT(ns->ns_nr_unused >= 0);
226         ns->ns_nr_unused++;
227 }
228
229 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
230 {
231         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
232
233         ENTRY;
234         cfs_spin_lock(&ns->ns_lock);
235         ldlm_lock_add_to_lru_nolock(lock);
236         cfs_spin_unlock(&ns->ns_lock);
237         EXIT;
238 }
239
240 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
241 {
242         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
243
244         ENTRY;
245         if (lock->l_ns_srv) {
246                 LASSERT(cfs_list_empty(&lock->l_lru));
247                 EXIT;
248                 return;
249         }
250
251         cfs_spin_lock(&ns->ns_lock);
252         if (!cfs_list_empty(&lock->l_lru)) {
253                 ldlm_lock_remove_from_lru_nolock(lock);
254                 ldlm_lock_add_to_lru_nolock(lock);
255         }
256         cfs_spin_unlock(&ns->ns_lock);
257         EXIT;
258 }
259
260 /* This used to have a 'strict' flag, which recovery would use to mark an
261  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
262  * shall explain why it's gone: with the new hash table scheme, once you call
263  * ldlm_lock_destroy, you can never drop your final references on this lock.
264  * Because it's not in the hash table anymore.  -phil */
265 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
266 {
267         ENTRY;
268
269         if (lock->l_readers || lock->l_writers) {
270                 LDLM_ERROR(lock, "lock still has references");
271                 ldlm_lock_dump(D_ERROR, lock, 0);
272                 LBUG();
273         }
274
275         if (!cfs_list_empty(&lock->l_res_link)) {
276                 LDLM_ERROR(lock, "lock still on resource");
277                 ldlm_lock_dump(D_ERROR, lock, 0);
278                 LBUG();
279         }
280
281         if (lock->l_destroyed) {
282                 LASSERT(cfs_list_empty(&lock->l_lru));
283                 EXIT;
284                 return 0;
285         }
286         lock->l_destroyed = 1;
287
288         if (lock->l_export && lock->l_export->exp_lock_hash &&
289             !cfs_hlist_unhashed(&lock->l_exp_hash))
290                 cfs_hash_del(lock->l_export->exp_lock_hash,
291                              &lock->l_remote_handle, &lock->l_exp_hash);
292
293         ldlm_lock_remove_from_lru(lock);
294         class_handle_unhash(&lock->l_handle);
295
296 #if 0
297         /* Wake anyone waiting for this lock */
298         /* FIXME: I should probably add yet another flag, instead of using
299          * l_export to only call this on clients */
300         if (lock->l_export)
301                 class_export_put(lock->l_export);
302         lock->l_export = NULL;
303         if (lock->l_export && lock->l_completion_ast)
304                 lock->l_completion_ast(lock, 0);
305 #endif
306         EXIT;
307         return 1;
308 }
309
310 void ldlm_lock_destroy(struct ldlm_lock *lock)
311 {
312         int first;
313         ENTRY;
314         lock_res_and_lock(lock);
315         first = ldlm_lock_destroy_internal(lock);
316         unlock_res_and_lock(lock);
317
318         /* drop reference from hashtable only for first destroy */
319         if (first) {
320                 lu_ref_del(&lock->l_reference, "hash", lock);
321                 LDLM_LOCK_RELEASE(lock);
322         }
323         EXIT;
324 }
325
326 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
327 {
328         int first;
329         ENTRY;
330         first = ldlm_lock_destroy_internal(lock);
331         /* drop reference from hashtable only for first destroy */
332         if (first) {
333                 lu_ref_del(&lock->l_reference, "hash", lock);
334                 LDLM_LOCK_RELEASE(lock);
335         }
336         EXIT;
337 }
338
339 /* this is called by portals_handle2object with the handle lock taken */
340 static void lock_handle_addref(void *lock)
341 {
342         LDLM_LOCK_GET((struct ldlm_lock *)lock);
343 }
344
345 /*
346  * usage: pass in a resource on which you have done ldlm_resource_get
347  *        new lock will take over the refcount.
348  * returns: lock with refcount 2 - one for current caller and one for remote
349  */
350 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
351 {
352         struct ldlm_lock *lock;
353         ENTRY;
354
355         if (resource == NULL)
356                 LBUG();
357
358         OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
359         if (lock == NULL)
360                 RETURN(NULL);
361
362         cfs_spin_lock_init(&lock->l_lock);
363         lock->l_resource = resource;
364         lu_ref_add(&resource->lr_reference, "lock", lock);
365
366         cfs_atomic_set(&lock->l_refc, 2);
367         CFS_INIT_LIST_HEAD(&lock->l_res_link);
368         CFS_INIT_LIST_HEAD(&lock->l_lru);
369         CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
370         CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
371         CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
372         CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
373         cfs_waitq_init(&lock->l_waitq);
374         lock->l_blocking_lock = NULL;
375         CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
376         CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
377         CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
378
379         lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
380                              LDLM_NSS_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         if (newres == NULL)
425                 RETURN(-ENOMEM);
426
427         lu_ref_add(&newres->lr_reference, "lock", lock);
428         /*
429          * To flip the lock from the old to the new resource, lock, oldres and
430          * newres have to be locked. Resource spin-locks are nested within
431          * lock->l_lock, and are taken in the memory address order to avoid
432          * dead-locks.
433          */
434         cfs_spin_lock(&lock->l_lock);
435         oldres = lock->l_resource;
436         if (oldres < newres) {
437                 lock_res(oldres);
438                 lock_res_nested(newres, LRT_NEW);
439         } else {
440                 lock_res(newres);
441                 lock_res_nested(oldres, LRT_NEW);
442         }
443         LASSERT(memcmp(new_resid, &oldres->lr_name,
444                        sizeof oldres->lr_name) != 0);
445         lock->l_resource = newres;
446         unlock_res(oldres);
447         unlock_res_and_lock(lock);
448
449         /* ...and the flowers are still standing! */
450         lu_ref_del(&oldres->lr_reference, "lock", lock);
451         ldlm_resource_putref(oldres);
452
453         RETURN(0);
454 }
455
456 /*
457  *  HANDLES
458  */
459
460 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
461 {
462         lockh->cookie = lock->l_handle.h_cookie;
463 }
464
465 /* if flags: atomically get the lock and set the flags.
466  *           Return NULL if flag already set
467  */
468
469 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
470                                      int flags)
471 {
472         struct ldlm_lock *lock;
473         ENTRY;
474
475         LASSERT(handle);
476
477         lock = class_handle2object(handle->cookie);
478         if (lock == NULL)
479                 RETURN(NULL);
480
481         /* It's unlikely but possible that someone marked the lock as
482          * destroyed after we did handle2object on it */
483         if (flags == 0 && !lock->l_destroyed) {
484                 lu_ref_add(&lock->l_reference, "handle", cfs_current());
485                 RETURN(lock);
486         }
487
488         lock_res_and_lock(lock);
489
490         LASSERT(lock->l_resource != NULL);
491
492         lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
493         if (unlikely(lock->l_destroyed)) {
494                 unlock_res_and_lock(lock);
495                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
496                 LDLM_LOCK_PUT(lock);
497                 RETURN(NULL);
498         }
499
500         if (flags && (lock->l_flags & flags)) {
501                 unlock_res_and_lock(lock);
502                 LDLM_LOCK_PUT(lock);
503                 RETURN(NULL);
504         }
505
506         if (flags)
507                 lock->l_flags |= flags;
508
509         unlock_res_and_lock(lock);
510         RETURN(lock);
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 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1544                               cfs_hlist_node_t *hnode, void *arg)
1545 {
1546         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1547         int    rc;
1548
1549         rc = reprocess_one_queue(res, arg);
1550
1551         return rc == LDLM_ITER_STOP;
1552 }
1553
1554 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1555 {
1556         ENTRY;
1557
1558         if (ns != NULL) {
1559                 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1560                                          ldlm_reprocess_res, NULL);
1561         }
1562         EXIT;
1563 }
1564
1565 void ldlm_reprocess_all(struct ldlm_resource *res)
1566 {
1567         CFS_LIST_HEAD(rpc_list);
1568         int rc;
1569         ENTRY;
1570
1571         /* Local lock trees don't get reprocessed. */
1572         if (ns_is_client(ldlm_res_to_ns(res))) {
1573                 EXIT;
1574                 return;
1575         }
1576
1577  restart:
1578         lock_res(res);
1579         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1580         if (rc == LDLM_ITER_CONTINUE)
1581                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1582         unlock_res(res);
1583
1584         rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1585         if (rc == -ERESTART) {
1586                 LASSERT(cfs_list_empty(&rpc_list));
1587                 goto restart;
1588         }
1589         EXIT;
1590 }
1591
1592 void ldlm_cancel_callback(struct ldlm_lock *lock)
1593 {
1594         check_res_locked(lock->l_resource);
1595         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1596                 lock->l_flags |= LDLM_FL_CANCEL;
1597                 if (lock->l_blocking_ast) {
1598                         // l_check_no_ns_lock(ns);
1599                         unlock_res_and_lock(lock);
1600                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1601                                              LDLM_CB_CANCELING);
1602                         lock_res_and_lock(lock);
1603                 } else {
1604                         LDLM_DEBUG(lock, "no blocking ast");
1605                 }
1606         }
1607         lock->l_flags |= LDLM_FL_BL_DONE;
1608 }
1609
1610 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1611 {
1612         if (req->l_resource->lr_type != LDLM_PLAIN &&
1613             req->l_resource->lr_type != LDLM_IBITS)
1614                 return;
1615
1616         cfs_list_del_init(&req->l_sl_policy);
1617         cfs_list_del_init(&req->l_sl_mode);
1618 }
1619
1620 void ldlm_lock_cancel(struct ldlm_lock *lock)
1621 {
1622         struct ldlm_resource *res;
1623         struct ldlm_namespace *ns;
1624         ENTRY;
1625
1626         lock_res_and_lock(lock);
1627
1628         res = lock->l_resource;
1629         ns  = ldlm_res_to_ns(res);
1630
1631         /* Please do not, no matter how tempting, remove this LBUG without
1632          * talking to me first. -phik */
1633         if (lock->l_readers || lock->l_writers) {
1634                 LDLM_ERROR(lock, "lock still has references");
1635                 LBUG();
1636         }
1637
1638         ldlm_del_waiting_lock(lock);
1639
1640         /* Releases cancel callback. */
1641         ldlm_cancel_callback(lock);
1642
1643         /* Yes, second time, just in case it was added again while we were
1644            running with no res lock in ldlm_cancel_callback */
1645         ldlm_del_waiting_lock(lock);
1646         ldlm_resource_unlink_lock(lock);
1647         ldlm_lock_destroy_nolock(lock);
1648
1649         if (lock->l_granted_mode == lock->l_req_mode)
1650                 ldlm_pool_del(&ns->ns_pool, lock);
1651
1652         /* Make sure we will not be called again for same lock what is possible
1653          * if not to zero out lock->l_granted_mode */
1654         lock->l_granted_mode = LCK_MINMODE;
1655         unlock_res_and_lock(lock);
1656
1657         EXIT;
1658 }
1659
1660 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1661 {
1662         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1663         ENTRY;
1664
1665         if (lock == NULL)
1666                 RETURN(-EINVAL);
1667
1668         lock->l_ast_data = data;
1669         LDLM_LOCK_PUT(lock);
1670         RETURN(0);
1671 }
1672
1673 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1674                                     cfs_hlist_node_t *hnode, void *data)
1675
1676 {
1677         struct obd_export    *exp  = data;
1678         struct ldlm_lock     *lock = cfs_hash_object(hs, hnode);
1679         struct ldlm_resource *res;
1680
1681         res = ldlm_resource_getref(lock->l_resource);
1682         LDLM_LOCK_GET(lock);
1683
1684         LDLM_DEBUG(lock, "export %p", exp);
1685         ldlm_res_lvbo_update(res, NULL, 1);
1686         ldlm_lock_cancel(lock);
1687         ldlm_reprocess_all(res);
1688         ldlm_resource_putref(res);
1689         LDLM_LOCK_RELEASE(lock);
1690         return 0;
1691 }
1692
1693 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1694 {
1695         cfs_hash_for_each_empty(exp->exp_lock_hash,
1696                                 ldlm_cancel_locks_for_export_cb, exp);
1697 }
1698
1699 /**
1700  * Downgrade an exclusive lock.
1701  *
1702  * A fast variant of ldlm_lock_convert for convertion of exclusive
1703  * locks. The convertion is always successful.
1704  *
1705  * \param lock A lock to convert
1706  * \param new_mode new lock mode
1707  */
1708 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1709 {
1710         ENTRY;
1711
1712         LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1713         LASSERT(new_mode == LCK_COS);
1714
1715         lock_res_and_lock(lock);
1716         ldlm_resource_unlink_lock(lock);
1717         /*
1718          * Remove the lock from pool as it will be added again in
1719          * ldlm_grant_lock() called below.
1720          */
1721         ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1722
1723         lock->l_req_mode = new_mode;
1724         ldlm_grant_lock(lock, NULL);
1725         unlock_res_and_lock(lock);
1726         ldlm_reprocess_all(lock->l_resource);
1727
1728         EXIT;
1729 }
1730
1731 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1732                                         __u32 *flags)
1733 {
1734         CFS_LIST_HEAD(rpc_list);
1735         struct ldlm_resource *res;
1736         struct ldlm_namespace *ns;
1737         int granted = 0;
1738         int old_mode, rc;
1739         struct sl_insert_point prev;
1740         ldlm_error_t err;
1741         struct ldlm_interval *node;
1742         ENTRY;
1743
1744         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1745                 *flags |= LDLM_FL_BLOCK_GRANTED;
1746                 RETURN(lock->l_resource);
1747         }
1748
1749         /* I can't check the type of lock here because the bitlock of lock
1750          * is not held here, so do the allocation blindly. -jay */
1751         OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1752         if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
1753                 RETURN(NULL);
1754
1755         LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1756                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1757
1758         lock_res_and_lock(lock);
1759
1760         res = lock->l_resource;
1761         ns  = ldlm_res_to_ns(res);
1762
1763         old_mode = lock->l_req_mode;
1764         lock->l_req_mode = new_mode;
1765         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1766                 /* remember the lock position where the lock might be
1767                  * added back to the granted list later and also
1768                  * remember the join mode for skiplist fixing. */
1769                 prev.res_link = lock->l_res_link.prev;
1770                 prev.mode_link = lock->l_sl_mode.prev;
1771                 prev.policy_link = lock->l_sl_policy.prev;
1772                 ldlm_resource_unlink_lock(lock);
1773         } else {
1774                 ldlm_resource_unlink_lock(lock);
1775                 if (res->lr_type == LDLM_EXTENT) {
1776                         /* FIXME: ugly code, I have to attach the lock to a
1777                          * interval node again since perhaps it will be granted
1778                          * soon */
1779                         CFS_INIT_LIST_HEAD(&node->li_group);
1780                         ldlm_interval_attach(node, lock);
1781                         node = NULL;
1782                 }
1783         }
1784
1785         /*
1786          * Remove old lock from the pool before adding the lock with new
1787          * mode below in ->policy()
1788          */
1789         ldlm_pool_del(&ns->ns_pool, lock);
1790
1791         /* If this is a local resource, put it on the appropriate list. */
1792         if (ns_is_client(ldlm_res_to_ns(res))) {
1793                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1794                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1795                 } else {
1796                         /* This should never happen, because of the way the
1797                          * server handles conversions. */
1798                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1799                                    *flags);
1800                         LBUG();
1801
1802                         ldlm_grant_lock(lock, &rpc_list);
1803                         granted = 1;
1804                         /* FIXME: completion handling not with lr_lock held ! */
1805                         if (lock->l_completion_ast)
1806                                 lock->l_completion_ast(lock, 0, NULL);
1807                 }
1808         } else {
1809                 int pflags = 0;
1810                 ldlm_processing_policy policy;
1811                 policy = ldlm_processing_policy_table[res->lr_type];
1812                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1813                 if (rc == LDLM_ITER_STOP) {
1814                         lock->l_req_mode = old_mode;
1815                         if (res->lr_type == LDLM_EXTENT)
1816                                 ldlm_extent_add_lock(res, lock);
1817                         else
1818                                 ldlm_granted_list_add_lock(lock, &prev);
1819
1820                         res = NULL;
1821                 } else {
1822                         *flags |= LDLM_FL_BLOCK_GRANTED;
1823                         granted = 1;
1824                 }
1825         }
1826         unlock_res_and_lock(lock);
1827
1828         if (granted)
1829                 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1830         if (node)
1831                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1832         RETURN(res);
1833 }
1834
1835 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1836 {
1837         struct obd_device *obd = NULL;
1838
1839         if (!((libcfs_debug | D_ERROR) & level))
1840                 return;
1841
1842         if (!lock) {
1843                 CDEBUG(level, "  NULL LDLM lock\n");
1844                 return;
1845         }
1846
1847         CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1848                lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1849                pos, lock->l_pid);
1850         if (lock->l_conn_export != NULL)
1851                 obd = lock->l_conn_export->exp_obd;
1852         if (lock->l_export && lock->l_export->exp_connection) {
1853                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1854                      libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1855                      lock->l_remote_handle.cookie);
1856         } else if (obd == NULL) {
1857                 CDEBUG(level, "  Node: local\n");
1858         } else {
1859                 struct obd_import *imp = obd->u.cli.cl_import;
1860                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1861                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1862                        lock->l_remote_handle.cookie);
1863         }
1864         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1865                   lock->l_resource,
1866                   lock->l_resource->lr_name.name[0],
1867                   lock->l_resource->lr_name.name[1],
1868                   lock->l_resource->lr_name.name[2]);
1869         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1870                "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1871                ldlm_lockname[lock->l_granted_mode],
1872                cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1873                lock->l_flags);
1874         if (lock->l_resource->lr_type == LDLM_EXTENT)
1875                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1876                        " (req "LPU64"-"LPU64")\n",
1877                        lock->l_policy_data.l_extent.start,
1878                        lock->l_policy_data.l_extent.end,
1879                        lock->l_req_extent.start, lock->l_req_extent.end);
1880         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1881                 CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
1882                        lock->l_policy_data.l_flock.pid,
1883                        lock->l_policy_data.l_flock.start,
1884                        lock->l_policy_data.l_flock.end);
1885        else if (lock->l_resource->lr_type == LDLM_IBITS)
1886                 CDEBUG(level, "  Bits: "LPX64"\n",
1887                        lock->l_policy_data.l_inodebits.bits);
1888 }
1889
1890 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1891 {
1892         struct ldlm_lock *lock;
1893
1894         if (!((libcfs_debug | D_ERROR) & level))
1895                 return;
1896
1897         lock = ldlm_handle2lock(lockh);
1898         if (lock == NULL)
1899                 return;
1900
1901         ldlm_lock_dump(D_OTHER, lock, 0);
1902
1903         LDLM_LOCK_PUT(lock);
1904 }
1905
1906 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1907                       struct libcfs_debug_msg_data *data, const char *fmt,
1908                       ...)
1909 {
1910         va_list args;
1911         cfs_debug_limit_state_t *cdls = data->msg_cdls;
1912
1913         va_start(args, fmt);
1914
1915         if (lock->l_resource == NULL) {
1916                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1917                                    data->msg_fn, data->msg_line, fmt, args,
1918                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1919                        "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1920                        LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1921                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1922                        lock->l_readers, lock->l_writers,
1923                        ldlm_lockname[lock->l_granted_mode],
1924                        ldlm_lockname[lock->l_req_mode],
1925                        lock->l_flags, lock->l_remote_handle.cookie,
1926                        lock->l_export ?
1927                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1928                        lock->l_pid, lock->l_callback_timeout);
1929                 va_end(args);
1930                 return;
1931         }
1932
1933         switch (lock->l_resource->lr_type) {
1934         case LDLM_EXTENT:
1935                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1936                                    data->msg_fn, data->msg_line, fmt, args,
1937                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1938                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1939                        "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1940                        " expref: %d pid: %u timeout %lu\n",
1941                        ldlm_lock_to_ns_name(lock), lock,
1942                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1943                        lock->l_readers, lock->l_writers,
1944                        ldlm_lockname[lock->l_granted_mode],
1945                        ldlm_lockname[lock->l_req_mode],
1946                        lock->l_resource->lr_name.name[0],
1947                        lock->l_resource->lr_name.name[1],
1948                        cfs_atomic_read(&lock->l_resource->lr_refcount),
1949                        ldlm_typename[lock->l_resource->lr_type],
1950                        lock->l_policy_data.l_extent.start,
1951                        lock->l_policy_data.l_extent.end,
1952                        lock->l_req_extent.start, lock->l_req_extent.end,
1953                        lock->l_flags, lock->l_remote_handle.cookie,
1954                        lock->l_export ?
1955                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1956                        lock->l_pid, lock->l_callback_timeout);
1957                 break;
1958
1959         case LDLM_FLOCK:
1960                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1961                                    data->msg_fn, data->msg_line, fmt, args,
1962                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1963                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1964                        "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1965                        " expref: %d pid: %u timeout: %lu\n",
1966                        ldlm_lock_to_ns_name(lock), lock,
1967                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1968                        lock->l_readers, lock->l_writers,
1969                        ldlm_lockname[lock->l_granted_mode],
1970                        ldlm_lockname[lock->l_req_mode],
1971                        lock->l_resource->lr_name.name[0],
1972                        lock->l_resource->lr_name.name[1],
1973                        cfs_atomic_read(&lock->l_resource->lr_refcount),
1974                        ldlm_typename[lock->l_resource->lr_type],
1975                        lock->l_policy_data.l_flock.pid,
1976                        lock->l_policy_data.l_flock.start,
1977                        lock->l_policy_data.l_flock.end,
1978                        lock->l_flags, lock->l_remote_handle.cookie,
1979                        lock->l_export ?
1980                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1981                        lock->l_pid, lock->l_callback_timeout);
1982                 break;
1983
1984         case LDLM_IBITS:
1985                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1986                                    data->msg_fn, data->msg_line, fmt, args,
1987                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1988                        "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1989                        "flags: "LPX64" remote: "LPX64" expref: %d "
1990                        "pid: %u timeout: %lu\n",
1991                        ldlm_lock_to_ns_name(lock),
1992                        lock, lock->l_handle.h_cookie,
1993                        cfs_atomic_read (&lock->l_refc),
1994                        lock->l_readers, lock->l_writers,
1995                        ldlm_lockname[lock->l_granted_mode],
1996                        ldlm_lockname[lock->l_req_mode],
1997                        lock->l_resource->lr_name.name[0],
1998                        lock->l_resource->lr_name.name[1],
1999                        lock->l_policy_data.l_inodebits.bits,
2000                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2001                        ldlm_typename[lock->l_resource->lr_type],
2002                        lock->l_flags, lock->l_remote_handle.cookie,
2003                        lock->l_export ?
2004                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2005                        lock->l_pid, lock->l_callback_timeout);
2006                 break;
2007
2008         default:
2009                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2010                                    data->msg_fn, data->msg_line, fmt, args,
2011                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2012                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2013                        "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2014                        ldlm_lock_to_ns_name(lock),
2015                        lock, lock->l_handle.h_cookie,
2016                        cfs_atomic_read (&lock->l_refc),
2017                        lock->l_readers, lock->l_writers,
2018                        ldlm_lockname[lock->l_granted_mode],
2019                        ldlm_lockname[lock->l_req_mode],
2020                        lock->l_resource->lr_name.name[0],
2021                        lock->l_resource->lr_name.name[1],
2022                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2023                        ldlm_typename[lock->l_resource->lr_type],
2024                        lock->l_flags, lock->l_remote_handle.cookie,
2025                        lock->l_export ?
2026                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2027                        lock->l_pid, lock->l_callback_timeout);
2028                 break;
2029         }
2030         va_end(args);
2031 }
2032 EXPORT_SYMBOL(_ldlm_lock_debug);