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