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