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