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