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