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