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