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