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