Whamcloud - gitweb
Branch HEAD
[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  * Copyright (C) 2002-2004 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30 #define DEBUG_SUBSYSTEM S_LDLM
31
32 #ifdef __KERNEL__
33 # include <libcfs/libcfs.h>
34 #else
35 # include <liblustre.h>
36 #endif
37
38 #include <lustre_dlm.h>
39 #include <obd_class.h>
40 #include <libcfs/list.h>
41 #include "ldlm_internal.h"
42
43 #ifdef __KERNEL__
44 static int ldlm_num_threads;
45 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
46                 "number of DLM service threads to start");
47 #endif
48
49 extern cfs_mem_cache_t *ldlm_resource_slab;
50 extern cfs_mem_cache_t *ldlm_lock_slab;
51 static struct semaphore ldlm_ref_sem;
52 static int ldlm_refcount;
53
54 /* LDLM state */
55
56 static struct ldlm_state *ldlm_state;
57
58 inline cfs_time_t round_timeout(cfs_time_t timeout)
59 {
60         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
61 }
62
63 /* timeout for initial callback (AST) reply */
64 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
65                                                unsigned int obd_timeout)
66 {
67         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
68
69         return timeout < 1 ? 1 : timeout;
70 }
71
72 #ifdef __KERNEL__
73 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
74 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
75 static struct list_head waiting_locks_list;
76 static cfs_timer_t waiting_locks_timer;
77
78 static struct expired_lock_thread {
79         cfs_waitq_t               elt_waitq;
80         int                       elt_state;
81         int                       elt_dump;
82         struct list_head          elt_expired_locks;
83 } expired_lock_thread;
84 #endif
85
86 #define ELT_STOPPED   0
87 #define ELT_READY     1
88 #define ELT_TERMINATE 2
89
90 struct ldlm_bl_pool {
91         spinlock_t              blp_lock;
92
93         /*
94          * blp_prio_list is used for callbacks that should be handled
95          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
96          * see bug 13843
97          */
98         struct list_head        blp_prio_list;
99
100         /*
101          * blp_list is used for all other callbacks which are likely
102          * to take longer to process.
103          */
104         struct list_head        blp_list;
105
106         cfs_waitq_t             blp_waitq;
107         struct completion       blp_comp;
108         atomic_t                blp_num_threads;
109         atomic_t                blp_busy_threads;
110         int                     blp_min_threads;
111         int                     blp_max_threads;
112 };
113
114 struct ldlm_bl_work_item {
115         struct list_head        blwi_entry;
116         struct ldlm_namespace   *blwi_ns;
117         struct ldlm_lock_desc   blwi_ld;
118         struct ldlm_lock        *blwi_lock;
119         struct list_head        blwi_head;
120         int                     blwi_count;
121 };
122
123 #ifdef __KERNEL__
124
125 static inline int have_expired_locks(void)
126 {
127         int need_to_run;
128
129         ENTRY;
130         spin_lock_bh(&waiting_locks_spinlock);
131         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
132         spin_unlock_bh(&waiting_locks_spinlock);
133
134         RETURN(need_to_run);
135 }
136
137 static int expired_lock_main(void *arg)
138 {
139         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
140         struct l_wait_info lwi = { 0 };
141         int do_dump;
142
143         ENTRY;
144         cfs_daemonize("ldlm_elt");
145
146         expired_lock_thread.elt_state = ELT_READY;
147         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
148
149         while (1) {
150                 l_wait_event(expired_lock_thread.elt_waitq,
151                              have_expired_locks() ||
152                              expired_lock_thread.elt_state == ELT_TERMINATE,
153                              &lwi);
154
155                 spin_lock_bh(&waiting_locks_spinlock);
156                 if (expired_lock_thread.elt_dump) {
157                         spin_unlock_bh(&waiting_locks_spinlock);
158
159                         /* from waiting_locks_callback, but not in timer */
160                         libcfs_debug_dumplog();
161                         libcfs_run_lbug_upcall(__FILE__,
162                                                 "waiting_locks_callback",
163                                                 expired_lock_thread.elt_dump);
164
165                         spin_lock_bh(&waiting_locks_spinlock);
166                         expired_lock_thread.elt_dump = 0;
167                 }
168
169                 do_dump = 0;
170
171                 while (!list_empty(expired)) {
172                         struct obd_export *export;
173                         struct ldlm_lock *lock;
174
175                         lock = list_entry(expired->next, struct ldlm_lock,
176                                           l_pending_chain);
177                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
178                             (void *)lock >= LP_POISON) {
179                                 spin_unlock_bh(&waiting_locks_spinlock);
180                                 CERROR("free lock on elt list %p\n", lock);
181                                 LBUG();
182                         }
183                         list_del_init(&lock->l_pending_chain);
184                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
185                             (void *)lock->l_export >= LP_POISON) {
186                                 CERROR("lock with free export on elt list %p\n",
187                                        lock->l_export);
188                                 lock->l_export = NULL;
189                                 LDLM_ERROR(lock, "free export");
190                                 continue;
191                         }
192                         export = class_export_get(lock->l_export);
193                         spin_unlock_bh(&waiting_locks_spinlock);
194
195                         do_dump++;
196                         class_fail_export(export);
197                         class_export_put(export);
198                         spin_lock_bh(&waiting_locks_spinlock);
199                 }
200                 spin_unlock_bh(&waiting_locks_spinlock);
201
202                 if (do_dump && obd_dump_on_eviction) {
203                         CERROR("dump the log upon eviction\n");
204                         libcfs_debug_dumplog();
205                 }
206
207                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
208                         break;
209         }
210
211         expired_lock_thread.elt_state = ELT_STOPPED;
212         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
213         RETURN(0);
214 }
215
216 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
217
218 /* This is called from within a timer interrupt and cannot schedule */
219 static void waiting_locks_callback(unsigned long unused)
220 {
221         struct ldlm_lock *lock, *last = NULL;
222
223 repeat:
224         spin_lock_bh(&waiting_locks_spinlock);
225         while (!list_empty(&waiting_locks_list)) {
226                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
227                                   l_pending_chain);
228
229                 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
230                     (lock->l_req_mode == LCK_GROUP))
231                         break;
232
233                 if (ptlrpc_check_suspend()) {
234                         /* there is a case when we talk to one mds, holding
235                          * lock from another mds. this way we easily can get
236                          * here, if second mds is being recovered. so, we
237                          * suspend timeouts. bug 6019 */
238
239                         LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
240                                    lock->l_export->exp_client_uuid.uuid,
241                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
242                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
243
244                         list_del_init(&lock->l_pending_chain);
245                         spin_unlock_bh(&waiting_locks_spinlock);
246                         ldlm_add_waiting_lock(lock);
247                         goto repeat;
248                 }
249
250                 /* if timeout overlaps the activation time of suspended timeouts
251                  * then extend it to give a chance for client to reconnect */
252                 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
253                                                  cfs_time_seconds(obd_timeout)/2),
254                                     ptlrpc_suspend_wakeup_time())) {
255                         LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
256                                    lock->l_export->exp_client_uuid.uuid,
257                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
258                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
259
260                         list_del_init(&lock->l_pending_chain);
261                         spin_unlock_bh(&waiting_locks_spinlock);
262                         ldlm_add_waiting_lock(lock);
263                         goto repeat;
264                 }
265
266                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
267                            "%s@%s nid %s\n",
268                            lock->l_export->exp_client_uuid.uuid,
269                            lock->l_export->exp_connection->c_remote_uuid.uuid,
270                            libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
271
272                 last = lock;
273
274                 list_del(&lock->l_pending_chain);
275                 list_add(&lock->l_pending_chain,
276                          &expired_lock_thread.elt_expired_locks);
277         }
278
279         if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
280                 if (obd_dump_on_timeout)
281                         expired_lock_thread.elt_dump = __LINE__;
282
283                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
284         }
285
286         /*
287          * Make sure the timer will fire again if we have any locks
288          * left.
289          */
290         if (!list_empty(&waiting_locks_list)) {
291                 cfs_time_t timeout_rounded;
292                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
293                                   l_pending_chain);
294                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
295                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
296         }
297         spin_unlock_bh(&waiting_locks_spinlock);
298 }
299
300 /*
301  * Indicate that we're waiting for a client to call us back cancelling a given
302  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
303  * timer to fire appropriately.  (We round up to the next second, to avoid
304  * floods of timer firings during periods of high lock contention and traffic).
305  *
306  * Called with the namespace lock held.
307  */
308 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
309 {
310         cfs_time_t timeout_rounded;
311
312         if (!list_empty(&lock->l_pending_chain))
313                 return 0;
314
315         lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
316                                                cfs_time_seconds(obd_timeout)/2);
317
318         timeout_rounded = round_timeout(lock->l_callback_timeout);
319
320         if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
321             !cfs_timer_is_armed(&waiting_locks_timer)) {
322                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
323
324         }
325         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
326         return 1;
327 }
328
329 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
330 {
331         int ret;
332
333         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
334
335         spin_lock_bh(&waiting_locks_spinlock);
336         if (lock->l_destroyed) {
337                 static cfs_time_t next;
338                 spin_unlock_bh(&waiting_locks_spinlock);
339                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
340                 if (cfs_time_after(cfs_time_current(), next)) {
341                         next = cfs_time_shift(14400);
342                         libcfs_debug_dumpstack(NULL);
343                 }
344                 return 0;
345         }
346
347         ret = __ldlm_add_waiting_lock(lock);
348         spin_unlock_bh(&waiting_locks_spinlock);
349
350         LDLM_DEBUG(lock, "%sadding to wait list",
351                    ret == 0 ? "not re-" : "");
352         return ret;
353 }
354
355 /*
356  * Remove a lock from the pending list, likely because it had its cancellation
357  * callback arrive without incident.  This adjusts the lock-timeout timer if
358  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
359  *
360  * Called with namespace lock held.
361  */
362 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
363 {
364         struct list_head *list_next;
365
366         if (list_empty(&lock->l_pending_chain))
367                 return 0;
368
369         list_next = lock->l_pending_chain.next;
370         if (lock->l_pending_chain.prev == &waiting_locks_list) {
371                 /* Removing the head of the list, adjust timer. */
372                 if (list_next == &waiting_locks_list) {
373                         /* No more, just cancel. */
374                         cfs_timer_disarm(&waiting_locks_timer);
375                 } else {
376                         struct ldlm_lock *next;
377                         next = list_entry(list_next, struct ldlm_lock,
378                                           l_pending_chain);
379                         cfs_timer_arm(&waiting_locks_timer,
380                                       round_timeout(next->l_callback_timeout));
381                 }
382         }
383         list_del_init(&lock->l_pending_chain);
384
385         return 1;
386 }
387
388 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
389 {
390         int ret;
391
392         if (lock->l_export == NULL) {
393                 /* We don't have a "waiting locks list" on clients. */
394                 LDLM_DEBUG(lock, "client lock: no-op");
395                 return 0;
396         }
397
398         spin_lock_bh(&waiting_locks_spinlock);
399         ret = __ldlm_del_waiting_lock(lock);
400         spin_unlock_bh(&waiting_locks_spinlock);
401
402         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
403         return ret;
404 }
405
406 /*
407  * Prolong the lock
408  *
409  * Called with namespace lock held.
410  */
411 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
412 {
413         if (lock->l_export == NULL) {
414                 /* We don't have a "waiting locks list" on clients. */
415                 LDLM_DEBUG(lock, "client lock: no-op");
416                 return 0;
417         }
418
419         spin_lock_bh(&waiting_locks_spinlock);
420
421         if (list_empty(&lock->l_pending_chain)) {
422                 spin_unlock_bh(&waiting_locks_spinlock);
423                 LDLM_DEBUG(lock, "wasn't waiting");
424                 return 0;
425         }
426
427         __ldlm_del_waiting_lock(lock);
428         __ldlm_add_waiting_lock(lock);
429         spin_unlock_bh(&waiting_locks_spinlock);
430
431         LDLM_DEBUG(lock, "refreshed");
432         return 1;
433 }
434
435 #else /* !__KERNEL__ */
436
437 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
438 {
439         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
440         RETURN(1);
441 }
442
443 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
444 {
445         RETURN(0);
446 }
447
448 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
449 {
450         RETURN(0);
451 }
452 #endif /* __KERNEL__ */
453
454 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
455                             const char *ast_type)
456 {
457         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
458         char                     *str = libcfs_nid2str(conn->c_peer.nid);
459
460         LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
461                            "service %s.\n", str,
462                            lock->l_export->exp_obd->obd_name);
463
464         LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
465                            "resource %d\n", ast_type,
466                            obd_export_nid2str(lock->l_export), rc);
467
468         if (obd_dump_on_timeout)
469                 libcfs_debug_dumplog();
470         class_fail_export(lock->l_export);
471 }
472
473 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
474                                  struct ptlrpc_request *req, int rc,
475                                  const char *ast_type)
476 {
477         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
478
479         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
480                 LASSERT(lock->l_export);
481                 if (lock->l_export->exp_libclient) {
482                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
483                                    " timeout, just cancelling lock", ast_type,
484                                    libcfs_nid2str(peer.nid));
485                         ldlm_lock_cancel(lock);
486                         rc = -ERESTART;
487                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
488                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
489                                    "cancel was received (AST reply lost?)",
490                                    ast_type, libcfs_nid2str(peer.nid));
491                         ldlm_lock_cancel(lock);
492                         rc = -ERESTART;
493                 } else {
494                         ldlm_del_waiting_lock(lock);
495                         ldlm_failed_ast(lock, rc, ast_type);
496                 }
497         } else if (rc) {
498                 if (rc == -EINVAL)
499                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
500                                " from %s AST - normal race",
501                                libcfs_nid2str(peer.nid),
502                                req->rq_repmsg ?
503                                lustre_msg_get_status(req->rq_repmsg) : -1,
504                                ast_type);
505                 else
506                         LDLM_ERROR(lock, "client (nid %s) returned %d "
507                                    "from %s AST", libcfs_nid2str(peer.nid),
508                                    (req->rq_repmsg != NULL) ?
509                                    lustre_msg_get_status(req->rq_repmsg) : 0,
510                                    ast_type);
511                 ldlm_lock_cancel(lock);
512                 /* Server-side AST functions are called from ldlm_reprocess_all,
513                  * which needs to be told to please restart its reprocessing. */
514                 rc = -ERESTART;
515         }
516
517         return rc;
518 }
519
520 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
521 {
522         struct ldlm_cb_set_arg *arg;
523         struct ldlm_lock *lock;
524         ENTRY;
525
526         LASSERT(data != NULL);
527
528         arg = req->rq_async_args.pointer_arg[0];
529         lock = req->rq_async_args.pointer_arg[1];
530         LASSERT(lock != NULL);
531         if (rc != 0) {
532                 /* If client canceled the lock but the cancel has not 
533                  * been recieved yet, we need to update lvbo to have the
534                  * proper attributes cached. */
535                 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
536                         ldlm_res_lvbo_update(lock->l_resource, NULL, 
537                                              0, 1);
538                 rc = ldlm_handle_ast_error(lock, req, rc, 
539                                            arg->type == LDLM_BL_CALLBACK
540                                            ? "blocking" : "completion");
541         }                
542
543         LDLM_LOCK_PUT(lock);
544
545         if (rc == -ERESTART)
546                 atomic_set(&arg->restart, 1);
547
548         RETURN(0);
549 }
550
551 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
552                                           struct ldlm_cb_set_arg *arg,
553                                           struct ldlm_lock *lock,
554                                           int instant_cancel)
555 {
556         int rc = 0;
557         ENTRY;
558
559         if (unlikely(instant_cancel)) {
560                 rc = ptl_send_rpc(req, 1);
561                 ptlrpc_req_finished(req);
562                 if (rc == 0)
563                         /* If we cancelled the lock, we need to restart
564                          * ldlm_reprocess_queue */
565                         atomic_set(&arg->restart, 1);
566         } else {
567                 LDLM_LOCK_GET(lock);
568                 ptlrpc_set_add_req(arg->set, req);
569         }       
570
571         RETURN(rc);
572 }
573
574 /*
575  * ->l_blocking_ast() method for server-side locks. This is invoked when newly
576  * enqueued server lock conflicts with given one.
577  *
578  * Sends blocking ast rpc to the client owning that lock; arms timeout timer
579  * to wait for client response.
580  */
581 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
582                              struct ldlm_lock_desc *desc,
583                              void *data, int flag)
584 {
585         struct ldlm_cb_set_arg *arg = data;
586         struct ldlm_request    *body;
587         struct ptlrpc_request  *req;
588         int                     instant_cancel = 0;
589         int                     rc = 0;
590         ENTRY;
591
592         if (flag == LDLM_CB_CANCELING)
593                 /* Don't need to do anything here. */
594                 RETURN(0);
595
596         LASSERT(lock);
597         LASSERT(data != NULL);
598         if (lock->l_export->exp_obd->obd_recovering != 0) {
599                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
600                 ldlm_lock_dump(D_ERROR, lock, 0);
601         }
602
603         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
604                                         &RQF_LDLM_BL_CALLBACK,
605                                         LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
606         if (req == NULL)
607                 RETURN(-ENOMEM);
608
609         req->rq_async_args.pointer_arg[0] = arg;
610         req->rq_async_args.pointer_arg[1] = lock;
611         req->rq_interpret_reply = ldlm_cb_interpret;
612         req->rq_no_resend = 1;
613
614         lock_res(lock->l_resource);
615         if (lock->l_granted_mode != lock->l_req_mode) {
616                 /* this blocking AST will be communicated as part of the
617                  * completion AST instead */
618                 unlock_res(lock->l_resource);
619                 ptlrpc_req_finished(req);
620                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
621                 RETURN(0);
622         }
623
624         if (lock->l_destroyed) {
625                 /* What's the point? */
626                 unlock_res(lock->l_resource);
627                 ptlrpc_req_finished(req);
628                 RETURN(0);
629         }
630
631         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
632                 instant_cancel = 1;
633
634         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
635         body->lock_handle[0] = lock->l_remote_handle;
636         body->lock_desc = *desc;
637         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
638
639         LDLM_DEBUG(lock, "server preparing blocking AST");
640
641         ptlrpc_request_set_replen(req);
642         if (instant_cancel) {
643                 unlock_res(lock->l_resource);
644                 ldlm_lock_cancel(lock);
645         } else {
646                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
647                 ldlm_add_waiting_lock(lock);
648                 unlock_res(lock->l_resource);
649         }
650
651         req->rq_send_state = LUSTRE_IMP_FULL;
652         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
653
654         if (lock->l_export && lock->l_export->exp_ldlm_stats)
655                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
656                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
657
658         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
659
660         RETURN(rc);
661 }
662
663 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
664 {
665         struct ldlm_cb_set_arg *arg = data;
666         struct ldlm_request    *body;
667         struct ptlrpc_request  *req;
668         struct timeval          granted_time;
669         long                    total_enqueue_wait;
670         int                     instant_cancel = 0;
671         int                     rc = 0;
672         ENTRY;
673
674         LASSERT(lock != NULL);
675         LASSERT(data != NULL);
676
677         do_gettimeofday(&granted_time);
678         total_enqueue_wait = cfs_timeval_sub(&granted_time,
679                                              &lock->l_enqueued_time, NULL);
680
681         if (total_enqueue_wait / 1000000 > obd_timeout)
682                 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
683                            total_enqueue_wait, lock->l_enqueued_time.tv_sec);
684
685          req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
686                                     &RQF_LDLM_CP_CALLBACK);
687          if (req == NULL)
688                  RETURN(-ENOMEM);
689
690         lock_res_and_lock(lock);
691         if (lock->l_resource->lr_lvb_len)
692                  req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
693                                       lock->l_resource->lr_lvb_len);
694         unlock_res_and_lock(lock);
695
696         rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
697         if (rc) {
698                 ptlrpc_request_free(req);
699                 RETURN(rc);
700         }
701
702         req->rq_async_args.pointer_arg[0] = arg;
703         req->rq_async_args.pointer_arg[1] = lock;
704         req->rq_interpret_reply = ldlm_cb_interpret;
705         req->rq_no_resend = 1;
706         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
707
708         body->lock_handle[0] = lock->l_remote_handle;
709         body->lock_flags = flags;
710         ldlm_lock2desc(lock, &body->lock_desc);
711         if (lock->l_resource->lr_lvb_len) {
712                 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
713
714                 lock_res_and_lock(lock);
715                 memcpy(lvb, lock->l_resource->lr_lvb_data,
716                        lock->l_resource->lr_lvb_len);
717                 unlock_res_and_lock(lock);
718         }
719
720         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
721                    total_enqueue_wait);
722
723         ptlrpc_request_set_replen(req);
724         req->rq_send_state = LUSTRE_IMP_FULL;
725         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
726
727         /* We only send real blocking ASTs after the lock is granted */
728         lock_res_and_lock(lock);
729         if (lock->l_flags & LDLM_FL_AST_SENT) {
730                 body->lock_flags |= LDLM_FL_AST_SENT;
731
732                 /* We might get here prior to ldlm_handle_enqueue setting
733                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
734                  * into waiting list, but this is safe and similar code in
735                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
736                  * that would not only cancel the lock, but will also remove
737                  * it from waiting list */
738                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
739                         unlock_res_and_lock(lock);
740                         ldlm_lock_cancel(lock);
741                         instant_cancel = 1;
742                         lock_res_and_lock(lock);
743                 } else {
744                         /* start the lock-timeout clock */
745                         ldlm_add_waiting_lock(lock);
746                 }
747         }
748         unlock_res_and_lock(lock);
749
750         if (lock->l_export && lock->l_export->exp_ldlm_stats)
751                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
752                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
753
754         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
755
756         RETURN(rc);
757 }
758
759 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
760 {
761         struct ldlm_resource  *res = lock->l_resource;
762         struct ldlm_request   *body;
763         struct ptlrpc_request *req;
764         int                    rc;
765         ENTRY;
766
767         LASSERT(lock != NULL);
768
769         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
770                                         &RQF_LDLM_GL_CALLBACK,
771                                         LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
772
773         if (req == NULL)
774                 RETURN(-ENOMEM);
775
776         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
777         body->lock_handle[0] = lock->l_remote_handle;
778         ldlm_lock2desc(lock, &body->lock_desc);
779
780         lock_res_and_lock(lock);
781         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
782                              lock->l_resource->lr_lvb_len);
783         unlock_res_and_lock(lock);
784         res = lock->l_resource;
785         ptlrpc_request_set_replen(req);
786
787
788         req->rq_send_state = LUSTRE_IMP_FULL;
789         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
790
791         if (lock->l_export && lock->l_export->exp_ldlm_stats)
792                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
793                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
794
795         rc = ptlrpc_queue_wait(req);
796         if (rc == -ELDLM_NO_LOCK_DATA)
797                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
798         else if (rc != 0)
799                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
800         else
801                 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
802                                           REPLY_REC_OFF, 1);
803         ptlrpc_req_finished(req);
804         if (rc == -ERESTART)
805                 ldlm_reprocess_all(res);
806
807         RETURN(rc);
808 }
809
810 static struct ldlm_lock *
811 find_existing_lock(struct obd_export *exp,
812                    const struct lustre_handle *remote_hdl)
813 {
814         struct list_head *iter;
815
816         spin_lock(&exp->exp_ldlm_data.led_lock);
817         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
818                 struct ldlm_lock *lock;
819                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
820                 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
821                         LDLM_LOCK_GET(lock);
822                         spin_unlock(&exp->exp_ldlm_data.led_lock);
823                         return lock;
824                 }
825         }
826         spin_unlock(&exp->exp_ldlm_data.led_lock);
827         return NULL;
828 }
829
830 #ifdef __KERNEL__
831 extern unsigned long long lu_time_stamp_get(void);
832 #else
833 #define lu_time_stamp_get() time(NULL)
834 #endif
835
836 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
837                        struct lprocfs_stats *srv_stats)
838 {
839         int lock_type = 0, op = 0;
840
841         lock_type = dlm_req->lock_desc.l_resource.lr_type;
842
843         switch (lock_type) {
844         case LDLM_PLAIN:
845                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
846                 break;
847         case LDLM_EXTENT:
848                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
849                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
850                 else
851                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
852                 break;
853         case LDLM_FLOCK:
854                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
855                 break;
856         case LDLM_IBITS:
857                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
858                 break;
859         default:
860                 op = 0;
861                 break;
862         }
863
864         if (op)
865                 lprocfs_counter_incr(srv_stats, op);
866
867         return ;
868 }
869
870 /*
871  * Main server-side entry point into LDLM. This is called by ptlrpc service
872  * threads to carry out client lock enqueueing requests.
873  */
874 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
875                          struct ptlrpc_request *req,
876                          const struct ldlm_request *dlm_req,
877                          const struct ldlm_callback_suite *cbs)
878 {
879         struct ldlm_reply *dlm_rep;
880         __u32 flags;
881         ldlm_error_t err = ELDLM_OK;
882         struct ldlm_lock *lock = NULL;
883         void *cookie = NULL;
884         int rc = 0;
885         ENTRY;
886
887         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
888
889         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
890         flags = dlm_req->lock_flags;
891
892         LASSERT(req->rq_export);
893
894         if (req->rq_rqbd->rqbd_service->srv_stats)
895                 ldlm_svc_get_eopc(dlm_req,
896                                   req->rq_rqbd->rqbd_service->srv_stats);
897
898         if (req->rq_export->exp_ldlm_stats)
899                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
900                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
901
902         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
903                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
904                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
905                           dlm_req->lock_desc.l_resource.lr_type);
906                 GOTO(out, rc = -EFAULT);
907         }
908
909         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
910                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
911                      dlm_req->lock_desc.l_req_mode &
912                      (dlm_req->lock_desc.l_req_mode-1))) {
913                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
914                           dlm_req->lock_desc.l_req_mode);
915                 GOTO(out, rc = -EFAULT);
916         }
917
918         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
919                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
920                              LDLM_PLAIN)) {
921                         DEBUG_REQ(D_ERROR, req,
922                                   "PLAIN lock request from IBITS client?");
923                         GOTO(out, rc = -EPROTO);
924                 }
925         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
926                             LDLM_IBITS)) {
927                 DEBUG_REQ(D_ERROR, req,
928                           "IBITS lock request from unaware client?");
929                 GOTO(out, rc = -EPROTO);
930         }
931
932 #if 0
933         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
934            against server's _CONNECT_SUPPORTED flags? (I don't want to use
935            ibits for mgc/mgs) */
936
937         /* INODEBITS_INTEROP: Perform conversion from plain lock to
938          * inodebits lock if client does not support them. */
939         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
940             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
941                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
942                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
943                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
944                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
945                         dlm_req->lock_desc.l_req_mode = LCK_CR;
946         }
947 #endif
948
949         if (unlikely(flags & LDLM_FL_REPLAY)) {
950                 lock = find_existing_lock(req->rq_export,
951                                           &dlm_req->lock_handle[0]);
952                 if (lock != NULL) {
953                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
954                                   LPX64, lock->l_handle.h_cookie);
955                         GOTO(existing_lock, rc = 0);
956                 }
957         }
958
959         /* The lock's callback data might be set in the policy function */
960         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
961                                 dlm_req->lock_desc.l_resource.lr_type,
962                                 dlm_req->lock_desc.l_req_mode,
963                                 cbs->lcs_blocking, cbs->lcs_completion,
964                                 cbs->lcs_glimpse, NULL, 0);
965
966         if (!lock)
967                 GOTO(out, rc = -ENOMEM);
968
969         do_gettimeofday(&lock->l_enqueued_time);
970         lock->l_remote_handle = dlm_req->lock_handle[0];
971         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
972
973         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
974         /* Don't enqueue a lock onto the export if it has already
975          * been evicted.  Cancel it now instead. (bug 3822) */
976         if (req->rq_export->exp_failed) {
977                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
978                 GOTO(out, rc = -ENOTCONN);
979         }
980         lock->l_export = class_export_get(req->rq_export);
981         spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
982         list_add(&lock->l_export_chain,
983                  &lock->l_export->exp_ldlm_data.led_held_locks);
984         spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
985
986 existing_lock:
987
988         if (flags & LDLM_FL_HAS_INTENT) {
989                 /* In this case, the reply buffer is allocated deep in
990                  * local_lock_enqueue by the policy function. */
991                 cookie = req;
992         } else {
993                 lock_res_and_lock(lock);
994                 if (lock->l_resource->lr_lvb_len) {
995                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
996                                              RCL_SERVER,
997                                              lock->l_resource->lr_lvb_len);
998                 }
999                 unlock_res_and_lock(lock);
1000
1001                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1002                         GOTO(out, rc = -ENOMEM);
1003
1004                 rc = req_capsule_server_pack(&req->rq_pill);
1005                 if (rc)
1006                         GOTO(out, rc);
1007         }
1008
1009         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1010                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1011         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1012                 lock->l_req_extent = lock->l_policy_data.l_extent;
1013
1014         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1015         if (err)
1016                 GOTO(out, err);
1017
1018         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1019         dlm_rep->lock_flags = flags;
1020
1021         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1022         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1023
1024         /* We never send a blocking AST until the lock is granted, but
1025          * we can tell it right now */
1026         lock_res_and_lock(lock);
1027
1028         /* Now take into account flags to be inherited from original lock
1029            request both in reply to client and in our own lock flags. */
1030         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1031         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1032
1033         /* Don't move a pending lock onto the export if it has already
1034          * been evicted.  Cancel it now instead. (bug 5683) */
1035         if (unlikely(req->rq_export->exp_failed ||
1036                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1037                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1038                 rc = -ENOTCONN;
1039         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1040                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1041                 if (lock->l_granted_mode == lock->l_req_mode) {
1042                         /*
1043                          * Only cancel lock if it was granted, because it would
1044                          * be destroyed immediatelly and would never be granted
1045                          * in the future, causing timeouts on client.  Not
1046                          * granted lock will be cancelled immediatelly after
1047                          * sending completion AST.
1048                          */
1049                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1050                                 unlock_res_and_lock(lock);
1051                                 ldlm_lock_cancel(lock);
1052                                 lock_res_and_lock(lock);
1053                         } else
1054                                 ldlm_add_waiting_lock(lock);
1055                 }
1056         }
1057         /* Make sure we never ever grant usual metadata locks to liblustre
1058            clients */
1059         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1060             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1061              req->rq_export->exp_libclient) {
1062                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1063                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1064                         CERROR("Granting sync lock to libclient. "
1065                                "req fl %d, rep fl %d, lock fl %d\n",
1066                                dlm_req->lock_flags, dlm_rep->lock_flags,
1067                                lock->l_flags);
1068                         LDLM_ERROR(lock, "sync lock");
1069                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1070                                 struct ldlm_intent *it;
1071
1072                                 it = req_capsule_client_get(&req->rq_pill,
1073                                                             &RMF_LDLM_INTENT);
1074                                 if (it != NULL) {
1075                                         CERROR("This is intent %s ("LPU64")\n",
1076                                                ldlm_it2str(it->opc), it->opc);
1077                                 }
1078                         }
1079                 }
1080         }
1081
1082         unlock_res_and_lock(lock);
1083
1084         EXIT;
1085  out:
1086         req->rq_status = err;
1087         if (req->rq_reply_state == NULL) {
1088                 err = lustre_pack_reply(req, 1, NULL, NULL);
1089                 if (rc == 0)
1090                         rc = err;
1091                 req->rq_status = rc;
1092         }
1093
1094         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1095          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1096         if (lock) {
1097                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1098                            "(err=%d, rc=%d)", err, rc);
1099
1100                 lock_res_and_lock(lock);
1101                 if (rc == 0) {
1102                         if (lock->l_resource->lr_lvb_len > 0) {
1103                                 void *lvb;
1104
1105                                 lvb = req_capsule_server_get(&req->rq_pill,
1106                                                              &RMF_DLM_LVB);
1107                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1108                                          req, lock);
1109
1110                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1111                                        lock->l_resource->lr_lvb_len);
1112                         }
1113                 } else {
1114                         ldlm_resource_unlink_lock(lock);
1115                         ldlm_lock_destroy_nolock(lock);
1116                 }
1117                 unlock_res_and_lock(lock);
1118
1119                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1120                         ldlm_reprocess_all(lock->l_resource);
1121
1122                 LDLM_LOCK_PUT(lock);
1123         }
1124
1125         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1126                           lock, rc);
1127
1128         return rc;
1129 }
1130
1131 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1132                         ldlm_completion_callback completion_callback,
1133                         ldlm_blocking_callback blocking_callback,
1134                         ldlm_glimpse_callback glimpse_callback)
1135 {
1136         struct ldlm_request *dlm_req;
1137         struct ldlm_callback_suite cbs = {
1138                 .lcs_completion = completion_callback,
1139                 .lcs_blocking   = blocking_callback,
1140                 .lcs_glimpse    = glimpse_callback
1141         };
1142         int rc;
1143
1144         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1145         if (dlm_req != NULL) {
1146                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1147                                           req, dlm_req, &cbs);
1148         } else {
1149                 rc = -EFAULT;
1150         }
1151         return rc;
1152 }
1153
1154 int ldlm_handle_convert0(struct ptlrpc_request *req,
1155                          const struct ldlm_request *dlm_req)
1156 {
1157         struct ldlm_reply *dlm_rep;
1158         struct ldlm_lock *lock;
1159         int rc;
1160         ENTRY;
1161
1162         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1163                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1164                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1165
1166         rc = req_capsule_server_pack(&req->rq_pill);
1167         if (rc)
1168                 RETURN(rc);
1169
1170         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1171         dlm_rep->lock_flags = dlm_req->lock_flags;
1172
1173         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1174         if (!lock) {
1175                 req->rq_status = EINVAL;
1176         } else {
1177                 void *res = NULL;
1178
1179                 LDLM_DEBUG(lock, "server-side convert handler START");
1180
1181                 do_gettimeofday(&lock->l_enqueued_time);
1182                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1183                                         &dlm_rep->lock_flags);
1184                 if (res) {
1185                         if (ldlm_del_waiting_lock(lock))
1186                                 LDLM_DEBUG(lock, "converted waiting lock");
1187                         req->rq_status = 0;
1188                 } else {
1189                         req->rq_status = EDEADLOCK;
1190                 }
1191         }
1192
1193         if (lock) {
1194                 if (!req->rq_status)
1195                         ldlm_reprocess_all(lock->l_resource);
1196                 LDLM_DEBUG(lock, "server-side convert handler END");
1197                 LDLM_LOCK_PUT(lock);
1198         } else
1199                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1200
1201         RETURN(0);
1202 }
1203
1204 int ldlm_handle_convert(struct ptlrpc_request *req)
1205 {
1206         int rc;
1207         struct ldlm_request *dlm_req;
1208
1209         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1210         if (dlm_req != NULL) {
1211                 rc = ldlm_handle_convert0(req, dlm_req);
1212         } else {
1213                 CERROR ("Can't unpack dlm_req\n");
1214                 rc = -EFAULT;
1215         }
1216         return rc;
1217 }
1218
1219 /* Cancel all the locks, which handles are packed into ldlm_request */
1220 int ldlm_request_cancel(struct ptlrpc_request *req,
1221                         const struct ldlm_request *dlm_req, int first)
1222 {
1223         struct ldlm_resource *res, *pres = NULL;
1224         struct ldlm_lock *lock;
1225         int i, count, done = 0;
1226         ENTRY;
1227
1228         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1229         if (first >= count)
1230                 RETURN(0);
1231
1232         /* There is no lock on the server at the replay time,
1233          * skip lock cancelling to make replay tests to pass. */
1234         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1235                 RETURN(0);
1236
1237         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1238                           "starting at %d", count, first);
1239
1240         for (i = first; i < count; i++) {
1241                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1242                 if (!lock) {
1243                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1244                                           "lock (cookie "LPU64")",
1245                                           dlm_req->lock_handle[i].cookie);
1246                         continue;
1247                 }
1248
1249                 res = lock->l_resource;
1250                 done++;
1251
1252                 if (res != pres) {
1253                         if (pres != NULL) {
1254                                 ldlm_reprocess_all(pres);
1255                                 ldlm_resource_putref(pres);
1256                         }
1257                         if (res != NULL) {
1258                                 ldlm_resource_getref(res);
1259                                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1260                         }
1261                         pres = res;
1262                 }
1263                 ldlm_lock_cancel(lock);
1264                 LDLM_LOCK_PUT(lock);
1265         }
1266         if (pres != NULL) {
1267                 ldlm_reprocess_all(pres);
1268                 ldlm_resource_putref(pres);
1269         }
1270         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1271         RETURN(done);
1272 }
1273
1274 int ldlm_handle_cancel(struct ptlrpc_request *req)
1275 {
1276         struct ldlm_request *dlm_req;
1277         int rc;
1278         ENTRY;
1279
1280         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1281         if (dlm_req == NULL) {
1282                 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1283                 RETURN(-EFAULT);
1284         }
1285
1286         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1287                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1288                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1289
1290         rc = req_capsule_server_pack(&req->rq_pill);
1291         if (rc)
1292                 RETURN(rc);
1293
1294         if (!ldlm_request_cancel(req, dlm_req, 0))
1295                 req->rq_status = ESTALE;
1296
1297         if (ptlrpc_reply(req) != 0)
1298                 LBUG();
1299
1300         RETURN(0);
1301 }
1302
1303 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1304                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1305 {
1306         int do_ast;
1307         ENTRY;
1308
1309         LDLM_DEBUG(lock, "client blocking AST callback handler START");
1310
1311         lock_res_and_lock(lock);
1312         lock->l_flags |= LDLM_FL_CBPENDING;
1313
1314         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1315                 lock->l_flags |= LDLM_FL_CANCEL;
1316
1317         do_ast = (!lock->l_readers && !lock->l_writers);
1318         unlock_res_and_lock(lock);
1319
1320         if (do_ast) {
1321                 LDLM_DEBUG(lock, "already unused, calling "
1322                            "callback (%p)", lock->l_blocking_ast);
1323                 if (lock->l_blocking_ast != NULL)
1324                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1325                                              LDLM_CB_BLOCKING);
1326         } else {
1327                 LDLM_DEBUG(lock, "Lock still has references, will be"
1328                            " cancelled later");
1329         }
1330
1331         LDLM_DEBUG(lock, "client blocking callback handler END");
1332         LDLM_LOCK_PUT(lock);
1333         EXIT;
1334 }
1335
1336 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1337                                     struct ldlm_namespace *ns,
1338                                     struct ldlm_request *dlm_req,
1339                                     struct ldlm_lock *lock)
1340 {
1341         CFS_LIST_HEAD(ast_list);
1342         ENTRY;
1343
1344         LDLM_DEBUG(lock, "client completion callback handler START");
1345
1346         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1347                 int to = cfs_time_seconds(1);
1348                 while (to > 0) {
1349                         to = schedule_timeout(to);
1350                         if (lock->l_granted_mode == lock->l_req_mode ||
1351                             lock->l_destroyed)
1352                                 break;
1353                 }
1354         }
1355
1356         lock_res_and_lock(lock);
1357         if (lock->l_destroyed ||
1358             lock->l_granted_mode == lock->l_req_mode) {
1359                 /* bug 11300: the lock has already been granted */
1360                 unlock_res_and_lock(lock);
1361                 LDLM_DEBUG(lock, "Double grant race happened");
1362                 LDLM_LOCK_PUT(lock);
1363                 EXIT;
1364                 return;
1365         }
1366
1367         /* If we receive the completion AST before the actual enqueue returned,
1368          * then we might need to switch lock modes, resources, or extents. */
1369         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1370                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1371                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1372         }
1373
1374         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1375                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1376                 LDLM_DEBUG(lock, "completion AST, new policy data");
1377         }
1378
1379         ldlm_resource_unlink_lock(lock);
1380         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1381                    &lock->l_resource->lr_name,
1382                    sizeof(lock->l_resource->lr_name)) != 0) {
1383                 unlock_res_and_lock(lock);
1384                 if (ldlm_lock_change_resource(ns, lock, 
1385                                 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1386                         LDLM_ERROR(lock, "Failed to allocate resource");
1387                         LDLM_LOCK_PUT(lock);
1388                         EXIT;
1389                         return;
1390                 }
1391                 LDLM_DEBUG(lock, "completion AST, new resource");
1392                 CERROR("change resource!\n");
1393                 lock_res_and_lock(lock);
1394         }
1395
1396         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1397                 /* BL_AST locks are not needed in lru.
1398                  * let ldlm_cancel_lru() be fast. */
1399                 ldlm_lock_remove_from_lru(lock);
1400                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1401                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1402         }
1403
1404         if (lock->l_lvb_len) {
1405                 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1406                                          RCL_CLIENT) < lock->l_lvb_len) {
1407                         LDLM_ERROR(lock, "completion AST did not contain "
1408                                    "expected LVB!");
1409                 } else {
1410                         void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1411                                                                 &RMF_DLM_LVB,
1412                                                   (void *)lock->l_lvb_swabber);
1413                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1414                 }
1415         }
1416
1417         ldlm_grant_lock(lock, &ast_list);
1418         unlock_res_and_lock(lock);
1419
1420         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1421
1422         ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1423
1424         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1425                           lock);
1426         LDLM_LOCK_PUT(lock);
1427         EXIT;
1428 }
1429
1430 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1431                                     struct ldlm_namespace *ns,
1432                                     struct ldlm_request *dlm_req,
1433                                     struct ldlm_lock *lock)
1434 {
1435         int rc = -ENOSYS;
1436         ENTRY;
1437
1438         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1439
1440         if (lock->l_glimpse_ast != NULL)
1441                 rc = lock->l_glimpse_ast(lock, req);
1442
1443         if (req->rq_repmsg != NULL) {
1444                 ptlrpc_reply(req);
1445         } else {
1446                 req->rq_status = rc;
1447                 ptlrpc_error(req);
1448         }
1449
1450         lock_res_and_lock(lock);
1451         if (lock->l_granted_mode == LCK_PW &&
1452             !lock->l_readers && !lock->l_writers &&
1453             cfs_time_after(cfs_time_current(),
1454                            cfs_time_add(lock->l_last_used,
1455                                         cfs_time_seconds(10)))) {
1456                 unlock_res_and_lock(lock);
1457                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1458                         ldlm_handle_bl_callback(ns, NULL, lock);
1459
1460                 EXIT;
1461                 return;
1462         }
1463         unlock_res_and_lock(lock);
1464         LDLM_LOCK_PUT(lock);
1465         EXIT;
1466 }
1467
1468 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1469 {
1470         if (req->rq_no_reply)
1471                 return 0;
1472
1473         req->rq_status = rc;
1474         if (req->rq_reply_state == NULL) {
1475                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1476                 if (rc)
1477                         return rc;
1478         }
1479         return ptlrpc_reply(req);
1480 }
1481
1482 #ifdef __KERNEL__
1483 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1484                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1485                              struct list_head *cancels, int count)
1486 {
1487         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1488         struct ldlm_bl_work_item *blwi;
1489         ENTRY;
1490
1491         if (cancels && count == 0)
1492                 RETURN(0);
1493
1494         OBD_ALLOC(blwi, sizeof(*blwi));
1495         if (blwi == NULL)
1496                 RETURN(-ENOMEM);
1497
1498         blwi->blwi_ns = ns;
1499         if (ld != NULL)
1500                 blwi->blwi_ld = *ld;
1501         if (count) {
1502                 list_add(&blwi->blwi_head, cancels);
1503                 list_del_init(cancels);
1504                 blwi->blwi_count = count;
1505         } else {
1506                 blwi->blwi_lock = lock;
1507         }
1508         spin_lock(&blp->blp_lock);
1509         if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1510                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1511                 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1512         } else {
1513                 /* other blocking callbacks are added to the regular list */
1514                 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1515         }
1516         cfs_waitq_signal(&blp->blp_waitq);
1517         spin_unlock(&blp->blp_lock);
1518
1519         RETURN(0);
1520 }
1521 #endif
1522
1523 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1524                            struct ldlm_lock *lock)
1525 {
1526 #ifdef __KERNEL__
1527         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1528 #else
1529         RETURN(-ENOSYS);
1530 #endif
1531 }
1532
1533 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1534                            struct list_head *cancels, int count)
1535 {
1536 #ifdef __KERNEL__
1537         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1538 #else
1539         RETURN(-ENOSYS);
1540 #endif
1541 }
1542
1543 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1544 static int ldlm_callback_handler(struct ptlrpc_request *req)
1545 {
1546         struct ldlm_namespace *ns;
1547         struct ldlm_request *dlm_req;
1548         struct ldlm_lock *lock;
1549         int rc;
1550         ENTRY;
1551
1552         /* Requests arrive in sender's byte order.  The ptlrpc service
1553          * handler has already checked and, if necessary, byte-swapped the
1554          * incoming request message body, but I am responsible for the
1555          * message buffers. */
1556
1557         /* do nothing for sec context finalize */
1558         if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1559                 RETURN(0);
1560
1561         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1562
1563         if (req->rq_export == NULL) {
1564                 struct ldlm_request *dlm_req;
1565
1566                 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1567                        "export cookie "LPX64"; this is "
1568                        "normal if this node rebooted with a lock held\n",
1569                        lustre_msg_get_opc(req->rq_reqmsg),
1570                        libcfs_id2str(req->rq_peer),
1571                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1572
1573                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1574                 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1575                 if (dlm_req != NULL)
1576                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1577                                dlm_req->lock_handle[0].cookie);
1578
1579                 ldlm_callback_reply(req, -ENOTCONN);
1580                 RETURN(0);
1581         }
1582
1583         LASSERT(req->rq_export != NULL);
1584         LASSERT(req->rq_export->exp_obd != NULL);
1585
1586         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1587         case LDLM_BL_CALLBACK:
1588                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1589                         RETURN(0);
1590                 break;
1591         case LDLM_CP_CALLBACK:
1592                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1593                         RETURN(0);
1594                 break;
1595         case LDLM_GL_CALLBACK:
1596                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1597                         RETURN(0);
1598                 break;
1599         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1600                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1601                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1602                         RETURN(0);
1603                 rc = llog_origin_handle_cancel(req);
1604                 ldlm_callback_reply(req, rc);
1605                 RETURN(0);
1606         case OBD_QC_CALLBACK:
1607                 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1608                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1609                         RETURN(0);
1610                 rc = target_handle_qc_callback(req);
1611                 ldlm_callback_reply(req, rc);
1612                 RETURN(0);
1613         case QUOTA_DQACQ:
1614         case QUOTA_DQREL:
1615                 /* reply in handler */
1616                 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1617                 rc = target_handle_dqacq_callback(req);
1618                 RETURN(0);
1619         case LLOG_ORIGIN_HANDLE_CREATE:
1620                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1621                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1622                         RETURN(0);
1623                 rc = llog_origin_handle_create(req);
1624                 ldlm_callback_reply(req, rc);
1625                 RETURN(0);
1626         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1627                 req_capsule_set(&req->rq_pill,
1628                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1629                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1630                         RETURN(0);
1631                 rc = llog_origin_handle_next_block(req);
1632                 ldlm_callback_reply(req, rc);
1633                 RETURN(0);
1634         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1635                 req_capsule_set(&req->rq_pill,
1636                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1637                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1638                         RETURN(0);
1639                 rc = llog_origin_handle_read_header(req);
1640                 ldlm_callback_reply(req, rc);
1641                 RETURN(0);
1642         case LLOG_ORIGIN_HANDLE_CLOSE:
1643                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1644                         RETURN(0);
1645                 rc = llog_origin_handle_close(req);
1646                 ldlm_callback_reply(req, rc);
1647                 RETURN(0);
1648         default:
1649                 CERROR("unknown opcode %u\n",
1650                        lustre_msg_get_opc(req->rq_reqmsg));
1651                 ldlm_callback_reply(req, -EPROTO);
1652                 RETURN(0);
1653         }
1654
1655         ns = req->rq_export->exp_obd->obd_namespace;
1656         LASSERT(ns != NULL);
1657
1658         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1659
1660         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1661         if (dlm_req == NULL) {
1662                 ldlm_callback_reply(req, -EPROTO);
1663                 RETURN(0);
1664         }
1665
1666         /* Force a known safe race, send a cancel to the server for a lock
1667          * which the server has already started a blocking callback on. */
1668         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1669             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1670                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1671                 if (rc < 0)
1672                         CERROR("ldlm_cli_cancel: %d\n", rc);
1673         }
1674
1675         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1676         if (!lock) {
1677                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1678                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1679                 ldlm_callback_reply(req, -EINVAL);
1680                 RETURN(0);
1681         }
1682
1683         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1684         lock_res_and_lock(lock);
1685         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1686         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1687                 /* If somebody cancels locks and cache is already droped,
1688                  * we can tell the server we have no lock. Otherwise, we
1689                  * should send cancel after dropping the cache. */
1690                 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1691                     (lock->l_flags & LDLM_FL_BL_DONE)) {
1692                         LDLM_DEBUG(lock, "callback on lock "
1693                                    LPX64" - lock disappeared\n",
1694                                    dlm_req->lock_handle[0].cookie);
1695                         unlock_res_and_lock(lock);
1696                         LDLM_LOCK_PUT(lock);
1697                         ldlm_callback_reply(req, -EINVAL);
1698                         RETURN(0);
1699                 }
1700                 /* BL_AST locks are not needed in lru.
1701                  * let ldlm_cancel_lru() be fast. */
1702                 ldlm_lock_remove_from_lru(lock);
1703                 lock->l_flags |= LDLM_FL_BL_AST;
1704         }
1705         unlock_res_and_lock(lock);
1706
1707         /* We want the ost thread to get this reply so that it can respond
1708          * to ost requests (write cache writeback) that might be triggered
1709          * in the callback.
1710          *
1711          * But we'd also like to be able to indicate in the reply that we're
1712          * cancelling right now, because it's unused, or have an intent result
1713          * in the reply, so we might have to push the responsibility for sending
1714          * the reply down into the AST handlers, alas. */
1715
1716         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1717         case LDLM_BL_CALLBACK:
1718                 CDEBUG(D_INODE, "blocking ast\n");
1719                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1720                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1721                         ldlm_callback_reply(req, 0);
1722                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1723                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1724                 break;
1725         case LDLM_CP_CALLBACK:
1726                 CDEBUG(D_INODE, "completion ast\n");
1727                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1728                 ldlm_callback_reply(req, 0);
1729                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1730                 break;
1731         case LDLM_GL_CALLBACK:
1732                 CDEBUG(D_INODE, "glimpse ast\n");
1733                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1734                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1735                 break;
1736         default:
1737                 LBUG();                         /* checked above */
1738         }
1739
1740         RETURN(0);
1741 }
1742
1743 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1744 {
1745         int rc;
1746         ENTRY;
1747
1748         /* Requests arrive in sender's byte order.  The ptlrpc service
1749          * handler has already checked and, if necessary, byte-swapped the
1750          * incoming request message body, but I am responsible for the
1751          * message buffers. */
1752
1753         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1754
1755         if (req->rq_export == NULL) {
1756                 struct ldlm_request *dlm_req;
1757
1758                 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1759                        lustre_msg_get_opc(req->rq_reqmsg),
1760                        libcfs_id2str(req->rq_peer),
1761                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1762
1763                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1764                 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1765                 if (dlm_req != NULL)
1766                         ldlm_lock_dump_handle(D_ERROR,
1767                                               &dlm_req->lock_handle[0]);
1768                 ldlm_callback_reply(req, -ENOTCONN);
1769                 RETURN(0);
1770         }
1771
1772         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1773
1774         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1775         case LDLM_CANCEL:
1776                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1777                 CDEBUG(D_INODE, "cancel\n");
1778                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1779                         RETURN(0);
1780                 rc = ldlm_handle_cancel(req);
1781                 if (rc)
1782                         break;
1783                 RETURN(0);
1784         case OBD_LOG_CANCEL:
1785                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1786                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1787                         RETURN(0);
1788                 rc = llog_origin_handle_cancel(req);
1789                 ldlm_callback_reply(req, rc);
1790                 RETURN(0);
1791         default:
1792                 CERROR("invalid opcode %d\n",
1793                        lustre_msg_get_opc(req->rq_reqmsg));
1794                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1795                 ldlm_callback_reply(req, -EINVAL);
1796         }
1797
1798         RETURN(0);
1799 }
1800
1801 void ldlm_revoke_export_locks(struct obd_export *exp)
1802 {
1803         struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
1804         struct list_head  rpc_list;
1805         struct ldlm_lock *lock, *next;
1806
1807         ENTRY;
1808         CFS_INIT_LIST_HEAD(&rpc_list);
1809
1810         spin_lock(&exp->exp_ldlm_data.led_lock);
1811         list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
1812                 lock_res_and_lock(lock);
1813
1814                 if (lock->l_req_mode != lock->l_granted_mode) {
1815                         unlock_res_and_lock(lock);
1816                         continue;
1817                 }
1818
1819                 LASSERT(lock->l_resource);
1820                 if (lock->l_resource->lr_type != LDLM_IBITS &&
1821                     lock->l_resource->lr_type != LDLM_PLAIN) {
1822                         unlock_res_and_lock(lock);
1823                         continue;
1824                 }
1825
1826                 if (lock->l_flags & LDLM_FL_AST_SENT) {
1827                         unlock_res_and_lock(lock);
1828                         continue;
1829                 }
1830
1831                 LASSERT(lock->l_blocking_ast);
1832                 LASSERT(!lock->l_blocking_lock);
1833
1834                 lock->l_flags |= LDLM_FL_AST_SENT;
1835                 list_move(&lock->l_export_chain, &rpc_list);
1836                 LDLM_LOCK_GET(lock);
1837
1838                 unlock_res_and_lock(lock);
1839         }
1840         spin_unlock(&exp->exp_ldlm_data.led_lock);
1841
1842         ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1843
1844         EXIT;
1845 }
1846
1847 #ifdef __KERNEL__
1848 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1849 {
1850         struct ldlm_bl_work_item *blwi = NULL;
1851         static unsigned int num_bl = 0;
1852
1853         spin_lock(&blp->blp_lock);
1854         /* process a request from the blp_list at least every blp_num_threads */
1855         if (!list_empty(&blp->blp_list) &&
1856             (list_empty(&blp->blp_prio_list) || num_bl == 0))
1857                 blwi = list_entry(blp->blp_list.next,
1858                                   struct ldlm_bl_work_item, blwi_entry);
1859         else
1860                 if (!list_empty(&blp->blp_prio_list))
1861                         blwi = list_entry(blp->blp_prio_list.next,
1862                                           struct ldlm_bl_work_item, blwi_entry);
1863
1864         if (blwi) {
1865                 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1866                         num_bl = 0;
1867                 list_del(&blwi->blwi_entry);
1868         }
1869         spin_unlock(&blp->blp_lock);
1870
1871         return blwi;
1872 }
1873
1874 /* This only contains temporary data until the thread starts */
1875 struct ldlm_bl_thread_data {
1876         char                    bltd_name[CFS_CURPROC_COMM_MAX];
1877         struct ldlm_bl_pool     *bltd_blp;
1878         struct completion       bltd_comp;
1879         int                     bltd_num;
1880 };
1881
1882 static int ldlm_bl_thread_main(void *arg);
1883
1884 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1885 {
1886         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1887         int rc;
1888
1889         init_completion(&bltd.bltd_comp);
1890         rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1891         if (rc < 0) {
1892                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1893                        atomic_read(&blp->blp_num_threads), rc);
1894                 return rc;
1895         }
1896         wait_for_completion(&bltd.bltd_comp);
1897
1898         return 0;
1899 }
1900
1901 static int ldlm_bl_thread_main(void *arg)
1902 {
1903         struct ldlm_bl_pool *blp;
1904         ENTRY;
1905
1906         {
1907                 struct ldlm_bl_thread_data *bltd = arg;
1908
1909                 blp = bltd->bltd_blp;
1910
1911                 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1912                 atomic_inc(&blp->blp_busy_threads);
1913
1914                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1915                         "ldlm_bl_%02d", bltd->bltd_num);
1916                 cfs_daemonize(bltd->bltd_name);
1917
1918                 complete(&bltd->bltd_comp);
1919                 /* cannot use bltd after this, it is only on caller's stack */
1920         }
1921
1922         while (1) {
1923                 struct l_wait_info lwi = { 0 };
1924                 struct ldlm_bl_work_item *blwi = NULL;
1925
1926                 blwi = ldlm_bl_get_work(blp);
1927
1928                 if (blwi == NULL) {
1929                         int busy;
1930
1931                         atomic_dec(&blp->blp_busy_threads);
1932                         l_wait_event_exclusive(blp->blp_waitq,
1933                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
1934                                          &lwi);
1935                         busy = atomic_inc_return(&blp->blp_busy_threads);
1936
1937                         if (blwi->blwi_ns == NULL)
1938                                 /* added by ldlm_cleanup() */
1939                                 break;
1940
1941                         /* Not fatal if racy and have a few too many threads */
1942                         if (unlikely(busy < blp->blp_max_threads &&
1943                                     busy >= atomic_read(&blp->blp_num_threads)))
1944                                 /* discard the return value, we tried */
1945                                 ldlm_bl_thread_start(blp);
1946                 } else {
1947                         if (blwi->blwi_ns == NULL)
1948                                 /* added by ldlm_cleanup() */
1949                                 break;
1950                 }
1951
1952                 if (blwi->blwi_count) {
1953                         /* The special case when we cancel locks in lru
1954                          * asynchronously, we pass the list of locks here.
1955                          * Thus lock is marked LDLM_FL_CANCELING, and already
1956                          * canceled locally. */
1957                         ldlm_cli_cancel_list(&blwi->blwi_head,
1958                                              blwi->blwi_count, NULL, 0);
1959                 } else {
1960                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1961                                                 blwi->blwi_lock);
1962                 }
1963                 OBD_FREE(blwi, sizeof(*blwi));
1964         }
1965
1966         atomic_dec(&blp->blp_busy_threads);
1967         atomic_dec(&blp->blp_num_threads);
1968         complete(&blp->blp_comp);
1969         RETURN(0);
1970 }
1971
1972 #endif
1973
1974 static int ldlm_setup(void);
1975 static int ldlm_cleanup(void);
1976
1977 int ldlm_get_ref(void)
1978 {
1979         int rc = 0;
1980         ENTRY;
1981         mutex_down(&ldlm_ref_sem);
1982         if (++ldlm_refcount == 1) {
1983                 rc = ldlm_setup();
1984                 if (rc)
1985                         ldlm_refcount--;
1986         }
1987         mutex_up(&ldlm_ref_sem);
1988
1989         RETURN(rc);
1990 }
1991
1992 void ldlm_put_ref(void)
1993 {
1994         ENTRY;
1995         mutex_down(&ldlm_ref_sem);
1996         if (ldlm_refcount == 1) {
1997                 int rc = ldlm_cleanup();
1998                 if (rc)
1999                         CERROR("ldlm_cleanup failed: %d\n", rc);
2000                 else
2001                         ldlm_refcount--;
2002         } else {
2003                 ldlm_refcount--;
2004         }
2005         mutex_up(&ldlm_ref_sem);
2006
2007         EXIT;
2008 }
2009
2010 static int ldlm_setup(void)
2011 {
2012         struct ldlm_bl_pool *blp;
2013         int rc = 0;
2014         int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2015         int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2016 #ifdef __KERNEL__
2017         int i;
2018 #endif
2019         ENTRY;
2020
2021         if (ldlm_state != NULL)
2022                 RETURN(-EALREADY);
2023
2024         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2025         if (ldlm_state == NULL)
2026                 RETURN(-ENOMEM);
2027
2028 #ifdef LPROCFS
2029         rc = ldlm_proc_setup();
2030         if (rc != 0)
2031                 GOTO(out_free, rc);
2032 #endif
2033
2034 #ifdef __KERNEL__
2035         if (ldlm_num_threads) {
2036                 /* If ldlm_num_threads is set, it is the min and the max. */
2037                 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2038                         ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2039                 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2040                         ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2041                 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2042         }
2043 #endif
2044
2045         ldlm_state->ldlm_cb_service =
2046                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2047                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2048                                 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
2049                                 ldlm_callback_handler, "ldlm_cbd",
2050                                 ldlm_svc_proc_dir, NULL,
2051                                 ldlm_min_threads, ldlm_max_threads,
2052                                 "ldlm_cb",
2053                                 LCT_MD_THREAD|LCT_DT_THREAD);
2054
2055         if (!ldlm_state->ldlm_cb_service) {
2056                 CERROR("failed to start service\n");
2057                 GOTO(out_proc, rc = -ENOMEM);
2058         }
2059
2060         ldlm_state->ldlm_cancel_service =
2061                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2062                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2063                                 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
2064                                 ldlm_cancel_handler, "ldlm_canceld",
2065                                 ldlm_svc_proc_dir, NULL,
2066                                 ldlm_min_threads, ldlm_max_threads,
2067                                 "ldlm_cn",
2068                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2069
2070         if (!ldlm_state->ldlm_cancel_service) {
2071                 CERROR("failed to start service\n");
2072                 GOTO(out_proc, rc = -ENOMEM);
2073         }
2074
2075         OBD_ALLOC(blp, sizeof(*blp));
2076         if (blp == NULL)
2077                 GOTO(out_proc, rc = -ENOMEM);
2078         ldlm_state->ldlm_bl_pool = blp;
2079
2080         spin_lock_init(&blp->blp_lock);
2081         CFS_INIT_LIST_HEAD(&blp->blp_list);
2082         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2083         cfs_waitq_init(&blp->blp_waitq);
2084         atomic_set(&blp->blp_num_threads, 0);
2085         atomic_set(&blp->blp_busy_threads, 0);
2086         blp->blp_min_threads = ldlm_min_threads;
2087         blp->blp_max_threads = ldlm_max_threads;
2088
2089 #ifdef __KERNEL__
2090         for (i = 0; i < blp->blp_min_threads; i++) {
2091                 rc = ldlm_bl_thread_start(blp);
2092                 if (rc < 0)
2093                         GOTO(out_thread, rc);
2094         }
2095
2096         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2097         if (rc)
2098                 GOTO(out_thread, rc);
2099
2100         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2101         if (rc)
2102                 GOTO(out_thread, rc);
2103
2104         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2105         expired_lock_thread.elt_state = ELT_STOPPED;
2106         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2107
2108         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2109         spin_lock_init(&waiting_locks_spinlock);
2110         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2111
2112         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2113         if (rc < 0) {
2114                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2115                 GOTO(out_thread, rc);
2116         }
2117
2118         wait_event(expired_lock_thread.elt_waitq,
2119                    expired_lock_thread.elt_state == ELT_READY);
2120 #endif
2121
2122 #ifdef __KERNEL__
2123         rc = ldlm_pools_init();
2124         if (rc)
2125                 GOTO(out_thread, rc);
2126 #endif
2127         RETURN(0);
2128
2129 #ifdef __KERNEL__
2130  out_thread:
2131         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2132         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2133 #endif
2134
2135  out_proc:
2136 #ifdef LPROCFS
2137         ldlm_proc_cleanup();
2138  out_free:
2139 #endif
2140         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2141         ldlm_state = NULL;
2142         return rc;
2143 }
2144
2145 static int ldlm_cleanup(void)
2146 {
2147 #ifdef __KERNEL__
2148         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2149 #endif
2150         ENTRY;
2151
2152         if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) || 
2153             !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2154                 CERROR("ldlm still has namespaces; clean these up first.\n");
2155                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2156                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2157                 RETURN(-EBUSY);
2158         }
2159
2160 #ifdef __KERNEL__
2161         ldlm_pools_fini();
2162 #endif
2163
2164 #ifdef __KERNEL__
2165         while (atomic_read(&blp->blp_num_threads) > 0) {
2166                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2167
2168                 init_completion(&blp->blp_comp);
2169
2170                 spin_lock(&blp->blp_lock);
2171                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2172                 cfs_waitq_signal(&blp->blp_waitq);
2173                 spin_unlock(&blp->blp_lock);
2174
2175                 wait_for_completion(&blp->blp_comp);
2176         }
2177         OBD_FREE(blp, sizeof(*blp));
2178
2179         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2180         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2181         ldlm_proc_cleanup();
2182
2183         expired_lock_thread.elt_state = ELT_TERMINATE;
2184         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2185         wait_event(expired_lock_thread.elt_waitq,
2186                    expired_lock_thread.elt_state == ELT_STOPPED);
2187 #else
2188         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2189         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2190 #endif
2191
2192         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2193         ldlm_state = NULL;
2194
2195         RETURN(0);
2196 }
2197
2198 int __init ldlm_init(void)
2199 {
2200         init_mutex(&ldlm_ref_sem);
2201         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2202         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2203         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2204                                                sizeof(struct ldlm_resource), 0,
2205                                                SLAB_HWCACHE_ALIGN);
2206         if (ldlm_resource_slab == NULL)
2207                 return -ENOMEM;
2208
2209         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2210                                            sizeof(struct ldlm_lock), 0,
2211                                            SLAB_HWCACHE_ALIGN);
2212         if (ldlm_lock_slab == NULL) {
2213                 cfs_mem_cache_destroy(ldlm_resource_slab);
2214                 return -ENOMEM;
2215         }
2216
2217         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2218                                         sizeof(struct ldlm_interval),
2219                                         0, SLAB_HWCACHE_ALIGN);
2220         if (ldlm_interval_slab == NULL) {
2221                 cfs_mem_cache_destroy(ldlm_resource_slab);
2222                 cfs_mem_cache_destroy(ldlm_lock_slab);
2223                 return -ENOMEM;
2224         }
2225
2226         return 0;
2227 }
2228
2229 void __exit ldlm_exit(void)
2230 {
2231         int rc;
2232         if (ldlm_refcount)
2233                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2234         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2235         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2236         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2237         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2238         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2239         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2240 }
2241
2242 /* ldlm_extent.c */
2243 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2244
2245 /* ldlm_lock.c */
2246 EXPORT_SYMBOL(ldlm_get_processing_policy);
2247 EXPORT_SYMBOL(ldlm_lock2desc);
2248 EXPORT_SYMBOL(ldlm_register_intent);
2249 EXPORT_SYMBOL(ldlm_lockname);
2250 EXPORT_SYMBOL(ldlm_typename);
2251 EXPORT_SYMBOL(ldlm_lock2handle);
2252 EXPORT_SYMBOL(__ldlm_handle2lock);
2253 EXPORT_SYMBOL(ldlm_lock_get);
2254 EXPORT_SYMBOL(ldlm_lock_put);
2255 EXPORT_SYMBOL(ldlm_lock_match);
2256 EXPORT_SYMBOL(ldlm_lock_cancel);
2257 EXPORT_SYMBOL(ldlm_lock_addref);
2258 EXPORT_SYMBOL(ldlm_lock_decref);
2259 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2260 EXPORT_SYMBOL(ldlm_lock_change_resource);
2261 EXPORT_SYMBOL(ldlm_lock_set_data);
2262 EXPORT_SYMBOL(ldlm_it2str);
2263 EXPORT_SYMBOL(ldlm_lock_dump);
2264 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2265 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2266 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2267 EXPORT_SYMBOL(ldlm_lock_allow_match);
2268
2269 /* ldlm_request.c */
2270 EXPORT_SYMBOL(ldlm_completion_ast);
2271 EXPORT_SYMBOL(ldlm_blocking_ast);
2272 EXPORT_SYMBOL(ldlm_glimpse_ast);
2273 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2274 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2275 EXPORT_SYMBOL(ldlm_prep_elc_req);
2276 EXPORT_SYMBOL(ldlm_cli_convert);
2277 EXPORT_SYMBOL(ldlm_cli_enqueue);
2278 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2279 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2280 EXPORT_SYMBOL(ldlm_cli_cancel);
2281 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2282 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2283 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2284 EXPORT_SYMBOL(ldlm_cli_join_lru);
2285 EXPORT_SYMBOL(ldlm_replay_locks);
2286 EXPORT_SYMBOL(ldlm_resource_foreach);
2287 EXPORT_SYMBOL(ldlm_namespace_foreach);
2288 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2289 EXPORT_SYMBOL(ldlm_resource_iterate);
2290 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2291 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2292
2293 /* ldlm_lockd.c */
2294 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2295 EXPORT_SYMBOL(ldlm_server_completion_ast);
2296 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2297 EXPORT_SYMBOL(ldlm_handle_enqueue);
2298 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2299 EXPORT_SYMBOL(ldlm_handle_cancel);
2300 EXPORT_SYMBOL(ldlm_request_cancel);
2301 EXPORT_SYMBOL(ldlm_handle_convert);
2302 EXPORT_SYMBOL(ldlm_handle_convert0);
2303 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2304 EXPORT_SYMBOL(ldlm_get_ref);
2305 EXPORT_SYMBOL(ldlm_put_ref);
2306 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2307 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2308
2309 /* ldlm_resource.c */
2310 EXPORT_SYMBOL(ldlm_namespace_new);
2311 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2312 EXPORT_SYMBOL(ldlm_namespace_free);
2313 EXPORT_SYMBOL(ldlm_namespace_dump);
2314 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2315 EXPORT_SYMBOL(ldlm_resource_get);
2316 EXPORT_SYMBOL(ldlm_resource_putref);
2317 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2318
2319 /* ldlm_lib.c */
2320 EXPORT_SYMBOL(client_import_add_conn);
2321 EXPORT_SYMBOL(client_import_del_conn);
2322 EXPORT_SYMBOL(client_obd_setup);
2323 EXPORT_SYMBOL(client_obd_cleanup);
2324 EXPORT_SYMBOL(client_connect_import);
2325 EXPORT_SYMBOL(client_disconnect_export);
2326 EXPORT_SYMBOL(target_start_recovery_thread);
2327 EXPORT_SYMBOL(target_stop_recovery_thread);
2328 EXPORT_SYMBOL(target_handle_connect);
2329 EXPORT_SYMBOL(target_cleanup_recovery);
2330 EXPORT_SYMBOL(target_destroy_export);
2331 EXPORT_SYMBOL(target_cancel_recovery_timer);
2332 EXPORT_SYMBOL(target_send_reply);
2333 EXPORT_SYMBOL(target_queue_recovery_request);
2334 EXPORT_SYMBOL(target_handle_ping);
2335 EXPORT_SYMBOL(target_pack_pool_reply);
2336 EXPORT_SYMBOL(target_handle_disconnect);
2337
2338 /* l_lock.c */
2339 EXPORT_SYMBOL(lock_res_and_lock);
2340 EXPORT_SYMBOL(unlock_res_and_lock);
2341