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