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