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