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