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