Whamcloud - gitweb
7a601e8f91d5cb9e29ab500cefd950cc44653eff
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.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_lockd.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 #else
47 # include <liblustre.h>
48 #endif
49
50 #include <lustre_dlm.h>
51 #include <obd_class.h>
52 #include <libcfs/list.h>
53 #include "ldlm_internal.h"
54
55 static int ldlm_num_threads;
56 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
57                 "number of DLM service threads to start");
58
59 static char *ldlm_cpts;
60 CFS_MODULE_PARM(ldlm_cpts, "s", charp, 0444,
61                 "CPU partitions ldlm threads should run on");
62
63 extern cfs_mem_cache_t *ldlm_resource_slab;
64 extern cfs_mem_cache_t *ldlm_lock_slab;
65 static cfs_mutex_t      ldlm_ref_mutex;
66 static int ldlm_refcount;
67
68 struct ldlm_cb_async_args {
69         struct ldlm_cb_set_arg *ca_set_arg;
70         struct ldlm_lock       *ca_lock;
71 };
72
73 /* LDLM state */
74
75 static struct ldlm_state *ldlm_state;
76
77 inline cfs_time_t round_timeout(cfs_time_t timeout)
78 {
79         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
80 }
81
82 /* timeout for initial callback (AST) reply (bz10399) */
83 static inline unsigned int ldlm_get_rq_timeout(void)
84 {
85         /* Non-AT value */
86         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
87
88         return timeout < 1 ? 1 : timeout;
89 }
90
91 #define ELT_STOPPED   0
92 #define ELT_READY     1
93 #define ELT_TERMINATE 2
94
95 struct ldlm_bl_pool {
96         cfs_spinlock_t          blp_lock;
97
98         /*
99          * blp_prio_list is used for callbacks that should be handled
100          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
101          * see bug 13843
102          */
103         cfs_list_t              blp_prio_list;
104
105         /*
106          * blp_list is used for all other callbacks which are likely
107          * to take longer to process.
108          */
109         cfs_list_t              blp_list;
110
111         cfs_waitq_t             blp_waitq;
112         cfs_completion_t        blp_comp;
113         cfs_atomic_t            blp_num_threads;
114         cfs_atomic_t            blp_busy_threads;
115         int                     blp_min_threads;
116         int                     blp_max_threads;
117 };
118
119 struct ldlm_bl_work_item {
120         cfs_list_t              blwi_entry;
121         struct ldlm_namespace  *blwi_ns;
122         struct ldlm_lock_desc   blwi_ld;
123         struct ldlm_lock       *blwi_lock;
124         cfs_list_t              blwi_head;
125         int                     blwi_count;
126         cfs_completion_t        blwi_comp;
127         int                     blwi_mode;
128         int                     blwi_mem_pressure;
129 };
130
131 #if defined(HAVE_SERVER_SUPPORT) && defined(__KERNEL__)
132
133 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
134 static cfs_spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
135 static cfs_list_t waiting_locks_list;
136 static cfs_timer_t waiting_locks_timer;
137
138 static struct expired_lock_thread {
139         cfs_waitq_t             elt_waitq;
140         int                     elt_state;
141         int                     elt_dump;
142         cfs_list_t              elt_expired_locks;
143 } expired_lock_thread;
144
145 static inline int have_expired_locks(void)
146 {
147         int need_to_run;
148
149         ENTRY;
150         cfs_spin_lock_bh(&waiting_locks_spinlock);
151         need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
152         cfs_spin_unlock_bh(&waiting_locks_spinlock);
153
154         RETURN(need_to_run);
155 }
156
157 static int expired_lock_main(void *arg)
158 {
159         cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
160         struct l_wait_info lwi = { 0 };
161         int do_dump;
162
163         ENTRY;
164         cfs_daemonize("ldlm_elt");
165
166         expired_lock_thread.elt_state = ELT_READY;
167         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
168
169         while (1) {
170                 l_wait_event(expired_lock_thread.elt_waitq,
171                              have_expired_locks() ||
172                              expired_lock_thread.elt_state == ELT_TERMINATE,
173                              &lwi);
174
175                 cfs_spin_lock_bh(&waiting_locks_spinlock);
176                 if (expired_lock_thread.elt_dump) {
177                         struct libcfs_debug_msg_data msgdata = {
178                                 .msg_file = __FILE__,
179                                 .msg_fn = "waiting_locks_callback",
180                                 .msg_line = expired_lock_thread.elt_dump };
181                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
182
183                         /* from waiting_locks_callback, but not in timer */
184                         libcfs_debug_dumplog();
185                         libcfs_run_lbug_upcall(&msgdata);
186
187                         cfs_spin_lock_bh(&waiting_locks_spinlock);
188                         expired_lock_thread.elt_dump = 0;
189                 }
190
191                 do_dump = 0;
192
193                 while (!cfs_list_empty(expired)) {
194                         struct obd_export *export;
195                         struct ldlm_lock *lock;
196
197                         lock = cfs_list_entry(expired->next, struct ldlm_lock,
198                                           l_pending_chain);
199                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
200                             (void *)lock >= LP_POISON) {
201                                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
202                                 CERROR("free lock on elt list %p\n", lock);
203                                 LBUG();
204                         }
205                         cfs_list_del_init(&lock->l_pending_chain);
206                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
207                             (void *)lock->l_export >= LP_POISON) {
208                                 CERROR("lock with free export on elt list %p\n",
209                                        lock->l_export);
210                                 lock->l_export = NULL;
211                                 LDLM_ERROR(lock, "free export");
212                                 /* release extra ref grabbed by
213                                  * ldlm_add_waiting_lock() or
214                                  * ldlm_failed_ast() */
215                                 LDLM_LOCK_RELEASE(lock);
216                                 continue;
217                         }
218
219                         if (lock->l_destroyed) {
220                                 /* release the lock refcount where
221                                  * waiting_locks_callback() founds */
222                                 LDLM_LOCK_RELEASE(lock);
223                                 continue;
224                         }
225                         export = class_export_lock_get(lock->l_export, lock);
226                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
227
228                         do_dump++;
229                         class_fail_export(export);
230                         class_export_lock_put(export, lock);
231
232                         /* release extra ref grabbed by ldlm_add_waiting_lock()
233                          * or ldlm_failed_ast() */
234                         LDLM_LOCK_RELEASE(lock);
235
236                         cfs_spin_lock_bh(&waiting_locks_spinlock);
237                 }
238                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
239
240                 if (do_dump && obd_dump_on_eviction) {
241                         CERROR("dump the log upon eviction\n");
242                         libcfs_debug_dumplog();
243                 }
244
245                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
246                         break;
247         }
248
249         expired_lock_thread.elt_state = ELT_STOPPED;
250         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
251         RETURN(0);
252 }
253
254 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
255 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
256
257 /**
258  * Check if there is a request in the export request list
259  * which prevents the lock canceling.
260  */
261 static int ldlm_lock_busy(struct ldlm_lock *lock)
262 {
263         struct ptlrpc_request *req;
264         int match = 0;
265         ENTRY;
266
267         if (lock->l_export == NULL)
268                 return 0;
269
270         cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
271         cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
272                                 rq_exp_list) {
273                 if (req->rq_ops->hpreq_lock_match) {
274                         match = req->rq_ops->hpreq_lock_match(req, lock);
275                         if (match)
276                                 break;
277                 }
278         }
279         cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
280         RETURN(match);
281 }
282
283 /* This is called from within a timer interrupt and cannot schedule */
284 static void waiting_locks_callback(unsigned long unused)
285 {
286         struct ldlm_lock        *lock;
287         int                     need_dump = 0;
288
289         cfs_spin_lock_bh(&waiting_locks_spinlock);
290         while (!cfs_list_empty(&waiting_locks_list)) {
291                 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
292                                       l_pending_chain);
293                 if (cfs_time_after(lock->l_callback_timeout,
294                                    cfs_time_current()) ||
295                     (lock->l_req_mode == LCK_GROUP))
296                         break;
297
298                 if (ptlrpc_check_suspend()) {
299                         /* there is a case when we talk to one mds, holding
300                          * lock from another mds. this way we easily can get
301                          * here, if second mds is being recovered. so, we
302                          * suspend timeouts. bug 6019 */
303
304                         LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
305                                    lock->l_export->exp_client_uuid.uuid,
306                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
307                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
308
309                         cfs_list_del_init(&lock->l_pending_chain);
310                         if (lock->l_destroyed) {
311                                 /* relay the lock refcount decrease to
312                                  * expired lock thread */
313                                 cfs_list_add(&lock->l_pending_chain,
314                                         &expired_lock_thread.elt_expired_locks);
315                         } else {
316                                 __ldlm_add_waiting_lock(lock,
317                                                 ldlm_get_enq_timeout(lock));
318                         }
319                         continue;
320                 }
321
322                 /* if timeout overlaps the activation time of suspended timeouts
323                  * then extend it to give a chance for client to reconnect */
324                 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
325                                                  cfs_time_seconds(obd_timeout)/2),
326                                     ptlrpc_suspend_wakeup_time())) {
327                         LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
328                                    lock->l_export->exp_client_uuid.uuid,
329                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
330                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
331
332                         cfs_list_del_init(&lock->l_pending_chain);
333                         if (lock->l_destroyed) {
334                                 /* relay the lock refcount decrease to
335                                  * expired lock thread */
336                                 cfs_list_add(&lock->l_pending_chain,
337                                         &expired_lock_thread.elt_expired_locks);
338                         } else {
339                                 __ldlm_add_waiting_lock(lock,
340                                                 ldlm_get_enq_timeout(lock));
341                         }
342                         continue;
343                 }
344
345                 /* Check if we need to prolong timeout */
346                 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
347                     ldlm_lock_busy(lock)) {
348                         int cont = 1;
349
350                         if (lock->l_pending_chain.next == &waiting_locks_list)
351                                 cont = 0;
352
353                         LDLM_LOCK_GET(lock);
354
355                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
356                         LDLM_DEBUG(lock, "prolong the busy lock");
357                         ldlm_refresh_waiting_lock(lock,
358                                                   ldlm_get_enq_timeout(lock));
359                         cfs_spin_lock_bh(&waiting_locks_spinlock);
360
361                         if (!cont) {
362                                 LDLM_LOCK_RELEASE(lock);
363                                 break;
364                         }
365
366                         LDLM_LOCK_RELEASE(lock);
367                         continue;
368                 }
369                 ldlm_lock_to_ns(lock)->ns_timeouts++;
370                 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
371                            "evicting client at %s ",
372                            cfs_time_current_sec()- lock->l_last_activity,
373                            libcfs_nid2str(
374                                    lock->l_export->exp_connection->c_peer.nid));
375
376                 /* no needs to take an extra ref on the lock since it was in
377                  * the waiting_locks_list and ldlm_add_waiting_lock()
378                  * already grabbed a ref */
379                 cfs_list_del(&lock->l_pending_chain);
380                 cfs_list_add(&lock->l_pending_chain,
381                              &expired_lock_thread.elt_expired_locks);
382                 need_dump = 1;
383         }
384
385         if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
386                 if (obd_dump_on_timeout && need_dump)
387                         expired_lock_thread.elt_dump = __LINE__;
388
389                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
390         }
391
392         /*
393          * Make sure the timer will fire again if we have any locks
394          * left.
395          */
396         if (!cfs_list_empty(&waiting_locks_list)) {
397                 cfs_time_t timeout_rounded;
398                 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
399                                       l_pending_chain);
400                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
401                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
402         }
403         cfs_spin_unlock_bh(&waiting_locks_spinlock);
404 }
405
406 /*
407  * Indicate that we're waiting for a client to call us back cancelling a given
408  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
409  * timer to fire appropriately.  (We round up to the next second, to avoid
410  * floods of timer firings during periods of high lock contention and traffic).
411  * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
412  * if it has been added to the waiting list (1 is returned).
413  *
414  * Called with the namespace lock held.
415  */
416 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
417 {
418         cfs_time_t timeout;
419         cfs_time_t timeout_rounded;
420
421         if (!cfs_list_empty(&lock->l_pending_chain))
422                 return 0;
423
424         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
425             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
426                 seconds = 1;
427
428         timeout = cfs_time_shift(seconds);
429         if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
430                 lock->l_callback_timeout = timeout;
431
432         timeout_rounded = round_timeout(lock->l_callback_timeout);
433
434         if (cfs_time_before(timeout_rounded,
435                             cfs_timer_deadline(&waiting_locks_timer)) ||
436             !cfs_timer_is_armed(&waiting_locks_timer)) {
437                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
438         }
439         /* if the new lock has a shorter timeout than something earlier on
440            the list, we'll wait the longer amount of time; no big deal. */
441         /* FIFO */
442         cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
443         return 1;
444 }
445
446 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
447 {
448         int ret;
449         int timeout = ldlm_get_enq_timeout(lock);
450
451         /* NB: must be called with hold of lock_res_and_lock() */
452         LASSERT(lock->l_res_locked);
453         lock->l_waited = 1;
454
455         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
456
457         cfs_spin_lock_bh(&waiting_locks_spinlock);
458         if (lock->l_destroyed) {
459                 static cfs_time_t next;
460                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
461                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
462                 if (cfs_time_after(cfs_time_current(), next)) {
463                         next = cfs_time_shift(14400);
464                         libcfs_debug_dumpstack(NULL);
465                 }
466                 return 0;
467         }
468
469         ret = __ldlm_add_waiting_lock(lock, timeout);
470         if (ret) {
471                 /* grab ref on the lock if it has been added to the
472                  * waiting list */
473                 LDLM_LOCK_GET(lock);
474         }
475         cfs_spin_unlock_bh(&waiting_locks_spinlock);
476
477         if (ret) {
478                 cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
479                 if (cfs_list_empty(&lock->l_exp_list))
480                         cfs_list_add(&lock->l_exp_list,
481                                      &lock->l_export->exp_bl_list);
482                 cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
483         }
484
485         LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
486                    ret == 0 ? "not re-" : "", timeout,
487                    AT_OFF ? "off" : "on");
488         return ret;
489 }
490
491 /*
492  * Remove a lock from the pending list, likely because it had its cancellation
493  * callback arrive without incident.  This adjusts the lock-timeout timer if
494  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
495  * As done by ldlm_del_waiting_lock(), the caller must release the lock
496  * reference when the lock is removed from any list (1 is returned).
497  *
498  * Called with namespace lock held.
499  */
500 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
501 {
502         cfs_list_t *list_next;
503
504         if (cfs_list_empty(&lock->l_pending_chain))
505                 return 0;
506
507         list_next = lock->l_pending_chain.next;
508         if (lock->l_pending_chain.prev == &waiting_locks_list) {
509                 /* Removing the head of the list, adjust timer. */
510                 if (list_next == &waiting_locks_list) {
511                         /* No more, just cancel. */
512                         cfs_timer_disarm(&waiting_locks_timer);
513                 } else {
514                         struct ldlm_lock *next;
515                         next = cfs_list_entry(list_next, struct ldlm_lock,
516                                               l_pending_chain);
517                         cfs_timer_arm(&waiting_locks_timer,
518                                       round_timeout(next->l_callback_timeout));
519                 }
520         }
521         cfs_list_del_init(&lock->l_pending_chain);
522
523         return 1;
524 }
525
526 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
527 {
528         int ret;
529
530         if (lock->l_export == NULL) {
531                 /* We don't have a "waiting locks list" on clients. */
532                 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
533                 return 0;
534         }
535
536         cfs_spin_lock_bh(&waiting_locks_spinlock);
537         ret = __ldlm_del_waiting_lock(lock);
538         cfs_spin_unlock_bh(&waiting_locks_spinlock);
539
540         /* remove the lock out of export blocking list */
541         cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
542         cfs_list_del_init(&lock->l_exp_list);
543         cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
544
545         if (ret) {
546                 /* release lock ref if it has indeed been removed
547                  * from a list */
548                 LDLM_LOCK_RELEASE(lock);
549         }
550
551         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
552         return ret;
553 }
554
555 /*
556  * Prolong the lock
557  *
558  * Called with namespace lock held.
559  */
560 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
561 {
562         if (lock->l_export == NULL) {
563                 /* We don't have a "waiting locks list" on clients. */
564                 LDLM_DEBUG(lock, "client lock: no-op");
565                 return 0;
566         }
567
568         cfs_spin_lock_bh(&waiting_locks_spinlock);
569
570         if (cfs_list_empty(&lock->l_pending_chain)) {
571                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
572                 LDLM_DEBUG(lock, "wasn't waiting");
573                 return 0;
574         }
575
576         /* we remove/add the lock to the waiting list, so no needs to
577          * release/take a lock reference */
578         __ldlm_del_waiting_lock(lock);
579         __ldlm_add_waiting_lock(lock, timeout);
580         cfs_spin_unlock_bh(&waiting_locks_spinlock);
581
582         LDLM_DEBUG(lock, "refreshed");
583         return 1;
584 }
585
586 #else /* !HAVE_SERVER_SUPPORT ||  !__KERNEL__ */
587
588 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
589 {
590         RETURN(0);
591 }
592
593 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
594 {
595         RETURN(0);
596 }
597
598 # ifdef HAVE_SERVER_SUPPORT
599 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
600 {
601         LASSERT(lock->l_res_locked);
602         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
603         RETURN(1);
604 }
605
606 # endif
607 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
608
609 #ifdef HAVE_SERVER_SUPPORT
610
611 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
612                             const char *ast_type)
613 {
614         LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
615                            "to a lock %s callback time out: rc %d\n",
616                            lock->l_export->exp_obd->obd_name,
617                            obd_export_nid2str(lock->l_export), ast_type, rc);
618
619         if (obd_dump_on_timeout)
620                 libcfs_debug_dumplog();
621 #ifdef __KERNEL__
622         cfs_spin_lock_bh(&waiting_locks_spinlock);
623         if (__ldlm_del_waiting_lock(lock) == 0)
624                 /* the lock was not in any list, grab an extra ref before adding
625                  * the lock to the expired list */
626                 LDLM_LOCK_GET(lock);
627         cfs_list_add(&lock->l_pending_chain,
628                      &expired_lock_thread.elt_expired_locks);
629         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
630         cfs_spin_unlock_bh(&waiting_locks_spinlock);
631 #else
632         class_fail_export(lock->l_export);
633 #endif
634 }
635
636 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
637                                  struct ptlrpc_request *req, int rc,
638                                  const char *ast_type)
639 {
640         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
641
642         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
643                 LASSERT(lock->l_export);
644                 if (lock->l_export->exp_libclient) {
645                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
646                                    " timeout, just cancelling lock", ast_type,
647                                    libcfs_nid2str(peer.nid));
648                         ldlm_lock_cancel(lock);
649                         rc = -ERESTART;
650                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
651                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
652                                    "cancel was received (AST reply lost?)",
653                                    ast_type, libcfs_nid2str(peer.nid));
654                         ldlm_lock_cancel(lock);
655                         rc = -ERESTART;
656                 } else {
657                         ldlm_del_waiting_lock(lock);
658                         ldlm_failed_ast(lock, rc, ast_type);
659                 }
660         } else if (rc) {
661                 if (rc == -EINVAL) {
662                         struct ldlm_resource *res = lock->l_resource;
663                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
664                                " from %s AST - normal race",
665                                libcfs_nid2str(peer.nid),
666                                req->rq_repmsg ?
667                                lustre_msg_get_status(req->rq_repmsg) : -1,
668                                ast_type);
669                         if (res) {
670                                 /* update lvbo to return proper attributes.
671                                  * see bug 23174 */
672                                 ldlm_resource_getref(res);
673                                 ldlm_res_lvbo_update(res, NULL, 1);
674                                 ldlm_resource_putref(res);
675                         }
676
677                 } else {
678                         LDLM_ERROR(lock, "client (nid %s) returned %d "
679                                    "from %s AST", libcfs_nid2str(peer.nid),
680                                    (req->rq_repmsg != NULL) ?
681                                    lustre_msg_get_status(req->rq_repmsg) : 0,
682                                    ast_type);
683                 }
684                 ldlm_lock_cancel(lock);
685                 /* Server-side AST functions are called from ldlm_reprocess_all,
686                  * which needs to be told to please restart its reprocessing. */
687                 rc = -ERESTART;
688         }
689
690         return rc;
691 }
692
693 static int ldlm_cb_interpret(const struct lu_env *env,
694                              struct ptlrpc_request *req, void *data, int rc)
695 {
696         struct ldlm_cb_async_args *ca   = data;
697         struct ldlm_lock          *lock = ca->ca_lock;
698         struct ldlm_cb_set_arg    *arg  = ca->ca_set_arg;
699         ENTRY;
700
701         LASSERT(lock != NULL);
702         if (rc != 0) {
703                 rc = ldlm_handle_ast_error(lock, req, rc,
704                                            arg->type == LDLM_BL_CALLBACK
705                                            ? "blocking" : "completion");
706                 if (rc == -ERESTART)
707                         cfs_atomic_inc(&arg->restart);
708         }
709         LDLM_LOCK_RELEASE(lock);
710
711         RETURN(0);
712 }
713
714 static inline int ldlm_bl_and_cp_ast_tail(struct ptlrpc_request *req,
715                                           struct ldlm_cb_set_arg *arg,
716                                           struct ldlm_lock *lock,
717                                           int instant_cancel)
718 {
719         int rc = 0;
720         ENTRY;
721
722         if (unlikely(instant_cancel)) {
723                 rc = ptl_send_rpc(req, 1);
724                 ptlrpc_req_finished(req);
725                 if (rc == 0)
726                         cfs_atomic_inc(&arg->restart);
727         } else {
728                 LDLM_LOCK_GET(lock);
729                 ptlrpc_set_add_req(arg->set, req);
730         }
731
732         RETURN(rc);
733 }
734
735 /**
736  * Check if there are requests in the export request list which prevent
737  * the lock canceling and make these requests high priority ones.
738  */
739 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
740 {
741         struct ptlrpc_request *req;
742         ENTRY;
743
744         if (lock->l_export == NULL) {
745                 LDLM_DEBUG(lock, "client lock: no-op");
746                 RETURN_EXIT;
747         }
748
749         cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
750         cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
751                                 rq_exp_list) {
752                 /* Do not process requests that were not yet added to there
753                  * incoming queue or were already removed from there for
754                  * processing */
755                 if (!req->rq_hp && !cfs_list_empty(&req->rq_list) &&
756                     req->rq_ops->hpreq_lock_match &&
757                     req->rq_ops->hpreq_lock_match(req, lock))
758                         ptlrpc_hpreq_reorder(req);
759         }
760         cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
761         EXIT;
762 }
763
764 /*
765  * ->l_blocking_ast() method for server-side locks. This is invoked when newly
766  * enqueued server lock conflicts with given one.
767  *
768  * Sends blocking ast rpc to the client owning that lock; arms timeout timer
769  * to wait for client response.
770  */
771 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
772                              struct ldlm_lock_desc *desc,
773                              void *data, int flag)
774 {
775         struct ldlm_cb_async_args *ca;
776         struct ldlm_cb_set_arg *arg = data;
777         struct ldlm_request    *body;
778         struct ptlrpc_request  *req;
779         int                     instant_cancel = 0;
780         int                     rc = 0;
781         ENTRY;
782
783         if (flag == LDLM_CB_CANCELING)
784                 /* Don't need to do anything here. */
785                 RETURN(0);
786
787         LASSERT(lock);
788         LASSERT(data != NULL);
789         if (lock->l_export->exp_obd->obd_recovering != 0)
790                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
791
792         ldlm_lock_reorder_req(lock);
793
794         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
795                                         &RQF_LDLM_BL_CALLBACK,
796                                         LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
797         if (req == NULL)
798                 RETURN(-ENOMEM);
799
800         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
801         ca = ptlrpc_req_async_args(req);
802         ca->ca_set_arg = arg;
803         ca->ca_lock = lock;
804
805         req->rq_interpret_reply = ldlm_cb_interpret;
806         req->rq_no_resend = 1;
807
808         lock_res_and_lock(lock);
809         if (lock->l_granted_mode != lock->l_req_mode) {
810                 /* this blocking AST will be communicated as part of the
811                  * completion AST instead */
812                 unlock_res_and_lock(lock);
813
814                 ptlrpc_req_finished(req);
815                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
816                 RETURN(0);
817         }
818
819         if (lock->l_destroyed) {
820                 /* What's the point? */
821                 unlock_res_and_lock(lock);
822                 ptlrpc_req_finished(req);
823                 RETURN(0);
824         }
825
826         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
827                 instant_cancel = 1;
828
829         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
830         body->lock_handle[0] = lock->l_remote_handle;
831         body->lock_desc = *desc;
832         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
833
834         LDLM_DEBUG(lock, "server preparing blocking AST");
835
836         ptlrpc_request_set_replen(req);
837         if (instant_cancel) {
838                 unlock_res_and_lock(lock);
839                 ldlm_lock_cancel(lock);
840         } else {
841                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
842                 ldlm_add_waiting_lock(lock);
843                 unlock_res_and_lock(lock);
844         }
845
846         req->rq_send_state = LUSTRE_IMP_FULL;
847         /* ptlrpc_request_alloc_pack already set timeout */
848         if (AT_OFF)
849                 req->rq_timeout = ldlm_get_rq_timeout();
850
851         if (lock->l_export && lock->l_export->exp_nid_stats &&
852             lock->l_export->exp_nid_stats->nid_ldlm_stats)
853                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
854                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
855
856         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
857
858         RETURN(rc);
859 }
860
861 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
862 {
863         struct ldlm_cb_set_arg *arg = data;
864         struct ldlm_request    *body;
865         struct ptlrpc_request  *req;
866         struct ldlm_cb_async_args *ca;
867         long                    total_enqueue_wait;
868         int                     instant_cancel = 0;
869         int                     rc = 0;
870         ENTRY;
871
872         LASSERT(lock != NULL);
873         LASSERT(data != NULL);
874
875         total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
876                                           lock->l_last_activity);
877
878         req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
879                                     &RQF_LDLM_CP_CALLBACK);
880         if (req == NULL)
881                 RETURN(-ENOMEM);
882
883         /* server namespace, doesn't need lock */
884         if (lock->l_resource->lr_lvb_len) {
885                  req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
886                                       lock->l_resource->lr_lvb_len);
887         }
888
889         rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
890         if (rc) {
891                 ptlrpc_request_free(req);
892                 RETURN(rc);
893         }
894
895         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
896         ca = ptlrpc_req_async_args(req);
897         ca->ca_set_arg = arg;
898         ca->ca_lock = lock;
899
900         req->rq_interpret_reply = ldlm_cb_interpret;
901         req->rq_no_resend = 1;
902         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
903
904         body->lock_handle[0] = lock->l_remote_handle;
905         body->lock_flags = flags;
906         ldlm_lock2desc(lock, &body->lock_desc);
907         if (lock->l_resource->lr_lvb_len) {
908                 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
909
910                 lock_res(lock->l_resource);
911                 memcpy(lvb, lock->l_resource->lr_lvb_data,
912                        lock->l_resource->lr_lvb_len);
913                 unlock_res(lock->l_resource);
914         }
915
916         LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
917                    total_enqueue_wait);
918
919         /* Server-side enqueue wait time estimate, used in
920             __ldlm_add_waiting_lock to set future enqueue timers */
921         if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
922                 at_measured(ldlm_lock_to_ns_at(lock),
923                             total_enqueue_wait);
924         else
925                 /* bz18618. Don't add lock enqueue time we spend waiting for a
926                    previous callback to fail. Locks waiting legitimately will
927                    get extended by ldlm_refresh_waiting_lock regardless of the
928                    estimate, so it's okay to underestimate here. */
929                 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
930                        "It is likely that a previous callback timed out.",
931                        total_enqueue_wait,
932                        at_get(ldlm_lock_to_ns_at(lock)));
933
934         ptlrpc_request_set_replen(req);
935
936         req->rq_send_state = LUSTRE_IMP_FULL;
937         /* ptlrpc_request_pack already set timeout */
938         if (AT_OFF)
939                 req->rq_timeout = ldlm_get_rq_timeout();
940
941         /* We only send real blocking ASTs after the lock is granted */
942         lock_res_and_lock(lock);
943         if (lock->l_flags & LDLM_FL_AST_SENT) {
944                 body->lock_flags |= LDLM_FL_AST_SENT;
945                 /* copy ast flags like LDLM_FL_DISCARD_DATA */
946                 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
947
948                 /* We might get here prior to ldlm_handle_enqueue setting
949                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
950                  * into waiting list, but this is safe and similar code in
951                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
952                  * that would not only cancel the lock, but will also remove
953                  * it from waiting list */
954                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
955                         unlock_res_and_lock(lock);
956                         ldlm_lock_cancel(lock);
957                         instant_cancel = 1;
958                         lock_res_and_lock(lock);
959                 } else {
960                         /* start the lock-timeout clock */
961                         ldlm_add_waiting_lock(lock);
962                 }
963         }
964         unlock_res_and_lock(lock);
965
966         if (lock->l_export && lock->l_export->exp_nid_stats &&
967             lock->l_export->exp_nid_stats->nid_ldlm_stats)
968                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
969                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
970
971         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
972
973         RETURN(rc);
974 }
975
976 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
977 {
978         struct ldlm_resource  *res = lock->l_resource;
979         struct ldlm_request   *body;
980         struct ptlrpc_request *req;
981         int                    rc;
982         ENTRY;
983
984         LASSERT(lock != NULL);
985
986         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
987                                         &RQF_LDLM_GL_CALLBACK,
988                                         LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
989
990         if (req == NULL)
991                 RETURN(-ENOMEM);
992
993         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
994         body->lock_handle[0] = lock->l_remote_handle;
995         ldlm_lock2desc(lock, &body->lock_desc);
996
997         /* server namespace, doesn't need lock */
998         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
999                              lock->l_resource->lr_lvb_len);
1000         res = lock->l_resource;
1001         ptlrpc_request_set_replen(req);
1002
1003
1004         req->rq_send_state = LUSTRE_IMP_FULL;
1005         /* ptlrpc_request_alloc_pack already set timeout */
1006         if (AT_OFF)
1007                 req->rq_timeout = ldlm_get_rq_timeout();
1008
1009         if (lock->l_export && lock->l_export->exp_nid_stats &&
1010             lock->l_export->exp_nid_stats->nid_ldlm_stats)
1011                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1012                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1013
1014         rc = ptlrpc_queue_wait(req);
1015         /* Update the LVB from disk if the AST failed (this is a legal race)
1016          *
1017          * - Glimpse callback of local lock just return -ELDLM_NO_LOCK_DATA.
1018          * - Glimpse callback of remote lock might return -ELDLM_NO_LOCK_DATA
1019          *   when inode is cleared. LU-274
1020          */
1021         if (rc == -ELDLM_NO_LOCK_DATA) {
1022                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
1023                 ldlm_res_lvbo_update(res, NULL, 1);
1024         } else if (rc != 0) {
1025                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
1026         } else {
1027                 rc = ldlm_res_lvbo_update(res, req, 1);
1028         }
1029
1030         ptlrpc_req_finished(req);
1031         if (rc == -ERESTART)
1032                 ldlm_reprocess_all(res);
1033
1034         RETURN(rc);
1035 }
1036
1037 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1038                        struct lprocfs_stats *srv_stats)
1039 {
1040         int lock_type = 0, op = 0;
1041
1042         lock_type = dlm_req->lock_desc.l_resource.lr_type;
1043
1044         switch (lock_type) {
1045         case LDLM_PLAIN:
1046                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1047                 break;
1048         case LDLM_EXTENT:
1049                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1050                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1051                 else
1052                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1053                 break;
1054         case LDLM_FLOCK:
1055                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1056                 break;
1057         case LDLM_IBITS:
1058                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1059                 break;
1060         default:
1061                 op = 0;
1062                 break;
1063         }
1064
1065         if (op)
1066                 lprocfs_counter_incr(srv_stats, op);
1067
1068         return;
1069 }
1070
1071 /*
1072  * Main server-side entry point into LDLM. This is called by ptlrpc service
1073  * threads to carry out client lock enqueueing requests.
1074  */
1075 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1076                          struct ptlrpc_request *req,
1077                          const struct ldlm_request *dlm_req,
1078                          const struct ldlm_callback_suite *cbs)
1079 {
1080         struct ldlm_reply *dlm_rep;
1081         __u32 flags;
1082         ldlm_error_t err = ELDLM_OK;
1083         struct ldlm_lock *lock = NULL;
1084         void *cookie = NULL;
1085         int rc = 0;
1086         ENTRY;
1087
1088         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1089
1090         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1091         flags = dlm_req->lock_flags;
1092
1093         LASSERT(req->rq_export);
1094
1095         if (ptlrpc_req2svc(req)->srv_stats != NULL)
1096                 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1097
1098         if (req->rq_export && req->rq_export->exp_nid_stats &&
1099             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1100                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1101                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
1102
1103         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1104                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1105                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1106                           dlm_req->lock_desc.l_resource.lr_type);
1107                 GOTO(out, rc = -EFAULT);
1108         }
1109
1110         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1111                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1112                      dlm_req->lock_desc.l_req_mode &
1113                      (dlm_req->lock_desc.l_req_mode-1))) {
1114                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1115                           dlm_req->lock_desc.l_req_mode);
1116                 GOTO(out, rc = -EFAULT);
1117         }
1118
1119         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1120                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1121                              LDLM_PLAIN)) {
1122                         DEBUG_REQ(D_ERROR, req,
1123                                   "PLAIN lock request from IBITS client?");
1124                         GOTO(out, rc = -EPROTO);
1125                 }
1126         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1127                             LDLM_IBITS)) {
1128                 DEBUG_REQ(D_ERROR, req,
1129                           "IBITS lock request from unaware client?");
1130                 GOTO(out, rc = -EPROTO);
1131         }
1132
1133 #if 0
1134         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1135            against server's _CONNECT_SUPPORTED flags? (I don't want to use
1136            ibits for mgc/mgs) */
1137
1138         /* INODEBITS_INTEROP: Perform conversion from plain lock to
1139          * inodebits lock if client does not support them. */
1140         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1141             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1142                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1143                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1144                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1145                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1146                         dlm_req->lock_desc.l_req_mode = LCK_CR;
1147         }
1148 #endif
1149
1150         if (unlikely(flags & LDLM_FL_REPLAY)) {
1151                 /* Find an existing lock in the per-export lock hash */
1152                 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1153                                        (void *)&dlm_req->lock_handle[0]);
1154                 if (lock != NULL) {
1155                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1156                                   LPX64, lock->l_handle.h_cookie);
1157                         GOTO(existing_lock, rc = 0);
1158                 }
1159         }
1160
1161         /* The lock's callback data might be set in the policy function */
1162         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1163                                 dlm_req->lock_desc.l_resource.lr_type,
1164                                 dlm_req->lock_desc.l_req_mode,
1165                                 cbs, NULL, 0);
1166
1167         if (!lock)
1168                 GOTO(out, rc = -ENOMEM);
1169
1170         lock->l_last_activity = cfs_time_current_sec();
1171         lock->l_remote_handle = dlm_req->lock_handle[0];
1172         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1173
1174         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1175         /* Don't enqueue a lock onto the export if it is been disonnected
1176          * due to eviction (bug 3822) or server umount (bug 24324).
1177          * Cancel it now instead. */
1178         if (req->rq_export->exp_disconnected) {
1179                 LDLM_ERROR(lock, "lock on disconnected export %p",
1180                            req->rq_export);
1181                 GOTO(out, rc = -ENOTCONN);
1182         }
1183
1184         lock->l_export = class_export_lock_get(req->rq_export, lock);
1185         if (lock->l_export->exp_lock_hash)
1186                 cfs_hash_add(lock->l_export->exp_lock_hash,
1187                              &lock->l_remote_handle,
1188                              &lock->l_exp_hash);
1189
1190 existing_lock:
1191
1192         if (flags & LDLM_FL_HAS_INTENT) {
1193                 /* In this case, the reply buffer is allocated deep in
1194                  * local_lock_enqueue by the policy function. */
1195                 cookie = req;
1196         } else {
1197                 /* based on the assumption that lvb size never changes during
1198                  * resource life time otherwise it need resource->lr_lock's
1199                  * protection */
1200                 if (lock->l_resource->lr_lvb_len) {
1201                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1202                                              RCL_SERVER,
1203                                              lock->l_resource->lr_lvb_len);
1204                 }
1205
1206                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1207                         GOTO(out, rc = -ENOMEM);
1208
1209                 rc = req_capsule_server_pack(&req->rq_pill);
1210                 if (rc)
1211                         GOTO(out, rc);
1212         }
1213
1214         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1215                 ldlm_convert_policy_to_local(req->rq_export,
1216                                           dlm_req->lock_desc.l_resource.lr_type,
1217                                           &dlm_req->lock_desc.l_policy_data,
1218                                           &lock->l_policy_data);
1219         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1220                 lock->l_req_extent = lock->l_policy_data.l_extent;
1221
1222         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1223         if (err)
1224                 GOTO(out, err);
1225
1226         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1227         dlm_rep->lock_flags = flags;
1228
1229         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1230         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1231
1232         /* We never send a blocking AST until the lock is granted, but
1233          * we can tell it right now */
1234         lock_res_and_lock(lock);
1235
1236         /* Now take into account flags to be inherited from original lock
1237            request both in reply to client and in our own lock flags. */
1238         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1239         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1240
1241         /* Don't move a pending lock onto the export if it has already been
1242          * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1243          * Cancel it now instead. */
1244         if (unlikely(req->rq_export->exp_disconnected ||
1245                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1246                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1247                 rc = -ENOTCONN;
1248         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1249                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1250                 if (lock->l_granted_mode == lock->l_req_mode) {
1251                         /*
1252                          * Only cancel lock if it was granted, because it would
1253                          * be destroyed immediately and would never be granted
1254                          * in the future, causing timeouts on client.  Not
1255                          * granted lock will be cancelled immediately after
1256                          * sending completion AST.
1257                          */
1258                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1259                                 unlock_res_and_lock(lock);
1260                                 ldlm_lock_cancel(lock);
1261                                 lock_res_and_lock(lock);
1262                         } else
1263                                 ldlm_add_waiting_lock(lock);
1264                 }
1265         }
1266         /* Make sure we never ever grant usual metadata locks to liblustre
1267            clients */
1268         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1269             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1270              req->rq_export->exp_libclient) {
1271                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1272                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1273                         CERROR("Granting sync lock to libclient. "
1274                                "req fl %d, rep fl %d, lock fl "LPX64"\n",
1275                                dlm_req->lock_flags, dlm_rep->lock_flags,
1276                                lock->l_flags);
1277                         LDLM_ERROR(lock, "sync lock");
1278                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1279                                 struct ldlm_intent *it;
1280
1281                                 it = req_capsule_client_get(&req->rq_pill,
1282                                                             &RMF_LDLM_INTENT);
1283                                 if (it != NULL) {
1284                                         CERROR("This is intent %s ("LPU64")\n",
1285                                                ldlm_it2str(it->opc), it->opc);
1286                                 }
1287                         }
1288                 }
1289         }
1290
1291         unlock_res_and_lock(lock);
1292
1293         EXIT;
1294  out:
1295         req->rq_status = rc ?: err; /* return either error - bug 11190 */
1296         if (!req->rq_packed_final) {
1297                 err = lustre_pack_reply(req, 1, NULL, NULL);
1298                 if (rc == 0)
1299                         rc = err;
1300         }
1301
1302         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1303          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1304         if (lock) {
1305                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1306                            "(err=%d, rc=%d)", err, rc);
1307
1308                 if (rc == 0) {
1309                         if (lock->l_resource->lr_lvb_len > 0) {
1310                                 /* MDT path won't handle lr_lvb_data, so
1311                                  * lock/unlock better be contained in the
1312                                  * if block */
1313                                 void *lvb;
1314
1315                                 lvb = req_capsule_server_get(&req->rq_pill,
1316                                                              &RMF_DLM_LVB);
1317                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1318                                          req, lock);
1319                                 lock_res(lock->l_resource);
1320                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1321                                        lock->l_resource->lr_lvb_len);
1322                                 unlock_res(lock->l_resource);
1323                         }
1324                 } else {
1325                         lock_res_and_lock(lock);
1326                         ldlm_resource_unlink_lock(lock);
1327                         ldlm_lock_destroy_nolock(lock);
1328                         unlock_res_and_lock(lock);
1329                 }
1330
1331                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1332                         ldlm_reprocess_all(lock->l_resource);
1333
1334                 LDLM_LOCK_RELEASE(lock);
1335         }
1336
1337         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1338                           lock, rc);
1339
1340         return rc;
1341 }
1342
1343 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1344                         ldlm_completion_callback completion_callback,
1345                         ldlm_blocking_callback blocking_callback,
1346                         ldlm_glimpse_callback glimpse_callback)
1347 {
1348         struct ldlm_request *dlm_req;
1349         struct ldlm_callback_suite cbs = {
1350                 .lcs_completion = completion_callback,
1351                 .lcs_blocking   = blocking_callback,
1352                 .lcs_glimpse    = glimpse_callback
1353         };
1354         int rc;
1355
1356         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1357         if (dlm_req != NULL) {
1358                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1359                                           req, dlm_req, &cbs);
1360         } else {
1361                 rc = -EFAULT;
1362         }
1363         return rc;
1364 }
1365
1366 int ldlm_handle_convert0(struct ptlrpc_request *req,
1367                          const struct ldlm_request *dlm_req)
1368 {
1369         struct ldlm_reply *dlm_rep;
1370         struct ldlm_lock *lock;
1371         int rc;
1372         ENTRY;
1373
1374         if (req->rq_export && req->rq_export->exp_nid_stats &&
1375             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1376                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1377                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1378
1379         rc = req_capsule_server_pack(&req->rq_pill);
1380         if (rc)
1381                 RETURN(rc);
1382
1383         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1384         dlm_rep->lock_flags = dlm_req->lock_flags;
1385
1386         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1387         if (!lock) {
1388                 req->rq_status = EINVAL;
1389         } else {
1390                 void *res = NULL;
1391
1392                 LDLM_DEBUG(lock, "server-side convert handler START");
1393
1394                 lock->l_last_activity = cfs_time_current_sec();
1395                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1396                                         &dlm_rep->lock_flags);
1397                 if (res) {
1398                         if (ldlm_del_waiting_lock(lock))
1399                                 LDLM_DEBUG(lock, "converted waiting lock");
1400                         req->rq_status = 0;
1401                 } else {
1402                         req->rq_status = EDEADLOCK;
1403                 }
1404         }
1405
1406         if (lock) {
1407                 if (!req->rq_status)
1408                         ldlm_reprocess_all(lock->l_resource);
1409                 LDLM_DEBUG(lock, "server-side convert handler END");
1410                 LDLM_LOCK_PUT(lock);
1411         } else
1412                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1413
1414         RETURN(0);
1415 }
1416
1417 int ldlm_handle_convert(struct ptlrpc_request *req)
1418 {
1419         int rc;
1420         struct ldlm_request *dlm_req;
1421
1422         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1423         if (dlm_req != NULL) {
1424                 rc = ldlm_handle_convert0(req, dlm_req);
1425         } else {
1426                 CERROR ("Can't unpack dlm_req\n");
1427                 rc = -EFAULT;
1428         }
1429         return rc;
1430 }
1431
1432 /* Cancel all the locks whos handles are packed into ldlm_request */
1433 int ldlm_request_cancel(struct ptlrpc_request *req,
1434                         const struct ldlm_request *dlm_req, int first)
1435 {
1436         struct ldlm_resource *res, *pres = NULL;
1437         struct ldlm_lock *lock;
1438         int i, count, done = 0;
1439         ENTRY;
1440
1441         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1442         if (first >= count)
1443                 RETURN(0);
1444
1445         /* There is no lock on the server at the replay time,
1446          * skip lock cancelling to make replay tests to pass. */
1447         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1448                 RETURN(0);
1449
1450         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1451                           "starting at %d", count, first);
1452
1453         for (i = first; i < count; i++) {
1454                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1455                 if (!lock) {
1456                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1457                                           "lock (cookie "LPU64")",
1458                                           dlm_req->lock_handle[i].cookie);
1459                         continue;
1460                 }
1461
1462                 res = lock->l_resource;
1463                 done++;
1464
1465                 if (res != pres) {
1466                         if (pres != NULL) {
1467                                 ldlm_reprocess_all(pres);
1468                                 LDLM_RESOURCE_DELREF(pres);
1469                                 ldlm_resource_putref(pres);
1470                         }
1471                         if (res != NULL) {
1472                                 ldlm_resource_getref(res);
1473                                 LDLM_RESOURCE_ADDREF(res);
1474                                 ldlm_res_lvbo_update(res, NULL, 1);
1475                         }
1476                         pres = res;
1477                 }
1478                 ldlm_lock_cancel(lock);
1479                 LDLM_LOCK_PUT(lock);
1480         }
1481         if (pres != NULL) {
1482                 ldlm_reprocess_all(pres);
1483                 LDLM_RESOURCE_DELREF(pres);
1484                 ldlm_resource_putref(pres);
1485         }
1486         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1487         RETURN(done);
1488 }
1489
1490 int ldlm_handle_cancel(struct ptlrpc_request *req)
1491 {
1492         struct ldlm_request *dlm_req;
1493         int rc;
1494         ENTRY;
1495
1496         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1497         if (dlm_req == NULL) {
1498                 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1499                 RETURN(-EFAULT);
1500         }
1501
1502         if (req->rq_export && req->rq_export->exp_nid_stats &&
1503             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1504                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1505                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1506
1507         rc = req_capsule_server_pack(&req->rq_pill);
1508         if (rc)
1509                 RETURN(rc);
1510
1511         if (!ldlm_request_cancel(req, dlm_req, 0))
1512                 req->rq_status = ESTALE;
1513
1514         RETURN(ptlrpc_reply(req));
1515 }
1516 #endif /* HAVE_SERVER_SUPPORT */
1517
1518 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1519                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1520 {
1521         int do_ast;
1522         ENTRY;
1523
1524         LDLM_DEBUG(lock, "client blocking AST callback handler");
1525
1526         lock_res_and_lock(lock);
1527         lock->l_flags |= LDLM_FL_CBPENDING;
1528
1529         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1530                 lock->l_flags |= LDLM_FL_CANCEL;
1531
1532         do_ast = (!lock->l_readers && !lock->l_writers);
1533         unlock_res_and_lock(lock);
1534
1535         if (do_ast) {
1536                 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1537                        lock, lock->l_blocking_ast);
1538                 if (lock->l_blocking_ast != NULL)
1539                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1540                                              LDLM_CB_BLOCKING);
1541         } else {
1542                 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1543                        lock);
1544         }
1545
1546         LDLM_DEBUG(lock, "client blocking callback handler END");
1547         LDLM_LOCK_RELEASE(lock);
1548         EXIT;
1549 }
1550
1551 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1552                                     struct ldlm_namespace *ns,
1553                                     struct ldlm_request *dlm_req,
1554                                     struct ldlm_lock *lock)
1555 {
1556         CFS_LIST_HEAD(ast_list);
1557         ENTRY;
1558
1559         LDLM_DEBUG(lock, "client completion callback handler START");
1560
1561         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1562                 int to = cfs_time_seconds(1);
1563                 while (to > 0) {
1564                         cfs_schedule_timeout_and_set_state(
1565                                 CFS_TASK_INTERRUPTIBLE, to);
1566                         if (lock->l_granted_mode == lock->l_req_mode ||
1567                             lock->l_destroyed)
1568                                 break;
1569                 }
1570         }
1571
1572         lock_res_and_lock(lock);
1573         if (lock->l_destroyed ||
1574             lock->l_granted_mode == lock->l_req_mode) {
1575                 /* bug 11300: the lock has already been granted */
1576                 unlock_res_and_lock(lock);
1577                 LDLM_DEBUG(lock, "Double grant race happened");
1578                 LDLM_LOCK_RELEASE(lock);
1579                 EXIT;
1580                 return;
1581         }
1582
1583         /* If we receive the completion AST before the actual enqueue returned,
1584          * then we might need to switch lock modes, resources, or extents. */
1585         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1586                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1587                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1588         }
1589
1590         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1591                 ldlm_convert_policy_to_local(req->rq_export,
1592                                           dlm_req->lock_desc.l_resource.lr_type,
1593                                           &dlm_req->lock_desc.l_policy_data,
1594                                           &lock->l_policy_data);
1595                 LDLM_DEBUG(lock, "completion AST, new policy data");
1596         }
1597
1598         ldlm_resource_unlink_lock(lock);
1599         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1600                    &lock->l_resource->lr_name,
1601                    sizeof(lock->l_resource->lr_name)) != 0) {
1602                 unlock_res_and_lock(lock);
1603                 if (ldlm_lock_change_resource(ns, lock,
1604                                 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1605                         LDLM_ERROR(lock, "Failed to allocate resource");
1606                         LDLM_LOCK_RELEASE(lock);
1607                         EXIT;
1608                         return;
1609                 }
1610                 LDLM_DEBUG(lock, "completion AST, new resource");
1611                 CERROR("change resource!\n");
1612                 lock_res_and_lock(lock);
1613         }
1614
1615         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1616                 /* BL_AST locks are not needed in lru.
1617                  * let ldlm_cancel_lru() be fast. */
1618                 ldlm_lock_remove_from_lru(lock);
1619                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1620                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1621         }
1622
1623         if (lock->l_lvb_len) {
1624                 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1625                                          RCL_CLIENT) < lock->l_lvb_len) {
1626                         LDLM_ERROR(lock, "completion AST did not contain "
1627                                    "expected LVB!");
1628                 } else {
1629                         void *lvb = req_capsule_client_get(&req->rq_pill,
1630                                                            &RMF_DLM_LVB);
1631                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1632                 }
1633         }
1634
1635         ldlm_grant_lock(lock, &ast_list);
1636         unlock_res_and_lock(lock);
1637
1638         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1639
1640         /* Let Enqueue to call osc_lock_upcall() and initialize
1641          * l_ast_data */
1642         OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1643
1644         ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1645
1646         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1647                           lock);
1648         LDLM_LOCK_RELEASE(lock);
1649         EXIT;
1650 }
1651
1652 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1653                                     struct ldlm_namespace *ns,
1654                                     struct ldlm_request *dlm_req,
1655                                     struct ldlm_lock *lock)
1656 {
1657         int rc = -ENOSYS;
1658         ENTRY;
1659
1660         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1661
1662         if (lock->l_glimpse_ast != NULL)
1663                 rc = lock->l_glimpse_ast(lock, req);
1664
1665         if (req->rq_repmsg != NULL) {
1666                 ptlrpc_reply(req);
1667         } else {
1668                 req->rq_status = rc;
1669                 ptlrpc_error(req);
1670         }
1671
1672         lock_res_and_lock(lock);
1673         if (lock->l_granted_mode == LCK_PW &&
1674             !lock->l_readers && !lock->l_writers &&
1675             cfs_time_after(cfs_time_current(),
1676                            cfs_time_add(lock->l_last_used,
1677                                         cfs_time_seconds(10)))) {
1678                 unlock_res_and_lock(lock);
1679                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1680                         ldlm_handle_bl_callback(ns, NULL, lock);
1681
1682                 EXIT;
1683                 return;
1684         }
1685         unlock_res_and_lock(lock);
1686         LDLM_LOCK_RELEASE(lock);
1687         EXIT;
1688 }
1689
1690 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1691 {
1692         if (req->rq_no_reply)
1693                 return 0;
1694
1695         req->rq_status = rc;
1696         if (!req->rq_packed_final) {
1697                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1698                 if (rc)
1699                         return rc;
1700         }
1701         return ptlrpc_reply(req);
1702 }
1703
1704 #ifdef __KERNEL__
1705 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1706 {
1707         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1708         ENTRY;
1709
1710         cfs_spin_lock(&blp->blp_lock);
1711         if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1712                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1713                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1714         } else {
1715                 /* other blocking callbacks are added to the regular list */
1716                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1717         }
1718         cfs_spin_unlock(&blp->blp_lock);
1719
1720         cfs_waitq_signal(&blp->blp_waitq);
1721
1722         /* can not use blwi->blwi_mode as blwi could be already freed in
1723            LDLM_ASYNC mode */
1724         if (mode == LDLM_SYNC)
1725                 cfs_wait_for_completion(&blwi->blwi_comp);
1726
1727         RETURN(0);
1728 }
1729
1730 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1731                              struct ldlm_namespace *ns,
1732                              struct ldlm_lock_desc *ld,
1733                              cfs_list_t *cancels, int count,
1734                              struct ldlm_lock *lock,
1735                              int mode)
1736 {
1737         cfs_init_completion(&blwi->blwi_comp);
1738         CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1739
1740         if (cfs_memory_pressure_get())
1741                 blwi->blwi_mem_pressure = 1;
1742
1743         blwi->blwi_ns = ns;
1744         blwi->blwi_mode = mode;
1745         if (ld != NULL)
1746                 blwi->blwi_ld = *ld;
1747         if (count) {
1748                 cfs_list_add(&blwi->blwi_head, cancels);
1749                 cfs_list_del_init(cancels);
1750                 blwi->blwi_count = count;
1751         } else {
1752                 blwi->blwi_lock = lock;
1753         }
1754 }
1755
1756 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1757                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1758                              cfs_list_t *cancels, int count, int mode)
1759 {
1760         ENTRY;
1761
1762         if (cancels && count == 0)
1763                 RETURN(0);
1764
1765         if (mode == LDLM_SYNC) {
1766                 /* if it is synchronous call do minimum mem alloc, as it could
1767                  * be triggered from kernel shrinker
1768                  */
1769                 struct ldlm_bl_work_item blwi;
1770                 memset(&blwi, 0, sizeof(blwi));
1771                 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1772                 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1773         } else {
1774                 struct ldlm_bl_work_item *blwi;
1775                 OBD_ALLOC(blwi, sizeof(*blwi));
1776                 if (blwi == NULL)
1777                         RETURN(-ENOMEM);
1778                 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1779
1780                 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1781         }
1782 }
1783
1784 #endif
1785
1786 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1787                            struct ldlm_lock *lock)
1788 {
1789 #ifdef __KERNEL__
1790         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1791 #else
1792         RETURN(-ENOSYS);
1793 #endif
1794 }
1795
1796 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1797                            cfs_list_t *cancels, int count, int mode)
1798 {
1799 #ifdef __KERNEL__
1800         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1801 #else
1802         RETURN(-ENOSYS);
1803 #endif
1804 }
1805
1806 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1807 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1808 {
1809         struct obd_device *obd = req->rq_export->exp_obd;
1810         char *key;
1811         void *val;
1812         int keylen, vallen;
1813         int rc = -ENOSYS;
1814         ENTRY;
1815
1816         DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1817
1818         req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1819
1820         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1821         if (key == NULL) {
1822                 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1823                 RETURN(-EFAULT);
1824         }
1825         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1826                                       RCL_CLIENT);
1827         val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1828         if (val == NULL) {
1829                 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1830                 RETURN(-EFAULT);
1831         }
1832         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1833                                       RCL_CLIENT);
1834
1835         /* We are responsible for swabbing contents of val */
1836
1837         if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1838                 /* Pass it on to mdc (the "export" in this case) */
1839                 rc = obd_set_info_async(req->rq_svc_thread->t_env,
1840                                         req->rq_export,
1841                                         sizeof(KEY_HSM_COPYTOOL_SEND),
1842                                         KEY_HSM_COPYTOOL_SEND,
1843                                         vallen, val, NULL);
1844         else
1845                 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1846
1847         return rc;
1848 }
1849
1850 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1851                                         const char *msg, int rc,
1852                                         struct lustre_handle *handle)
1853 {
1854         DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1855                   "%s: [nid %s] [rc %d] [lock "LPX64"]",
1856                   msg, libcfs_id2str(req->rq_peer), rc,
1857                   handle ? handle->cookie : 0);
1858         if (req->rq_no_reply)
1859                 CWARN("No reply was sent, maybe cause bug 21636.\n");
1860         else if (rc)
1861                 CWARN("Send reply failed, maybe cause bug 21636.\n");
1862 }
1863
1864 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1865 static int ldlm_callback_handler(struct ptlrpc_request *req)
1866 {
1867         struct ldlm_namespace *ns;
1868         struct ldlm_request *dlm_req;
1869         struct ldlm_lock *lock;
1870         int rc;
1871         ENTRY;
1872
1873         /* Requests arrive in sender's byte order.  The ptlrpc service
1874          * handler has already checked and, if necessary, byte-swapped the
1875          * incoming request message body, but I am responsible for the
1876          * message buffers. */
1877
1878         /* do nothing for sec context finalize */
1879         if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1880                 RETURN(0);
1881
1882         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1883
1884         if (req->rq_export == NULL) {
1885                 rc = ldlm_callback_reply(req, -ENOTCONN);
1886                 ldlm_callback_errmsg(req, "Operate on unconnected server",
1887                                      rc, NULL);
1888                 RETURN(0);
1889         }
1890
1891         LASSERT(req->rq_export != NULL);
1892         LASSERT(req->rq_export->exp_obd != NULL);
1893
1894         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1895         case LDLM_BL_CALLBACK:
1896                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1897                         RETURN(0);
1898                 break;
1899         case LDLM_CP_CALLBACK:
1900                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1901                         RETURN(0);
1902                 break;
1903         case LDLM_GL_CALLBACK:
1904                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1905                         RETURN(0);
1906                 break;
1907         case LDLM_SET_INFO:
1908                 rc = ldlm_handle_setinfo(req);
1909                 ldlm_callback_reply(req, rc);
1910                 RETURN(0);
1911         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1912                 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1913                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1914                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1915                         RETURN(0);
1916                 rc = llog_origin_handle_cancel(req);
1917                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1918                         RETURN(0);
1919                 ldlm_callback_reply(req, rc);
1920                 RETURN(0);
1921         case OBD_QC_CALLBACK:
1922                 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1923                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1924                         RETURN(0);
1925                 rc = target_handle_qc_callback(req);
1926                 ldlm_callback_reply(req, rc);
1927                 RETURN(0);
1928         case QUOTA_DQACQ:
1929         case QUOTA_DQREL:
1930                 /* reply in handler */
1931                 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1932                 rc = target_handle_dqacq_callback(req);
1933                 RETURN(0);
1934         case LLOG_ORIGIN_HANDLE_CREATE:
1935                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1936                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1937                         RETURN(0);
1938                 rc = llog_origin_handle_create(req);
1939                 ldlm_callback_reply(req, rc);
1940                 RETURN(0);
1941         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1942                 req_capsule_set(&req->rq_pill,
1943                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1944                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1945                         RETURN(0);
1946                 rc = llog_origin_handle_next_block(req);
1947                 ldlm_callback_reply(req, rc);
1948                 RETURN(0);
1949         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1950                 req_capsule_set(&req->rq_pill,
1951                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1952                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1953                         RETURN(0);
1954                 rc = llog_origin_handle_read_header(req);
1955                 ldlm_callback_reply(req, rc);
1956                 RETURN(0);
1957         case LLOG_ORIGIN_HANDLE_CLOSE:
1958                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1959                         RETURN(0);
1960                 rc = llog_origin_handle_close(req);
1961                 ldlm_callback_reply(req, rc);
1962                 RETURN(0);
1963         default:
1964                 CERROR("unknown opcode %u\n",
1965                        lustre_msg_get_opc(req->rq_reqmsg));
1966                 ldlm_callback_reply(req, -EPROTO);
1967                 RETURN(0);
1968         }
1969
1970         ns = req->rq_export->exp_obd->obd_namespace;
1971         LASSERT(ns != NULL);
1972
1973         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1974
1975         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1976         if (dlm_req == NULL) {
1977                 rc = ldlm_callback_reply(req, -EPROTO);
1978                 ldlm_callback_errmsg(req, "Operate without parameter", rc,
1979                                      NULL);
1980                 RETURN(0);
1981         }
1982
1983         /* Force a known safe race, send a cancel to the server for a lock
1984          * which the server has already started a blocking callback on. */
1985         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1986             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1987                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1988                 if (rc < 0)
1989                         CERROR("ldlm_cli_cancel: %d\n", rc);
1990         }
1991
1992         lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1993         if (!lock) {
1994                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1995                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1996                 rc = ldlm_callback_reply(req, -EINVAL);
1997                 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
1998                                      &dlm_req->lock_handle[0]);
1999                 RETURN(0);
2000         }
2001
2002         if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2003             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2004                 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2005
2006         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2007         lock_res_and_lock(lock);
2008         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
2009         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2010                 /* If somebody cancels lock and cache is already dropped,
2011                  * or lock is failed before cp_ast received on client,
2012                  * we can tell the server we have no lock. Otherwise, we
2013                  * should send cancel after dropping the cache. */
2014                 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2015                     (lock->l_flags & LDLM_FL_BL_DONE)) ||
2016                     (lock->l_flags & LDLM_FL_FAILED)) {
2017                         LDLM_DEBUG(lock, "callback on lock "
2018                                    LPX64" - lock disappeared\n",
2019                                    dlm_req->lock_handle[0].cookie);
2020                         unlock_res_and_lock(lock);
2021                         LDLM_LOCK_RELEASE(lock);
2022                         rc = ldlm_callback_reply(req, -EINVAL);
2023                         ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2024                                              &dlm_req->lock_handle[0]);
2025                         RETURN(0);
2026                 }
2027                 /* BL_AST locks are not needed in lru.
2028                  * let ldlm_cancel_lru() be fast. */
2029                 ldlm_lock_remove_from_lru(lock);
2030                 lock->l_flags |= LDLM_FL_BL_AST;
2031         }
2032         unlock_res_and_lock(lock);
2033
2034         /* We want the ost thread to get this reply so that it can respond
2035          * to ost requests (write cache writeback) that might be triggered
2036          * in the callback.
2037          *
2038          * But we'd also like to be able to indicate in the reply that we're
2039          * cancelling right now, because it's unused, or have an intent result
2040          * in the reply, so we might have to push the responsibility for sending
2041          * the reply down into the AST handlers, alas. */
2042
2043         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2044         case LDLM_BL_CALLBACK:
2045                 CDEBUG(D_INODE, "blocking ast\n");
2046                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2047                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2048                         rc = ldlm_callback_reply(req, 0);
2049                         if (req->rq_no_reply || rc)
2050                                 ldlm_callback_errmsg(req, "Normal process", rc,
2051                                                      &dlm_req->lock_handle[0]);
2052                 }
2053                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2054                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2055                 break;
2056         case LDLM_CP_CALLBACK:
2057                 CDEBUG(D_INODE, "completion ast\n");
2058                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2059                 ldlm_callback_reply(req, 0);
2060                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2061                 break;
2062         case LDLM_GL_CALLBACK:
2063                 CDEBUG(D_INODE, "glimpse ast\n");
2064                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2065                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2066                 break;
2067         default:
2068                 LBUG();                         /* checked above */
2069         }
2070
2071         RETURN(0);
2072 }
2073
2074 #ifdef HAVE_SERVER_SUPPORT
2075 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2076 {
2077         int rc;
2078         ENTRY;
2079
2080         /* Requests arrive in sender's byte order.  The ptlrpc service
2081          * handler has already checked and, if necessary, byte-swapped the
2082          * incoming request message body, but I am responsible for the
2083          * message buffers. */
2084
2085         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2086
2087         if (req->rq_export == NULL) {
2088                 struct ldlm_request *dlm_req;
2089
2090                 CERROR("%s from %s arrived at %lu with bad export cookie "
2091                        LPU64"\n",
2092                        ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2093                        libcfs_nid2str(req->rq_peer.nid),
2094                        req->rq_arrival_time.tv_sec,
2095                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2096
2097                 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2098                         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2099                         dlm_req = req_capsule_client_get(&req->rq_pill,
2100                                                          &RMF_DLM_REQ);
2101                         if (dlm_req != NULL)
2102                                 ldlm_lock_dump_handle(D_ERROR,
2103                                                       &dlm_req->lock_handle[0]);
2104                 }
2105                 ldlm_callback_reply(req, -ENOTCONN);
2106                 RETURN(0);
2107         }
2108
2109         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2110
2111         /* XXX FIXME move this back to mds/handler.c, bug 249 */
2112         case LDLM_CANCEL:
2113                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2114                 CDEBUG(D_INODE, "cancel\n");
2115                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2116                         RETURN(0);
2117                 rc = ldlm_handle_cancel(req);
2118                 if (rc)
2119                         break;
2120                 RETURN(0);
2121         case OBD_LOG_CANCEL:
2122                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2123                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2124                         RETURN(0);
2125                 rc = llog_origin_handle_cancel(req);
2126                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2127                         RETURN(0);
2128                 ldlm_callback_reply(req, rc);
2129                 RETURN(0);
2130         default:
2131                 CERROR("invalid opcode %d\n",
2132                        lustre_msg_get_opc(req->rq_reqmsg));
2133                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2134                 ldlm_callback_reply(req, -EINVAL);
2135         }
2136
2137         RETURN(0);
2138 }
2139
2140 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2141                                         struct ldlm_lock *lock)
2142 {
2143         struct ldlm_request *dlm_req;
2144         struct lustre_handle lockh;
2145         int rc = 0;
2146         int i;
2147         ENTRY;
2148
2149         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2150         if (dlm_req == NULL)
2151                 RETURN(0);
2152
2153         ldlm_lock2handle(lock, &lockh);
2154         for (i = 0; i < dlm_req->lock_count; i++) {
2155                 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2156                                         &lockh)) {
2157                         DEBUG_REQ(D_RPCTRACE, req,
2158                                   "Prio raised by lock "LPX64".", lockh.cookie);
2159
2160                         rc = 1;
2161                         break;
2162                 }
2163         }
2164
2165         RETURN(rc);
2166
2167 }
2168
2169 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2170 {
2171         struct ldlm_request *dlm_req;
2172         int rc = 0;
2173         int i;
2174         ENTRY;
2175
2176         /* no prolong in recovery */
2177         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2178                 RETURN(0);
2179
2180         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2181         if (dlm_req == NULL)
2182                 RETURN(-EFAULT);
2183
2184         for (i = 0; i < dlm_req->lock_count; i++) {
2185                 struct ldlm_lock *lock;
2186
2187                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2188                 if (lock == NULL)
2189                         continue;
2190
2191                 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2192                 if (rc)
2193                         LDLM_DEBUG(lock, "hpreq cancel lock");
2194                 LDLM_LOCK_PUT(lock);
2195
2196                 if (rc)
2197                         break;
2198         }
2199
2200         RETURN(rc);
2201 }
2202
2203 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2204         .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2205         .hpreq_check      = ldlm_cancel_hpreq_check
2206 };
2207
2208 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2209 {
2210         ENTRY;
2211
2212         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2213
2214         if (req->rq_export == NULL)
2215                 RETURN(0);
2216
2217         if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2218                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2219                 req->rq_ops = &ldlm_cancel_hpreq_ops;
2220         }
2221         RETURN(0);
2222 }
2223
2224 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2225                         cfs_hlist_node_t *hnode, void *data)
2226
2227 {
2228         cfs_list_t         *rpc_list = data;
2229         struct ldlm_lock   *lock = cfs_hash_object(hs, hnode);
2230
2231         lock_res_and_lock(lock);
2232
2233         if (lock->l_req_mode != lock->l_granted_mode) {
2234                 unlock_res_and_lock(lock);
2235                 return 0;
2236         }
2237
2238         LASSERT(lock->l_resource);
2239         if (lock->l_resource->lr_type != LDLM_IBITS &&
2240             lock->l_resource->lr_type != LDLM_PLAIN) {
2241                 unlock_res_and_lock(lock);
2242                 return 0;
2243         }
2244
2245         if (lock->l_flags & LDLM_FL_AST_SENT) {
2246                 unlock_res_and_lock(lock);
2247                 return 0;
2248         }
2249
2250         LASSERT(lock->l_blocking_ast);
2251         LASSERT(!lock->l_blocking_lock);
2252
2253         lock->l_flags |= LDLM_FL_AST_SENT;
2254         if (lock->l_export && lock->l_export->exp_lock_hash) {
2255                 /* NB: it's safe to call cfs_hash_del() even lock isn't
2256                  * in exp_lock_hash. */
2257                 cfs_hash_del(lock->l_export->exp_lock_hash,
2258                              &lock->l_remote_handle, &lock->l_exp_hash);
2259         }
2260
2261         cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2262         LDLM_LOCK_GET(lock);
2263
2264         unlock_res_and_lock(lock);
2265         return 0;
2266 }
2267
2268 void ldlm_revoke_export_locks(struct obd_export *exp)
2269 {
2270         cfs_list_t  rpc_list;
2271         ENTRY;
2272
2273         CFS_INIT_LIST_HEAD(&rpc_list);
2274         cfs_hash_for_each_empty(exp->exp_lock_hash,
2275                                 ldlm_revoke_lock_cb, &rpc_list);
2276         ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2277                           LDLM_WORK_REVOKE_AST);
2278
2279         EXIT;
2280 }
2281 #endif /* HAVE_SERVER_SUPPORT */
2282
2283 #ifdef __KERNEL__
2284 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2285 {
2286         struct ldlm_bl_work_item *blwi = NULL;
2287         static unsigned int num_bl = 0;
2288
2289         cfs_spin_lock(&blp->blp_lock);
2290         /* process a request from the blp_list at least every blp_num_threads */
2291         if (!cfs_list_empty(&blp->blp_list) &&
2292             (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2293                 blwi = cfs_list_entry(blp->blp_list.next,
2294                                       struct ldlm_bl_work_item, blwi_entry);
2295         else
2296                 if (!cfs_list_empty(&blp->blp_prio_list))
2297                         blwi = cfs_list_entry(blp->blp_prio_list.next,
2298                                               struct ldlm_bl_work_item,
2299                                               blwi_entry);
2300
2301         if (blwi) {
2302                 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2303                         num_bl = 0;
2304                 cfs_list_del(&blwi->blwi_entry);
2305         }
2306         cfs_spin_unlock(&blp->blp_lock);
2307
2308         return blwi;
2309 }
2310
2311 /* This only contains temporary data until the thread starts */
2312 struct ldlm_bl_thread_data {
2313         char                    bltd_name[CFS_CURPROC_COMM_MAX];
2314         struct ldlm_bl_pool     *bltd_blp;
2315         cfs_completion_t        bltd_comp;
2316         int                     bltd_num;
2317 };
2318
2319 static int ldlm_bl_thread_main(void *arg);
2320
2321 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2322 {
2323         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2324         int rc;
2325
2326         cfs_init_completion(&bltd.bltd_comp);
2327         rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2328         if (rc < 0) {
2329                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2330                        cfs_atomic_read(&blp->blp_num_threads), rc);
2331                 return rc;
2332         }
2333         cfs_wait_for_completion(&bltd.bltd_comp);
2334
2335         return 0;
2336 }
2337
2338 static int ldlm_bl_thread_main(void *arg)
2339 {
2340         struct ldlm_bl_pool *blp;
2341         ENTRY;
2342
2343         {
2344                 struct ldlm_bl_thread_data *bltd = arg;
2345
2346                 blp = bltd->bltd_blp;
2347
2348                 bltd->bltd_num =
2349                         cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2350                 cfs_atomic_inc(&blp->blp_busy_threads);
2351
2352                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2353                         "ldlm_bl_%02d", bltd->bltd_num);
2354                 cfs_daemonize(bltd->bltd_name);
2355
2356                 cfs_complete(&bltd->bltd_comp);
2357                 /* cannot use bltd after this, it is only on caller's stack */
2358         }
2359
2360         while (1) {
2361                 struct l_wait_info lwi = { 0 };
2362                 struct ldlm_bl_work_item *blwi = NULL;
2363                 int busy;
2364
2365                 blwi = ldlm_bl_get_work(blp);
2366
2367                 if (blwi == NULL) {
2368                         cfs_atomic_dec(&blp->blp_busy_threads);
2369                         l_wait_event_exclusive(blp->blp_waitq,
2370                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
2371                                          &lwi);
2372                         busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2373                 } else {
2374                         busy = cfs_atomic_read(&blp->blp_busy_threads);
2375                 }
2376
2377                 if (blwi->blwi_ns == NULL)
2378                         /* added by ldlm_cleanup() */
2379                         break;
2380
2381                 /* Not fatal if racy and have a few too many threads */
2382                 if (unlikely(busy < blp->blp_max_threads &&
2383                              busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2384                              !blwi->blwi_mem_pressure))
2385                         /* discard the return value, we tried */
2386                         ldlm_bl_thread_start(blp);
2387
2388                 if (blwi->blwi_mem_pressure)
2389                         cfs_memory_pressure_set();
2390
2391                 if (blwi->blwi_count) {
2392                         int count;
2393                         /* The special case when we cancel locks in lru
2394                          * asynchronously, we pass the list of locks here.
2395                          * Thus locks are marked LDLM_FL_CANCELING, but NOT
2396                          * canceled locally yet. */
2397                         count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2398                                                            blwi->blwi_count,
2399                                                            LCF_BL_AST);
2400                         ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2401                 } else {
2402                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2403                                                 blwi->blwi_lock);
2404                 }
2405                 if (blwi->blwi_mem_pressure)
2406                         cfs_memory_pressure_clr();
2407
2408                 if (blwi->blwi_mode == LDLM_ASYNC)
2409                         OBD_FREE(blwi, sizeof(*blwi));
2410                 else
2411                         cfs_complete(&blwi->blwi_comp);
2412         }
2413
2414         cfs_atomic_dec(&blp->blp_busy_threads);
2415         cfs_atomic_dec(&blp->blp_num_threads);
2416         cfs_complete(&blp->blp_comp);
2417         RETURN(0);
2418 }
2419
2420 #endif
2421
2422 static int ldlm_setup(void);
2423 static int ldlm_cleanup(void);
2424
2425 int ldlm_get_ref(void)
2426 {
2427         int rc = 0;
2428         ENTRY;
2429         cfs_mutex_lock(&ldlm_ref_mutex);
2430         if (++ldlm_refcount == 1) {
2431                 rc = ldlm_setup();
2432                 if (rc)
2433                         ldlm_refcount--;
2434         }
2435         cfs_mutex_unlock(&ldlm_ref_mutex);
2436
2437         RETURN(rc);
2438 }
2439
2440 void ldlm_put_ref(void)
2441 {
2442         ENTRY;
2443         cfs_mutex_lock(&ldlm_ref_mutex);
2444         if (ldlm_refcount == 1) {
2445                 int rc = ldlm_cleanup();
2446                 if (rc)
2447                         CERROR("ldlm_cleanup failed: %d\n", rc);
2448                 else
2449                         ldlm_refcount--;
2450         } else {
2451                 ldlm_refcount--;
2452         }
2453         cfs_mutex_unlock(&ldlm_ref_mutex);
2454
2455         EXIT;
2456 }
2457
2458 /*
2459  * Export handle<->lock hash operations.
2460  */
2461 static unsigned
2462 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2463 {
2464         return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2465 }
2466
2467 static void *
2468 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2469 {
2470         struct ldlm_lock *lock;
2471
2472         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2473         return &lock->l_remote_handle;
2474 }
2475
2476 static void
2477 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2478 {
2479         struct ldlm_lock     *lock;
2480
2481         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2482         lock->l_remote_handle = *(struct lustre_handle *)key;
2483 }
2484
2485 static int
2486 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2487 {
2488         return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2489 }
2490
2491 static void *
2492 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2493 {
2494         return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2495 }
2496
2497 static void
2498 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2499 {
2500         struct ldlm_lock *lock;
2501
2502         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2503         LDLM_LOCK_GET(lock);
2504 }
2505
2506 static void
2507 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2508 {
2509         struct ldlm_lock *lock;
2510
2511         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2512         LDLM_LOCK_RELEASE(lock);
2513 }
2514
2515 static cfs_hash_ops_t ldlm_export_lock_ops = {
2516         .hs_hash        = ldlm_export_lock_hash,
2517         .hs_key         = ldlm_export_lock_key,
2518         .hs_keycmp      = ldlm_export_lock_keycmp,
2519         .hs_keycpy      = ldlm_export_lock_keycpy,
2520         .hs_object      = ldlm_export_lock_object,
2521         .hs_get         = ldlm_export_lock_get,
2522         .hs_put         = ldlm_export_lock_put,
2523         .hs_put_locked  = ldlm_export_lock_put,
2524 };
2525
2526 int ldlm_init_export(struct obd_export *exp)
2527 {
2528         ENTRY;
2529
2530         exp->exp_lock_hash =
2531                 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2532                                 HASH_EXP_LOCK_CUR_BITS,
2533                                 HASH_EXP_LOCK_MAX_BITS,
2534                                 HASH_EXP_LOCK_BKT_BITS, 0,
2535                                 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2536                                 &ldlm_export_lock_ops,
2537                                 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2538                                 CFS_HASH_NBLK_CHANGE);
2539
2540         if (!exp->exp_lock_hash)
2541                 RETURN(-ENOMEM);
2542
2543         RETURN(0);
2544 }
2545 EXPORT_SYMBOL(ldlm_init_export);
2546
2547 void ldlm_destroy_export(struct obd_export *exp)
2548 {
2549         ENTRY;
2550         cfs_hash_putref(exp->exp_lock_hash);
2551         exp->exp_lock_hash = NULL;
2552
2553         ldlm_destroy_flock_export(exp);
2554         EXIT;
2555 }
2556 EXPORT_SYMBOL(ldlm_destroy_export);
2557
2558 static int ldlm_setup(void)
2559 {
2560         static struct ptlrpc_service_conf       conf;
2561         struct ldlm_bl_pool                     *blp = NULL;
2562         int rc = 0;
2563 #ifdef __KERNEL__
2564         int i;
2565 #endif
2566         ENTRY;
2567
2568         if (ldlm_state != NULL)
2569                 RETURN(-EALREADY);
2570
2571         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2572         if (ldlm_state == NULL)
2573                 RETURN(-ENOMEM);
2574
2575 #ifdef LPROCFS
2576         rc = ldlm_proc_setup();
2577         if (rc != 0)
2578                 GOTO(out, rc);
2579 #endif
2580
2581         memset(&conf, 0, sizeof(conf));
2582         conf = (typeof(conf)) {
2583                 .psc_name               = "ldlm_cbd",
2584                 .psc_watchdog_factor    = 2,
2585                 .psc_buf                = {
2586                         .bc_nbufs               = LDLM_NBUFS,
2587                         .bc_buf_size            = LDLM_BUFSIZE,
2588                         .bc_req_max_size        = LDLM_MAXREQSIZE,
2589                         .bc_rep_max_size        = LDLM_MAXREPSIZE,
2590                         .bc_req_portal          = LDLM_CB_REQUEST_PORTAL,
2591                         .bc_rep_portal          = LDLM_CB_REPLY_PORTAL,
2592                 },
2593                 .psc_thr                = {
2594                         .tc_thr_name            = "ldlm_cb",
2595                         .tc_thr_factor          = LDLM_THR_FACTOR,
2596                         .tc_nthrs_init          = LDLM_NTHRS_INIT,
2597                         .tc_nthrs_base          = LDLM_NTHRS_BASE,
2598                         .tc_nthrs_max           = LDLM_NTHRS_MAX,
2599                         .tc_nthrs_user          = ldlm_num_threads,
2600                         .tc_cpu_affinity        = 1,
2601                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD,
2602                 },
2603                 .psc_cpt                = {
2604                         .cc_pattern             = ldlm_cpts,
2605                 },
2606                 .psc_ops                = {
2607                         .so_req_handler         = ldlm_callback_handler,
2608                 },
2609         };
2610         ldlm_state->ldlm_cb_service = \
2611                         ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2612         if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2613                 CERROR("failed to start service\n");
2614                 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2615                 ldlm_state->ldlm_cb_service = NULL;
2616                 GOTO(out, rc);
2617         }
2618
2619 #ifdef HAVE_SERVER_SUPPORT
2620         memset(&conf, 0, sizeof(conf));
2621         conf = (typeof(conf)) {
2622                 .psc_name               = "ldlm_canceld",
2623                 .psc_watchdog_factor    = 6,
2624                 .psc_buf                = {
2625                         .bc_nbufs               = LDLM_NBUFS,
2626                         .bc_buf_size            = LDLM_BUFSIZE,
2627                         .bc_req_max_size        = LDLM_MAXREQSIZE,
2628                         .bc_rep_max_size        = LDLM_MAXREPSIZE,
2629                         .bc_req_portal          = LDLM_CANCEL_REQUEST_PORTAL,
2630                         .bc_rep_portal          = LDLM_CANCEL_REPLY_PORTAL,
2631
2632                 },
2633                 .psc_thr                = {
2634                         .tc_thr_name            = "ldlm_cn",
2635                         .tc_thr_factor          = LDLM_THR_FACTOR,
2636                         .tc_nthrs_init          = LDLM_NTHRS_INIT,
2637                         .tc_nthrs_base          = LDLM_NTHRS_BASE,
2638                         .tc_nthrs_max           = LDLM_NTHRS_MAX,
2639                         .tc_nthrs_user          = ldlm_num_threads,
2640                         .tc_cpu_affinity        = 1,
2641                         .tc_ctx_tags            = LCT_MD_THREAD | \
2642                                                   LCT_DT_THREAD | \
2643                                                   LCT_CL_THREAD,
2644                 },
2645                 .psc_cpt                = {
2646                         .cc_pattern             = ldlm_cpts,
2647                 },
2648                 .psc_ops                = {
2649                         .so_req_handler         = ldlm_cancel_handler,
2650                         .so_hpreq_handler       = ldlm_hpreq_handler,
2651                 },
2652         };
2653         ldlm_state->ldlm_cancel_service = \
2654                         ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2655         if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2656                 CERROR("failed to start service\n");
2657                 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2658                 ldlm_state->ldlm_cancel_service = NULL;
2659                 GOTO(out, rc);
2660         }
2661 #endif
2662
2663         OBD_ALLOC(blp, sizeof(*blp));
2664         if (blp == NULL)
2665                 GOTO(out, rc = -ENOMEM);
2666         ldlm_state->ldlm_bl_pool = blp;
2667
2668         cfs_spin_lock_init(&blp->blp_lock);
2669         CFS_INIT_LIST_HEAD(&blp->blp_list);
2670         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2671         cfs_waitq_init(&blp->blp_waitq);
2672         cfs_atomic_set(&blp->blp_num_threads, 0);
2673         cfs_atomic_set(&blp->blp_busy_threads, 0);
2674
2675 #ifdef __KERNEL__
2676         if (ldlm_num_threads == 0) {
2677                 blp->blp_min_threads = LDLM_NTHRS_INIT;
2678                 blp->blp_max_threads = LDLM_NTHRS_MAX;
2679         } else {
2680                 blp->blp_min_threads = blp->blp_max_threads = \
2681                         min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2682                                                          ldlm_num_threads));
2683         }
2684
2685         for (i = 0; i < blp->blp_min_threads; i++) {
2686                 rc = ldlm_bl_thread_start(blp);
2687                 if (rc < 0)
2688                         GOTO(out, rc);
2689         }
2690
2691 # ifdef HAVE_SERVER_SUPPORT
2692         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2693         expired_lock_thread.elt_state = ELT_STOPPED;
2694         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2695
2696         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2697         cfs_spin_lock_init(&waiting_locks_spinlock);
2698         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2699
2700         rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2701         if (rc < 0) {
2702                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2703                 GOTO(out, rc);
2704         }
2705
2706         cfs_wait_event(expired_lock_thread.elt_waitq,
2707                        expired_lock_thread.elt_state == ELT_READY);
2708 # endif /* HAVE_SERVER_SUPPORT */
2709
2710         rc = ldlm_pools_init();
2711         if (rc) {
2712                 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2713                 GOTO(out, rc);
2714         }
2715 #endif
2716         RETURN(0);
2717
2718  out:
2719         ldlm_cleanup();
2720         RETURN(rc);
2721 }
2722
2723 static int ldlm_cleanup(void)
2724 {
2725         ENTRY;
2726
2727         if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2728             !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2729                 CERROR("ldlm still has namespaces; clean these up first.\n");
2730                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2731                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2732                 RETURN(-EBUSY);
2733         }
2734
2735 #ifdef __KERNEL__
2736         ldlm_pools_fini();
2737
2738         if (ldlm_state->ldlm_bl_pool != NULL) {
2739                 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2740
2741                 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2742                         struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2743
2744                         cfs_init_completion(&blp->blp_comp);
2745
2746                         cfs_spin_lock(&blp->blp_lock);
2747                         cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2748                         cfs_waitq_signal(&blp->blp_waitq);
2749                         cfs_spin_unlock(&blp->blp_lock);
2750
2751                         cfs_wait_for_completion(&blp->blp_comp);
2752                 }
2753
2754                 OBD_FREE(blp, sizeof(*blp));
2755         }
2756 #endif /* __KERNEL__ */
2757
2758         if (ldlm_state->ldlm_cb_service != NULL)
2759                 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2760 # ifdef HAVE_SERVER_SUPPORT
2761         if (ldlm_state->ldlm_cancel_service != NULL)
2762                 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2763 # endif
2764
2765 #ifdef __KERNEL__
2766         ldlm_proc_cleanup();
2767
2768 # ifdef HAVE_SERVER_SUPPORT
2769         if (expired_lock_thread.elt_state != ELT_STOPPED) {
2770                 expired_lock_thread.elt_state = ELT_TERMINATE;
2771                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2772                 cfs_wait_event(expired_lock_thread.elt_waitq,
2773                                expired_lock_thread.elt_state == ELT_STOPPED);
2774         }
2775 # endif
2776 #endif /* __KERNEL__ */
2777
2778         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2779         ldlm_state = NULL;
2780
2781         RETURN(0);
2782 }
2783
2784 int ldlm_init(void)
2785 {
2786         cfs_mutex_init(&ldlm_ref_mutex);
2787         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2788         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2789         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2790                                                sizeof(struct ldlm_resource), 0,
2791                                                CFS_SLAB_HWCACHE_ALIGN);
2792         if (ldlm_resource_slab == NULL)
2793                 return -ENOMEM;
2794
2795         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2796                               sizeof(struct ldlm_lock), 0,
2797                               CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2798         if (ldlm_lock_slab == NULL) {
2799                 cfs_mem_cache_destroy(ldlm_resource_slab);
2800                 return -ENOMEM;
2801         }
2802
2803         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2804                                         sizeof(struct ldlm_interval),
2805                                         0, CFS_SLAB_HWCACHE_ALIGN);
2806         if (ldlm_interval_slab == NULL) {
2807                 cfs_mem_cache_destroy(ldlm_resource_slab);
2808                 cfs_mem_cache_destroy(ldlm_lock_slab);
2809                 return -ENOMEM;
2810         }
2811 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2812         class_export_dump_hook = ldlm_dump_export_locks;
2813 #endif
2814         return 0;
2815 }
2816
2817 void ldlm_exit(void)
2818 {
2819         int rc;
2820         if (ldlm_refcount)
2821                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2822         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2823         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2824 #ifdef __KERNEL__
2825         /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2826          * synchronize_rcu() to wait a grace period elapsed, so that
2827          * ldlm_lock_free() get a chance to be called. */
2828         synchronize_rcu();
2829 #endif
2830         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2831         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2832         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2833         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2834 }
2835
2836 /* ldlm_extent.c */
2837 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2838
2839 /* ldlm_lock.c */
2840 #ifdef HAVE_SERVER_SUPPORT
2841 EXPORT_SYMBOL(ldlm_get_processing_policy);
2842 #endif
2843 EXPORT_SYMBOL(ldlm_lock2desc);
2844 EXPORT_SYMBOL(ldlm_register_intent);
2845 EXPORT_SYMBOL(ldlm_lockname);
2846 EXPORT_SYMBOL(ldlm_typename);
2847 EXPORT_SYMBOL(ldlm_lock2handle);
2848 EXPORT_SYMBOL(__ldlm_handle2lock);
2849 EXPORT_SYMBOL(ldlm_lock_get);
2850 EXPORT_SYMBOL(ldlm_lock_put);
2851 EXPORT_SYMBOL(ldlm_lock_match);
2852 EXPORT_SYMBOL(ldlm_lock_cancel);
2853 EXPORT_SYMBOL(ldlm_lock_addref);
2854 EXPORT_SYMBOL(ldlm_lock_addref_try);
2855 EXPORT_SYMBOL(ldlm_lock_decref);
2856 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2857 EXPORT_SYMBOL(ldlm_lock_change_resource);
2858 EXPORT_SYMBOL(ldlm_it2str);
2859 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2860 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2861 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2862 EXPORT_SYMBOL(ldlm_lock_allow_match);
2863 EXPORT_SYMBOL(ldlm_lock_downgrade);
2864 EXPORT_SYMBOL(ldlm_lock_convert);
2865
2866 /* ldlm_request.c */
2867 EXPORT_SYMBOL(ldlm_completion_ast_async);
2868 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2869 EXPORT_SYMBOL(ldlm_completion_ast);
2870 EXPORT_SYMBOL(ldlm_blocking_ast);
2871 EXPORT_SYMBOL(ldlm_glimpse_ast);
2872 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2873 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2874 EXPORT_SYMBOL(ldlm_prep_elc_req);
2875 EXPORT_SYMBOL(ldlm_cli_convert);
2876 EXPORT_SYMBOL(ldlm_cli_enqueue);
2877 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2878 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2879 EXPORT_SYMBOL(ldlm_cli_cancel);
2880 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2881 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2882 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2883 EXPORT_SYMBOL(ldlm_replay_locks);
2884 EXPORT_SYMBOL(ldlm_resource_foreach);
2885 EXPORT_SYMBOL(ldlm_namespace_foreach);
2886 EXPORT_SYMBOL(ldlm_resource_iterate);
2887 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2888 EXPORT_SYMBOL(ldlm_cli_cancel_list_local);
2889 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2890
2891 /* ldlm_lockd.c */
2892 #ifdef HAVE_SERVER_SUPPORT
2893 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2894 EXPORT_SYMBOL(ldlm_server_completion_ast);
2895 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2896 EXPORT_SYMBOL(ldlm_handle_enqueue);
2897 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2898 EXPORT_SYMBOL(ldlm_handle_cancel);
2899 EXPORT_SYMBOL(ldlm_request_cancel);
2900 EXPORT_SYMBOL(ldlm_handle_convert);
2901 EXPORT_SYMBOL(ldlm_handle_convert0);
2902 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2903 #endif
2904 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2905 EXPORT_SYMBOL(ldlm_get_ref);
2906 EXPORT_SYMBOL(ldlm_put_ref);
2907 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2908
2909 /* ldlm_resource.c */
2910 EXPORT_SYMBOL(ldlm_namespace_new);
2911 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2912 EXPORT_SYMBOL(ldlm_namespace_free);
2913 EXPORT_SYMBOL(ldlm_namespace_dump);
2914 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2915 EXPORT_SYMBOL(ldlm_resource_get);
2916 EXPORT_SYMBOL(ldlm_resource_putref);
2917 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2918
2919 /* ldlm_lib.c */
2920 EXPORT_SYMBOL(client_import_add_conn);
2921 EXPORT_SYMBOL(client_import_del_conn);
2922 EXPORT_SYMBOL(client_obd_setup);
2923 EXPORT_SYMBOL(client_obd_cleanup);
2924 EXPORT_SYMBOL(client_connect_import);
2925 EXPORT_SYMBOL(client_disconnect_export);
2926 EXPORT_SYMBOL(target_send_reply);
2927 EXPORT_SYMBOL(target_pack_pool_reply);
2928
2929 #ifdef HAVE_SERVER_SUPPORT
2930 EXPORT_SYMBOL(server_disconnect_export);
2931 EXPORT_SYMBOL(target_stop_recovery_thread);
2932 EXPORT_SYMBOL(target_handle_connect);
2933 EXPORT_SYMBOL(target_cleanup_recovery);
2934 EXPORT_SYMBOL(target_destroy_export);
2935 EXPORT_SYMBOL(target_cancel_recovery_timer);
2936 EXPORT_SYMBOL(target_queue_recovery_request);
2937 EXPORT_SYMBOL(target_handle_ping);
2938 EXPORT_SYMBOL(target_handle_disconnect);
2939 #endif
2940
2941 /* l_lock.c */
2942 EXPORT_SYMBOL(lock_res_and_lock);
2943 EXPORT_SYMBOL(unlock_res_and_lock);