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