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