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