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