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