Whamcloud - gitweb
6a4d7fdd2ef39ac07b524332b45dbf2247d3bf70
[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_NO_LRU) &&
774                    !(lock->l_flags & LDLM_FL_BL_AST)) {
775
776                 LDLM_DEBUG(lock, "add lock into lru list");
777
778                 /* If this is a client-side namespace and this was the last
779                  * reference, put it on the LRU. */
780                 ldlm_lock_add_to_lru(lock);
781                 unlock_res_and_lock(lock);
782
783                 if (lock->l_flags & LDLM_FL_FAIL_LOC)
784                         OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
785
786                 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
787                  * are not supported by the server, otherwise, it is done on
788                  * enqueue. */
789                 if (!exp_connect_cancelset(lock->l_conn_export) &&
790                     !ns_connect_lru_resize(ns))
791                         ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
792         } else {
793                 LDLM_DEBUG(lock, "do not add lock into lru list");
794                 unlock_res_and_lock(lock);
795         }
796
797         EXIT;
798 }
799
800 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
801 {
802         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
803         LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
804         ldlm_lock_decref_internal(lock, mode);
805         LDLM_LOCK_PUT(lock);
806 }
807
808 /* This will drop a lock reference and mark it for destruction, but will not
809  * necessarily cancel the lock before returning. */
810 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
811 {
812         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
813         ENTRY;
814
815         LASSERT(lock != NULL);
816
817         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
818         lock_res_and_lock(lock);
819         lock->l_flags |= LDLM_FL_CBPENDING;
820         unlock_res_and_lock(lock);
821         ldlm_lock_decref_internal(lock, mode);
822         LDLM_LOCK_PUT(lock);
823 }
824
825 struct sl_insert_point {
826         cfs_list_t *res_link;
827         cfs_list_t *mode_link;
828         cfs_list_t *policy_link;
829 };
830
831 /*
832  * search_granted_lock
833  *
834  * Description:
835  *      Finds a position to insert the new lock.
836  * Parameters:
837  *      queue [input]:  the granted list where search acts on;
838  *      req [input]:    the lock whose position to be located;
839  *      prev [output]:  positions within 3 lists to insert @req to
840  * Return Value:
841  *      filled @prev
842  * NOTE: called by
843  *  - ldlm_grant_lock_with_skiplist
844  */
845 static void search_granted_lock(cfs_list_t *queue,
846                                 struct ldlm_lock *req,
847                                 struct sl_insert_point *prev)
848 {
849         cfs_list_t *tmp;
850         struct ldlm_lock *lock, *mode_end, *policy_end;
851         ENTRY;
852
853         cfs_list_for_each(tmp, queue) {
854                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
855
856                 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
857                                           struct ldlm_lock, l_sl_mode);
858
859                 if (lock->l_req_mode != req->l_req_mode) {
860                         /* jump to last lock of mode group */
861                         tmp = &mode_end->l_res_link;
862                         continue;
863                 }
864
865                 /* suitable mode group is found */
866                 if (lock->l_resource->lr_type == LDLM_PLAIN) {
867                         /* insert point is last lock of the mode group */
868                         prev->res_link = &mode_end->l_res_link;
869                         prev->mode_link = &mode_end->l_sl_mode;
870                         prev->policy_link = &req->l_sl_policy;
871                         EXIT;
872                         return;
873                 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
874                         for (;;) {
875                                 policy_end =
876                                         cfs_list_entry(lock->l_sl_policy.prev,
877                                                        struct ldlm_lock,
878                                                        l_sl_policy);
879
880                                 if (lock->l_policy_data.l_inodebits.bits ==
881                                     req->l_policy_data.l_inodebits.bits) {
882                                         /* insert point is last lock of
883                                          * the policy group */
884                                         prev->res_link =
885                                                 &policy_end->l_res_link;
886                                         prev->mode_link =
887                                                 &policy_end->l_sl_mode;
888                                         prev->policy_link =
889                                                 &policy_end->l_sl_policy;
890                                         EXIT;
891                                         return;
892                                 }
893
894                                 if (policy_end == mode_end)
895                                         /* done with mode group */
896                                         break;
897
898                                 /* go to next policy group within mode group */
899                                 tmp = policy_end->l_res_link.next;
900                                 lock = cfs_list_entry(tmp, struct ldlm_lock,
901                                                       l_res_link);
902                         }  /* loop over policy groups within the mode group */
903
904                         /* insert point is last lock of the mode group,
905                          * new policy group is started */
906                         prev->res_link = &mode_end->l_res_link;
907                         prev->mode_link = &mode_end->l_sl_mode;
908                         prev->policy_link = &req->l_sl_policy;
909                         EXIT;
910                         return;
911                 } else {
912                         LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
913                         LBUG();
914                 }
915         }
916
917         /* insert point is last lock on the queue,
918          * new mode group and new policy group are started */
919         prev->res_link = queue->prev;
920         prev->mode_link = &req->l_sl_mode;
921         prev->policy_link = &req->l_sl_policy;
922         EXIT;
923         return;
924 }
925
926 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
927                                        struct sl_insert_point *prev)
928 {
929         struct ldlm_resource *res = lock->l_resource;
930         ENTRY;
931
932         check_res_locked(res);
933
934         ldlm_resource_dump(D_INFO, res);
935         CDEBUG(D_OTHER, "About to add this lock:\n");
936         ldlm_lock_dump(D_OTHER, lock, 0);
937
938         if (lock->l_destroyed) {
939                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
940                 return;
941         }
942
943         LASSERT(cfs_list_empty(&lock->l_res_link));
944         LASSERT(cfs_list_empty(&lock->l_sl_mode));
945         LASSERT(cfs_list_empty(&lock->l_sl_policy));
946
947         cfs_list_add(&lock->l_res_link, prev->res_link);
948         cfs_list_add(&lock->l_sl_mode, prev->mode_link);
949         cfs_list_add(&lock->l_sl_policy, prev->policy_link);
950
951         EXIT;
952 }
953
954 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
955 {
956         struct sl_insert_point prev;
957         ENTRY;
958
959         LASSERT(lock->l_req_mode == lock->l_granted_mode);
960
961         search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
962         ldlm_granted_list_add_lock(lock, &prev);
963         EXIT;
964 }
965
966 /* NOTE: called by
967  *  - ldlm_lock_enqueue
968  *  - ldlm_reprocess_queue
969  *  - ldlm_lock_convert
970  *
971  * must be called with lr_lock held
972  */
973 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
974 {
975         struct ldlm_resource *res = lock->l_resource;
976         ENTRY;
977
978         check_res_locked(res);
979
980         lock->l_granted_mode = lock->l_req_mode;
981         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
982                 ldlm_grant_lock_with_skiplist(lock);
983         else if (res->lr_type == LDLM_EXTENT)
984                 ldlm_extent_add_lock(res, lock);
985         else
986                 ldlm_resource_add_lock(res, &res->lr_granted, lock);
987
988         if (lock->l_granted_mode < res->lr_most_restr)
989                 res->lr_most_restr = lock->l_granted_mode;
990
991         if (work_list && lock->l_completion_ast != NULL)
992                 ldlm_add_ast_work_item(lock, NULL, work_list);
993
994         ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
995         EXIT;
996 }
997
998 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
999  * comment above ldlm_lock_match */
1000 static struct ldlm_lock *search_queue(cfs_list_t *queue,
1001                                       ldlm_mode_t *mode,
1002                                       ldlm_policy_data_t *policy,
1003                                       struct ldlm_lock *old_lock,
1004                                       int flags, int unref)
1005 {
1006         struct ldlm_lock *lock;
1007         cfs_list_t       *tmp;
1008
1009         cfs_list_for_each(tmp, queue) {
1010                 ldlm_mode_t match;
1011
1012                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1013
1014                 if (lock == old_lock)
1015                         break;
1016
1017                 /* llite sometimes wants to match locks that will be
1018                  * canceled when their users drop, but we allow it to match
1019                  * if it passes in CBPENDING and the lock still has users.
1020                  * this is generally only going to be used by children
1021                  * whose parents already hold a lock so forward progress
1022                  * can still happen. */
1023                 if (lock->l_flags & LDLM_FL_CBPENDING &&
1024                     !(flags & LDLM_FL_CBPENDING))
1025                         continue;
1026                 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1027                     lock->l_readers == 0 && lock->l_writers == 0)
1028                         continue;
1029
1030                 if (!(lock->l_req_mode & *mode))
1031                         continue;
1032                 match = lock->l_req_mode;
1033
1034                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1035                     (lock->l_policy_data.l_extent.start >
1036                      policy->l_extent.start ||
1037                      lock->l_policy_data.l_extent.end < policy->l_extent.end))
1038                         continue;
1039
1040                 if (unlikely(match == LCK_GROUP) &&
1041                     lock->l_resource->lr_type == LDLM_EXTENT &&
1042                     lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1043                         continue;
1044
1045                 /* We match if we have existing lock with same or wider set
1046                    of bits. */
1047                 if (lock->l_resource->lr_type == LDLM_IBITS &&
1048                      ((lock->l_policy_data.l_inodebits.bits &
1049                       policy->l_inodebits.bits) !=
1050                       policy->l_inodebits.bits))
1051                         continue;
1052
1053                 if (!unref &&
1054                     (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
1055                         continue;
1056
1057                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1058                     !(lock->l_flags & LDLM_FL_LOCAL))
1059                         continue;
1060
1061                 if (flags & LDLM_FL_TEST_LOCK) {
1062                         LDLM_LOCK_GET(lock);
1063                         ldlm_lock_touch_in_lru(lock);
1064                 } else {
1065                         ldlm_lock_addref_internal_nolock(lock, match);
1066                 }
1067                 *mode = match;
1068                 return lock;
1069         }
1070
1071         return NULL;
1072 }
1073
1074 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1075 {
1076         lock->l_flags |= LDLM_FL_LVB_READY;
1077         cfs_waitq_signal(&lock->l_waitq);
1078 }
1079
1080 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1081 {
1082         lock_res_and_lock(lock);
1083         ldlm_lock_allow_match_locked(lock);
1084         unlock_res_and_lock(lock);
1085 }
1086
1087 /* Can be called in two ways:
1088  *
1089  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1090  * for a duplicate of.
1091  *
1092  * Otherwise, all of the fields must be filled in, to match against.
1093  *
1094  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1095  *     server (ie, connh is NULL)
1096  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1097  *     list will be considered
1098  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1099  *     to be canceled can still be matched as long as they still have reader
1100  *     or writer refernces
1101  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1102  *     just tell us if we would have matched.
1103  *
1104  * Returns 1 if it finds an already-existing lock that is compatible; in this
1105  * case, lockh is filled in with a addref()ed lock
1106  *
1107  * we also check security context, if that failed we simply return 0 (to keep
1108  * caller code unchanged), the context failure will be discovered by caller
1109  * sometime later.
1110  */
1111 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1112                             const struct ldlm_res_id *res_id, ldlm_type_t type,
1113                             ldlm_policy_data_t *policy, ldlm_mode_t mode,
1114                             struct lustre_handle *lockh, int unref)
1115 {
1116         struct ldlm_resource *res;
1117         struct ldlm_lock *lock, *old_lock = NULL;
1118         int rc = 0;
1119         ENTRY;
1120
1121         if (ns == NULL) {
1122                 old_lock = ldlm_handle2lock(lockh);
1123                 LASSERT(old_lock);
1124
1125                 ns = ldlm_lock_to_ns(old_lock);
1126                 res_id = &old_lock->l_resource->lr_name;
1127                 type = old_lock->l_resource->lr_type;
1128                 mode = old_lock->l_req_mode;
1129         }
1130
1131         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1132         if (res == NULL) {
1133                 LASSERT(old_lock == NULL);
1134                 RETURN(0);
1135         }
1136
1137         LDLM_RESOURCE_ADDREF(res);
1138         lock_res(res);
1139
1140         lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1141                             flags, unref);
1142         if (lock != NULL)
1143                 GOTO(out, rc = 1);
1144         if (flags & LDLM_FL_BLOCK_GRANTED)
1145                 GOTO(out, rc = 0);
1146         lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1147                             flags, unref);
1148         if (lock != NULL)
1149                 GOTO(out, rc = 1);
1150         lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1151                             flags, unref);
1152         if (lock != NULL)
1153                 GOTO(out, rc = 1);
1154
1155         EXIT;
1156  out:
1157         unlock_res(res);
1158         LDLM_RESOURCE_DELREF(res);
1159         ldlm_resource_putref(res);
1160
1161         if (lock) {
1162                 ldlm_lock2handle(lock, lockh);
1163                 if ((flags & LDLM_FL_LVB_READY) &&
1164                     (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1165                         struct l_wait_info lwi;
1166                         if (lock->l_completion_ast) {
1167                                 int err = lock->l_completion_ast(lock,
1168                                                           LDLM_FL_WAIT_NOREPROC,
1169                                                                  NULL);
1170                                 if (err) {
1171                                         if (flags & LDLM_FL_TEST_LOCK)
1172                                                 LDLM_LOCK_RELEASE(lock);
1173                                         else
1174                                                 ldlm_lock_decref_internal(lock,
1175                                                                           mode);
1176                                         rc = 0;
1177                                         goto out2;
1178                                 }
1179                         }
1180
1181                         lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1182                                                NULL, LWI_ON_SIGNAL_NOOP, NULL);
1183
1184                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1185                         l_wait_event(lock->l_waitq,
1186                                      (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1187                 }
1188         }
1189  out2:
1190         if (rc) {
1191                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1192                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1193                                 res_id->name[2] : policy->l_extent.start,
1194                            (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1195                                 res_id->name[3] : policy->l_extent.end);
1196
1197                 /* check user's security context */
1198                 if (lock->l_conn_export &&
1199                     sptlrpc_import_check_ctx(
1200                                 class_exp2cliimp(lock->l_conn_export))) {
1201                         if (!(flags & LDLM_FL_TEST_LOCK))
1202                                 ldlm_lock_decref_internal(lock, mode);
1203                         rc = 0;
1204                 }
1205
1206                 if (flags & LDLM_FL_TEST_LOCK)
1207                         LDLM_LOCK_RELEASE(lock);
1208
1209         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1210                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1211                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1212                                   type, mode, res_id->name[0], res_id->name[1],
1213                                   (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1214                                         res_id->name[2] :policy->l_extent.start,
1215                                   (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1216                                         res_id->name[3] : policy->l_extent.end);
1217         }
1218         if (old_lock)
1219                 LDLM_LOCK_PUT(old_lock);
1220
1221         return rc ? mode : 0;
1222 }
1223
1224 /* Returns a referenced lock */
1225 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1226                                    const struct ldlm_res_id *res_id,
1227                                    ldlm_type_t type,
1228                                    ldlm_mode_t mode,
1229                                    const struct ldlm_callback_suite *cbs,
1230                                    void *data, __u32 lvb_len)
1231 {
1232         struct ldlm_lock *lock;
1233         struct ldlm_resource *res;
1234         ENTRY;
1235
1236         res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1237         if (res == NULL)
1238                 RETURN(NULL);
1239
1240         lock = ldlm_lock_new(res);
1241
1242         if (lock == NULL)
1243                 RETURN(NULL);
1244
1245         lock->l_req_mode = mode;
1246         lock->l_ast_data = data;
1247         lock->l_pid = cfs_curproc_pid();
1248         lock->l_ns_srv = ns_is_server(ns);
1249         if (cbs) {
1250                 lock->l_blocking_ast = cbs->lcs_blocking;
1251                 lock->l_completion_ast = cbs->lcs_completion;
1252                 lock->l_glimpse_ast = cbs->lcs_glimpse;
1253                 lock->l_weigh_ast = cbs->lcs_weigh;
1254         }
1255
1256         lock->l_tree_node = NULL;
1257         /* if this is the extent lock, allocate the interval tree node */
1258         if (type == LDLM_EXTENT) {
1259                 if (ldlm_interval_alloc(lock) == NULL)
1260                         GOTO(out, 0);
1261         }
1262
1263         if (lvb_len) {
1264                 lock->l_lvb_len = lvb_len;
1265                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1266                 if (lock->l_lvb_data == NULL)
1267                         GOTO(out, 0);
1268         }
1269
1270         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1271                 GOTO(out, 0);
1272
1273         RETURN(lock);
1274
1275 out:
1276         ldlm_lock_destroy(lock);
1277         LDLM_LOCK_RELEASE(lock);
1278         return NULL;
1279 }
1280
1281 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1282                                struct ldlm_lock **lockp,
1283                                void *cookie, int *flags)
1284 {
1285         struct ldlm_lock *lock = *lockp;
1286         struct ldlm_resource *res = lock->l_resource;
1287         int local = ns_is_client(ldlm_res_to_ns(res));
1288         ldlm_processing_policy policy;
1289         ldlm_error_t rc = ELDLM_OK;
1290         struct ldlm_interval *node = NULL;
1291         ENTRY;
1292
1293         lock->l_last_activity = cfs_time_current_sec();
1294         /* policies are not executed on the client or during replay */
1295         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1296             && !local && ns->ns_policy) {
1297                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1298                                    NULL);
1299                 if (rc == ELDLM_LOCK_REPLACED) {
1300                         /* The lock that was returned has already been granted,
1301                          * and placed into lockp.  If it's not the same as the
1302                          * one we passed in, then destroy the old one and our
1303                          * work here is done. */
1304                         if (lock != *lockp) {
1305                                 ldlm_lock_destroy(lock);
1306                                 LDLM_LOCK_RELEASE(lock);
1307                         }
1308                         *flags |= LDLM_FL_LOCK_CHANGED;
1309                         RETURN(0);
1310                 } else if (rc != ELDLM_OK ||
1311                            (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1312                         ldlm_lock_destroy(lock);
1313                         RETURN(rc);
1314                 }
1315         }
1316
1317         /* For a replaying lock, it might be already in granted list. So
1318          * unlinking the lock will cause the interval node to be freed, we
1319          * have to allocate the interval node early otherwise we can't regrant
1320          * this lock in the future. - jay */
1321         if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1322                 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1323
1324         lock_res_and_lock(lock);
1325         if (local && lock->l_req_mode == lock->l_granted_mode) {
1326                 /* The server returned a blocked lock, but it was granted
1327                  * before we got a chance to actually enqueue it.  We don't
1328                  * need to do anything else. */
1329                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1330                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1331                 GOTO(out, ELDLM_OK);
1332         }
1333
1334         ldlm_resource_unlink_lock(lock);
1335         if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1336                 if (node == NULL) {
1337                         ldlm_lock_destroy_nolock(lock);
1338                         GOTO(out, rc = -ENOMEM);
1339                 }
1340
1341                 CFS_INIT_LIST_HEAD(&node->li_group);
1342                 ldlm_interval_attach(node, lock);
1343                 node = NULL;
1344         }
1345
1346         /* Some flags from the enqueue want to make it into the AST, via the
1347          * lock's l_flags. */
1348         lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1349
1350         /* This distinction between local lock trees is very important; a client
1351          * namespace only has information about locks taken by that client, and
1352          * thus doesn't have enough information to decide for itself if it can
1353          * be granted (below).  In this case, we do exactly what the server
1354          * tells us to do, as dictated by the 'flags'.
1355          *
1356          * We do exactly the same thing during recovery, when the server is
1357          * more or less trusting the clients not to lie.
1358          *
1359          * FIXME (bug 268): Detect obvious lies by checking compatibility in
1360          * granted/converting queues. */
1361         if (local) {
1362                 if (*flags & LDLM_FL_BLOCK_CONV)
1363                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1364                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1365                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1366                 else
1367                         ldlm_grant_lock(lock, NULL);
1368                 GOTO(out, ELDLM_OK);
1369         } else if (*flags & LDLM_FL_REPLAY) {
1370                 if (*flags & LDLM_FL_BLOCK_CONV) {
1371                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1372                         GOTO(out, ELDLM_OK);
1373                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1374                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1375                         GOTO(out, ELDLM_OK);
1376                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1377                         ldlm_grant_lock(lock, NULL);
1378                         GOTO(out, ELDLM_OK);
1379                 }
1380                 /* If no flags, fall through to normal enqueue path. */
1381         }
1382
1383         policy = ldlm_processing_policy_table[res->lr_type];
1384         policy(lock, flags, 1, &rc, NULL);
1385         GOTO(out, rc);
1386 out:
1387         unlock_res_and_lock(lock);
1388         if (node)
1389                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1390         return rc;
1391 }
1392
1393 /* Must be called with namespace taken: queue is waiting or converting. */
1394 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1395                          cfs_list_t *work_list)
1396 {
1397         cfs_list_t *tmp, *pos;
1398         ldlm_processing_policy policy;
1399         int flags;
1400         int rc = LDLM_ITER_CONTINUE;
1401         ldlm_error_t err;
1402         ENTRY;
1403
1404         check_res_locked(res);
1405
1406         policy = ldlm_processing_policy_table[res->lr_type];
1407         LASSERT(policy);
1408
1409         cfs_list_for_each_safe(tmp, pos, queue) {
1410                 struct ldlm_lock *pending;
1411                 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1412
1413                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1414
1415                 flags = 0;
1416                 rc = policy(pending, &flags, 0, &err, work_list);
1417                 if (rc != LDLM_ITER_CONTINUE)
1418                         break;
1419         }
1420
1421         RETURN(rc);
1422 }
1423
1424 /* Helper function for ldlm_run_ast_work().
1425  *
1426  * Send an existing rpc set specified by @arg->set and then
1427  * destroy it. Create new one if @do_create flag is set. */
1428 static int ldlm_deliver_cb_set(struct ldlm_cb_set_arg *arg, int do_create)
1429 {
1430         int rc = 0;
1431         ENTRY;
1432
1433         if (arg->set) {
1434                 ptlrpc_set_wait(arg->set);
1435                 if (arg->type == LDLM_BL_CALLBACK)
1436                         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1437                 ptlrpc_set_destroy(arg->set);
1438                 arg->set = NULL;
1439                 arg->rpcs = 0;
1440         }
1441
1442         if (do_create) {
1443                 arg->set = ptlrpc_prep_set();
1444                 if (arg->set == NULL)
1445                         rc = -ENOMEM;
1446         }
1447
1448         RETURN(rc);
1449 }
1450
1451 static int
1452 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1453 {
1454         struct ldlm_lock_desc d;
1455         struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1456                                                 l_bl_ast);
1457         int rc;
1458         ENTRY;
1459
1460         /* nobody should touch l_bl_ast */
1461         lock_res_and_lock(lock);
1462         cfs_list_del_init(&lock->l_bl_ast);
1463
1464         LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1465         LASSERT(lock->l_bl_ast_run == 0);
1466         LASSERT(lock->l_blocking_lock);
1467         lock->l_bl_ast_run++;
1468         unlock_res_and_lock(lock);
1469
1470         ldlm_lock2desc(lock->l_blocking_lock, &d);
1471
1472         rc = lock->l_blocking_ast(lock, &d, (void *)arg,
1473                                   LDLM_CB_BLOCKING);
1474         LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1475         lock->l_blocking_lock = NULL;
1476         LDLM_LOCK_RELEASE(lock);
1477
1478         RETURN(rc);
1479 }
1480
1481 static int
1482 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1483 {
1484         struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1485         ldlm_completion_callback completion_callback;
1486         int rc = 0;
1487         ENTRY;
1488
1489         /* It's possible to receive a completion AST before we've set
1490          * the l_completion_ast pointer: either because the AST arrived
1491          * before the reply, or simply because there's a small race
1492          * window between receiving the reply and finishing the local
1493          * enqueue. (bug 842)
1494          *
1495          * This can't happen with the blocking_ast, however, because we
1496          * will never call the local blocking_ast until we drop our
1497          * reader/writer reference, which we won't do until we get the
1498          * reply and finish enqueueing. */
1499
1500         /* nobody should touch l_cp_ast */
1501         lock_res_and_lock(lock);
1502         cfs_list_del_init(&lock->l_cp_ast);
1503         LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1504         /* save l_completion_ast since it can be changed by
1505          * mds_intent_policy(), see bug 14225 */
1506         completion_callback = lock->l_completion_ast;
1507         lock->l_flags &= ~LDLM_FL_CP_REQD;
1508         unlock_res_and_lock(lock);
1509
1510         if (completion_callback != NULL)
1511                 rc = completion_callback(lock, 0, (void *)arg);
1512         LDLM_LOCK_RELEASE(lock);
1513
1514         RETURN(rc);
1515 }
1516
1517 static int
1518 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1519 {
1520         struct ldlm_lock_desc desc;
1521         struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1522                                                 l_rk_ast);
1523         int rc;
1524         ENTRY;
1525
1526         cfs_list_del_init(&lock->l_rk_ast);
1527
1528         /* the desc just pretend to exclusive */
1529         ldlm_lock2desc(lock, &desc);
1530         desc.l_req_mode = LCK_EX;
1531         desc.l_granted_mode = 0;
1532
1533         rc = lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1534         LDLM_LOCK_RELEASE(lock);
1535
1536         RETURN(rc);
1537 }
1538
1539 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
1540                       ldlm_desc_ast_t ast_type)
1541 {
1542         struct ldlm_cb_set_arg arg = { 0 };
1543         cfs_list_t *tmp, *pos;
1544         int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1545         unsigned int max_ast_count;
1546         int rc;
1547         ENTRY;
1548
1549         if (cfs_list_empty(rpc_list))
1550                 RETURN(0);
1551
1552         rc = ldlm_deliver_cb_set(&arg, 1);
1553         if (rc != 0)
1554                 RETURN(rc);
1555
1556         switch (ast_type) {
1557         case LDLM_WORK_BL_AST:
1558                 arg.type = LDLM_BL_CALLBACK;
1559                 work_ast_lock = ldlm_work_bl_ast_lock;
1560                 break;
1561         case LDLM_WORK_CP_AST:
1562                 arg.type = LDLM_CP_CALLBACK;
1563                 work_ast_lock = ldlm_work_cp_ast_lock;
1564                 break;
1565         case LDLM_WORK_REVOKE_AST:
1566                 arg.type = LDLM_BL_CALLBACK;
1567                 work_ast_lock = ldlm_work_revoke_ast_lock;
1568                 break;
1569         default:
1570                 LBUG();
1571         }
1572
1573         max_ast_count = ns->ns_max_parallel_ast ? : UINT_MAX;
1574
1575         cfs_list_for_each_safe(tmp, pos, rpc_list) {
1576                 (void)work_ast_lock(tmp, &arg);
1577                 if (arg.rpcs > max_ast_count) {
1578                         rc = ldlm_deliver_cb_set(&arg, 1);
1579                         if (rc != 0)
1580                                 break;
1581                 }
1582         }
1583
1584         (void)ldlm_deliver_cb_set(&arg, 0);
1585
1586         if (rc == 0 && cfs_atomic_read(&arg.restart))
1587                 rc = -ERESTART;
1588
1589         RETURN(rc);
1590 }
1591
1592 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1593 {
1594         ldlm_reprocess_all(res);
1595         return LDLM_ITER_CONTINUE;
1596 }
1597
1598 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1599                               cfs_hlist_node_t *hnode, void *arg)
1600 {
1601         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1602         int    rc;
1603
1604         rc = reprocess_one_queue(res, arg);
1605
1606         return rc == LDLM_ITER_STOP;
1607 }
1608
1609 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1610 {
1611         ENTRY;
1612
1613         if (ns != NULL) {
1614                 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1615                                          ldlm_reprocess_res, NULL);
1616         }
1617         EXIT;
1618 }
1619
1620 void ldlm_reprocess_all(struct ldlm_resource *res)
1621 {
1622         CFS_LIST_HEAD(rpc_list);
1623         int rc;
1624         ENTRY;
1625
1626         /* Local lock trees don't get reprocessed. */
1627         if (ns_is_client(ldlm_res_to_ns(res))) {
1628                 EXIT;
1629                 return;
1630         }
1631
1632  restart:
1633         lock_res(res);
1634         rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1635         if (rc == LDLM_ITER_CONTINUE)
1636                 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1637         unlock_res(res);
1638
1639         rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
1640                                LDLM_WORK_CP_AST);
1641         if (rc == -ERESTART) {
1642                 LASSERT(cfs_list_empty(&rpc_list));
1643                 goto restart;
1644         }
1645         EXIT;
1646 }
1647
1648 void ldlm_cancel_callback(struct ldlm_lock *lock)
1649 {
1650         check_res_locked(lock->l_resource);
1651         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1652                 lock->l_flags |= LDLM_FL_CANCEL;
1653                 if (lock->l_blocking_ast) {
1654                         // l_check_no_ns_lock(ns);
1655                         unlock_res_and_lock(lock);
1656                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1657                                              LDLM_CB_CANCELING);
1658                         lock_res_and_lock(lock);
1659                 } else {
1660                         LDLM_DEBUG(lock, "no blocking ast");
1661                 }
1662         }
1663         lock->l_flags |= LDLM_FL_BL_DONE;
1664 }
1665
1666 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1667 {
1668         if (req->l_resource->lr_type != LDLM_PLAIN &&
1669             req->l_resource->lr_type != LDLM_IBITS)
1670                 return;
1671
1672         cfs_list_del_init(&req->l_sl_policy);
1673         cfs_list_del_init(&req->l_sl_mode);
1674 }
1675
1676 void ldlm_lock_cancel(struct ldlm_lock *lock)
1677 {
1678         struct ldlm_resource *res;
1679         struct ldlm_namespace *ns;
1680         ENTRY;
1681
1682         lock_res_and_lock(lock);
1683
1684         res = lock->l_resource;
1685         ns  = ldlm_res_to_ns(res);
1686
1687         /* Please do not, no matter how tempting, remove this LBUG without
1688          * talking to me first. -phik */
1689         if (lock->l_readers || lock->l_writers) {
1690                 LDLM_ERROR(lock, "lock still has references");
1691                 LBUG();
1692         }
1693
1694         ldlm_del_waiting_lock(lock);
1695
1696         /* Releases cancel callback. */
1697         ldlm_cancel_callback(lock);
1698
1699         /* Yes, second time, just in case it was added again while we were
1700            running with no res lock in ldlm_cancel_callback */
1701         ldlm_del_waiting_lock(lock);
1702         ldlm_resource_unlink_lock(lock);
1703         ldlm_lock_destroy_nolock(lock);
1704
1705         if (lock->l_granted_mode == lock->l_req_mode)
1706                 ldlm_pool_del(&ns->ns_pool, lock);
1707
1708         /* Make sure we will not be called again for same lock what is possible
1709          * if not to zero out lock->l_granted_mode */
1710         lock->l_granted_mode = LCK_MINMODE;
1711         unlock_res_and_lock(lock);
1712
1713         EXIT;
1714 }
1715
1716 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1717 {
1718         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1719         int rc = -EINVAL;
1720         ENTRY;
1721
1722         if (lock) {
1723                 if (lock->l_ast_data == NULL)
1724                         lock->l_ast_data = data;
1725                 if (lock->l_ast_data == data)
1726                         rc = 0;
1727                 LDLM_LOCK_PUT(lock);
1728         }
1729         RETURN(rc);
1730 }
1731 EXPORT_SYMBOL(ldlm_lock_set_data);
1732
1733 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1734                                     cfs_hlist_node_t *hnode, void *data)
1735
1736 {
1737         struct obd_export    *exp  = data;
1738         struct ldlm_lock     *lock = cfs_hash_object(hs, hnode);
1739         struct ldlm_resource *res;
1740
1741         res = ldlm_resource_getref(lock->l_resource);
1742         LDLM_LOCK_GET(lock);
1743
1744         LDLM_DEBUG(lock, "export %p", exp);
1745         ldlm_res_lvbo_update(res, NULL, 1);
1746         ldlm_lock_cancel(lock);
1747         ldlm_reprocess_all(res);
1748         ldlm_resource_putref(res);
1749         LDLM_LOCK_RELEASE(lock);
1750         return 0;
1751 }
1752
1753 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1754 {
1755         cfs_hash_for_each_empty(exp->exp_lock_hash,
1756                                 ldlm_cancel_locks_for_export_cb, exp);
1757 }
1758
1759 /**
1760  * Downgrade an exclusive lock.
1761  *
1762  * A fast variant of ldlm_lock_convert for convertion of exclusive
1763  * locks. The convertion is always successful.
1764  *
1765  * \param lock A lock to convert
1766  * \param new_mode new lock mode
1767  */
1768 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1769 {
1770         ENTRY;
1771
1772         LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1773         LASSERT(new_mode == LCK_COS);
1774
1775         lock_res_and_lock(lock);
1776         ldlm_resource_unlink_lock(lock);
1777         /*
1778          * Remove the lock from pool as it will be added again in
1779          * ldlm_grant_lock() called below.
1780          */
1781         ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1782
1783         lock->l_req_mode = new_mode;
1784         ldlm_grant_lock(lock, NULL);
1785         unlock_res_and_lock(lock);
1786         ldlm_reprocess_all(lock->l_resource);
1787
1788         EXIT;
1789 }
1790
1791 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1792                                         __u32 *flags)
1793 {
1794         CFS_LIST_HEAD(rpc_list);
1795         struct ldlm_resource *res;
1796         struct ldlm_namespace *ns;
1797         int granted = 0;
1798         int old_mode, rc;
1799         struct sl_insert_point prev;
1800         ldlm_error_t err;
1801         struct ldlm_interval *node;
1802         ENTRY;
1803
1804         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1805                 *flags |= LDLM_FL_BLOCK_GRANTED;
1806                 RETURN(lock->l_resource);
1807         }
1808
1809         /* I can't check the type of lock here because the bitlock of lock
1810          * is not held here, so do the allocation blindly. -jay */
1811         OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1812         if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
1813                 RETURN(NULL);
1814
1815         LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1816                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1817
1818         lock_res_and_lock(lock);
1819
1820         res = lock->l_resource;
1821         ns  = ldlm_res_to_ns(res);
1822
1823         old_mode = lock->l_req_mode;
1824         lock->l_req_mode = new_mode;
1825         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1826                 /* remember the lock position where the lock might be
1827                  * added back to the granted list later and also
1828                  * remember the join mode for skiplist fixing. */
1829                 prev.res_link = lock->l_res_link.prev;
1830                 prev.mode_link = lock->l_sl_mode.prev;
1831                 prev.policy_link = lock->l_sl_policy.prev;
1832                 ldlm_resource_unlink_lock(lock);
1833         } else {
1834                 ldlm_resource_unlink_lock(lock);
1835                 if (res->lr_type == LDLM_EXTENT) {
1836                         /* FIXME: ugly code, I have to attach the lock to a
1837                          * interval node again since perhaps it will be granted
1838                          * soon */
1839                         CFS_INIT_LIST_HEAD(&node->li_group);
1840                         ldlm_interval_attach(node, lock);
1841                         node = NULL;
1842                 }
1843         }
1844
1845         /*
1846          * Remove old lock from the pool before adding the lock with new
1847          * mode below in ->policy()
1848          */
1849         ldlm_pool_del(&ns->ns_pool, lock);
1850
1851         /* If this is a local resource, put it on the appropriate list. */
1852         if (ns_is_client(ldlm_res_to_ns(res))) {
1853                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1854                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1855                 } else {
1856                         /* This should never happen, because of the way the
1857                          * server handles conversions. */
1858                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1859                                    *flags);
1860                         LBUG();
1861
1862                         ldlm_grant_lock(lock, &rpc_list);
1863                         granted = 1;
1864                         /* FIXME: completion handling not with lr_lock held ! */
1865                         if (lock->l_completion_ast)
1866                                 lock->l_completion_ast(lock, 0, NULL);
1867                 }
1868         } else {
1869                 int pflags = 0;
1870                 ldlm_processing_policy policy;
1871                 policy = ldlm_processing_policy_table[res->lr_type];
1872                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1873                 if (rc == LDLM_ITER_STOP) {
1874                         lock->l_req_mode = old_mode;
1875                         if (res->lr_type == LDLM_EXTENT)
1876                                 ldlm_extent_add_lock(res, lock);
1877                         else
1878                                 ldlm_granted_list_add_lock(lock, &prev);
1879
1880                         res = NULL;
1881                 } else {
1882                         *flags |= LDLM_FL_BLOCK_GRANTED;
1883                         granted = 1;
1884                 }
1885         }
1886         unlock_res_and_lock(lock);
1887
1888         if (granted)
1889                 ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
1890         if (node)
1891                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1892         RETURN(res);
1893 }
1894
1895 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1896 {
1897         struct obd_device *obd = NULL;
1898
1899         if (!((libcfs_debug | D_ERROR) & level))
1900                 return;
1901
1902         if (!lock) {
1903                 CDEBUG(level, "  NULL LDLM lock\n");
1904                 return;
1905         }
1906
1907         CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1908                lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1909                pos, lock->l_pid);
1910         if (lock->l_conn_export != NULL)
1911                 obd = lock->l_conn_export->exp_obd;
1912         if (lock->l_export && lock->l_export->exp_connection) {
1913                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1914                      libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1915                      lock->l_remote_handle.cookie);
1916         } else if (obd == NULL) {
1917                 CDEBUG(level, "  Node: local\n");
1918         } else {
1919                 struct obd_import *imp = obd->u.cli.cl_import;
1920                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1921                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1922                        lock->l_remote_handle.cookie);
1923         }
1924         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1925                   lock->l_resource,
1926                   lock->l_resource->lr_name.name[0],
1927                   lock->l_resource->lr_name.name[1],
1928                   lock->l_resource->lr_name.name[2]);
1929         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1930                "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1931                ldlm_lockname[lock->l_granted_mode],
1932                cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1933                lock->l_flags);
1934         if (lock->l_resource->lr_type == LDLM_EXTENT)
1935                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1936                        " (req "LPU64"-"LPU64")\n",
1937                        lock->l_policy_data.l_extent.start,
1938                        lock->l_policy_data.l_extent.end,
1939                        lock->l_req_extent.start, lock->l_req_extent.end);
1940         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1941                 CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
1942                        lock->l_policy_data.l_flock.pid,
1943                        lock->l_policy_data.l_flock.start,
1944                        lock->l_policy_data.l_flock.end);
1945        else if (lock->l_resource->lr_type == LDLM_IBITS)
1946                 CDEBUG(level, "  Bits: "LPX64"\n",
1947                        lock->l_policy_data.l_inodebits.bits);
1948 }
1949
1950 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1951 {
1952         struct ldlm_lock *lock;
1953
1954         if (!((libcfs_debug | D_ERROR) & level))
1955                 return;
1956
1957         lock = ldlm_handle2lock(lockh);
1958         if (lock == NULL)
1959                 return;
1960
1961         ldlm_lock_dump(D_OTHER, lock, 0);
1962
1963         LDLM_LOCK_PUT(lock);
1964 }
1965
1966 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1967                       struct libcfs_debug_msg_data *data, const char *fmt,
1968                       ...)
1969 {
1970         va_list args;
1971         cfs_debug_limit_state_t *cdls = data->msg_cdls;
1972
1973         va_start(args, fmt);
1974
1975         if (lock->l_resource == NULL) {
1976                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1977                                    data->msg_fn, data->msg_line, fmt, args,
1978                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1979                        "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1980                        LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1981                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1982                        lock->l_readers, lock->l_writers,
1983                        ldlm_lockname[lock->l_granted_mode],
1984                        ldlm_lockname[lock->l_req_mode],
1985                        lock->l_flags, lock->l_remote_handle.cookie,
1986                        lock->l_export ?
1987                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1988                        lock->l_pid, lock->l_callback_timeout);
1989                 va_end(args);
1990                 return;
1991         }
1992
1993         switch (lock->l_resource->lr_type) {
1994         case LDLM_EXTENT:
1995                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1996                                    data->msg_fn, data->msg_line, fmt, args,
1997                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1998                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1999                        "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
2000                        " expref: %d pid: %u timeout %lu\n",
2001                        ldlm_lock_to_ns_name(lock), lock,
2002                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2003                        lock->l_readers, lock->l_writers,
2004                        ldlm_lockname[lock->l_granted_mode],
2005                        ldlm_lockname[lock->l_req_mode],
2006                        lock->l_resource->lr_name.name[0],
2007                        lock->l_resource->lr_name.name[1],
2008                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2009                        ldlm_typename[lock->l_resource->lr_type],
2010                        lock->l_policy_data.l_extent.start,
2011                        lock->l_policy_data.l_extent.end,
2012                        lock->l_req_extent.start, lock->l_req_extent.end,
2013                        lock->l_flags, lock->l_remote_handle.cookie,
2014                        lock->l_export ?
2015                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2016                        lock->l_pid, lock->l_callback_timeout);
2017                 break;
2018
2019         case LDLM_FLOCK:
2020                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2021                                    data->msg_fn, data->msg_line, fmt, args,
2022                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2023                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
2024                        "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
2025                        " expref: %d pid: %u timeout: %lu\n",
2026                        ldlm_lock_to_ns_name(lock), lock,
2027                        lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2028                        lock->l_readers, lock->l_writers,
2029                        ldlm_lockname[lock->l_granted_mode],
2030                        ldlm_lockname[lock->l_req_mode],
2031                        lock->l_resource->lr_name.name[0],
2032                        lock->l_resource->lr_name.name[1],
2033                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2034                        ldlm_typename[lock->l_resource->lr_type],
2035                        lock->l_policy_data.l_flock.pid,
2036                        lock->l_policy_data.l_flock.start,
2037                        lock->l_policy_data.l_flock.end,
2038                        lock->l_flags, lock->l_remote_handle.cookie,
2039                        lock->l_export ?
2040                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2041                        lock->l_pid, lock->l_callback_timeout);
2042                 break;
2043
2044         case LDLM_IBITS:
2045                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2046                                    data->msg_fn, data->msg_line, fmt, args,
2047                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2048                        "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
2049                        "flags: "LPX64" remote: "LPX64" expref: %d "
2050                        "pid: %u timeout: %lu\n",
2051                        ldlm_lock_to_ns_name(lock),
2052                        lock, lock->l_handle.h_cookie,
2053                        cfs_atomic_read (&lock->l_refc),
2054                        lock->l_readers, lock->l_writers,
2055                        ldlm_lockname[lock->l_granted_mode],
2056                        ldlm_lockname[lock->l_req_mode],
2057                        lock->l_resource->lr_name.name[0],
2058                        lock->l_resource->lr_name.name[1],
2059                        lock->l_policy_data.l_inodebits.bits,
2060                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2061                        ldlm_typename[lock->l_resource->lr_type],
2062                        lock->l_flags, lock->l_remote_handle.cookie,
2063                        lock->l_export ?
2064                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2065                        lock->l_pid, lock->l_callback_timeout);
2066                 break;
2067
2068         default:
2069                 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2070                                    data->msg_fn, data->msg_line, fmt, args,
2071                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2072                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2073                        "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2074                        ldlm_lock_to_ns_name(lock),
2075                        lock, lock->l_handle.h_cookie,
2076                        cfs_atomic_read (&lock->l_refc),
2077                        lock->l_readers, lock->l_writers,
2078                        ldlm_lockname[lock->l_granted_mode],
2079                        ldlm_lockname[lock->l_req_mode],
2080                        lock->l_resource->lr_name.name[0],
2081                        lock->l_resource->lr_name.name[1],
2082                        cfs_atomic_read(&lock->l_resource->lr_refcount),
2083                        ldlm_typename[lock->l_resource->lr_type],
2084                        lock->l_flags, lock->l_remote_handle.cookie,
2085                        lock->l_export ?
2086                        cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2087                        lock->l_pid, lock->l_callback_timeout);
2088                 break;
2089         }
2090         va_end(args);
2091 }
2092 EXPORT_SYMBOL(_ldlm_lock_debug);