Whamcloud - gitweb
9f2b1d5d474290f59fe14c43d4b747da382fe286
[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 extern cfs_mem_cache_t *ldlm_resource_slab;
44 extern cfs_mem_cache_t *ldlm_lock_slab;
45 extern struct lustre_lock ldlm_handle_lock;
46 extern struct list_head ldlm_namespace_list;
47
48 extern struct semaphore ldlm_namespace_lock;
49 static struct semaphore ldlm_ref_sem;
50 static int ldlm_refcount;
51
52 /* LDLM state */
53
54 static struct ldlm_state *ldlm_state;
55
56 inline cfs_time_t round_timeout(cfs_time_t timeout)
57 {
58         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
59 }
60
61 /* timeout for initial callback (AST) reply */
62 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
63                                                unsigned int obd_timeout)
64 {
65         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
66
67         return timeout < 1 ? 1 : timeout;
68 }
69
70 #ifdef __KERNEL__
71 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
72 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
73 static struct list_head waiting_locks_list;
74 static cfs_timer_t waiting_locks_timer;
75
76 static struct expired_lock_thread {
77         cfs_waitq_t               elt_waitq;
78         int                       elt_state;
79         int                       elt_dump;
80         struct list_head          elt_expired_locks;
81 } expired_lock_thread;
82 #endif
83
84 #define ELT_STOPPED   0
85 #define ELT_READY     1
86 #define ELT_TERMINATE 2
87
88 struct ldlm_bl_pool {
89         spinlock_t              blp_lock;
90         struct list_head        blp_list;
91         cfs_waitq_t             blp_waitq;
92         atomic_t                blp_num_threads;
93         struct completion       blp_comp;
94 };
95
96 struct ldlm_bl_work_item {
97         struct list_head        blwi_entry;
98         struct ldlm_namespace   *blwi_ns;
99         struct ldlm_lock_desc   blwi_ld;
100         struct ldlm_lock        *blwi_lock;
101 };
102
103 #ifdef __KERNEL__
104
105 static inline int have_expired_locks(void)
106 {
107         int need_to_run;
108
109         ENTRY;
110         spin_lock_bh(&waiting_locks_spinlock);
111         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
112         spin_unlock_bh(&waiting_locks_spinlock);
113
114         RETURN(need_to_run);
115 }
116
117 static int expired_lock_main(void *arg)
118 {
119         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
120         struct l_wait_info lwi = { 0 };
121         int do_dump;
122
123         ENTRY;
124         cfs_daemonize("ldlm_elt");
125
126         expired_lock_thread.elt_state = ELT_READY;
127         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
128
129         while (1) {
130                 l_wait_event(expired_lock_thread.elt_waitq,
131                              have_expired_locks() ||
132                              expired_lock_thread.elt_state == ELT_TERMINATE,
133                              &lwi);
134
135                 spin_lock_bh(&waiting_locks_spinlock);
136                 if (expired_lock_thread.elt_dump) {
137                         spin_unlock_bh(&waiting_locks_spinlock);
138
139                         /* from waiting_locks_callback, but not in timer */
140                         libcfs_debug_dumplog();
141                         libcfs_run_lbug_upcall(__FILE__,
142                                                 "waiting_locks_callback",
143                                                 expired_lock_thread.elt_dump);
144
145                         spin_lock_bh(&waiting_locks_spinlock);
146                         expired_lock_thread.elt_dump = 0;
147                 }
148
149                 do_dump = 0;
150
151                 while (!list_empty(expired)) {
152                         struct obd_export *export;
153                         struct ldlm_lock *lock;
154
155                         lock = list_entry(expired->next, struct ldlm_lock,
156                                           l_pending_chain);
157                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
158                             (void *)lock >= LP_POISON) {
159                                 spin_unlock_bh(&waiting_locks_spinlock);
160                                 CERROR("free lock on elt list %p\n", lock);
161                                 LBUG();
162                         }
163                         list_del_init(&lock->l_pending_chain);
164                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
165                             (void *)lock->l_export >= LP_POISON) {
166                                 CERROR("lock with free export on elt list %p\n",
167                                        lock->l_export);
168                                 lock->l_export = NULL;
169                                 LDLM_ERROR(lock, "free export");
170                                 continue;
171                         }
172                         export = class_export_get(lock->l_export);
173                         spin_unlock_bh(&waiting_locks_spinlock);
174
175                         do_dump++;
176                         class_fail_export(export);
177                         class_export_put(export);
178                         spin_lock_bh(&waiting_locks_spinlock);
179                 }
180                 spin_unlock_bh(&waiting_locks_spinlock);
181
182                 if (do_dump && obd_dump_on_eviction) {
183                         CERROR("dump the log upon eviction\n");
184                         libcfs_debug_dumplog();
185                 }
186
187                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
188                         break;
189         }
190
191         expired_lock_thread.elt_state = ELT_STOPPED;
192         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
193         RETURN(0);
194 }
195
196 /* This is called from within a timer interrupt and cannot schedule */
197 static void waiting_locks_callback(unsigned long unused)
198 {
199         struct ldlm_lock *lock, *last = NULL;
200
201         spin_lock_bh(&waiting_locks_spinlock);
202         while (!list_empty(&waiting_locks_list)) {
203                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
204                                   l_pending_chain);
205
206                 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
207                     (lock->l_req_mode == LCK_GROUP))
208                         break;
209
210                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
211                            "%s@%s nid %s ",lock->l_export->exp_client_uuid.uuid,
212                            lock->l_export->exp_connection->c_remote_uuid.uuid,
213                            libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
214
215                 if (lock == last) {
216                         LDLM_ERROR(lock, "waiting on lock multiple times");
217                         CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
218                                &waiting_locks_list,
219                                waiting_locks_list.next, waiting_locks_list.prev,
220                                &lock->l_pending_chain,
221                                lock->l_pending_chain.next,
222                                lock->l_pending_chain.prev);
223
224                         CFS_INIT_LIST_HEAD(&waiting_locks_list);    /* HACK */
225                         expired_lock_thread.elt_dump = __LINE__;
226
227                         /* LBUG(); */
228                         CEMERG("would be an LBUG, but isn't (bug 5653)\n");
229                         libcfs_debug_dumpstack(NULL);
230                         /*blocks* libcfs_debug_dumplog(); */
231                         /*blocks* libcfs_run_lbug_upcall(file, func, line); */
232                         break;
233                 }
234                 last = lock;
235
236                 list_del(&lock->l_pending_chain);
237                 list_add(&lock->l_pending_chain,
238                          &expired_lock_thread.elt_expired_locks);
239         }
240
241         if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
242                 if (obd_dump_on_timeout)
243                         expired_lock_thread.elt_dump = __LINE__;
244
245                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
246         }
247
248         /*
249          * Make sure the timer will fire again if we have any locks
250          * left.
251          */
252         if (!list_empty(&waiting_locks_list)) {
253                 cfs_time_t timeout_rounded;
254                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
255                                   l_pending_chain);
256                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
257                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
258         }
259         spin_unlock_bh(&waiting_locks_spinlock);
260 }
261
262 /*
263  * Indicate that we're waiting for a client to call us back cancelling a given
264  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
265  * timer to fire appropriately.  (We round up to the next second, to avoid
266  * floods of timer firings during periods of high lock contention and traffic).
267  *
268  * Called with the namespace lock held.
269  */
270 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
271 {
272         cfs_time_t timeout_rounded;
273
274         if (!list_empty(&lock->l_pending_chain))
275                 return 0;
276
277         lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
278                                                cfs_time_seconds(obd_timeout)/2);
279
280         timeout_rounded = round_timeout(lock->l_callback_timeout);
281
282         if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
283             !cfs_timer_is_armed(&waiting_locks_timer)) {
284                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
285
286         }
287         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
288         return 1;
289 }
290
291 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
292 {
293         int ret;
294
295         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
296
297         spin_lock_bh(&waiting_locks_spinlock);
298         if (lock->l_destroyed) {
299                 static cfs_time_t next;
300                 spin_unlock_bh(&waiting_locks_spinlock);
301                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
302                 if (cfs_time_after(cfs_time_current(), next)) {
303                         next = cfs_time_shift(14400);
304                         libcfs_debug_dumpstack(NULL);
305                 }
306                 return 0;
307         }
308
309         ret = __ldlm_add_waiting_lock(lock);
310         spin_unlock_bh(&waiting_locks_spinlock);
311
312         LDLM_DEBUG(lock, "%sadding to wait list",
313                    ret == 0 ? "not re-" : "");
314         return ret;
315 }
316
317 /*
318  * Remove a lock from the pending list, likely because it had its cancellation
319  * callback arrive without incident.  This adjusts the lock-timeout timer if
320  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
321  *
322  * Called with namespace lock held.
323  */
324 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
325 {
326         struct list_head *list_next;
327
328         if (list_empty(&lock->l_pending_chain))
329                 return 0;
330
331         list_next = lock->l_pending_chain.next;
332         if (lock->l_pending_chain.prev == &waiting_locks_list) {
333                 /* Removing the head of the list, adjust timer. */
334                 if (list_next == &waiting_locks_list) {
335                         /* No more, just cancel. */
336                         cfs_timer_disarm(&waiting_locks_timer);
337                 } else {
338                         struct ldlm_lock *next;
339                         next = list_entry(list_next, struct ldlm_lock,
340                                           l_pending_chain);
341                         cfs_timer_arm(&waiting_locks_timer,
342                                       round_timeout(next->l_callback_timeout));
343                 }
344         }
345         list_del_init(&lock->l_pending_chain);
346
347         return 1;
348 }
349
350 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
351 {
352         int ret;
353
354         if (lock->l_export == NULL) {
355                 /* We don't have a "waiting locks list" on clients. */
356                 LDLM_DEBUG(lock, "client lock: no-op");
357                 return 0;
358         }
359
360         spin_lock_bh(&waiting_locks_spinlock);
361         ret = __ldlm_del_waiting_lock(lock);
362         spin_unlock_bh(&waiting_locks_spinlock);
363
364         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
365         return ret;
366 }
367
368 /*
369  * Prolong the lock
370  * 
371  * Called with namespace lock held.
372  */
373 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
374 {
375         if (lock->l_export == NULL) {
376                 /* We don't have a "waiting locks list" on clients. */
377                 LDLM_DEBUG(lock, "client lock: no-op");
378                 return 0;
379         }
380
381         spin_lock_bh(&waiting_locks_spinlock);
382
383         if (list_empty(&lock->l_pending_chain)) {
384                 spin_unlock_bh(&waiting_locks_spinlock);
385                 LDLM_DEBUG(lock, "wasn't waiting");
386                 return 0;
387         }
388
389         __ldlm_del_waiting_lock(lock);
390         __ldlm_add_waiting_lock(lock);
391         spin_unlock_bh(&waiting_locks_spinlock);
392
393         LDLM_DEBUG(lock, "refreshed");
394         return 1;
395 }
396
397 #else /* !__KERNEL__ */
398
399 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
400 {
401         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
402         RETURN(1);
403 }
404
405 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
406 {
407         RETURN(0);
408 }
409
410 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
411 {
412         RETURN(0);
413 }
414 #endif /* __KERNEL__ */
415
416 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
417                             const char *ast_type)
418 {
419         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
420         char                     *str = libcfs_nid2str(conn->c_peer.nid);
421
422         LCONSOLE_ERROR("A client on nid %s was evicted from service %s.\n",
423                        str, lock->l_export->exp_obd->obd_name);
424
425         LDLM_ERROR(lock, "%s AST failed (%d): evicting client %s@%s NID %s"
426                    " (%s)", ast_type, rc, lock->l_export->exp_client_uuid.uuid,
427                    conn->c_remote_uuid.uuid, libcfs_nid2str(conn->c_peer.nid),
428                    str);
429
430         if (obd_dump_on_timeout)
431                 libcfs_debug_dumplog();
432         class_fail_export(lock->l_export);
433 }
434
435 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
436                                  struct ptlrpc_request *req, int rc,
437                                  const char *ast_type)
438 {
439         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
440
441         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
442                 LASSERT(lock->l_export);
443                 if (lock->l_export->exp_libclient) {
444                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
445                                    " timeout, just cancelling lock", ast_type,
446                                    libcfs_nid2str(peer.nid));
447                         ldlm_lock_cancel(lock);
448                         rc = -ERESTART;
449                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
450                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
451                                    "cancel was received (AST reply lost?)",
452                                    ast_type, libcfs_nid2str(peer.nid));
453                         ldlm_lock_cancel(lock);
454                         rc = -ERESTART;
455                 } else {
456                         ldlm_del_waiting_lock(lock);
457                         ldlm_failed_ast(lock, rc, ast_type);
458                 }
459         } else if (rc) {
460                 if (rc == -EINVAL)
461                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
462                                    " from %s AST - normal race",
463                                    libcfs_nid2str(peer.nid),
464                                    lustre_msg_get_status(req->rq_repmsg),
465                                    ast_type);
466                 else
467                         LDLM_ERROR(lock, "client (nid %s) returned %d "
468                                    "from %s AST", libcfs_nid2str(peer.nid),
469                                    (req->rq_repmsg != NULL) ?
470                                    lustre_msg_get_status(req->rq_repmsg) : 0,
471                                    ast_type);
472                 ldlm_lock_cancel(lock);
473                 /* Server-side AST functions are called from ldlm_reprocess_all,
474                  * which needs to be told to please restart its reprocessing. */
475                 rc = -ERESTART;
476         }
477
478         return rc;
479 }
480
481 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
482                              struct ldlm_lock_desc *desc,
483                              void *data, int flag)
484 {
485         struct ldlm_request *body;
486         struct ptlrpc_request *req;
487         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
488                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
489         int instant_cancel = 0, rc = 0;
490         ENTRY;
491
492         if (flag == LDLM_CB_CANCELING) {
493                 /* Don't need to do anything here. */
494                 RETURN(0);
495         }
496
497         LASSERT(lock);
498
499         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
500                               LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
501                               NULL);
502         if (req == NULL)
503                 RETURN(-ENOMEM);
504
505         lock_res(lock->l_resource);
506         if (lock->l_granted_mode != lock->l_req_mode) {
507                 /* this blocking AST will be communicated as part of the
508                  * completion AST instead */
509                 unlock_res(lock->l_resource);
510                 ptlrpc_req_finished(req);
511                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
512                 RETURN(0);
513         }
514
515         if (lock->l_destroyed) {
516                 /* What's the point? */
517                 unlock_res(lock->l_resource);
518                 ptlrpc_req_finished(req);
519                 RETURN(0);
520         }
521
522 #if 0
523         if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
524                 unlock_res(lock->l_resource);
525                 ptlrpc_req_finished(req);
526                 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
527                 RETURN(-ETIMEDOUT);
528         }
529 #endif
530
531         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
532                 instant_cancel = 1;
533
534         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
535         body->lock_handle1 = lock->l_remote_handle;
536         body->lock_desc = *desc;
537         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
538
539         LDLM_DEBUG(lock, "server preparing blocking AST");
540
541         ptlrpc_req_set_repsize(req, 1, NULL);
542         if (instant_cancel) {
543                 unlock_res(lock->l_resource);
544                 ldlm_lock_cancel(lock);
545         } else if (lock->l_granted_mode == lock->l_req_mode) {
546                 ldlm_add_waiting_lock(lock);
547                 unlock_res(lock->l_resource);
548         }
549
550         req->rq_send_state = LUSTRE_IMP_FULL;
551         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
552
553         if (lock->l_export && lock->l_export->exp_ldlm_stats)
554                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
555                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
556
557         if (unlikely(instant_cancel)) {
558                 rc = ptl_send_rpc(req, 1);
559         } else {
560                 rc = ptlrpc_queue_wait(req);
561                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
562         }
563         if (rc != 0)
564                 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
565
566         ptlrpc_req_finished(req);
567
568         /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
569         if (!rc && instant_cancel)
570                 rc = -ERESTART;
571
572         RETURN(rc);
573 }
574
575 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
576 {
577         struct ldlm_request *body;
578         struct ptlrpc_request *req;
579         struct timeval granted_time;
580         long total_enqueue_wait;
581         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
582                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
583         int rc = 0, buffers = 2, instant_cancel = 0;
584         ENTRY;
585
586         LASSERT(lock != NULL);
587
588         do_gettimeofday(&granted_time);
589         total_enqueue_wait = cfs_timeval_sub(&granted_time,
590                                              &lock->l_enqueued_time, NULL);
591
592         if (total_enqueue_wait / 1000000 > obd_timeout)
593                 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
594                            total_enqueue_wait, lock->l_enqueued_time.tv_sec);
595
596         lock_res_and_lock(lock);
597         if (lock->l_resource->lr_lvb_len) {
598                 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
599                 buffers = 3;
600         }
601         unlock_res_and_lock(lock);
602
603         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
604                               LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
605                               size, NULL);
606         if (req == NULL)
607                 RETURN(-ENOMEM);
608
609         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
610         body->lock_handle1 = lock->l_remote_handle;
611         body->lock_flags = flags;
612         ldlm_lock2desc(lock, &body->lock_desc);
613
614         if (buffers == 3) {
615                 void *lvb;
616
617                 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
618                                      lock->l_resource->lr_lvb_len);
619                 lock_res_and_lock(lock);
620                 memcpy(lvb, lock->l_resource->lr_lvb_data,
621                        lock->l_resource->lr_lvb_len);
622                 unlock_res_and_lock(lock);
623         }
624
625         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
626                    total_enqueue_wait);
627
628         ptlrpc_req_set_repsize(req, 1, NULL);
629
630         req->rq_send_state = LUSTRE_IMP_FULL;
631         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
632
633         /* We only send real blocking ASTs after the lock is granted */
634         lock_res_and_lock(lock);
635         if (lock->l_flags & LDLM_FL_AST_SENT) {
636                 body->lock_flags |= LDLM_FL_AST_SENT;
637
638                 /* We might get here prior to ldlm_handle_enqueue setting
639                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
640                  * into waiting list, but this is safe and similar code in
641                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
642                  * that would not only cancel the lock, but will also remove
643                  * it from waiting list */
644                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
645                         unlock_res_and_lock(lock);
646                         ldlm_lock_cancel(lock);
647                         instant_cancel = 1;
648                         lock_res_and_lock(lock);
649                 } else {
650                         ldlm_add_waiting_lock(lock); /* start the lock-timeout
651                                                          clock */
652                 }
653         }
654         unlock_res_and_lock(lock);
655
656         if (lock->l_export && lock->l_export->exp_ldlm_stats)
657                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
658                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
659         
660         rc = ptlrpc_queue_wait(req);
661         if (rc != 0)
662                 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
663
664         ptlrpc_req_finished(req);
665
666         /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
667         if (!rc && instant_cancel)
668                 rc = -ERESTART;
669
670         RETURN(rc);
671 }
672
673 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
674 {
675         struct ldlm_resource *res = lock->l_resource;
676         struct ldlm_request *body;
677         struct ptlrpc_request *req;
678         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
679                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
680         int rc = 0;
681         ENTRY;
682
683         LASSERT(lock != NULL);
684
685         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
686                               LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
687                               NULL);
688         if (req == NULL)
689                 RETURN(-ENOMEM);
690
691         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
692         body->lock_handle1 = lock->l_remote_handle;
693         ldlm_lock2desc(lock, &body->lock_desc);
694
695         lock_res_and_lock(lock);
696         size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
697         unlock_res_and_lock(lock);
698         res = lock->l_resource;
699         ptlrpc_req_set_repsize(req, 2, size);
700
701         req->rq_send_state = LUSTRE_IMP_FULL;
702         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
703
704         if (lock->l_export && lock->l_export->exp_ldlm_stats)
705                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
706                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
707
708         rc = ptlrpc_queue_wait(req);
709         if (rc == -ELDLM_NO_LOCK_DATA)
710                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
711         else if (rc != 0)
712                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
713         else
714                 rc = res->lr_namespace->ns_lvbo->lvbo_update
715                         (res, req->rq_repmsg, REPLY_REC_OFF, 1);
716         ptlrpc_req_finished(req);
717         RETURN(rc);
718 }
719
720 static struct ldlm_lock *
721 find_existing_lock(struct obd_export *exp, struct lustre_handle *remote_hdl)
722 {
723         struct list_head *iter;
724
725         spin_lock(&exp->exp_ldlm_data.led_lock);
726         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
727                 struct ldlm_lock *lock;
728                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
729                 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
730                         LDLM_LOCK_GET(lock);
731                         spin_unlock(&exp->exp_ldlm_data.led_lock);
732                         return lock;
733                 }
734         }
735         spin_unlock(&exp->exp_ldlm_data.led_lock);
736         return NULL;
737 }
738
739
740 /*
741  * Main server-side entry point into LDLM. This is called by ptlrpc service
742  * threads to carry out client lock enqueueing requests.
743  */
744 int ldlm_handle_enqueue(struct ptlrpc_request *req,
745                         ldlm_completion_callback completion_callback,
746                         ldlm_blocking_callback blocking_callback,
747                         ldlm_glimpse_callback glimpse_callback)
748 {
749         struct obd_device *obddev = req->rq_export->exp_obd;
750         struct ldlm_reply *dlm_rep;
751         struct ldlm_request *dlm_req;
752         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
753                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
754         int rc = 0;
755         __u32 flags;
756         ldlm_error_t err = ELDLM_OK;
757         struct ldlm_lock *lock = NULL;
758         void *cookie = NULL;
759         ENTRY;
760
761         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
762
763         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
764                                      lustre_swab_ldlm_request);
765         if (dlm_req == NULL) {
766                 CERROR ("Can't unpack dlm_req\n");
767                 GOTO(out, rc = -EFAULT);
768         }
769
770         flags = dlm_req->lock_flags;
771
772         LASSERT(req->rq_export);
773         
774         if (req->rq_export->exp_ldlm_stats)
775                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
776                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
777
778         if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
779             dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
780                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d\n",
781                           dlm_req->lock_desc.l_resource.lr_type);
782                 GOTO(out, rc = -EFAULT);
783         }
784
785         if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
786             dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
787             dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
788                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d\n",
789                           dlm_req->lock_desc.l_req_mode);
790                 GOTO(out, rc = -EFAULT);
791         }
792
793         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
794                 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
795                         DEBUG_REQ(D_ERROR, req,
796                                   "PLAIN lock request from IBITS client?\n");
797                         GOTO(out, rc = -EPROTO);
798                 }
799         } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
800                 DEBUG_REQ(D_ERROR, req,
801                           "IBITS lock request from unaware client?\n");
802                 GOTO(out, rc = -EPROTO);
803         }
804
805 #if 0
806         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check 
807            against server's _CONNECT_SUPPORTED flags? (I don't want to use
808            ibits for mgc/mgs) */
809
810         /* INODEBITS_INTEROP: Perform conversion from plain lock to
811          * inodebits lock if client does not support them. */
812         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
813             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
814                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
815                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
816                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
817                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
818                         dlm_req->lock_desc.l_req_mode = LCK_CR;
819         }
820 #endif
821
822         if (flags & LDLM_FL_REPLAY) {
823                 lock = find_existing_lock(req->rq_export,
824                                           &dlm_req->lock_handle1);
825                 if (lock != NULL) {
826                         DEBUG_REQ(D_HA, req, "found existing lock cookie "LPX64,
827                                   lock->l_handle.h_cookie);
828                         GOTO(existing_lock, rc = 0);
829                 }
830         }
831
832         /* The lock's callback data might be set in the policy function */
833         lock = ldlm_lock_create(obddev->obd_namespace, &dlm_req->lock_handle2,
834                                 dlm_req->lock_desc.l_resource.lr_name,
835                                 dlm_req->lock_desc.l_resource.lr_type,
836                                 dlm_req->lock_desc.l_req_mode,
837                                 blocking_callback, completion_callback,
838                                 glimpse_callback, NULL, 0);
839         if (!lock)
840                 GOTO(out, rc = -ENOMEM);
841
842         do_gettimeofday(&lock->l_enqueued_time);
843         lock->l_remote_handle = dlm_req->lock_handle1;
844         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
845
846         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
847         /* Don't enqueue a lock onto the export if it has already
848          * been evicted.  Cancel it now instead. (bug 3822) */
849         if (req->rq_export->exp_failed) {
850                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
851                 GOTO(out, rc = -ENOTCONN);
852         }
853         lock->l_export = class_export_get(req->rq_export);
854         spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
855         list_add(&lock->l_export_chain,
856                  &lock->l_export->exp_ldlm_data.led_held_locks);
857         spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
858
859 existing_lock:
860
861         if (flags & LDLM_FL_HAS_INTENT) {
862                 /* In this case, the reply buffer is allocated deep in
863                  * local_lock_enqueue by the policy function. */
864                 cookie = req;
865         } else {
866                 int buffers = 2;
867
868                 lock_res_and_lock(lock);
869                 if (lock->l_resource->lr_lvb_len) {
870                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
871                         buffers = 3;
872                 }
873                 unlock_res_and_lock(lock);
874
875                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
876                         GOTO(out, rc = -ENOMEM);
877
878                 rc = lustre_pack_reply(req, buffers, size, NULL);
879                 if (rc)
880                         GOTO(out, rc);
881         }
882
883         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
884                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
885         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
886                 lock->l_req_extent = lock->l_policy_data.l_extent;
887
888         err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, &flags);
889         if (err)
890                 GOTO(out, err);
891
892         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
893                                  sizeof(*dlm_rep));
894         dlm_rep->lock_flags = flags;
895
896         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
897         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
898
899         /* We never send a blocking AST until the lock is granted, but
900          * we can tell it right now */
901         lock_res_and_lock(lock);
902
903         /* Now take into account flags to be inherited from original lock
904            request both in reply to client and in our own lock flags. */
905         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
906         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
907
908         /* Don't move a pending lock onto the export if it has already
909          * been evicted.  Cancel it now instead. (bug 5683) */
910         if (req->rq_export->exp_failed ||
911             OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
912                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
913                 rc = -ENOTCONN;
914         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
915                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
916                 if (lock->l_granted_mode == lock->l_req_mode) {
917                         /* Only cancel lock if it was granted, because it
918                          * would be destroyed immediatelly and would never
919                          * be granted in the future, causing timeouts on client.
920                          * Not granted lock will be cancelled immediatelly after
921                          * sending completion AST.
922                          */
923                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
924                                 unlock_res_and_lock(lock);
925                                 ldlm_lock_cancel(lock);
926                                 lock_res_and_lock(lock);
927                         } else if (lock->l_granted_mode == lock->l_req_mode)
928                                 ldlm_add_waiting_lock(lock);
929                 }
930         }
931         /* Make sure we never ever grant usual metadata locks to liblustre
932            clients */
933         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
934             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
935              req->rq_export->exp_libclient) {
936                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
937                     !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
938                         CERROR("Granting sync lock to libclient. "
939                                "req fl %d, rep fl %d, lock fl %d\n",
940                                dlm_req->lock_flags, dlm_rep->lock_flags,
941                                lock->l_flags);
942                         LDLM_ERROR(lock, "sync lock");
943                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
944                                 struct ldlm_intent *it;
945                                 it = lustre_msg_buf(req->rq_reqmsg,
946                                                     DLM_INTENT_IT_OFF,
947                                                     sizeof(*it));
948                                 if (it != NULL) {
949                                         CERROR("This is intent %s ("LPU64")\n",
950                                                ldlm_it2str(it->opc), it->opc);
951                                 }
952                         }
953                 }
954         }
955
956         unlock_res_and_lock(lock);
957
958         EXIT;
959  out:
960         req->rq_status = err;
961         if (req->rq_reply_state == NULL) {
962                 err = lustre_pack_reply(req, 1, NULL, NULL);
963                 if (rc == 0)
964                         rc = err;
965                 req->rq_status = rc;
966         }
967
968         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
969          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
970         if (lock) {
971                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
972                            "(err=%d, rc=%d)", err, rc);
973
974                 if (rc == 0) {
975                         lock_res_and_lock(lock);
976                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
977                         if (size[DLM_REPLY_REC_OFF] > 0) {
978                                 void *lvb = lustre_msg_buf(req->rq_repmsg,
979                                                        DLM_REPLY_REC_OFF,
980                                                        size[DLM_REPLY_REC_OFF]);
981                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
982                                          req, lock);
983
984                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
985                                        size[DLM_REPLY_REC_OFF]);
986                         }
987                         unlock_res_and_lock(lock);
988                 } else {
989                         lock_res_and_lock(lock);
990                         ldlm_resource_unlink_lock(lock);
991                         ldlm_lock_destroy_nolock(lock);
992                         unlock_res_and_lock(lock);
993                 }
994
995                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
996                         ldlm_reprocess_all(lock->l_resource);
997
998                 LDLM_LOCK_PUT(lock);
999         }
1000
1001         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1002                           lock, rc);
1003
1004         return rc;
1005 }
1006
1007 int ldlm_handle_convert(struct ptlrpc_request *req)
1008 {
1009         struct ldlm_request *dlm_req;
1010         struct ldlm_reply *dlm_rep;
1011         struct ldlm_lock *lock;
1012         int rc;
1013         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1014                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
1015         ENTRY;
1016
1017         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1018                                      lustre_swab_ldlm_request);
1019         if (dlm_req == NULL) {
1020                 CERROR ("Can't unpack dlm_req\n");
1021                 RETURN (-EFAULT);
1022         }
1023
1024         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1025                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1026                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1027
1028         rc = lustre_pack_reply(req, 2, size, NULL);
1029         if (rc) {
1030                 CERROR("out of memory\n");
1031                 RETURN(-ENOMEM);
1032         }
1033         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1034                                  sizeof(*dlm_rep));
1035         dlm_rep->lock_flags = dlm_req->lock_flags;
1036
1037         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1038         if (!lock) {
1039                 req->rq_status = EINVAL;
1040         } else {
1041                 void *res = NULL;
1042
1043                 LDLM_DEBUG(lock, "server-side convert handler START");
1044
1045                 do_gettimeofday(&lock->l_enqueued_time);
1046                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1047                                         &dlm_rep->lock_flags);
1048                 if (res) {
1049                         if (ldlm_del_waiting_lock(lock))
1050                                 LDLM_DEBUG(lock, "converted waiting lock");
1051                         req->rq_status = 0;
1052                 } else {
1053                         req->rq_status = EDEADLOCK;
1054                 }
1055         }
1056
1057         if (lock) {
1058                 if (!req->rq_status)
1059                         ldlm_reprocess_all(lock->l_resource);
1060                 LDLM_DEBUG(lock, "server-side convert handler END");
1061                 LDLM_LOCK_PUT(lock);
1062         } else
1063                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1064
1065         RETURN(0);
1066 }
1067
1068 int ldlm_handle_cancel(struct ptlrpc_request *req)
1069 {
1070         struct ldlm_request *dlm_req;
1071         struct ldlm_lock *lock;
1072         struct ldlm_resource *res;
1073         int rc;
1074         ENTRY;
1075
1076         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1077                                      lustre_swab_ldlm_request);
1078         if (dlm_req == NULL) {
1079                 CERROR("bad request buffer for cancel\n");
1080                 RETURN(-EFAULT);
1081         }
1082
1083         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1084                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1085                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1086
1087         rc = lustre_pack_reply(req, 1, NULL, NULL);
1088         if (rc) {
1089                 CERROR("out of memory\n");
1090                 RETURN(-ENOMEM);
1091         }
1092
1093         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1094         if (!lock) {
1095                 CERROR("received cancel for unknown lock cookie "LPX64
1096                        " from client %s id %s\n",
1097                        dlm_req->lock_handle1.cookie,
1098                        req->rq_export->exp_client_uuid.uuid,
1099                        libcfs_id2str(req->rq_peer));
1100                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
1101                                   "(cookie "LPU64")",
1102                                   dlm_req->lock_handle1.cookie);
1103                 req->rq_status = ESTALE;
1104         } else {
1105                 LDLM_DEBUG(lock, "server-side cancel handler START");
1106                 res = lock->l_resource;
1107                 if (res && res->lr_namespace->ns_lvbo &&
1108                     res->lr_namespace->ns_lvbo->lvbo_update) {
1109                         (void)res->lr_namespace->ns_lvbo->lvbo_update
1110                                 (res, NULL, 0, 1); 
1111                                 //(res, req->rq_reqmsg, 1, 1);
1112                 }
1113
1114                 ldlm_lock_cancel(lock);
1115                 req->rq_status = rc;
1116         }
1117
1118         if (ptlrpc_reply(req) != 0)
1119                 LBUG();
1120
1121         if (lock) {
1122                 ldlm_reprocess_all(lock->l_resource);
1123                 LDLM_DEBUG(lock, "server-side cancel handler END");
1124                 LDLM_LOCK_PUT(lock);
1125         }
1126
1127         RETURN(0);
1128 }
1129
1130 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1131                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1132 {
1133         int do_ast;
1134         ENTRY;
1135
1136         LDLM_DEBUG(lock, "client blocking AST callback handler START");
1137
1138         lock_res_and_lock(lock);
1139         lock->l_flags |= LDLM_FL_CBPENDING;
1140
1141         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1142                 lock->l_flags |= LDLM_FL_CANCEL;
1143
1144         do_ast = (!lock->l_readers && !lock->l_writers);
1145         unlock_res_and_lock(lock);
1146
1147         if (do_ast) {
1148                 LDLM_DEBUG(lock, "already unused, calling "
1149                            "callback (%p)", lock->l_blocking_ast);
1150                 if (lock->l_blocking_ast != NULL)
1151                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1152                                              LDLM_CB_BLOCKING);
1153         } else {
1154                 LDLM_DEBUG(lock, "Lock still has references, will be"
1155                            " cancelled later");
1156         }
1157
1158         LDLM_DEBUG(lock, "client blocking callback handler END");
1159         LDLM_LOCK_PUT(lock);
1160         EXIT;
1161 }
1162
1163 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1164                                     struct ldlm_namespace *ns,
1165                                     struct ldlm_request *dlm_req,
1166                                     struct ldlm_lock *lock)
1167 {
1168         CFS_LIST_HEAD(ast_list);
1169         ENTRY;
1170
1171         LDLM_DEBUG(lock, "client completion callback handler START");
1172
1173         lock_res_and_lock(lock);
1174
1175         /* If we receive the completion AST before the actual enqueue returned,
1176          * then we might need to switch lock modes, resources, or extents. */
1177         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1178                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1179                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1180         }
1181
1182         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1183                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1184                 LDLM_DEBUG(lock, "completion AST, new policy data");
1185         }
1186
1187         ldlm_resource_unlink_lock(lock);
1188         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1189                    &lock->l_resource->lr_name,
1190                    sizeof(lock->l_resource->lr_name)) != 0) {
1191                 unlock_res_and_lock(lock);
1192                 ldlm_lock_change_resource(ns, lock,
1193                                          dlm_req->lock_desc.l_resource.lr_name);
1194                 LDLM_DEBUG(lock, "completion AST, new resource");
1195                 CERROR("change resource!\n");
1196                 lock_res_and_lock(lock);
1197         }
1198
1199         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1200                 lock->l_flags |= LDLM_FL_CBPENDING;
1201                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1202         }
1203
1204         if (lock->l_lvb_len) {
1205                 void *lvb;
1206                 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1207                                          lock->l_lvb_swabber);
1208                 if (lvb == NULL) {
1209                         LDLM_ERROR(lock, "completion AST did not contain "
1210                                    "expected LVB!");
1211                 } else {
1212                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1213                 }
1214         }
1215
1216         ldlm_grant_lock(lock, &ast_list);
1217         unlock_res_and_lock(lock);
1218
1219         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1220
1221         ldlm_run_cp_ast_work(&ast_list);
1222
1223         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1224                           lock);
1225         LDLM_LOCK_PUT(lock);
1226         EXIT;
1227 }
1228
1229 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1230                                     struct ldlm_namespace *ns,
1231                                     struct ldlm_request *dlm_req,
1232                                     struct ldlm_lock *lock)
1233 {
1234         int rc = -ENOSYS;
1235         ENTRY;
1236
1237         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1238
1239         if (lock->l_glimpse_ast != NULL)
1240                 rc = lock->l_glimpse_ast(lock, req);
1241
1242         if (req->rq_repmsg != NULL) {
1243                 ptlrpc_reply(req);
1244         } else {
1245                 req->rq_status = rc;
1246                 ptlrpc_error(req);
1247         }
1248
1249         lock_res_and_lock(lock);
1250         if (lock->l_granted_mode == LCK_PW &&
1251             !lock->l_readers && !lock->l_writers &&
1252             cfs_time_after(cfs_time_current(), 
1253                            cfs_time_add(lock->l_last_used, cfs_time_seconds(10)))) {
1254                 unlock_res_and_lock(lock);
1255                 if (ldlm_bl_to_thread(ns, NULL, lock))
1256                         ldlm_handle_bl_callback(ns, NULL, lock);
1257
1258                 EXIT;
1259                 return;
1260         }
1261         unlock_res_and_lock(lock);
1262         LDLM_LOCK_PUT(lock);
1263         EXIT;
1264 }
1265
1266 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1267 {
1268         req->rq_status = rc;
1269         if (req->rq_reply_state == NULL) {
1270                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1271                 if (rc)
1272                         return rc;
1273         }
1274         return ptlrpc_reply(req);
1275 }
1276
1277 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1278                       struct ldlm_lock *lock)
1279 {
1280 #ifdef __KERNEL__
1281         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1282         struct ldlm_bl_work_item *blwi;
1283         ENTRY;
1284
1285         OBD_ALLOC(blwi, sizeof(*blwi));
1286         if (blwi == NULL)
1287                 RETURN(-ENOMEM);
1288
1289         blwi->blwi_ns = ns;
1290         if (ld != NULL)
1291                 blwi->blwi_ld = *ld;
1292         blwi->blwi_lock = lock;
1293
1294         spin_lock(&blp->blp_lock);
1295         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1296         cfs_waitq_signal(&blp->blp_waitq);
1297         spin_unlock(&blp->blp_lock);
1298
1299         RETURN(0);
1300 #else
1301         RETURN(-ENOSYS);
1302 #endif
1303 }
1304
1305 static int ldlm_callback_handler(struct ptlrpc_request *req)
1306 {
1307         struct ldlm_namespace *ns;
1308         struct ldlm_request *dlm_req;
1309         struct ldlm_lock *lock;
1310         int rc;
1311         ENTRY;
1312
1313         /* Requests arrive in sender's byte order.  The ptlrpc service
1314          * handler has already checked and, if necessary, byte-swapped the
1315          * incoming request message body, but I am responsible for the
1316          * message buffers. */
1317
1318         if (req->rq_export == NULL) {
1319                 struct ldlm_request *dlm_req;
1320
1321                 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1322                        "export cookie "LPX64"; this is "
1323                        "normal if this node rebooted with a lock held\n",
1324                        lustre_msg_get_opc(req->rq_reqmsg),
1325                        libcfs_id2str(req->rq_peer),
1326                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1327
1328                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1329                                              sizeof(*dlm_req),
1330                                              lustre_swab_ldlm_request);
1331                 if (dlm_req != NULL)
1332                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1333                                dlm_req->lock_handle1.cookie);
1334
1335                 ldlm_callback_reply(req, -ENOTCONN);
1336                 RETURN(0);
1337         }
1338
1339         LASSERT(req->rq_export != NULL);
1340         LASSERT(req->rq_export->exp_obd != NULL);
1341
1342         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1343         case LDLM_BL_CALLBACK:
1344                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1345                 break;
1346         case LDLM_CP_CALLBACK:
1347                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1348                 break;
1349         case LDLM_GL_CALLBACK:
1350                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1351                 break;
1352         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1353                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1354                 rc = llog_origin_handle_cancel(req);
1355                 ldlm_callback_reply(req, rc);
1356                 RETURN(0);
1357         case OBD_QC_CALLBACK:
1358                 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1359                 rc = target_handle_qc_callback(req);
1360                 ldlm_callback_reply(req, rc);
1361                 RETURN(0);
1362         case QUOTA_DQACQ:
1363         case QUOTA_DQREL:
1364                 /* reply in handler */
1365                 rc = target_handle_dqacq_callback(req);
1366                 RETURN(0);
1367         case LLOG_ORIGIN_HANDLE_CREATE:
1368                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1369                 rc = llog_origin_handle_create(req);
1370                 ldlm_callback_reply(req, rc);
1371                 RETURN(0);
1372         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1373                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1374                 rc = llog_origin_handle_next_block(req);
1375                 ldlm_callback_reply(req, rc);
1376                 RETURN(0);
1377         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1378                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1379                 rc = llog_origin_handle_read_header(req);
1380                 ldlm_callback_reply(req, rc);
1381                 RETURN(0);
1382         case LLOG_ORIGIN_HANDLE_CLOSE:
1383                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1384                 rc = llog_origin_handle_close(req);
1385                 ldlm_callback_reply(req, rc);
1386                 RETURN(0);
1387         default:
1388                 CERROR("unknown opcode %u\n",
1389                        lustre_msg_get_opc(req->rq_reqmsg));
1390                 ldlm_callback_reply(req, -EPROTO);
1391                 RETURN(0);
1392         }
1393
1394         ns = req->rq_export->exp_obd->obd_namespace;
1395         LASSERT(ns != NULL);
1396
1397         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1398                                      lustre_swab_ldlm_request);
1399         if (dlm_req == NULL) {
1400                 CERROR ("can't unpack dlm_req\n");
1401                 ldlm_callback_reply(req, -EPROTO);
1402                 RETURN (0);
1403         }
1404
1405         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle1);
1406         if (!lock) {
1407                 CDEBUG(D_INODE, "callback on lock "LPX64" - lock disappeared\n",
1408                        dlm_req->lock_handle1.cookie);
1409                 ldlm_callback_reply(req, -EINVAL);
1410                 RETURN(0);
1411         }
1412
1413         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1414         lock_res_and_lock(lock);
1415         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1416         unlock_res_and_lock(lock);
1417
1418         /* We want the ost thread to get this reply so that it can respond
1419          * to ost requests (write cache writeback) that might be triggered
1420          * in the callback.
1421          *
1422          * But we'd also like to be able to indicate in the reply that we're
1423          * cancelling right now, because it's unused, or have an intent result
1424          * in the reply, so we might have to push the responsibility for sending
1425          * the reply down into the AST handlers, alas. */
1426
1427         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1428         case LDLM_BL_CALLBACK:
1429                 CDEBUG(D_INODE, "blocking ast\n");
1430                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1431                         ldlm_callback_reply(req, 0);
1432                 if (ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock))
1433                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1434                 break;
1435         case LDLM_CP_CALLBACK:
1436                 CDEBUG(D_INODE, "completion ast\n");
1437                 ldlm_callback_reply(req, 0);
1438                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1439                 break;
1440         case LDLM_GL_CALLBACK:
1441                 CDEBUG(D_INODE, "glimpse ast\n");
1442                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1443                 break;
1444         default:
1445                 LBUG();                         /* checked above */
1446         }
1447
1448         RETURN(0);
1449 }
1450
1451 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1452 {
1453         int rc;
1454         ENTRY;
1455
1456         /* Requests arrive in sender's byte order.  The ptlrpc service
1457          * handler has already checked and, if necessary, byte-swapped the
1458          * incoming request message body, but I am responsible for the
1459          * message buffers. */
1460
1461         if (req->rq_export == NULL) {
1462                 struct ldlm_request *dlm_req;
1463
1464                 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1465                        lustre_msg_get_opc(req->rq_reqmsg),
1466                        libcfs_id2str(req->rq_peer),
1467                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1468
1469                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1470                                              sizeof(*dlm_req),
1471                                              lustre_swab_ldlm_request);
1472                 if (dlm_req != NULL)
1473                         ldlm_lock_dump_handle(D_ERROR, &dlm_req->lock_handle1);
1474
1475                 ldlm_callback_reply(req, -ENOTCONN);
1476                 RETURN(0);
1477         }
1478
1479         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1480
1481         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1482         case LDLM_CANCEL:
1483                 CDEBUG(D_INODE, "cancel\n");
1484                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1485                 rc = ldlm_handle_cancel(req);
1486                 if (rc)
1487                         break;
1488                 RETURN(0);
1489         case OBD_LOG_CANCEL:
1490                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1491                 rc = llog_origin_handle_cancel(req);
1492                 ldlm_callback_reply(req, rc);
1493                 RETURN(0);
1494         default:
1495                 CERROR("invalid opcode %d\n",
1496                        lustre_msg_get_opc(req->rq_reqmsg));
1497                 ldlm_callback_reply(req, -EINVAL);
1498         }
1499
1500         RETURN(0);
1501 }
1502
1503 #ifdef __KERNEL__
1504 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1505 {
1506         struct ldlm_bl_work_item *blwi = NULL;
1507
1508         spin_lock(&blp->blp_lock);
1509         if (!list_empty(&blp->blp_list)) {
1510                 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
1511                                   blwi_entry);
1512                 list_del(&blwi->blwi_entry);
1513         }
1514         spin_unlock(&blp->blp_lock);
1515
1516         return blwi;
1517 }
1518
1519 struct ldlm_bl_thread_data {
1520         int                     bltd_num;
1521         struct ldlm_bl_pool     *bltd_blp;
1522 };
1523
1524 static int ldlm_bl_thread_main(void *arg)
1525 {
1526         struct ldlm_bl_thread_data *bltd = arg;
1527         struct ldlm_bl_pool *blp = bltd->bltd_blp;
1528         ENTRY;
1529
1530         {
1531                 char name[CFS_CURPROC_COMM_MAX];
1532                 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1533                          bltd->bltd_num);
1534                 cfs_daemonize(name);
1535         }
1536
1537         atomic_inc(&blp->blp_num_threads);
1538         complete(&blp->blp_comp);
1539
1540         while(1) {
1541                 struct l_wait_info lwi = { 0 };
1542                 struct ldlm_bl_work_item *blwi = NULL;
1543
1544                 l_wait_event_exclusive(blp->blp_waitq,
1545                                        (blwi = ldlm_bl_get_work(blp)) != NULL,
1546                                        &lwi);
1547
1548                 if (blwi->blwi_ns == NULL)
1549                         break;
1550
1551                 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1552                                         blwi->blwi_lock);
1553                 OBD_FREE(blwi, sizeof(*blwi));
1554         }
1555
1556         atomic_dec(&blp->blp_num_threads);
1557         complete(&blp->blp_comp);
1558         RETURN(0);
1559 }
1560
1561 #endif
1562
1563 static int ldlm_setup(void);
1564 static int ldlm_cleanup(int force);
1565
1566 int ldlm_get_ref(void)
1567 {
1568         int rc = 0;
1569         ENTRY;
1570         mutex_down(&ldlm_ref_sem);
1571         if (++ldlm_refcount == 1) {
1572                 rc = ldlm_setup();
1573                 if (rc)
1574                         ldlm_refcount--;
1575         }
1576         mutex_up(&ldlm_ref_sem);
1577
1578         RETURN(rc);
1579 }
1580
1581 void ldlm_put_ref(int force)
1582 {
1583         ENTRY;
1584         mutex_down(&ldlm_ref_sem);
1585         if (ldlm_refcount == 1) {
1586                 int rc = ldlm_cleanup(force);
1587                 if (rc)
1588                         CERROR("ldlm_cleanup failed: %d\n", rc);
1589                 else
1590                         ldlm_refcount--;
1591         } else {
1592                 ldlm_refcount--;
1593         }
1594         mutex_up(&ldlm_ref_sem);
1595
1596         EXIT;
1597 }
1598
1599 static int ldlm_setup(void)
1600 {
1601         struct ldlm_bl_pool *blp;
1602         int rc = 0;
1603 #ifdef __KERNEL__
1604         int i;
1605 #endif
1606         ENTRY;
1607
1608         if (ldlm_state != NULL)
1609                 RETURN(-EALREADY);
1610
1611         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1612         if (ldlm_state == NULL)
1613                 RETURN(-ENOMEM);
1614
1615 #ifdef LPROCFS
1616         rc = ldlm_proc_setup();
1617         if (rc != 0)
1618                 GOTO(out_free, rc);
1619 #endif
1620
1621         ldlm_state->ldlm_cb_service =
1622                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1623                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
1624                                 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
1625                                 ldlm_callback_handler, "ldlm_cbd",
1626                                 ldlm_svc_proc_dir, NULL,
1627                                 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1628                                 "ldlm_cb");
1629
1630         if (!ldlm_state->ldlm_cb_service) {
1631                 CERROR("failed to start service\n");
1632                 GOTO(out_proc, rc = -ENOMEM);
1633         }
1634
1635         ldlm_state->ldlm_cancel_service =
1636                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1637                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
1638                                 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
1639                                 ldlm_cancel_handler, "ldlm_canceld",
1640                                 ldlm_svc_proc_dir, NULL,
1641                                 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX, 
1642                                 "ldlm_cn");
1643
1644         if (!ldlm_state->ldlm_cancel_service) {
1645                 CERROR("failed to start service\n");
1646                 GOTO(out_proc, rc = -ENOMEM);
1647         }
1648
1649         OBD_ALLOC(blp, sizeof(*blp));
1650         if (blp == NULL)
1651                 GOTO(out_proc, rc = -ENOMEM);
1652         ldlm_state->ldlm_bl_pool = blp;
1653
1654         atomic_set(&blp->blp_num_threads, 0);
1655         cfs_waitq_init(&blp->blp_waitq);
1656         spin_lock_init(&blp->blp_lock);
1657
1658         CFS_INIT_LIST_HEAD(&blp->blp_list);
1659
1660 #ifdef __KERNEL__
1661         for (i = 0; i < LDLM_BL_THREADS; i++) {
1662                 struct ldlm_bl_thread_data bltd = {
1663                         .bltd_num = i,
1664                         .bltd_blp = blp,
1665                 };
1666                 init_completion(&blp->blp_comp);
1667                 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1668                 if (rc < 0) {
1669                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1670                         GOTO(out_thread, rc);
1671                 }
1672                 wait_for_completion(&blp->blp_comp);
1673         }
1674
1675         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
1676         if (rc)
1677                 GOTO(out_thread, rc);
1678
1679         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
1680         if (rc)
1681                 GOTO(out_thread, rc);
1682
1683         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1684         expired_lock_thread.elt_state = ELT_STOPPED;
1685         cfs_waitq_init(&expired_lock_thread.elt_waitq);
1686
1687         CFS_INIT_LIST_HEAD(&waiting_locks_list);
1688         spin_lock_init(&waiting_locks_spinlock);
1689         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
1690
1691         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
1692         if (rc < 0) {
1693                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1694                 GOTO(out_thread, rc);
1695         }
1696
1697         wait_event(expired_lock_thread.elt_waitq,
1698                    expired_lock_thread.elt_state == ELT_READY);
1699 #endif
1700
1701         RETURN(0);
1702
1703 #ifdef __KERNEL__
1704  out_thread:
1705         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1706         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1707 #endif
1708
1709  out_proc:
1710 #ifdef LPROCFS
1711         ldlm_proc_cleanup();
1712  out_free:
1713 #endif
1714         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1715         ldlm_state = NULL;
1716         return rc;
1717 }
1718
1719 static int ldlm_cleanup(int force)
1720 {
1721 #ifdef __KERNEL__
1722         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1723 #endif
1724         ENTRY;
1725
1726         if (!list_empty(&ldlm_namespace_list)) {
1727                 CERROR("ldlm still has namespaces; clean these up first.\n");
1728                 ldlm_dump_all_namespaces(D_DLMTRACE);
1729                 RETURN(-EBUSY);
1730         }
1731
1732 #ifdef __KERNEL__
1733         while (atomic_read(&blp->blp_num_threads) > 0) {
1734                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1735
1736                 init_completion(&blp->blp_comp);
1737
1738                 spin_lock(&blp->blp_lock);
1739                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1740                 cfs_waitq_signal(&blp->blp_waitq);
1741                 spin_unlock(&blp->blp_lock);
1742
1743                 wait_for_completion(&blp->blp_comp);
1744         }
1745         OBD_FREE(blp, sizeof(*blp));
1746
1747         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1748         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1749         ldlm_proc_cleanup();
1750
1751         expired_lock_thread.elt_state = ELT_TERMINATE;
1752         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
1753         wait_event(expired_lock_thread.elt_waitq,
1754                    expired_lock_thread.elt_state == ELT_STOPPED);
1755 #else
1756         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1757         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1758 #endif
1759
1760         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1761         ldlm_state = NULL;
1762
1763         RETURN(0);
1764 }
1765
1766 int __init ldlm_init(void)
1767 {
1768         init_mutex(&ldlm_ref_sem);
1769         init_mutex(&ldlm_namespace_lock);
1770         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
1771                                                sizeof(struct ldlm_resource), 0,
1772                                                SLAB_HWCACHE_ALIGN);
1773         if (ldlm_resource_slab == NULL)
1774                 return -ENOMEM;
1775
1776         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
1777                                            sizeof(struct ldlm_lock), 0,
1778                                            SLAB_HWCACHE_ALIGN);
1779         if (ldlm_lock_slab == NULL) {
1780                 cfs_mem_cache_destroy(ldlm_resource_slab);
1781                 return -ENOMEM;
1782         }
1783
1784         return 0;
1785 }
1786
1787 void __exit ldlm_exit(void)
1788 {
1789         int rc;
1790         if (ldlm_refcount)
1791                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1792         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
1793         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
1794         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
1795         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
1796 }
1797
1798 /* ldlm_extent.c */
1799 EXPORT_SYMBOL(ldlm_extent_shift_kms);
1800
1801 /* ldlm_lock.c */
1802 EXPORT_SYMBOL(ldlm_get_processing_policy);
1803 EXPORT_SYMBOL(ldlm_lock2desc);
1804 EXPORT_SYMBOL(ldlm_register_intent);
1805 EXPORT_SYMBOL(ldlm_lockname);
1806 EXPORT_SYMBOL(ldlm_typename);
1807 EXPORT_SYMBOL(ldlm_lock2handle);
1808 EXPORT_SYMBOL(__ldlm_handle2lock);
1809 EXPORT_SYMBOL(ldlm_lock_get);
1810 EXPORT_SYMBOL(ldlm_lock_put);
1811 EXPORT_SYMBOL(ldlm_lock_match);
1812 EXPORT_SYMBOL(ldlm_lock_cancel);
1813 EXPORT_SYMBOL(ldlm_lock_addref);
1814 EXPORT_SYMBOL(ldlm_lock_decref);
1815 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1816 EXPORT_SYMBOL(ldlm_lock_change_resource);
1817 EXPORT_SYMBOL(ldlm_lock_set_data);
1818 EXPORT_SYMBOL(ldlm_it2str);
1819 EXPORT_SYMBOL(ldlm_lock_dump);
1820 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1821 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1822 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1823 EXPORT_SYMBOL(ldlm_lock_allow_match);
1824
1825 /* ldlm_request.c */
1826 EXPORT_SYMBOL(ldlm_completion_ast);
1827 EXPORT_SYMBOL(ldlm_blocking_ast);
1828 EXPORT_SYMBOL(ldlm_glimpse_ast);
1829 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1830 EXPORT_SYMBOL(ldlm_cli_convert);
1831 EXPORT_SYMBOL(ldlm_cli_enqueue);
1832 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
1833 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
1834 EXPORT_SYMBOL(ldlm_cli_cancel);
1835 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1836 EXPORT_SYMBOL(ldlm_cli_join_lru);
1837 EXPORT_SYMBOL(ldlm_replay_locks);
1838 EXPORT_SYMBOL(ldlm_resource_foreach);
1839 EXPORT_SYMBOL(ldlm_namespace_foreach);
1840 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1841 EXPORT_SYMBOL(ldlm_resource_iterate);
1842
1843 /* ldlm_lockd.c */
1844 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1845 EXPORT_SYMBOL(ldlm_server_completion_ast);
1846 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1847 EXPORT_SYMBOL(ldlm_handle_enqueue);
1848 EXPORT_SYMBOL(ldlm_handle_cancel);
1849 EXPORT_SYMBOL(ldlm_handle_convert);
1850 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1851 EXPORT_SYMBOL(ldlm_get_ref);
1852 EXPORT_SYMBOL(ldlm_put_ref);
1853 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
1854
1855 /* ldlm_resource.c */
1856 EXPORT_SYMBOL(ldlm_namespace_new);
1857 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1858 EXPORT_SYMBOL(ldlm_namespace_free);
1859 EXPORT_SYMBOL(ldlm_namespace_dump);
1860 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1861 EXPORT_SYMBOL(ldlm_resource_get);
1862 EXPORT_SYMBOL(ldlm_resource_putref);
1863 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1864
1865 /* ldlm_lib.c */
1866 EXPORT_SYMBOL(client_import_add_conn);
1867 EXPORT_SYMBOL(client_import_del_conn);
1868 EXPORT_SYMBOL(client_obd_setup);
1869 EXPORT_SYMBOL(client_obd_cleanup);
1870 EXPORT_SYMBOL(client_connect_import);
1871 EXPORT_SYMBOL(client_disconnect_export);
1872 EXPORT_SYMBOL(target_abort_recovery);
1873 EXPORT_SYMBOL(target_cleanup_recovery);
1874 EXPORT_SYMBOL(target_handle_connect);
1875 EXPORT_SYMBOL(target_destroy_export);
1876 EXPORT_SYMBOL(target_cancel_recovery_timer);
1877 EXPORT_SYMBOL(target_send_reply);
1878 EXPORT_SYMBOL(target_queue_recovery_request);
1879 EXPORT_SYMBOL(target_handle_ping);
1880 EXPORT_SYMBOL(target_handle_disconnect);
1881 EXPORT_SYMBOL(target_queue_final_reply);
1882
1883 /* l_lock.c */
1884 EXPORT_SYMBOL(lock_res_and_lock);
1885 EXPORT_SYMBOL(unlock_res_and_lock);
1886