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