Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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                 ldlm_lock_dump(D_ERROR, lock, 0);
770         }
771
772         ldlm_lock_reorder_req(lock);
773
774         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
775                                         &RQF_LDLM_BL_CALLBACK,
776                                         LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
777         if (req == NULL)
778                 RETURN(-ENOMEM);
779
780         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
781         ca = ptlrpc_req_async_args(req);
782         ca->ca_set_arg = arg;
783         ca->ca_lock = lock;
784
785         req->rq_interpret_reply = ldlm_cb_interpret;
786         req->rq_no_resend = 1;
787
788         lock_res(lock->l_resource);
789         if (lock->l_granted_mode != lock->l_req_mode) {
790                 /* this blocking AST will be communicated as part of the
791                  * completion AST instead */
792                 unlock_res(lock->l_resource);
793                 ptlrpc_req_finished(req);
794                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
795                 RETURN(0);
796         }
797
798         if (lock->l_destroyed) {
799                 /* What's the point? */
800                 unlock_res(lock->l_resource);
801                 ptlrpc_req_finished(req);
802                 RETURN(0);
803         }
804
805         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
806                 instant_cancel = 1;
807
808         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
809         body->lock_handle[0] = lock->l_remote_handle;
810         body->lock_desc = *desc;
811         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
812
813         LDLM_DEBUG(lock, "server preparing blocking AST");
814
815         ptlrpc_request_set_replen(req);
816         if (instant_cancel) {
817                 unlock_res(lock->l_resource);
818                 ldlm_lock_cancel(lock);
819         } else {
820                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
821                 ldlm_add_waiting_lock(lock);
822                 unlock_res(lock->l_resource);
823         }
824
825         req->rq_send_state = LUSTRE_IMP_FULL;
826         /* ptlrpc_request_alloc_pack already set timeout */
827         if (AT_OFF)
828                 req->rq_timeout = ldlm_get_rq_timeout();
829
830         if (lock->l_export && lock->l_export->exp_nid_stats &&
831             lock->l_export->exp_nid_stats->nid_ldlm_stats)
832                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
833                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
834
835         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
836
837         RETURN(rc);
838 }
839
840 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
841 {
842         struct ldlm_cb_set_arg *arg = data;
843         struct ldlm_request    *body;
844         struct ptlrpc_request  *req;
845         struct ldlm_cb_async_args *ca;
846         long                    total_enqueue_wait;
847         int                     instant_cancel = 0;
848         int                     rc = 0;
849         ENTRY;
850
851         LASSERT(lock != NULL);
852         LASSERT(data != NULL);
853
854         total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
855                                           lock->l_last_activity);
856
857         req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
858                                     &RQF_LDLM_CP_CALLBACK);
859         if (req == NULL)
860                 RETURN(-ENOMEM);
861
862         /* server namespace, doesn't need lock */
863         if (lock->l_resource->lr_lvb_len) {
864                  req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
865                                       lock->l_resource->lr_lvb_len);
866         }
867
868         rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
869         if (rc) {
870                 ptlrpc_request_free(req);
871                 RETURN(rc);
872         }
873
874         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
875         ca = ptlrpc_req_async_args(req);
876         ca->ca_set_arg = arg;
877         ca->ca_lock = lock;
878
879         req->rq_interpret_reply = ldlm_cb_interpret;
880         req->rq_no_resend = 1;
881         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
882
883         body->lock_handle[0] = lock->l_remote_handle;
884         body->lock_flags = flags;
885         ldlm_lock2desc(lock, &body->lock_desc);
886         if (lock->l_resource->lr_lvb_len) {
887                 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
888
889                 lock_res(lock->l_resource);
890                 memcpy(lvb, lock->l_resource->lr_lvb_data,
891                        lock->l_resource->lr_lvb_len);
892                 unlock_res(lock->l_resource);
893         }
894
895         LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
896                    total_enqueue_wait);
897
898         /* Server-side enqueue wait time estimate, used in
899             __ldlm_add_waiting_lock to set future enqueue timers */
900         if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
901                 at_measured(ldlm_lock_to_ns_at(lock),
902                             total_enqueue_wait);
903         else
904                 /* bz18618. Don't add lock enqueue time we spend waiting for a
905                    previous callback to fail. Locks waiting legitimately will
906                    get extended by ldlm_refresh_waiting_lock regardless of the
907                    estimate, so it's okay to underestimate here. */
908                 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
909                        "It is likely that a previous callback timed out.",
910                        total_enqueue_wait,
911                        at_get(ldlm_lock_to_ns_at(lock)));
912
913         ptlrpc_request_set_replen(req);
914
915         req->rq_send_state = LUSTRE_IMP_FULL;
916         /* ptlrpc_request_pack already set timeout */
917         if (AT_OFF)
918                 req->rq_timeout = ldlm_get_rq_timeout();
919
920         /* We only send real blocking ASTs after the lock is granted */
921         lock_res_and_lock(lock);
922         if (lock->l_flags & LDLM_FL_AST_SENT) {
923                 body->lock_flags |= LDLM_FL_AST_SENT;
924                 /* copy ast flags like LDLM_FL_DISCARD_DATA */
925                 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
926
927                 /* We might get here prior to ldlm_handle_enqueue setting
928                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
929                  * into waiting list, but this is safe and similar code in
930                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
931                  * that would not only cancel the lock, but will also remove
932                  * it from waiting list */
933                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
934                         unlock_res_and_lock(lock);
935                         ldlm_lock_cancel(lock);
936                         instant_cancel = 1;
937                         lock_res_and_lock(lock);
938                 } else {
939                         /* start the lock-timeout clock */
940                         ldlm_add_waiting_lock(lock);
941                 }
942         }
943         unlock_res_and_lock(lock);
944
945         if (lock->l_export && lock->l_export->exp_nid_stats &&
946             lock->l_export->exp_nid_stats->nid_ldlm_stats)
947                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
948                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
949
950         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
951
952         RETURN(rc);
953 }
954
955 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
956 {
957         struct ldlm_resource  *res = lock->l_resource;
958         struct ldlm_request   *body;
959         struct ptlrpc_request *req;
960         int                    rc;
961         ENTRY;
962
963         LASSERT(lock != NULL);
964
965         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
966                                         &RQF_LDLM_GL_CALLBACK,
967                                         LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
968
969         if (req == NULL)
970                 RETURN(-ENOMEM);
971
972         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
973         body->lock_handle[0] = lock->l_remote_handle;
974         ldlm_lock2desc(lock, &body->lock_desc);
975
976         /* server namespace, doesn't need lock */
977         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
978                              lock->l_resource->lr_lvb_len);
979         res = lock->l_resource;
980         ptlrpc_request_set_replen(req);
981
982
983         req->rq_send_state = LUSTRE_IMP_FULL;
984         /* ptlrpc_request_alloc_pack already set timeout */
985         if (AT_OFF)
986                 req->rq_timeout = ldlm_get_rq_timeout();
987
988         if (lock->l_export && lock->l_export->exp_nid_stats &&
989             lock->l_export->exp_nid_stats->nid_ldlm_stats)
990                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
991                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
992
993         rc = ptlrpc_queue_wait(req);
994         /* Update the LVB from disk if the AST failed (this is a legal race)
995          *
996          * - Glimpse callback of local lock just return -ELDLM_NO_LOCK_DATA.
997          * - Glimpse callback of remote lock might return -ELDLM_NO_LOCK_DATA
998          *   when inode is cleared. LU-274
999          */
1000         if (rc == -ELDLM_NO_LOCK_DATA) {
1001                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
1002                 ldlm_res_lvbo_update(res, NULL, 1);
1003         } else if (rc != 0) {
1004                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
1005         } else {
1006                 rc = ldlm_res_lvbo_update(res, req, 1);
1007         }
1008
1009         ptlrpc_req_finished(req);
1010         if (rc == -ERESTART)
1011                 ldlm_reprocess_all(res);
1012
1013         RETURN(rc);
1014 }
1015
1016 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1017                        struct lprocfs_stats *srv_stats)
1018 {
1019         int lock_type = 0, op = 0;
1020
1021         lock_type = dlm_req->lock_desc.l_resource.lr_type;
1022
1023         switch (lock_type) {
1024         case LDLM_PLAIN:
1025                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1026                 break;
1027         case LDLM_EXTENT:
1028                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1029                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1030                 else
1031                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1032                 break;
1033         case LDLM_FLOCK:
1034                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1035                 break;
1036         case LDLM_IBITS:
1037                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1038                 break;
1039         default:
1040                 op = 0;
1041                 break;
1042         }
1043
1044         if (op)
1045                 lprocfs_counter_incr(srv_stats, op);
1046
1047         return;
1048 }
1049
1050 /*
1051  * Main server-side entry point into LDLM. This is called by ptlrpc service
1052  * threads to carry out client lock enqueueing requests.
1053  */
1054 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1055                          struct ptlrpc_request *req,
1056                          const struct ldlm_request *dlm_req,
1057                          const struct ldlm_callback_suite *cbs)
1058 {
1059         struct ldlm_reply *dlm_rep;
1060         __u32 flags;
1061         ldlm_error_t err = ELDLM_OK;
1062         struct ldlm_lock *lock = NULL;
1063         void *cookie = NULL;
1064         int rc = 0;
1065         ENTRY;
1066
1067         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1068
1069         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1070         flags = dlm_req->lock_flags;
1071
1072         LASSERT(req->rq_export);
1073
1074         if (req->rq_rqbd->rqbd_service->srv_stats)
1075                 ldlm_svc_get_eopc(dlm_req,
1076                                   req->rq_rqbd->rqbd_service->srv_stats);
1077
1078         if (req->rq_export && req->rq_export->exp_nid_stats &&
1079             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1080                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1081                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
1082
1083         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1084                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1085                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1086                           dlm_req->lock_desc.l_resource.lr_type);
1087                 GOTO(out, rc = -EFAULT);
1088         }
1089
1090         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1091                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1092                      dlm_req->lock_desc.l_req_mode &
1093                      (dlm_req->lock_desc.l_req_mode-1))) {
1094                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1095                           dlm_req->lock_desc.l_req_mode);
1096                 GOTO(out, rc = -EFAULT);
1097         }
1098
1099         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1100                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1101                              LDLM_PLAIN)) {
1102                         DEBUG_REQ(D_ERROR, req,
1103                                   "PLAIN lock request from IBITS client?");
1104                         GOTO(out, rc = -EPROTO);
1105                 }
1106         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1107                             LDLM_IBITS)) {
1108                 DEBUG_REQ(D_ERROR, req,
1109                           "IBITS lock request from unaware client?");
1110                 GOTO(out, rc = -EPROTO);
1111         }
1112
1113 #if 0
1114         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1115            against server's _CONNECT_SUPPORTED flags? (I don't want to use
1116            ibits for mgc/mgs) */
1117
1118         /* INODEBITS_INTEROP: Perform conversion from plain lock to
1119          * inodebits lock if client does not support them. */
1120         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1121             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1122                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1123                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1124                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1125                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1126                         dlm_req->lock_desc.l_req_mode = LCK_CR;
1127         }
1128 #endif
1129
1130         if (unlikely(flags & LDLM_FL_REPLAY)) {
1131                 /* Find an existing lock in the per-export lock hash */
1132                 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1133                                        (void *)&dlm_req->lock_handle[0]);
1134                 if (lock != NULL) {
1135                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1136                                   LPX64, lock->l_handle.h_cookie);
1137                         GOTO(existing_lock, rc = 0);
1138                 }
1139         }
1140
1141         /* The lock's callback data might be set in the policy function */
1142         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1143                                 dlm_req->lock_desc.l_resource.lr_type,
1144                                 dlm_req->lock_desc.l_req_mode,
1145                                 cbs, NULL, 0);
1146
1147         if (!lock)
1148                 GOTO(out, rc = -ENOMEM);
1149
1150         lock->l_last_activity = cfs_time_current_sec();
1151         lock->l_remote_handle = dlm_req->lock_handle[0];
1152         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1153
1154         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1155         /* Don't enqueue a lock onto the export if it is been disonnected
1156          * due to eviction (bug 3822) or server umount (bug 24324).
1157          * Cancel it now instead. */
1158         if (req->rq_export->exp_disconnected) {
1159                 LDLM_ERROR(lock, "lock on disconnected export %p",
1160                            req->rq_export);
1161                 GOTO(out, rc = -ENOTCONN);
1162         }
1163
1164         lock->l_export = class_export_lock_get(req->rq_export, lock);
1165         if (lock->l_export->exp_lock_hash)
1166                 cfs_hash_add(lock->l_export->exp_lock_hash,
1167                              &lock->l_remote_handle,
1168                              &lock->l_exp_hash);
1169
1170 existing_lock:
1171
1172         if (flags & LDLM_FL_HAS_INTENT) {
1173                 /* In this case, the reply buffer is allocated deep in
1174                  * local_lock_enqueue by the policy function. */
1175                 cookie = req;
1176         } else {
1177                 /* based on the assumption that lvb size never changes during
1178                  * resource life time otherwise it need resource->lr_lock's
1179                  * protection */
1180                 if (lock->l_resource->lr_lvb_len) {
1181                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1182                                              RCL_SERVER,
1183                                              lock->l_resource->lr_lvb_len);
1184                 }
1185
1186                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1187                         GOTO(out, rc = -ENOMEM);
1188
1189                 rc = req_capsule_server_pack(&req->rq_pill);
1190                 if (rc)
1191                         GOTO(out, rc);
1192         }
1193
1194         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1195                 ldlm_convert_policy_to_local(req->rq_export,
1196                                           dlm_req->lock_desc.l_resource.lr_type,
1197                                           &dlm_req->lock_desc.l_policy_data,
1198                                           &lock->l_policy_data);
1199         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1200                 lock->l_req_extent = lock->l_policy_data.l_extent;
1201
1202         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1203         if (err)
1204                 GOTO(out, err);
1205
1206         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1207         dlm_rep->lock_flags = flags;
1208
1209         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1210         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1211
1212         /* We never send a blocking AST until the lock is granted, but
1213          * we can tell it right now */
1214         lock_res_and_lock(lock);
1215
1216         /* Now take into account flags to be inherited from original lock
1217            request both in reply to client and in our own lock flags. */
1218         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1219         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1220
1221         /* Don't move a pending lock onto the export if it has already been
1222          * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1223          * Cancel it now instead. */
1224         if (unlikely(req->rq_export->exp_disconnected ||
1225                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1226                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1227                 rc = -ENOTCONN;
1228         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1229                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1230                 if (lock->l_granted_mode == lock->l_req_mode) {
1231                         /*
1232                          * Only cancel lock if it was granted, because it would
1233                          * be destroyed immediately and would never be granted
1234                          * in the future, causing timeouts on client.  Not
1235                          * granted lock will be cancelled immediately after
1236                          * sending completion AST.
1237                          */
1238                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1239                                 unlock_res_and_lock(lock);
1240                                 ldlm_lock_cancel(lock);
1241                                 lock_res_and_lock(lock);
1242                         } else
1243                                 ldlm_add_waiting_lock(lock);
1244                 }
1245         }
1246         /* Make sure we never ever grant usual metadata locks to liblustre
1247            clients */
1248         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1249             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1250              req->rq_export->exp_libclient) {
1251                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1252                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1253                         CERROR("Granting sync lock to libclient. "
1254                                "req fl %d, rep fl %d, lock fl "LPX64"\n",
1255                                dlm_req->lock_flags, dlm_rep->lock_flags,
1256                                lock->l_flags);
1257                         LDLM_ERROR(lock, "sync lock");
1258                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1259                                 struct ldlm_intent *it;
1260
1261                                 it = req_capsule_client_get(&req->rq_pill,
1262                                                             &RMF_LDLM_INTENT);
1263                                 if (it != NULL) {
1264                                         CERROR("This is intent %s ("LPU64")\n",
1265                                                ldlm_it2str(it->opc), it->opc);
1266                                 }
1267                         }
1268                 }
1269         }
1270
1271         unlock_res_and_lock(lock);
1272
1273         EXIT;
1274  out:
1275         req->rq_status = rc ?: err; /* return either error - bug 11190 */
1276         if (!req->rq_packed_final) {
1277                 err = lustre_pack_reply(req, 1, NULL, NULL);
1278                 if (rc == 0)
1279                         rc = err;
1280         }
1281
1282         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1283          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1284         if (lock) {
1285                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1286                            "(err=%d, rc=%d)", err, rc);
1287
1288                 if (rc == 0) {
1289                         if (lock->l_resource->lr_lvb_len > 0) {
1290                                 /* MDT path won't handle lr_lvb_data, so
1291                                  * lock/unlock better be contained in the
1292                                  * if block */
1293                                 void *lvb;
1294
1295                                 lvb = req_capsule_server_get(&req->rq_pill,
1296                                                              &RMF_DLM_LVB);
1297                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1298                                          req, lock);
1299                                 lock_res(lock->l_resource);
1300                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1301                                        lock->l_resource->lr_lvb_len);
1302                                 unlock_res(lock->l_resource);
1303                         }
1304                 } else {
1305                         lock_res_and_lock(lock);
1306                         ldlm_resource_unlink_lock(lock);
1307                         ldlm_lock_destroy_nolock(lock);
1308                         unlock_res_and_lock(lock);
1309                 }
1310
1311                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1312                         ldlm_reprocess_all(lock->l_resource);
1313
1314                 LDLM_LOCK_RELEASE(lock);
1315         }
1316
1317         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1318                           lock, rc);
1319
1320         return rc;
1321 }
1322
1323 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1324                         ldlm_completion_callback completion_callback,
1325                         ldlm_blocking_callback blocking_callback,
1326                         ldlm_glimpse_callback glimpse_callback)
1327 {
1328         struct ldlm_request *dlm_req;
1329         struct ldlm_callback_suite cbs = {
1330                 .lcs_completion = completion_callback,
1331                 .lcs_blocking   = blocking_callback,
1332                 .lcs_glimpse    = glimpse_callback
1333         };
1334         int rc;
1335
1336         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1337         if (dlm_req != NULL) {
1338                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1339                                           req, dlm_req, &cbs);
1340         } else {
1341                 rc = -EFAULT;
1342         }
1343         return rc;
1344 }
1345
1346 int ldlm_handle_convert0(struct ptlrpc_request *req,
1347                          const struct ldlm_request *dlm_req)
1348 {
1349         struct ldlm_reply *dlm_rep;
1350         struct ldlm_lock *lock;
1351         int rc;
1352         ENTRY;
1353
1354         if (req->rq_export && req->rq_export->exp_nid_stats &&
1355             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1356                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1357                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1358
1359         rc = req_capsule_server_pack(&req->rq_pill);
1360         if (rc)
1361                 RETURN(rc);
1362
1363         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1364         dlm_rep->lock_flags = dlm_req->lock_flags;
1365
1366         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1367         if (!lock) {
1368                 req->rq_status = EINVAL;
1369         } else {
1370                 void *res = NULL;
1371
1372                 LDLM_DEBUG(lock, "server-side convert handler START");
1373
1374                 lock->l_last_activity = cfs_time_current_sec();
1375                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1376                                         &dlm_rep->lock_flags);
1377                 if (res) {
1378                         if (ldlm_del_waiting_lock(lock))
1379                                 LDLM_DEBUG(lock, "converted waiting lock");
1380                         req->rq_status = 0;
1381                 } else {
1382                         req->rq_status = EDEADLOCK;
1383                 }
1384         }
1385
1386         if (lock) {
1387                 if (!req->rq_status)
1388                         ldlm_reprocess_all(lock->l_resource);
1389                 LDLM_DEBUG(lock, "server-side convert handler END");
1390                 LDLM_LOCK_PUT(lock);
1391         } else
1392                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1393
1394         RETURN(0);
1395 }
1396
1397 int ldlm_handle_convert(struct ptlrpc_request *req)
1398 {
1399         int rc;
1400         struct ldlm_request *dlm_req;
1401
1402         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1403         if (dlm_req != NULL) {
1404                 rc = ldlm_handle_convert0(req, dlm_req);
1405         } else {
1406                 CERROR ("Can't unpack dlm_req\n");
1407                 rc = -EFAULT;
1408         }
1409         return rc;
1410 }
1411
1412 /* Cancel all the locks whos handles are packed into ldlm_request */
1413 int ldlm_request_cancel(struct ptlrpc_request *req,
1414                         const struct ldlm_request *dlm_req, int first)
1415 {
1416         struct ldlm_resource *res, *pres = NULL;
1417         struct ldlm_lock *lock;
1418         int i, count, done = 0;
1419         ENTRY;
1420
1421         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1422         if (first >= count)
1423                 RETURN(0);
1424
1425         /* There is no lock on the server at the replay time,
1426          * skip lock cancelling to make replay tests to pass. */
1427         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1428                 RETURN(0);
1429
1430         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1431                           "starting at %d", count, first);
1432
1433         for (i = first; i < count; i++) {
1434                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1435                 if (!lock) {
1436                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1437                                           "lock (cookie "LPU64")",
1438                                           dlm_req->lock_handle[i].cookie);
1439                         continue;
1440                 }
1441
1442                 res = lock->l_resource;
1443                 done++;
1444
1445                 if (res != pres) {
1446                         if (pres != NULL) {
1447                                 ldlm_reprocess_all(pres);
1448                                 LDLM_RESOURCE_DELREF(pres);
1449                                 ldlm_resource_putref(pres);
1450                         }
1451                         if (res != NULL) {
1452                                 ldlm_resource_getref(res);
1453                                 LDLM_RESOURCE_ADDREF(res);
1454                                 ldlm_res_lvbo_update(res, NULL, 1);
1455                         }
1456                         pres = res;
1457                 }
1458                 ldlm_lock_cancel(lock);
1459                 LDLM_LOCK_PUT(lock);
1460         }
1461         if (pres != NULL) {
1462                 ldlm_reprocess_all(pres);
1463                 LDLM_RESOURCE_DELREF(pres);
1464                 ldlm_resource_putref(pres);
1465         }
1466         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1467         RETURN(done);
1468 }
1469
1470 int ldlm_handle_cancel(struct ptlrpc_request *req)
1471 {
1472         struct ldlm_request *dlm_req;
1473         int rc;
1474         ENTRY;
1475
1476         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1477         if (dlm_req == NULL) {
1478                 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1479                 RETURN(-EFAULT);
1480         }
1481
1482         if (req->rq_export && req->rq_export->exp_nid_stats &&
1483             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1484                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1485                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1486
1487         rc = req_capsule_server_pack(&req->rq_pill);
1488         if (rc)
1489                 RETURN(rc);
1490
1491         if (!ldlm_request_cancel(req, dlm_req, 0))
1492                 req->rq_status = ESTALE;
1493
1494         RETURN(ptlrpc_reply(req));
1495 }
1496 #endif /* HAVE_SERVER_SUPPORT */
1497
1498 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1499                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1500 {
1501         int do_ast;
1502         ENTRY;
1503
1504         LDLM_DEBUG(lock, "client blocking AST callback handler");
1505
1506         lock_res_and_lock(lock);
1507         lock->l_flags |= LDLM_FL_CBPENDING;
1508
1509         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1510                 lock->l_flags |= LDLM_FL_CANCEL;
1511
1512         do_ast = (!lock->l_readers && !lock->l_writers);
1513         unlock_res_and_lock(lock);
1514
1515         if (do_ast) {
1516                 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1517                        lock, lock->l_blocking_ast);
1518                 if (lock->l_blocking_ast != NULL)
1519                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1520                                              LDLM_CB_BLOCKING);
1521         } else {
1522                 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1523                        lock);
1524         }
1525
1526         LDLM_DEBUG(lock, "client blocking callback handler END");
1527         LDLM_LOCK_RELEASE(lock);
1528         EXIT;
1529 }
1530
1531 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1532                                     struct ldlm_namespace *ns,
1533                                     struct ldlm_request *dlm_req,
1534                                     struct ldlm_lock *lock)
1535 {
1536         CFS_LIST_HEAD(ast_list);
1537         ENTRY;
1538
1539         LDLM_DEBUG(lock, "client completion callback handler START");
1540
1541         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1542                 int to = cfs_time_seconds(1);
1543                 while (to > 0) {
1544                         cfs_schedule_timeout_and_set_state(
1545                                 CFS_TASK_INTERRUPTIBLE, to);
1546                         if (lock->l_granted_mode == lock->l_req_mode ||
1547                             lock->l_destroyed)
1548                                 break;
1549                 }
1550         }
1551
1552         lock_res_and_lock(lock);
1553         if (lock->l_destroyed ||
1554             lock->l_granted_mode == lock->l_req_mode) {
1555                 /* bug 11300: the lock has already been granted */
1556                 unlock_res_and_lock(lock);
1557                 LDLM_DEBUG(lock, "Double grant race happened");
1558                 LDLM_LOCK_RELEASE(lock);
1559                 EXIT;
1560                 return;
1561         }
1562
1563         /* If we receive the completion AST before the actual enqueue returned,
1564          * then we might need to switch lock modes, resources, or extents. */
1565         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1566                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1567                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1568         }
1569
1570         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1571                 ldlm_convert_policy_to_local(req->rq_export,
1572                                           dlm_req->lock_desc.l_resource.lr_type,
1573                                           &dlm_req->lock_desc.l_policy_data,
1574                                           &lock->l_policy_data);
1575                 LDLM_DEBUG(lock, "completion AST, new policy data");
1576         }
1577
1578         ldlm_resource_unlink_lock(lock);
1579         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1580                    &lock->l_resource->lr_name,
1581                    sizeof(lock->l_resource->lr_name)) != 0) {
1582                 unlock_res_and_lock(lock);
1583                 if (ldlm_lock_change_resource(ns, lock,
1584                                 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1585                         LDLM_ERROR(lock, "Failed to allocate resource");
1586                         LDLM_LOCK_RELEASE(lock);
1587                         EXIT;
1588                         return;
1589                 }
1590                 LDLM_DEBUG(lock, "completion AST, new resource");
1591                 CERROR("change resource!\n");
1592                 lock_res_and_lock(lock);
1593         }
1594
1595         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1596                 /* BL_AST locks are not needed in lru.
1597                  * let ldlm_cancel_lru() be fast. */
1598                 ldlm_lock_remove_from_lru(lock);
1599                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1600                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1601         }
1602
1603         if (lock->l_lvb_len) {
1604                 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1605                                          RCL_CLIENT) < lock->l_lvb_len) {
1606                         LDLM_ERROR(lock, "completion AST did not contain "
1607                                    "expected LVB!");
1608                 } else {
1609                         void *lvb = req_capsule_client_get(&req->rq_pill,
1610                                                            &RMF_DLM_LVB);
1611                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1612                 }
1613         }
1614
1615         ldlm_grant_lock(lock, &ast_list);
1616         unlock_res_and_lock(lock);
1617
1618         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1619
1620         /* Let Enqueue to call osc_lock_upcall() and initialize
1621          * l_ast_data */
1622         OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1623
1624         ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1625
1626         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1627                           lock);
1628         LDLM_LOCK_RELEASE(lock);
1629         EXIT;
1630 }
1631
1632 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1633                                     struct ldlm_namespace *ns,
1634                                     struct ldlm_request *dlm_req,
1635                                     struct ldlm_lock *lock)
1636 {
1637         int rc = -ENOSYS;
1638         ENTRY;
1639
1640         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1641
1642         if (lock->l_glimpse_ast != NULL)
1643                 rc = lock->l_glimpse_ast(lock, req);
1644
1645         if (req->rq_repmsg != NULL) {
1646                 ptlrpc_reply(req);
1647         } else {
1648                 req->rq_status = rc;
1649                 ptlrpc_error(req);
1650         }
1651
1652         lock_res_and_lock(lock);
1653         if (lock->l_granted_mode == LCK_PW &&
1654             !lock->l_readers && !lock->l_writers &&
1655             cfs_time_after(cfs_time_current(),
1656                            cfs_time_add(lock->l_last_used,
1657                                         cfs_time_seconds(10)))) {
1658                 unlock_res_and_lock(lock);
1659                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1660                         ldlm_handle_bl_callback(ns, NULL, lock);
1661
1662                 EXIT;
1663                 return;
1664         }
1665         unlock_res_and_lock(lock);
1666         LDLM_LOCK_RELEASE(lock);
1667         EXIT;
1668 }
1669
1670 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1671 {
1672         if (req->rq_no_reply)
1673                 return 0;
1674
1675         req->rq_status = rc;
1676         if (!req->rq_packed_final) {
1677                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1678                 if (rc)
1679                         return rc;
1680         }
1681         return ptlrpc_reply(req);
1682 }
1683
1684 #ifdef __KERNEL__
1685 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1686 {
1687         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1688         ENTRY;
1689
1690         cfs_spin_lock(&blp->blp_lock);
1691         if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1692                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1693                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1694         } else {
1695                 /* other blocking callbacks are added to the regular list */
1696                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1697         }
1698         cfs_spin_unlock(&blp->blp_lock);
1699
1700         cfs_waitq_signal(&blp->blp_waitq);
1701
1702         /* can not use blwi->blwi_mode as blwi could be already freed in
1703            LDLM_ASYNC mode */
1704         if (mode == LDLM_SYNC)
1705                 cfs_wait_for_completion(&blwi->blwi_comp);
1706
1707         RETURN(0);
1708 }
1709
1710 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1711                              struct ldlm_namespace *ns,
1712                              struct ldlm_lock_desc *ld,
1713                              cfs_list_t *cancels, int count,
1714                              struct ldlm_lock *lock,
1715                              int mode)
1716 {
1717         cfs_init_completion(&blwi->blwi_comp);
1718         CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1719
1720         if (cfs_memory_pressure_get())
1721                 blwi->blwi_mem_pressure = 1;
1722
1723         blwi->blwi_ns = ns;
1724         blwi->blwi_mode = mode;
1725         if (ld != NULL)
1726                 blwi->blwi_ld = *ld;
1727         if (count) {
1728                 cfs_list_add(&blwi->blwi_head, cancels);
1729                 cfs_list_del_init(cancels);
1730                 blwi->blwi_count = count;
1731         } else {
1732                 blwi->blwi_lock = lock;
1733         }
1734 }
1735
1736 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1737                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1738                              cfs_list_t *cancels, int count, int mode)
1739 {
1740         ENTRY;
1741
1742         if (cancels && count == 0)
1743                 RETURN(0);
1744
1745         if (mode == LDLM_SYNC) {
1746                 /* if it is synchronous call do minimum mem alloc, as it could
1747                  * be triggered from kernel shrinker
1748                  */
1749                 struct ldlm_bl_work_item blwi;
1750                 memset(&blwi, 0, sizeof(blwi));
1751                 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1752                 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1753         } else {
1754                 struct ldlm_bl_work_item *blwi;
1755                 OBD_ALLOC(blwi, sizeof(*blwi));
1756                 if (blwi == NULL)
1757                         RETURN(-ENOMEM);
1758                 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1759
1760                 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1761         }
1762 }
1763
1764 #endif
1765
1766 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1767                            struct ldlm_lock *lock)
1768 {
1769 #ifdef __KERNEL__
1770         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1771 #else
1772         RETURN(-ENOSYS);
1773 #endif
1774 }
1775
1776 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1777                            cfs_list_t *cancels, int count, int mode)
1778 {
1779 #ifdef __KERNEL__
1780         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1781 #else
1782         RETURN(-ENOSYS);
1783 #endif
1784 }
1785
1786 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1787 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1788 {
1789         struct obd_device *obd = req->rq_export->exp_obd;
1790         char *key;
1791         void *val;
1792         int keylen, vallen;
1793         int rc = -ENOSYS;
1794         ENTRY;
1795
1796         DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1797
1798         req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1799
1800         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1801         if (key == NULL) {
1802                 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1803                 RETURN(-EFAULT);
1804         }
1805         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1806                                       RCL_CLIENT);
1807         val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1808         if (val == NULL) {
1809                 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1810                 RETURN(-EFAULT);
1811         }
1812         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1813                                       RCL_CLIENT);
1814
1815         /* We are responsible for swabbing contents of val */
1816
1817         if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1818                 /* Pass it on to mdc (the "export" in this case) */
1819                 rc = obd_set_info_async(req->rq_export,
1820                                         sizeof(KEY_HSM_COPYTOOL_SEND),
1821                                         KEY_HSM_COPYTOOL_SEND,
1822                                         vallen, val, NULL);
1823         else
1824                 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1825
1826         return rc;
1827 }
1828
1829 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1830                                         const char *msg, int rc,
1831                                         struct lustre_handle *handle)
1832 {
1833         DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1834                   "%s: [nid %s] [rc %d] [lock "LPX64"]",
1835                   msg, libcfs_id2str(req->rq_peer), rc,
1836                   handle ? handle->cookie : 0);
1837         if (req->rq_no_reply)
1838                 CWARN("No reply was sent, maybe cause bug 21636.\n");
1839         else if (rc)
1840                 CWARN("Send reply failed, maybe cause bug 21636.\n");
1841 }
1842
1843 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1844 static int ldlm_callback_handler(struct ptlrpc_request *req)
1845 {
1846         struct ldlm_namespace *ns;
1847         struct ldlm_request *dlm_req;
1848         struct ldlm_lock *lock;
1849         int rc;
1850         ENTRY;
1851
1852         /* Requests arrive in sender's byte order.  The ptlrpc service
1853          * handler has already checked and, if necessary, byte-swapped the
1854          * incoming request message body, but I am responsible for the
1855          * message buffers. */
1856
1857         /* do nothing for sec context finalize */
1858         if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1859                 RETURN(0);
1860
1861         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1862
1863         if (req->rq_export == NULL) {
1864                 rc = ldlm_callback_reply(req, -ENOTCONN);
1865                 ldlm_callback_errmsg(req, "Operate on unconnected server",
1866                                      rc, NULL);
1867                 RETURN(0);
1868         }
1869
1870         LASSERT(req->rq_export != NULL);
1871         LASSERT(req->rq_export->exp_obd != NULL);
1872
1873         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1874         case LDLM_BL_CALLBACK:
1875                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1876                         RETURN(0);
1877                 break;
1878         case LDLM_CP_CALLBACK:
1879                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1880                         RETURN(0);
1881                 break;
1882         case LDLM_GL_CALLBACK:
1883                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1884                         RETURN(0);
1885                 break;
1886         case LDLM_SET_INFO:
1887                 rc = ldlm_handle_setinfo(req);
1888                 ldlm_callback_reply(req, rc);
1889                 RETURN(0);
1890         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1891                 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1892                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1893                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1894                         RETURN(0);
1895                 rc = llog_origin_handle_cancel(req);
1896                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1897                         RETURN(0);
1898                 ldlm_callback_reply(req, rc);
1899                 RETURN(0);
1900         case OBD_QC_CALLBACK:
1901                 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1902                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1903                         RETURN(0);
1904                 rc = target_handle_qc_callback(req);
1905                 ldlm_callback_reply(req, rc);
1906                 RETURN(0);
1907         case QUOTA_DQACQ:
1908         case QUOTA_DQREL:
1909                 /* reply in handler */
1910                 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1911                 rc = target_handle_dqacq_callback(req);
1912                 RETURN(0);
1913         case LLOG_ORIGIN_HANDLE_CREATE:
1914                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1915                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1916                         RETURN(0);
1917                 rc = llog_origin_handle_create(req);
1918                 ldlm_callback_reply(req, rc);
1919                 RETURN(0);
1920         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1921                 req_capsule_set(&req->rq_pill,
1922                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1923                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1924                         RETURN(0);
1925                 rc = llog_origin_handle_next_block(req);
1926                 ldlm_callback_reply(req, rc);
1927                 RETURN(0);
1928         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1929                 req_capsule_set(&req->rq_pill,
1930                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1931                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1932                         RETURN(0);
1933                 rc = llog_origin_handle_read_header(req);
1934                 ldlm_callback_reply(req, rc);
1935                 RETURN(0);
1936         case LLOG_ORIGIN_HANDLE_CLOSE:
1937                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1938                         RETURN(0);
1939                 rc = llog_origin_handle_close(req);
1940                 ldlm_callback_reply(req, rc);
1941                 RETURN(0);
1942         default:
1943                 CERROR("unknown opcode %u\n",
1944                        lustre_msg_get_opc(req->rq_reqmsg));
1945                 ldlm_callback_reply(req, -EPROTO);
1946                 RETURN(0);
1947         }
1948
1949         ns = req->rq_export->exp_obd->obd_namespace;
1950         LASSERT(ns != NULL);
1951
1952         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1953
1954         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1955         if (dlm_req == NULL) {
1956                 rc = ldlm_callback_reply(req, -EPROTO);
1957                 ldlm_callback_errmsg(req, "Operate without parameter", rc,
1958                                      NULL);
1959                 RETURN(0);
1960         }
1961
1962         /* Force a known safe race, send a cancel to the server for a lock
1963          * which the server has already started a blocking callback on. */
1964         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1965             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1966                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1967                 if (rc < 0)
1968                         CERROR("ldlm_cli_cancel: %d\n", rc);
1969         }
1970
1971         lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1972         if (!lock) {
1973                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1974                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1975                 rc = ldlm_callback_reply(req, -EINVAL);
1976                 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
1977                                      &dlm_req->lock_handle[0]);
1978                 RETURN(0);
1979         }
1980
1981         if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1982             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1983                 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1984
1985         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1986         lock_res_and_lock(lock);
1987         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1988         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1989                 /* If somebody cancels lock and cache is already dropped,
1990                  * or lock is failed before cp_ast received on client,
1991                  * we can tell the server we have no lock. Otherwise, we
1992                  * should send cancel after dropping the cache. */
1993                 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1994                     (lock->l_flags & LDLM_FL_BL_DONE)) ||
1995                     (lock->l_flags & LDLM_FL_FAILED)) {
1996                         LDLM_DEBUG(lock, "callback on lock "
1997                                    LPX64" - lock disappeared\n",
1998                                    dlm_req->lock_handle[0].cookie);
1999                         unlock_res_and_lock(lock);
2000                         LDLM_LOCK_RELEASE(lock);
2001                         rc = ldlm_callback_reply(req, -EINVAL);
2002                         ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2003                                              &dlm_req->lock_handle[0]);
2004                         RETURN(0);
2005                 }
2006                 /* BL_AST locks are not needed in lru.
2007                  * let ldlm_cancel_lru() be fast. */
2008                 ldlm_lock_remove_from_lru(lock);
2009                 lock->l_flags |= LDLM_FL_BL_AST;
2010         }
2011         unlock_res_and_lock(lock);
2012
2013         /* We want the ost thread to get this reply so that it can respond
2014          * to ost requests (write cache writeback) that might be triggered
2015          * in the callback.
2016          *
2017          * But we'd also like to be able to indicate in the reply that we're
2018          * cancelling right now, because it's unused, or have an intent result
2019          * in the reply, so we might have to push the responsibility for sending
2020          * the reply down into the AST handlers, alas. */
2021
2022         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2023         case LDLM_BL_CALLBACK:
2024                 CDEBUG(D_INODE, "blocking ast\n");
2025                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2026                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2027                         rc = ldlm_callback_reply(req, 0);
2028                         if (req->rq_no_reply || rc)
2029                                 ldlm_callback_errmsg(req, "Normal process", rc,
2030                                                      &dlm_req->lock_handle[0]);
2031                 }
2032                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2033                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2034                 break;
2035         case LDLM_CP_CALLBACK:
2036                 CDEBUG(D_INODE, "completion ast\n");
2037                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2038                 ldlm_callback_reply(req, 0);
2039                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2040                 break;
2041         case LDLM_GL_CALLBACK:
2042                 CDEBUG(D_INODE, "glimpse ast\n");
2043                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2044                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2045                 break;
2046         default:
2047                 LBUG();                         /* checked above */
2048         }
2049
2050         RETURN(0);
2051 }
2052
2053 #ifdef HAVE_SERVER_SUPPORT
2054 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2055 {
2056         int rc;
2057         ENTRY;
2058
2059         /* Requests arrive in sender's byte order.  The ptlrpc service
2060          * handler has already checked and, if necessary, byte-swapped the
2061          * incoming request message body, but I am responsible for the
2062          * message buffers. */
2063
2064         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2065
2066         if (req->rq_export == NULL) {
2067                 struct ldlm_request *dlm_req;
2068
2069                 CERROR("%s from %s arrived at %lu with bad export cookie "
2070                        LPU64"\n",
2071                        ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2072                        libcfs_nid2str(req->rq_peer.nid),
2073                        req->rq_arrival_time.tv_sec,
2074                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2075
2076                 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2077                         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2078                         dlm_req = req_capsule_client_get(&req->rq_pill,
2079                                                          &RMF_DLM_REQ);
2080                         if (dlm_req != NULL)
2081                                 ldlm_lock_dump_handle(D_ERROR,
2082                                                       &dlm_req->lock_handle[0]);
2083                 }
2084                 ldlm_callback_reply(req, -ENOTCONN);
2085                 RETURN(0);
2086         }
2087
2088         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2089
2090         /* XXX FIXME move this back to mds/handler.c, bug 249 */
2091         case LDLM_CANCEL:
2092                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2093                 CDEBUG(D_INODE, "cancel\n");
2094                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2095                         RETURN(0);
2096                 rc = ldlm_handle_cancel(req);
2097                 if (rc)
2098                         break;
2099                 RETURN(0);
2100         case OBD_LOG_CANCEL:
2101                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2102                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2103                         RETURN(0);
2104                 rc = llog_origin_handle_cancel(req);
2105                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2106                         RETURN(0);
2107                 ldlm_callback_reply(req, rc);
2108                 RETURN(0);
2109         default:
2110                 CERROR("invalid opcode %d\n",
2111                        lustre_msg_get_opc(req->rq_reqmsg));
2112                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2113                 ldlm_callback_reply(req, -EINVAL);
2114         }
2115
2116         RETURN(0);
2117 }
2118
2119 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2120                                         struct ldlm_lock *lock)
2121 {
2122         struct ldlm_request *dlm_req;
2123         struct lustre_handle lockh;
2124         int rc = 0;
2125         int i;
2126         ENTRY;
2127
2128         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2129         if (dlm_req == NULL)
2130                 RETURN(0);
2131
2132         ldlm_lock2handle(lock, &lockh);
2133         for (i = 0; i < dlm_req->lock_count; i++) {
2134                 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2135                                         &lockh)) {
2136                         DEBUG_REQ(D_RPCTRACE, req,
2137                                   "Prio raised by lock "LPX64".", lockh.cookie);
2138
2139                         rc = 1;
2140                         break;
2141                 }
2142         }
2143
2144         RETURN(rc);
2145
2146 }
2147
2148 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2149 {
2150         struct ldlm_request *dlm_req;
2151         int rc = 0;
2152         int i;
2153         ENTRY;
2154
2155         /* no prolong in recovery */
2156         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2157                 RETURN(0);
2158
2159         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2160         if (dlm_req == NULL)
2161                 RETURN(-EFAULT);
2162
2163         for (i = 0; i < dlm_req->lock_count; i++) {
2164                 struct ldlm_lock *lock;
2165
2166                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2167                 if (lock == NULL)
2168                         continue;
2169
2170                 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2171                 if (rc)
2172                         LDLM_DEBUG(lock, "hpreq cancel lock");
2173                 LDLM_LOCK_PUT(lock);
2174
2175                 if (rc)
2176                         break;
2177         }
2178
2179         RETURN(rc);
2180 }
2181
2182 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2183         .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2184         .hpreq_check      = ldlm_cancel_hpreq_check
2185 };
2186
2187 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2188 {
2189         ENTRY;
2190
2191         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2192
2193         if (req->rq_export == NULL)
2194                 RETURN(0);
2195
2196         if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2197                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2198                 req->rq_ops = &ldlm_cancel_hpreq_ops;
2199         }
2200         RETURN(0);
2201 }
2202
2203 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2204                         cfs_hlist_node_t *hnode, void *data)
2205
2206 {
2207         cfs_list_t         *rpc_list = data;
2208         struct ldlm_lock   *lock = cfs_hash_object(hs, hnode);
2209
2210         lock_res_and_lock(lock);
2211
2212         if (lock->l_req_mode != lock->l_granted_mode) {
2213                 unlock_res_and_lock(lock);
2214                 return 0;
2215         }
2216
2217         LASSERT(lock->l_resource);
2218         if (lock->l_resource->lr_type != LDLM_IBITS &&
2219             lock->l_resource->lr_type != LDLM_PLAIN) {
2220                 unlock_res_and_lock(lock);
2221                 return 0;
2222         }
2223
2224         if (lock->l_flags & LDLM_FL_AST_SENT) {
2225                 unlock_res_and_lock(lock);
2226                 return 0;
2227         }
2228
2229         LASSERT(lock->l_blocking_ast);
2230         LASSERT(!lock->l_blocking_lock);
2231
2232         lock->l_flags |= LDLM_FL_AST_SENT;
2233         if (lock->l_export && lock->l_export->exp_lock_hash &&
2234             !cfs_hlist_unhashed(&lock->l_exp_hash))
2235                 cfs_hash_del(lock->l_export->exp_lock_hash,
2236                              &lock->l_remote_handle, &lock->l_exp_hash);
2237         cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2238         LDLM_LOCK_GET(lock);
2239
2240         unlock_res_and_lock(lock);
2241         return 0;
2242 }
2243
2244 void ldlm_revoke_export_locks(struct obd_export *exp)
2245 {
2246         cfs_list_t  rpc_list;
2247         ENTRY;
2248
2249         CFS_INIT_LIST_HEAD(&rpc_list);
2250         cfs_hash_for_each_empty(exp->exp_lock_hash,
2251                                 ldlm_revoke_lock_cb, &rpc_list);
2252         ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2253                           LDLM_WORK_REVOKE_AST);
2254
2255         EXIT;
2256 }
2257 #endif /* HAVE_SERVER_SUPPORT */
2258
2259 #ifdef __KERNEL__
2260 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2261 {
2262         struct ldlm_bl_work_item *blwi = NULL;
2263         static unsigned int num_bl = 0;
2264
2265         cfs_spin_lock(&blp->blp_lock);
2266         /* process a request from the blp_list at least every blp_num_threads */
2267         if (!cfs_list_empty(&blp->blp_list) &&
2268             (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2269                 blwi = cfs_list_entry(blp->blp_list.next,
2270                                       struct ldlm_bl_work_item, blwi_entry);
2271         else
2272                 if (!cfs_list_empty(&blp->blp_prio_list))
2273                         blwi = cfs_list_entry(blp->blp_prio_list.next,
2274                                               struct ldlm_bl_work_item,
2275                                               blwi_entry);
2276
2277         if (blwi) {
2278                 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2279                         num_bl = 0;
2280                 cfs_list_del(&blwi->blwi_entry);
2281         }
2282         cfs_spin_unlock(&blp->blp_lock);
2283
2284         return blwi;
2285 }
2286
2287 /* This only contains temporary data until the thread starts */
2288 struct ldlm_bl_thread_data {
2289         char                    bltd_name[CFS_CURPROC_COMM_MAX];
2290         struct ldlm_bl_pool     *bltd_blp;
2291         cfs_completion_t        bltd_comp;
2292         int                     bltd_num;
2293 };
2294
2295 static int ldlm_bl_thread_main(void *arg);
2296
2297 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2298 {
2299         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2300         int rc;
2301
2302         cfs_init_completion(&bltd.bltd_comp);
2303         rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2304         if (rc < 0) {
2305                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2306                        cfs_atomic_read(&blp->blp_num_threads), rc);
2307                 return rc;
2308         }
2309         cfs_wait_for_completion(&bltd.bltd_comp);
2310
2311         return 0;
2312 }
2313
2314 static int ldlm_bl_thread_main(void *arg)
2315 {
2316         struct ldlm_bl_pool *blp;
2317         ENTRY;
2318
2319         {
2320                 struct ldlm_bl_thread_data *bltd = arg;
2321
2322                 blp = bltd->bltd_blp;
2323
2324                 bltd->bltd_num =
2325                         cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2326                 cfs_atomic_inc(&blp->blp_busy_threads);
2327
2328                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2329                         "ldlm_bl_%02d", bltd->bltd_num);
2330                 cfs_daemonize(bltd->bltd_name);
2331
2332                 cfs_complete(&bltd->bltd_comp);
2333                 /* cannot use bltd after this, it is only on caller's stack */
2334         }
2335
2336         while (1) {
2337                 struct l_wait_info lwi = { 0 };
2338                 struct ldlm_bl_work_item *blwi = NULL;
2339                 int busy;
2340
2341                 blwi = ldlm_bl_get_work(blp);
2342
2343                 if (blwi == NULL) {
2344                         cfs_atomic_dec(&blp->blp_busy_threads);
2345                         l_wait_event_exclusive(blp->blp_waitq,
2346                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
2347                                          &lwi);
2348                         busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2349                 } else {
2350                         busy = cfs_atomic_read(&blp->blp_busy_threads);
2351                 }
2352
2353                 if (blwi->blwi_ns == NULL)
2354                         /* added by ldlm_cleanup() */
2355                         break;
2356
2357                 /* Not fatal if racy and have a few too many threads */
2358                 if (unlikely(busy < blp->blp_max_threads &&
2359                              busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2360                              !blwi->blwi_mem_pressure))
2361                         /* discard the return value, we tried */
2362                         ldlm_bl_thread_start(blp);
2363
2364                 if (blwi->blwi_mem_pressure)
2365                         cfs_memory_pressure_set();
2366
2367                 if (blwi->blwi_count) {
2368                         int count;
2369                         /* The special case when we cancel locks in lru
2370                          * asynchronously, we pass the list of locks here.
2371                          * Thus locks are marked LDLM_FL_CANCELING, but NOT
2372                          * canceled locally yet. */
2373                         count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2374                                                            blwi->blwi_count,
2375                                                            LCF_BL_AST);
2376                         ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2377                 } else {
2378                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2379                                                 blwi->blwi_lock);
2380                 }
2381                 if (blwi->blwi_mem_pressure)
2382                         cfs_memory_pressure_clr();
2383
2384                 if (blwi->blwi_mode == LDLM_ASYNC)
2385                         OBD_FREE(blwi, sizeof(*blwi));
2386                 else
2387                         cfs_complete(&blwi->blwi_comp);
2388         }
2389
2390         cfs_atomic_dec(&blp->blp_busy_threads);
2391         cfs_atomic_dec(&blp->blp_num_threads);
2392         cfs_complete(&blp->blp_comp);
2393         RETURN(0);
2394 }
2395
2396 #endif
2397
2398 static int ldlm_setup(void);
2399 static int ldlm_cleanup(void);
2400
2401 int ldlm_get_ref(void)
2402 {
2403         int rc = 0;
2404         ENTRY;
2405         cfs_mutex_lock(&ldlm_ref_mutex);
2406         if (++ldlm_refcount == 1) {
2407                 rc = ldlm_setup();
2408                 if (rc)
2409                         ldlm_refcount--;
2410         }
2411         cfs_mutex_unlock(&ldlm_ref_mutex);
2412
2413         RETURN(rc);
2414 }
2415
2416 void ldlm_put_ref(void)
2417 {
2418         ENTRY;
2419         cfs_mutex_lock(&ldlm_ref_mutex);
2420         if (ldlm_refcount == 1) {
2421                 int rc = ldlm_cleanup();
2422                 if (rc)
2423                         CERROR("ldlm_cleanup failed: %d\n", rc);
2424                 else
2425                         ldlm_refcount--;
2426         } else {
2427                 ldlm_refcount--;
2428         }
2429         cfs_mutex_unlock(&ldlm_ref_mutex);
2430
2431         EXIT;
2432 }
2433
2434 /*
2435  * Export handle<->lock hash operations.
2436  */
2437 static unsigned
2438 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2439 {
2440         return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2441 }
2442
2443 static void *
2444 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2445 {
2446         struct ldlm_lock *lock;
2447
2448         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2449         return &lock->l_remote_handle;
2450 }
2451
2452 static void
2453 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2454 {
2455         struct ldlm_lock     *lock;
2456
2457         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2458         lock->l_remote_handle = *(struct lustre_handle *)key;
2459 }
2460
2461 static int
2462 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2463 {
2464         return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2465 }
2466
2467 static void *
2468 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2469 {
2470         return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2471 }
2472
2473 static void
2474 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2475 {
2476         struct ldlm_lock *lock;
2477
2478         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2479         LDLM_LOCK_GET(lock);
2480 }
2481
2482 static void
2483 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2484 {
2485         struct ldlm_lock *lock;
2486
2487         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2488         LDLM_LOCK_RELEASE(lock);
2489 }
2490
2491 static cfs_hash_ops_t ldlm_export_lock_ops = {
2492         .hs_hash        = ldlm_export_lock_hash,
2493         .hs_key         = ldlm_export_lock_key,
2494         .hs_keycmp      = ldlm_export_lock_keycmp,
2495         .hs_keycpy      = ldlm_export_lock_keycpy,
2496         .hs_object      = ldlm_export_lock_object,
2497         .hs_get         = ldlm_export_lock_get,
2498         .hs_put         = ldlm_export_lock_put,
2499         .hs_put_locked  = ldlm_export_lock_put,
2500 };
2501
2502 int ldlm_init_export(struct obd_export *exp)
2503 {
2504         ENTRY;
2505
2506         exp->exp_lock_hash =
2507                 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2508                                 HASH_EXP_LOCK_CUR_BITS,
2509                                 HASH_EXP_LOCK_MAX_BITS,
2510                                 HASH_EXP_LOCK_BKT_BITS, 0,
2511                                 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2512                                 &ldlm_export_lock_ops,
2513                                 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2514                                 CFS_HASH_NBLK_CHANGE);
2515
2516         if (!exp->exp_lock_hash)
2517                 RETURN(-ENOMEM);
2518
2519         RETURN(0);
2520 }
2521 EXPORT_SYMBOL(ldlm_init_export);
2522
2523 void ldlm_destroy_export(struct obd_export *exp)
2524 {
2525         ENTRY;
2526         cfs_hash_putref(exp->exp_lock_hash);
2527         exp->exp_lock_hash = NULL;
2528         EXIT;
2529 }
2530 EXPORT_SYMBOL(ldlm_destroy_export);
2531
2532 static int ldlm_setup(void)
2533 {
2534         struct ldlm_bl_pool *blp;
2535         int rc = 0;
2536         int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2537         int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2538 #ifdef __KERNEL__
2539         int i;
2540 #endif
2541         ENTRY;
2542
2543         if (ldlm_state != NULL)
2544                 RETURN(-EALREADY);
2545
2546         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2547         if (ldlm_state == NULL)
2548                 RETURN(-ENOMEM);
2549
2550 #ifdef LPROCFS
2551         rc = ldlm_proc_setup();
2552         if (rc != 0)
2553                 GOTO(out_free, rc);
2554 #endif
2555
2556 #ifdef __KERNEL__
2557         if (ldlm_num_threads) {
2558                 /* If ldlm_num_threads is set, it is the min and the max. */
2559                 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2560                         ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2561                 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2562                         ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2563                 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2564         }
2565 #endif
2566
2567         ldlm_state->ldlm_cb_service =
2568                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2569                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2570                                 LDLM_CB_REPLY_PORTAL, 2,
2571                                 ldlm_callback_handler, "ldlm_cbd",
2572                                 ldlm_svc_proc_dir, NULL,
2573                                 ldlm_min_threads, ldlm_max_threads,
2574                                 "ldlm_cb",
2575                                 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2576
2577         if (!ldlm_state->ldlm_cb_service) {
2578                 CERROR("failed to start service\n");
2579                 GOTO(out_proc, rc = -ENOMEM);
2580         }
2581
2582 #ifdef HAVE_SERVER_SUPPORT
2583         ldlm_state->ldlm_cancel_service =
2584                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2585                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2586                                 LDLM_CANCEL_REPLY_PORTAL, 6,
2587                                 ldlm_cancel_handler, "ldlm_canceld",
2588                                 ldlm_svc_proc_dir, NULL,
2589                                 ldlm_min_threads, ldlm_max_threads,
2590                                 "ldlm_cn",
2591                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2592                                 ldlm_hpreq_handler);
2593
2594         if (!ldlm_state->ldlm_cancel_service) {
2595                 CERROR("failed to start service\n");
2596                 GOTO(out_proc, rc = -ENOMEM);
2597         }
2598 #endif
2599
2600         OBD_ALLOC(blp, sizeof(*blp));
2601         if (blp == NULL)
2602                 GOTO(out_proc, rc = -ENOMEM);
2603         ldlm_state->ldlm_bl_pool = blp;
2604
2605         cfs_spin_lock_init(&blp->blp_lock);
2606         CFS_INIT_LIST_HEAD(&blp->blp_list);
2607         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2608         cfs_waitq_init(&blp->blp_waitq);
2609         cfs_atomic_set(&blp->blp_num_threads, 0);
2610         cfs_atomic_set(&blp->blp_busy_threads, 0);
2611         blp->blp_min_threads = ldlm_min_threads;
2612         blp->blp_max_threads = ldlm_max_threads;
2613
2614 #ifdef __KERNEL__
2615         for (i = 0; i < blp->blp_min_threads; i++) {
2616                 rc = ldlm_bl_thread_start(blp);
2617                 if (rc < 0)
2618                         GOTO(out_thread, rc);
2619         }
2620
2621 # ifdef HAVE_SERVER_SUPPORT
2622         rc = ptlrpc_start_threads(ldlm_state->ldlm_cancel_service);
2623         if (rc)
2624                 GOTO(out_thread, rc);
2625 # endif
2626
2627         rc = ptlrpc_start_threads(ldlm_state->ldlm_cb_service);
2628         if (rc)
2629                 GOTO(out_thread, rc);
2630
2631         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2632         expired_lock_thread.elt_state = ELT_STOPPED;
2633         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2634
2635         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2636         cfs_spin_lock_init(&waiting_locks_spinlock);
2637         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2638
2639         rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2640         if (rc < 0) {
2641                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2642                 GOTO(out_thread, rc);
2643         }
2644
2645         cfs_wait_event(expired_lock_thread.elt_waitq,
2646                        expired_lock_thread.elt_state == ELT_READY);
2647 #endif
2648
2649 #ifdef __KERNEL__
2650         rc = ldlm_pools_init();
2651         if (rc)
2652                 GOTO(out_thread, rc);
2653 #endif
2654         RETURN(0);
2655
2656 #ifdef __KERNEL__
2657  out_thread:
2658 # ifdef HAVE_SERVER_SUPPORT
2659         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2660 # endif
2661         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2662 #endif
2663
2664  out_proc:
2665 #ifdef LPROCFS
2666         ldlm_proc_cleanup();
2667  out_free:
2668 #endif
2669         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2670         ldlm_state = NULL;
2671         return rc;
2672 }
2673
2674 static int ldlm_cleanup(void)
2675 {
2676 #ifdef __KERNEL__
2677         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2678 #endif
2679         ENTRY;
2680
2681         if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2682             !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2683                 CERROR("ldlm still has namespaces; clean these up first.\n");
2684                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2685                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2686                 RETURN(-EBUSY);
2687         }
2688
2689 #ifdef __KERNEL__
2690         ldlm_pools_fini();
2691 #endif
2692
2693 #ifdef __KERNEL__
2694         while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2695                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2696
2697                 cfs_init_completion(&blp->blp_comp);
2698
2699                 cfs_spin_lock(&blp->blp_lock);
2700                 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2701                 cfs_waitq_signal(&blp->blp_waitq);
2702                 cfs_spin_unlock(&blp->blp_lock);
2703
2704                 cfs_wait_for_completion(&blp->blp_comp);
2705         }
2706         OBD_FREE(blp, sizeof(*blp));
2707
2708         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2709 # ifdef HAVE_SERVER_SUPPORT
2710         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2711 # endif
2712         ldlm_proc_cleanup();
2713
2714         expired_lock_thread.elt_state = ELT_TERMINATE;
2715         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2716         cfs_wait_event(expired_lock_thread.elt_waitq,
2717                        expired_lock_thread.elt_state == ELT_STOPPED);
2718 #else /* !__KERNEL__ */
2719         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2720 # ifdef HAVE_SERVER_SUPPORT
2721         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2722 # endif
2723 #endif /* __KERNEL__ */
2724
2725         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2726         ldlm_state = NULL;
2727
2728         RETURN(0);
2729 }
2730
2731 int ldlm_init(void)
2732 {
2733         cfs_mutex_init(&ldlm_ref_mutex);
2734         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2735         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2736         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2737                                                sizeof(struct ldlm_resource), 0,
2738                                                CFS_SLAB_HWCACHE_ALIGN);
2739         if (ldlm_resource_slab == NULL)
2740                 return -ENOMEM;
2741
2742         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2743                               sizeof(struct ldlm_lock), 0,
2744                               CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2745         if (ldlm_lock_slab == NULL) {
2746                 cfs_mem_cache_destroy(ldlm_resource_slab);
2747                 return -ENOMEM;
2748         }
2749
2750         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2751                                         sizeof(struct ldlm_interval),
2752                                         0, CFS_SLAB_HWCACHE_ALIGN);
2753         if (ldlm_interval_slab == NULL) {
2754                 cfs_mem_cache_destroy(ldlm_resource_slab);
2755                 cfs_mem_cache_destroy(ldlm_lock_slab);
2756                 return -ENOMEM;
2757         }
2758 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2759         class_export_dump_hook = ldlm_dump_export_locks;
2760 #endif
2761         return 0;
2762 }
2763
2764 void ldlm_exit(void)
2765 {
2766         int rc;
2767         if (ldlm_refcount)
2768                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2769         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2770         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2771 #ifdef __KERNEL__
2772         /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2773          * synchronize_rcu() to wait a grace period elapsed, so that
2774          * ldlm_lock_free() get a chance to be called. */
2775         synchronize_rcu();
2776 #endif
2777         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2778         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2779         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2780         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2781 }
2782
2783 /* ldlm_extent.c */
2784 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2785
2786 /* ldlm_lock.c */
2787 #ifdef HAVE_SERVER_SUPPORT
2788 EXPORT_SYMBOL(ldlm_get_processing_policy);
2789 #endif
2790 EXPORT_SYMBOL(ldlm_lock2desc);
2791 EXPORT_SYMBOL(ldlm_register_intent);
2792 EXPORT_SYMBOL(ldlm_lockname);
2793 EXPORT_SYMBOL(ldlm_typename);
2794 EXPORT_SYMBOL(ldlm_lock2handle);
2795 EXPORT_SYMBOL(__ldlm_handle2lock);
2796 EXPORT_SYMBOL(ldlm_lock_get);
2797 EXPORT_SYMBOL(ldlm_lock_put);
2798 EXPORT_SYMBOL(ldlm_lock_match);
2799 EXPORT_SYMBOL(ldlm_lock_cancel);
2800 EXPORT_SYMBOL(ldlm_lock_addref);
2801 EXPORT_SYMBOL(ldlm_lock_addref_try);
2802 EXPORT_SYMBOL(ldlm_lock_decref);
2803 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2804 EXPORT_SYMBOL(ldlm_lock_change_resource);
2805 EXPORT_SYMBOL(ldlm_it2str);
2806 EXPORT_SYMBOL(ldlm_lock_dump);
2807 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2808 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2809 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2810 EXPORT_SYMBOL(ldlm_lock_allow_match);
2811 EXPORT_SYMBOL(ldlm_lock_downgrade);
2812 EXPORT_SYMBOL(ldlm_lock_convert);
2813
2814 /* ldlm_request.c */
2815 EXPORT_SYMBOL(ldlm_completion_ast_async);
2816 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2817 EXPORT_SYMBOL(ldlm_completion_ast);
2818 EXPORT_SYMBOL(ldlm_blocking_ast);
2819 EXPORT_SYMBOL(ldlm_glimpse_ast);
2820 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2821 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2822 EXPORT_SYMBOL(ldlm_prep_elc_req);
2823 EXPORT_SYMBOL(ldlm_cli_convert);
2824 EXPORT_SYMBOL(ldlm_cli_enqueue);
2825 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2826 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2827 EXPORT_SYMBOL(ldlm_cli_cancel);
2828 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2829 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2830 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2831 EXPORT_SYMBOL(ldlm_replay_locks);
2832 EXPORT_SYMBOL(ldlm_resource_foreach);
2833 EXPORT_SYMBOL(ldlm_namespace_foreach);
2834 EXPORT_SYMBOL(ldlm_resource_iterate);
2835 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2836 EXPORT_SYMBOL(ldlm_cli_cancel_list_local);
2837 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2838
2839 /* ldlm_lockd.c */
2840 #ifdef HAVE_SERVER_SUPPORT
2841 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2842 EXPORT_SYMBOL(ldlm_server_completion_ast);
2843 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2844 EXPORT_SYMBOL(ldlm_handle_enqueue);
2845 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2846 EXPORT_SYMBOL(ldlm_handle_cancel);
2847 EXPORT_SYMBOL(ldlm_request_cancel);
2848 EXPORT_SYMBOL(ldlm_handle_convert);
2849 EXPORT_SYMBOL(ldlm_handle_convert0);
2850 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2851 #endif
2852 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2853 EXPORT_SYMBOL(ldlm_get_ref);
2854 EXPORT_SYMBOL(ldlm_put_ref);
2855 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2856
2857 /* ldlm_resource.c */
2858 EXPORT_SYMBOL(ldlm_namespace_new);
2859 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2860 EXPORT_SYMBOL(ldlm_namespace_free);
2861 EXPORT_SYMBOL(ldlm_namespace_dump);
2862 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2863 EXPORT_SYMBOL(ldlm_resource_get);
2864 EXPORT_SYMBOL(ldlm_resource_putref);
2865 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2866
2867 /* ldlm_lib.c */
2868 EXPORT_SYMBOL(client_import_add_conn);
2869 EXPORT_SYMBOL(client_import_del_conn);
2870 EXPORT_SYMBOL(client_obd_setup);
2871 EXPORT_SYMBOL(client_obd_cleanup);
2872 EXPORT_SYMBOL(client_connect_import);
2873 EXPORT_SYMBOL(client_disconnect_export);
2874 EXPORT_SYMBOL(target_send_reply);
2875 EXPORT_SYMBOL(target_pack_pool_reply);
2876
2877 #ifdef HAVE_SERVER_SUPPORT
2878 EXPORT_SYMBOL(server_disconnect_export);
2879 EXPORT_SYMBOL(target_stop_recovery_thread);
2880 EXPORT_SYMBOL(target_handle_connect);
2881 EXPORT_SYMBOL(target_cleanup_recovery);
2882 EXPORT_SYMBOL(target_destroy_export);
2883 EXPORT_SYMBOL(target_cancel_recovery_timer);
2884 EXPORT_SYMBOL(target_queue_recovery_request);
2885 EXPORT_SYMBOL(target_handle_ping);
2886 EXPORT_SYMBOL(target_handle_disconnect);
2887 #endif
2888
2889 /* l_lock.c */
2890 EXPORT_SYMBOL(lock_res_and_lock);
2891 EXPORT_SYMBOL(unlock_res_and_lock);