Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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, 2003 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 Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27 #define DEBUG_SUBSYSTEM S_LDLM
28
29 #ifdef __KERNEL__
30 # include <linux/module.h>
31 # include <linux/slab.h>
32 # include <linux/init.h>
33 # include <linux/wait.h>
34 #else
35 # include <liblustre.h>
36 #endif
37
38 #include <linux/lustre_dlm.h>
39 #include <linux/obd_class.h>
40 #include "ldlm_internal.h"
41
42 extern kmem_cache_t *ldlm_resource_slab;
43 extern kmem_cache_t *ldlm_lock_slab;
44 extern struct lustre_lock ldlm_handle_lock;
45 extern struct list_head ldlm_namespace_list;
46 extern int (*mds_reint_p)(int offset, struct ptlrpc_request *req);
47 extern int (*mds_getattr_name_p)(int offset, struct ptlrpc_request *req);
48
49 static DECLARE_MUTEX(ldlm_ref_sem);
50 static int ldlm_refcount = 0;
51
52 /* LDLM state */ 
53
54 static struct ldlm_state *ldlm ;
55
56 inline unsigned long round_timeout(unsigned long timeout)
57 {
58         return ((timeout / HZ) + 1) * HZ;
59 }
60
61 #ifdef __KERNEL__
62 /* XXX should this be per-ldlm? */
63 static struct list_head waiting_locks_list;
64 static spinlock_t waiting_locks_spinlock;
65 static struct timer_list waiting_locks_timer;
66
67 static struct expired_lock_thread {
68         wait_queue_head_t         elt_waitq;
69         int                       elt_state;
70         struct list_head          elt_expired_locks;
71         spinlock_t                elt_lock;
72 } expired_lock_thread;
73 #endif
74
75 #define ELT_STOPPED   0
76 #define ELT_READY     1
77 #define ELT_TERMINATE 2
78
79 struct ldlm_bl_pool {
80         spinlock_t              blp_lock;
81         struct list_head        blp_list;
82         wait_queue_head_t       blp_waitq;
83         atomic_t                blp_num_threads;
84         struct completion       blp_comp;
85 };
86
87 struct ldlm_bl_work_item {
88         struct list_head        blwi_entry;
89         struct ldlm_namespace   *blwi_ns;
90         struct ldlm_lock_desc   blwi_ld;
91         struct ldlm_lock        *blwi_lock;
92 };
93
94 #ifdef __KERNEL__
95
96 static inline int have_expired_locks(void)
97 {
98         int need_to_run;
99
100         spin_lock_bh(&expired_lock_thread.elt_lock);
101         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
102         spin_unlock_bh(&expired_lock_thread.elt_lock);
103
104         RETURN(need_to_run);
105 }
106
107 static int expired_lock_main(void *arg)
108 {
109         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
110         struct l_wait_info lwi = { 0 };
111         unsigned long flags;
112
113         ENTRY;
114         lock_kernel();
115         kportal_daemonize("ldlm_elt");
116
117         SIGNAL_MASK_LOCK(current, flags);
118         sigfillset(&current->blocked);
119         RECALC_SIGPENDING;
120         SIGNAL_MASK_UNLOCK(current, flags);
121
122         unlock_kernel();
123
124         expired_lock_thread.elt_state = ELT_READY;
125         wake_up(&expired_lock_thread.elt_waitq);
126
127         while (1) {
128                 l_wait_event(expired_lock_thread.elt_waitq,
129                              have_expired_locks() ||
130                              expired_lock_thread.elt_state == ELT_TERMINATE,
131                              &lwi);
132
133                 spin_lock_bh(&expired_lock_thread.elt_lock);
134                 while (!list_empty(expired)) {
135                         struct ldlm_lock *lock = list_entry(expired->next,
136                                                             struct ldlm_lock,
137                                                             l_pending_chain);
138                         spin_unlock_bh(&expired_lock_thread.elt_lock);
139
140                         ptlrpc_fail_export(lock->l_export);
141
142                         spin_lock_bh(&expired_lock_thread.elt_lock);
143                 }
144                 spin_unlock_bh(&expired_lock_thread.elt_lock);
145
146                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
147                         break;
148         }
149
150         expired_lock_thread.elt_state = ELT_STOPPED;
151         wake_up(&expired_lock_thread.elt_waitq);
152         RETURN(0);
153 }
154
155 static void waiting_locks_callback(unsigned long unused)
156 {
157         struct ldlm_lock *lock;
158         char str[PTL_NALFMT_SIZE];
159
160         spin_lock_bh(&waiting_locks_spinlock);
161         while (!list_empty(&waiting_locks_list)) {
162                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
163                                   l_pending_chain);
164
165                 if (lock->l_callback_timeout > jiffies)
166                         break;
167
168                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
169                            "%s@%s nid "LPX64" (%s) ",
170                            lock->l_export->exp_client_uuid.uuid,
171                            lock->l_export->exp_connection->c_remote_uuid.uuid,
172                            lock->l_export->exp_connection->c_peer.peer_nid,
173                            portals_nid2str(lock->l_export->exp_connection->c_peer.peer_ni->pni_number,
174                                            lock->l_export->exp_connection->c_peer.peer_nid,
175                                            str));
176
177                 spin_lock_bh(&expired_lock_thread.elt_lock);
178                 list_del(&lock->l_pending_chain);
179                 list_add(&lock->l_pending_chain,
180                          &expired_lock_thread.elt_expired_locks);
181                 spin_unlock_bh(&expired_lock_thread.elt_lock);
182                 wake_up(&expired_lock_thread.elt_waitq);
183         }
184
185         spin_unlock_bh(&waiting_locks_spinlock);
186 }
187
188 /*
189  * Indicate that we're waiting for a client to call us back cancelling a given
190  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
191  * timer to fire appropriately.  (We round up to the next second, to avoid
192  * floods of timer firings during periods of high lock contention and traffic).
193  */
194 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
195 {
196         unsigned long timeout_rounded;
197
198         spin_lock_bh(&waiting_locks_spinlock);
199         if (!list_empty(&lock->l_pending_chain)) {
200                 LDLM_DEBUG(lock, "not re-adding to wait list");
201                 spin_unlock_bh(&waiting_locks_spinlock);
202                 return 0;
203         }
204         LDLM_DEBUG(lock, "adding to wait list");
205
206         lock->l_callback_timeout = jiffies + (obd_timeout * HZ / 2);
207
208         timeout_rounded = round_timeout(lock->l_callback_timeout);
209
210         if (timeout_rounded < waiting_locks_timer.expires ||
211             !timer_pending(&waiting_locks_timer)) {
212                 mod_timer(&waiting_locks_timer, timeout_rounded);
213         }
214         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
215         spin_unlock_bh(&waiting_locks_spinlock);
216         return 1;
217 }
218
219 /*
220  * Remove a lock from the pending list, likely because it had its cancellation
221  * callback arrive without incident.  This adjusts the lock-timeout timer if
222  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
223  */
224 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
225 {
226         struct list_head *list_next;
227
228         if (lock->l_export == NULL) {
229                 /* We don't have a "waiting locks list" on clients. */
230                 LDLM_DEBUG(lock, "client lock: no-op");
231                 return 0;
232         }
233
234         spin_lock_bh(&waiting_locks_spinlock);
235
236         if (list_empty(&lock->l_pending_chain)) {
237                 spin_unlock_bh(&waiting_locks_spinlock);
238                 LDLM_DEBUG(lock, "wasn't waiting");
239                 return 0;
240         }
241
242         list_next = lock->l_pending_chain.next;
243         if (lock->l_pending_chain.prev == &waiting_locks_list) {
244                 /* Removing the head of the list, adjust timer. */
245                 if (list_next == &waiting_locks_list) {
246                         /* No more, just cancel. */
247                         del_timer(&waiting_locks_timer);
248                 } else {
249                         struct ldlm_lock *next;
250                         next = list_entry(list_next, struct ldlm_lock,
251                                           l_pending_chain);
252                         mod_timer(&waiting_locks_timer,
253                                   round_timeout(next->l_callback_timeout));
254                 }
255         }
256         list_del_init(&lock->l_pending_chain);
257         spin_unlock_bh(&waiting_locks_spinlock);
258         LDLM_DEBUG(lock, "removed");
259         return 1;
260 }
261
262 #else /* !__KERNEL__ */
263
264 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
265 {
266         RETURN(1);
267 }
268
269 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
270 {
271         RETURN(0);
272 }
273
274 #endif /* __KERNEL__ */
275
276 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc, char *ast_type)
277 {
278         const struct ptlrpc_connection *conn = lock->l_export->exp_connection;
279         char str[PTL_NALFMT_SIZE];
280
281         CERROR("%s AST failed (%d) for res "LPU64"/"LPU64
282                ", mode %s: evicting client %s@%s NID "LPX64" (%s)\n",
283                ast_type, rc,
284                lock->l_resource->lr_name.name[0],
285                lock->l_resource->lr_name.name[1],
286                ldlm_lockname[lock->l_granted_mode],
287                lock->l_export->exp_client_uuid.uuid,
288                conn->c_remote_uuid.uuid, conn->c_peer.peer_nid,
289                portals_nid2str(conn->c_peer.peer_ni->pni_number,
290                                conn->c_peer.peer_nid, str));
291         ptlrpc_fail_export(lock->l_export);
292 }
293
294 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
295                              struct ldlm_lock_desc *desc,
296                              void *data, int flag)
297 {
298         struct ldlm_request *body;
299         struct ptlrpc_request *req;
300         int rc = 0, size = sizeof(*body);
301         ENTRY;
302
303         if (flag == LDLM_CB_CANCELING) {
304                 /* Don't need to do anything here. */
305                 RETURN(0);
306         }
307
308         LASSERT(lock);
309
310         l_lock(&lock->l_resource->lr_namespace->ns_lock);
311         if (lock->l_granted_mode != lock->l_req_mode) {
312                 /* this blocking AST will be communicated as part of the
313                  * completion AST instead */
314                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
315                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");                 RETURN(0);
316         }
317
318         if (lock->l_destroyed) {
319                 /* What's the point? */
320                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
321                 RETURN(0);
322         }
323
324 #if 0
325         if (LTIME_S(CURRENT_TIME) - lock->l_export->exp_last_request_time > 30){
326                 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
327                 RETURN(-ETIMEDOUT);
328         }
329 #endif
330
331         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
332                               LDLM_BL_CALLBACK, 1, &size, NULL);
333         if (!req)
334                 RETURN(-ENOMEM);
335
336         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
337         memcpy(&body->lock_handle1, &lock->l_remote_handle,
338                sizeof(body->lock_handle1));
339         memcpy(&body->lock_desc, desc, sizeof(*desc));
340         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
341
342         LDLM_DEBUG(lock, "server preparing blocking AST");
343         req->rq_replen = lustre_msg_size(0, NULL);
344
345         if (lock->l_granted_mode == lock->l_req_mode)
346                 ldlm_add_waiting_lock(lock);
347         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
348
349         req->rq_send_state = LUSTRE_IMP_FULL;
350         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
351         rc = ptlrpc_queue_wait(req);
352         if (rc == -ETIMEDOUT || rc == -EINTR) {
353 #ifdef __KERNEL__
354                 ldlm_del_waiting_lock(lock);
355                 ldlm_failed_ast(lock, rc, "blocking");
356 #else
357                 /* XXX
358                  * Here we treat all clients as liblustre. When BLOCKING AST
359                  * timeout we don't evicting the client and only cancel
360                  * the lock.
361                  * restore to orignial implementation later!!!
362                  * XXX
363                  */
364                 CERROR("BLOCKING AST to client (nid "LPU64") timeout, "
365                        "simply cancel lock 0x%p\n",
366                        req->rq_connection->c_peer.peer_nid, lock);
367                 ldlm_lock_cancel(lock);
368                 rc = -ERESTART;
369 #endif
370         } else if (rc) {
371                 if (rc == -EINVAL)
372                         CDEBUG(D_DLMTRACE, "client (nid "LPU64") returned %d "
373                                "from blocking AST for lock %p--normal race\n",
374                                req->rq_connection->c_peer.peer_nid,
375                                req->rq_repmsg->status, lock);
376                 else if (rc == -ENOTCONN)
377                         CDEBUG(D_DLMTRACE, "client (nid "LPU64") returned %d "
378                                "from blocking AST for lock %p--this client was "
379                                "probably rebooted while it held a lock, nothing"
380                                " serious\n",req->rq_connection->c_peer.peer_nid,
381                                req->rq_repmsg->status, lock);
382                 else
383                         CDEBUG(D_ERROR, "client (nid "LPU64") returned %d "
384                                "from blocking AST for lock %p\n",
385                                req->rq_connection->c_peer.peer_nid,
386                                (req->rq_repmsg != NULL)?
387                                req->rq_repmsg->status : 0,
388                                lock);
389                 LDLM_DEBUG(lock, "client sent rc %d rq_status %d from blocking "
390                            "AST", rc, req->rq_status);
391                 ldlm_lock_cancel(lock);
392                 /* Server-side AST functions are called from ldlm_reprocess_all,
393                  * which needs to be told to please restart its reprocessing. */
394                 rc = -ERESTART;
395         }
396
397         ptlrpc_req_finished(req);
398
399         RETURN(rc);
400 }
401
402 /* XXX copied from ptlrpc/service.c */
403 static long timeval_sub(struct timeval *large, struct timeval *small)
404 {
405         return (large->tv_sec - small->tv_sec) * 1000000 +
406                 (large->tv_usec - small->tv_usec);
407 }
408
409 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
410 {
411         struct ldlm_request *body;
412         struct ptlrpc_request *req;
413         struct timeval granted_time;
414         long total_enqueue_wait;
415         int rc = 0, size = sizeof(*body);
416         ENTRY;
417
418         if (lock == NULL) {
419                 LBUG();
420                 RETURN(-EINVAL);
421         }
422
423         do_gettimeofday(&granted_time);
424         total_enqueue_wait = timeval_sub(&granted_time, &lock->l_enqueued_time);
425
426         if (total_enqueue_wait / 1000000 > obd_timeout)
427                 LDLM_ERROR(lock, "enqueue wait took %ldus", total_enqueue_wait);
428
429         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
430                               LDLM_CP_CALLBACK, 1, &size, NULL);
431         if (!req)
432                 RETURN(-ENOMEM);
433
434         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
435         memcpy(&body->lock_handle1, &lock->l_remote_handle,
436                sizeof(body->lock_handle1));
437         body->lock_flags = flags;
438         ldlm_lock2desc(lock, &body->lock_desc);
439
440         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
441                    total_enqueue_wait);
442         req->rq_replen = lustre_msg_size(0, NULL);
443
444         req->rq_send_state = LUSTRE_IMP_FULL;
445         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
446
447         /* We only send real blocking ASTs after the lock is granted */
448         l_lock(&lock->l_resource->lr_namespace->ns_lock);
449         if (lock->l_flags & LDLM_FL_AST_SENT) {
450                 body->lock_flags |= LDLM_FL_AST_SENT;
451                 ldlm_add_waiting_lock(lock); /* start the lock-timeout clock */
452         }
453         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
454
455         rc = ptlrpc_queue_wait(req);
456         if (rc == -ETIMEDOUT || rc == -EINTR) {
457                 ldlm_del_waiting_lock(lock);
458                 ldlm_failed_ast(lock, rc, "completion");
459         } else if (rc) {
460                 LDLM_ERROR(lock, "client sent rc %d rq_status %d from "
461                            "completion AST\n", rc, req->rq_status);
462                 ldlm_lock_cancel(lock);
463                 /* Server-side AST functions are called from ldlm_reprocess_all,
464                  * which needs to be told to please restart its reprocessing. */
465                 rc = -ERESTART;
466         }
467         ptlrpc_req_finished(req);
468
469         RETURN(rc);
470 }
471
472 int ldlm_handle_enqueue(struct ptlrpc_request *req,
473                         ldlm_completion_callback completion_callback,
474                         ldlm_blocking_callback blocking_callback)
475 {
476         struct obd_device *obddev = req->rq_export->exp_obd;
477         struct ldlm_reply *dlm_rep;
478         struct ldlm_request *dlm_req;
479         int rc, size = sizeof(*dlm_rep), cookielen = 0;
480         __u32 flags;
481         ldlm_error_t err;
482         struct ldlm_lock *lock = NULL;
483         void *cookie = NULL;
484         ENTRY;
485
486         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
487
488         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
489                                       lustre_swab_ldlm_request);
490         if (dlm_req == NULL) {
491                 CERROR ("Can't unpack dlm_req\n");
492                 RETURN (-EFAULT);
493         }
494
495         flags = dlm_req->lock_flags;
496         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN &&
497             (flags & LDLM_FL_HAS_INTENT)) {
498                 /* In this case, the reply buffer is allocated deep in
499                  * local_lock_enqueue by the policy function. */
500                 cookie = req;
501                 cookielen = sizeof(*req);
502         } else {
503                 rc = lustre_pack_reply(req, 1, &size, NULL);
504                 if (rc) {
505                         CERROR("out of memory\n");
506                         RETURN(-ENOMEM);
507                 }
508                 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN) {
509                         cookie = &dlm_req->lock_desc.l_policy_data;
510                         cookielen = sizeof(ldlm_policy_data_t);
511                 }
512         }
513
514         /* The lock's callback data might be set in the policy function */
515         lock = ldlm_lock_create(obddev->obd_namespace,
516                                 &dlm_req->lock_handle2,
517                                 dlm_req->lock_desc.l_resource.lr_name,
518                                 dlm_req->lock_desc.l_resource.lr_type,
519                                 dlm_req->lock_desc.l_req_mode,
520                                 blocking_callback, completion_callback, NULL);
521         if (!lock)
522                 GOTO(out, err = -ENOMEM);
523
524         do_gettimeofday(&lock->l_enqueued_time);
525         memcpy(&lock->l_remote_handle, &dlm_req->lock_handle1,
526                sizeof(lock->l_remote_handle));
527         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
528
529         LASSERT(req->rq_export);
530         lock->l_export = class_export_get(req->rq_export);
531         l_lock(&lock->l_resource->lr_namespace->ns_lock);
532         list_add(&lock->l_export_chain,
533                  &lock->l_export->exp_ldlm_data.led_held_locks);
534         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
535
536         err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, cookielen,
537                                 &flags);
538         if (err)
539                 GOTO(out, err);
540
541         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
542         dlm_rep->lock_flags = flags;
543
544         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
545         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN) {
546                 memcpy(&dlm_rep->lock_policy_data, &lock->l_policy_data,
547                        cookielen);
548         }
549         if (dlm_rep->lock_flags & LDLM_FL_LOCK_CHANGED) {
550                 memcpy(&dlm_rep->lock_resource_name, &lock->l_resource->lr_name,
551                        sizeof(dlm_rep->lock_resource_name));
552                 dlm_rep->lock_mode = lock->l_req_mode;
553         }
554
555         /* We never send a blocking AST until the lock is granted, but
556          * we can tell it right now */
557         l_lock(&lock->l_resource->lr_namespace->ns_lock);
558         if (lock->l_flags & LDLM_FL_AST_SENT)
559                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
560         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
561
562         EXIT;
563  out:
564         req->rq_status = err;
565
566         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
567          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
568         if (lock) {
569                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
570                            "(err=%d)", err);
571                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
572                         ldlm_reprocess_all(lock->l_resource);
573                 LDLM_LOCK_PUT(lock);
574         }
575         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p)", lock);
576
577         return 0;
578 }
579
580 int ldlm_handle_convert(struct ptlrpc_request *req)
581 {
582         struct ldlm_request *dlm_req;
583         struct ldlm_reply *dlm_rep;
584         struct ldlm_lock *lock;
585         int rc, size = sizeof(*dlm_rep);
586         ENTRY;
587
588         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
589                                       lustre_swab_ldlm_request);
590         if (dlm_req == NULL) {
591                 CERROR ("Can't unpack dlm_req\n");
592                 RETURN (-EFAULT);
593         }
594
595         rc = lustre_pack_reply(req, 1, &size, NULL);
596         if (rc) {
597                 CERROR("out of memory\n");
598                 RETURN(-ENOMEM);
599         }
600         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
601         dlm_rep->lock_flags = dlm_req->lock_flags;
602
603         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
604         if (!lock) {
605                 req->rq_status = EINVAL;
606         } else {
607                 LDLM_DEBUG(lock, "server-side convert handler START");
608                 ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
609                                   &dlm_rep->lock_flags);
610                 if (ldlm_del_waiting_lock(lock))
611                         CDEBUG(D_DLMTRACE, "converted waiting lock %p\n", lock);
612                 req->rq_status = 0;
613         }
614
615         if (lock) {
616                 ldlm_reprocess_all(lock->l_resource);
617                 LDLM_DEBUG(lock, "server-side convert handler END");
618                 LDLM_LOCK_PUT(lock);
619         } else
620                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
621
622         RETURN(0);
623 }
624
625 int ldlm_handle_cancel(struct ptlrpc_request *req)
626 {
627         struct ldlm_request *dlm_req;
628         struct ldlm_lock *lock;
629         char str[PTL_NALFMT_SIZE];
630         int rc;
631         ENTRY;
632
633         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
634                                       lustre_swab_ldlm_request);
635         if (dlm_req == NULL) {
636                 CERROR("bad request buffer for cancel\n");
637                 RETURN(-EFAULT);
638         }
639
640         rc = lustre_pack_reply(req, 0, NULL, NULL);
641         if (rc) {
642                 CERROR("out of memory\n");
643                 RETURN(-ENOMEM);
644         }
645
646         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
647         if (!lock) {
648                 CERROR("received cancel for unknown lock cookie "LPX64
649                        " from nid "LPX64" (%s)\n", dlm_req->lock_handle1.cookie,
650                        req->rq_connection->c_peer.peer_nid,
651                        portals_nid2str(req->rq_connection->c_peer.peer_ni->pni_number,
652                                        req->rq_connection->c_peer.peer_nid, str));
653                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
654                                   "(cookie "LPU64")",
655                                   dlm_req->lock_handle1.cookie);
656                 req->rq_status = ESTALE;
657         } else {
658                 LDLM_DEBUG(lock, "server-side cancel handler START");
659                 ldlm_lock_cancel(lock);
660                 if (ldlm_del_waiting_lock(lock))
661                         CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
662                 req->rq_status = 0;
663         }
664
665         if (ptlrpc_reply(req) != 0)
666                 LBUG();
667
668         if (lock) {
669                 ldlm_reprocess_all(lock->l_resource);
670                 LDLM_DEBUG(lock, "server-side cancel handler END");
671                 LDLM_LOCK_PUT(lock);
672         }
673
674         RETURN(0);
675 }
676
677 static void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
678                                     struct ldlm_lock_desc *ld,
679                                     struct ldlm_lock *lock)
680 {
681         int do_ast;
682         ENTRY;
683
684         l_lock(&ns->ns_lock);
685         LDLM_DEBUG(lock, "client blocking AST callback handler START");
686
687         lock->l_flags |= LDLM_FL_CBPENDING;
688         do_ast = (!lock->l_readers && !lock->l_writers);
689
690         if (do_ast) {
691                 LDLM_DEBUG(lock, "already unused, calling "
692                            "callback (%p)", lock->l_blocking_ast);
693                 if (lock->l_blocking_ast != NULL) {
694                         l_unlock(&ns->ns_lock);
695                         l_check_no_ns_lock(ns);
696                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
697                                              LDLM_CB_BLOCKING);
698                         l_lock(&ns->ns_lock);
699                 }
700         } else {
701                 LDLM_DEBUG(lock, "Lock still has references, will be"
702                            " cancelled later");
703         }
704
705         LDLM_DEBUG(lock, "client blocking callback handler END");
706         l_unlock(&ns->ns_lock);
707         LDLM_LOCK_PUT(lock);
708         EXIT;
709 }
710
711 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
712                                     struct ldlm_namespace *ns,
713                                     struct ldlm_request *dlm_req,
714                                     struct ldlm_lock *lock)
715 {
716         LIST_HEAD(ast_list);
717         ENTRY;
718
719         l_lock(&ns->ns_lock);
720         LDLM_DEBUG(lock, "client completion callback handler START");
721
722         /* If we receive the completion AST before the actual enqueue returned,
723          * then we might need to switch lock modes, resources, or extents. */
724         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
725                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
726                 LDLM_DEBUG(lock, "completion AST, new lock mode");
727         }
728         if (lock->l_resource->lr_type != LDLM_PLAIN)
729                 memcpy(&lock->l_policy_data, &dlm_req->lock_desc.l_policy_data,
730                        sizeof(lock->l_policy_data));
731
732         ldlm_resource_unlink_lock(lock);
733         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
734                    &lock->l_resource->lr_name,
735                    sizeof(lock->l_resource->lr_name)) != 0) {
736                 ldlm_lock_change_resource(ns, lock,
737                                          dlm_req->lock_desc.l_resource.lr_name);
738                 LDLM_DEBUG(lock, "completion AST, new resource");
739         }
740
741         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
742                 lock->l_flags |= LDLM_FL_CBPENDING;
743                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
744         }
745
746         lock->l_resource->lr_tmp = &ast_list;
747         ldlm_grant_lock(lock, req, sizeof(*req), 1);
748         lock->l_resource->lr_tmp = NULL;
749         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
750         l_unlock(&ns->ns_lock);
751         LDLM_LOCK_PUT(lock);
752
753         ldlm_run_ast_work(ns, &ast_list);
754
755         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
756                           lock);
757         EXIT;
758 }
759
760 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
761 {
762         req->rq_status = rc;
763         rc = lustre_pack_reply(req, 0, NULL, NULL);
764         if (rc)
765                 return rc;
766         return ptlrpc_reply(req);
767 }
768
769 #ifdef __KERNEL__
770 static int ldlm_bl_to_thread(struct ldlm_state *ldlm, struct ldlm_namespace *ns,
771                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
772 {
773         struct ldlm_bl_pool *blp = ldlm->ldlm_bl_pool;
774         struct ldlm_bl_work_item *blwi;
775         ENTRY;
776
777         OBD_ALLOC(blwi, sizeof(*blwi));
778         if (blwi == NULL)
779                 RETURN(-ENOMEM);
780
781         blwi->blwi_ns = ns;
782         blwi->blwi_ld = *ld;
783         blwi->blwi_lock = lock;
784
785         spin_lock(&blp->blp_lock);
786         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
787         wake_up(&blp->blp_waitq);
788         spin_unlock(&blp->blp_lock);
789
790         RETURN(0);
791 }
792 #endif
793
794 static int ldlm_callback_handler(struct ptlrpc_request *req)
795 {
796         struct ldlm_namespace *ns;
797         struct ldlm_request *dlm_req;
798         struct ldlm_lock *lock;
799         char str[PTL_NALFMT_SIZE];
800         int rc;
801         ENTRY;
802
803         /* Requests arrive in sender's byte order.  The ptlrpc service
804          * handler has already checked and, if necessary, byte-swapped the
805          * incoming request message body, but I am responsible for the
806          * message buffers. */
807
808         if (req->rq_export == NULL) {
809                 struct ldlm_request *dlm_req;
810
811                 CDEBUG(D_RPCTRACE, "operation %d from nid "LPX64" (%s) with bad "
812                        "export cookie "LPX64" (ptl req %d/rep %d); this is "
813                        "normal if this node rebooted with a lock held\n",
814                        req->rq_reqmsg->opc, req->rq_connection->c_peer.peer_nid,
815                        portals_nid2str(req->rq_connection->c_peer.peer_ni->pni_number,
816                                        req->rq_connection->c_peer.peer_nid, str),
817                        req->rq_reqmsg->handle.cookie,
818                        req->rq_request_portal, req->rq_reply_portal);
819
820                 dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
821                                              lustre_swab_ldlm_request);
822                 if (dlm_req != NULL)
823                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
824                                dlm_req->lock_handle1.cookie);
825
826                 ldlm_callback_reply(req, -ENOTCONN);
827                 RETURN(0);
828         }
829
830         if (req->rq_reqmsg->opc == LDLM_BL_CALLBACK) {
831                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
832         } else if (req->rq_reqmsg->opc == LDLM_CP_CALLBACK) {
833                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
834         } else if (req->rq_reqmsg->opc == OBD_LOG_CANCEL) {
835                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
836         } else if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_CREATE) {
837                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
838         } else if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_NEXT_BLOCK) {
839                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
840         } else if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_READ_HEADER) {
841                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
842         } else if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_CLOSE) {
843                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
844         } else {
845                 ldlm_callback_reply(req, -EPROTO);
846                 RETURN(0);
847         }
848
849         LASSERT(req->rq_export != NULL);
850         LASSERT(req->rq_export->exp_obd != NULL);
851
852 #ifdef ENABLE_ORPHANS
853         /* FIXME - how to send reply */
854         if (req->rq_reqmsg->opc == OBD_LOG_CANCEL) {
855                 int rc = llog_origin_handle_cancel(req);
856                 ldlm_callback_reply(req, rc);
857                 RETURN(0);
858         }
859
860         if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_CREATE) {
861                 int rc = llog_origin_handle_create(req);
862                 req->rq_status = rc;
863                 ptlrpc_reply(req);
864                 RETURN(0);
865         }
866
867         if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_NEXT_BLOCK) {
868                 int rc = llog_origin_handle_next_block(req);
869                 req->rq_status = rc;
870                 ptlrpc_reply(req);
871                 RETURN(0);
872         }
873
874         if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_READ_HEADER) {
875                 int rc = llog_origin_handle_read_header(req);
876                 req->rq_status = rc;
877                 ptlrpc_reply(req);
878                 RETURN(0);
879         }
880
881         if (req->rq_reqmsg->opc == LLOG_ORIGIN_HANDLE_CLOSE) {
882                 int rc = llog_origin_handle_close(req);
883                 ldlm_callback_reply(req, rc);
884                 RETURN(0);
885         }
886 #endif
887         ns = req->rq_export->exp_obd->obd_namespace;
888         LASSERT(ns != NULL);
889
890         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
891                                       lustre_swab_ldlm_request);
892         if (dlm_req == NULL) {
893                 CERROR ("can't unpack dlm_req\n");
894                 ldlm_callback_reply (req, -EPROTO);
895                 RETURN (0);
896         }
897
898         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle1);
899         if (!lock) {
900                 CDEBUG(D_INODE, "callback on lock "LPX64" - lock disappeared\n",
901                        dlm_req->lock_handle1.cookie);
902                 ldlm_callback_reply(req, -EINVAL);
903                 RETURN(0);
904         }
905
906         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
907         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
908
909         /* We want the ost thread to get this reply so that it can respond
910          * to ost requests (write cache writeback) that might be triggered
911          * in the callback.
912          *
913          * But we'd also like to be able to indicate in the reply that we're
914          * cancelling right now, because it's unused, or have an intent result
915          * in the reply, so we might have to push the responsibility for sending
916          * the reply down into the AST handlers, alas. */
917         if (req->rq_reqmsg->opc != LDLM_BL_CALLBACK)
918                 ldlm_callback_reply(req, 0);
919
920         switch (req->rq_reqmsg->opc) {
921         case LDLM_BL_CALLBACK:
922                 CDEBUG(D_INODE, "blocking ast\n");
923 #ifdef __KERNEL__
924                 rc = ldlm_bl_to_thread(ldlm, ns, &dlm_req->lock_desc, lock);
925                 ldlm_callback_reply(req, rc);
926 #else
927                 rc = 0;
928                 ldlm_callback_reply(req, rc);
929                 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
930 #endif
931                 break;
932         case LDLM_CP_CALLBACK:
933                 CDEBUG(D_INODE, "completion ast\n");
934                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
935                 break;
936         default:
937                 LBUG();                         /* checked above */
938         }
939
940         RETURN(0);
941 }
942
943 static int ldlm_cancel_handler(struct ptlrpc_request *req)
944 {
945         int rc;
946         ENTRY;
947
948         /* Requests arrive in sender's byte order.  The ptlrpc service
949          * handler has already checked and, if necessary, byte-swapped the
950          * incoming request message body, but I am responsible for the
951          * message buffers. */
952
953         if (req->rq_export == NULL) {
954                 struct ldlm_request *dlm_req;
955                 CERROR("operation %d with bad export (ptl req %d/rep %d)\n",
956                        req->rq_reqmsg->opc, req->rq_request_portal,
957                        req->rq_reply_portal);
958                 CERROR("--> export cookie: "LPX64"\n",
959                        req->rq_reqmsg->handle.cookie);
960                 dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
961                                              lustre_swab_ldlm_request);
962                 if (dlm_req != NULL)
963                         ldlm_lock_dump_handle(D_ERROR, &dlm_req->lock_handle1);
964                 RETURN(-ENOTCONN);
965         }
966
967         switch (req->rq_reqmsg->opc) {
968
969         /* XXX FIXME move this back to mds/handler.c, bug 249 */
970         case LDLM_CANCEL:
971                 CDEBUG(D_INODE, "cancel\n");
972                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
973                 rc = ldlm_handle_cancel(req);
974                 if (rc)
975                         break;
976                 RETURN(0);
977
978         default:
979                 CERROR("invalid opcode %d\n", req->rq_reqmsg->opc);
980                 RETURN(-EINVAL);
981         }
982
983         RETURN(0);
984 }
985
986 #ifdef __KERNEL__
987 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
988 {
989         struct ldlm_bl_work_item *blwi = NULL;
990
991         spin_lock(&blp->blp_lock);
992         if (!list_empty(&blp->blp_list)) {
993                 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
994                                   blwi_entry);
995                 list_del(&blwi->blwi_entry);
996         }
997         spin_unlock(&blp->blp_lock);
998
999         return blwi;
1000 }
1001
1002 struct ldlm_bl_thread_data {
1003         int                     bltd_num;
1004         struct ldlm_bl_pool     *bltd_blp;
1005 };
1006
1007 static int ldlm_bl_thread_main(void *arg)
1008 {
1009         struct ldlm_bl_thread_data *bltd = arg;
1010         struct ldlm_bl_pool *blp = bltd->bltd_blp;
1011         unsigned long flags;
1012         ENTRY;
1013
1014         /* XXX boiler-plate */
1015         {
1016                 char name[sizeof(current->comm)];
1017                 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1018                          bltd->bltd_num);
1019                 kportal_daemonize(name);
1020         }
1021         SIGNAL_MASK_LOCK(current, flags);
1022         sigfillset(&current->blocked);
1023         RECALC_SIGPENDING;
1024         SIGNAL_MASK_UNLOCK(current, flags);
1025
1026         atomic_inc(&blp->blp_num_threads);
1027         complete(&blp->blp_comp);
1028
1029         while(1) {
1030                 struct l_wait_info lwi = { 0 };
1031                 struct ldlm_bl_work_item *blwi = NULL;
1032
1033                 l_wait_event_exclusive(blp->blp_waitq,
1034                                        (blwi = ldlm_bl_get_work(blp)) != NULL,
1035                                        &lwi);
1036
1037                 if (blwi->blwi_ns == NULL)
1038                         break;
1039
1040                 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1041                                         blwi->blwi_lock);
1042                 OBD_FREE(blwi, sizeof(*blwi));
1043         }
1044
1045         atomic_dec(&blp->blp_num_threads);
1046         complete(&blp->blp_comp);
1047         RETURN(0);
1048 }
1049
1050 #endif
1051
1052 static int ldlm_setup(void);
1053 static int ldlm_cleanup(int force);
1054
1055 int ldlm_get_ref(void)
1056 {
1057         int rc = 0;
1058         down(&ldlm_ref_sem);
1059         if (++ldlm_refcount == 1) {
1060                 rc = ldlm_setup();
1061                 if (rc)
1062                         ldlm_refcount--;
1063         }
1064         up(&ldlm_ref_sem);
1065
1066         RETURN(rc);
1067 }
1068
1069 void ldlm_put_ref(int force)
1070 {
1071         down(&ldlm_ref_sem);
1072         if (ldlm_refcount == 1) {
1073                 int rc = ldlm_cleanup(force);
1074                 if (rc)
1075                         CERROR("ldlm_cleanup failed: %d\n", rc);
1076                 else
1077                         ldlm_refcount--;
1078         } else {
1079                 ldlm_refcount--;
1080         }
1081         up(&ldlm_ref_sem);
1082
1083         EXIT;
1084 }
1085
1086 static int ldlm_setup(void)
1087 {
1088         struct ldlm_bl_pool *blp;
1089         int rc = 0;
1090 #ifdef __KERNEL__
1091         int i;
1092 #endif
1093         ENTRY;
1094
1095         if (ldlm != NULL)
1096                 RETURN(-EALREADY);
1097
1098         OBD_ALLOC(ldlm, sizeof(*ldlm));
1099         if (ldlm == NULL)
1100                 RETURN(-ENOMEM);
1101
1102 #ifdef __KERNEL__
1103         rc = ldlm_proc_setup();
1104         if (rc != 0)
1105                 GOTO(out_free, rc);
1106 #endif
1107
1108         ldlm->ldlm_cb_service =
1109                 ptlrpc_init_svc(LDLM_NEVENTS, LDLM_NBUFS, LDLM_BUFSIZE,
1110                                 LDLM_MAXREQSIZE, LDLM_CB_REQUEST_PORTAL,
1111                                 LDLM_CB_REPLY_PORTAL,
1112                                 ldlm_callback_handler, "ldlm_cbd", 
1113                                 ldlm_svc_proc_dir);
1114
1115         if (!ldlm->ldlm_cb_service) {
1116                 CERROR("failed to start service\n");
1117                 GOTO(out_proc, rc = -ENOMEM);
1118         }
1119
1120         ldlm->ldlm_cancel_service =
1121                 ptlrpc_init_svc(LDLM_NEVENTS, LDLM_NBUFS, LDLM_BUFSIZE,
1122                                 LDLM_MAXREQSIZE, LDLM_CANCEL_REQUEST_PORTAL,
1123                                 LDLM_CANCEL_REPLY_PORTAL,
1124                                 ldlm_cancel_handler, "ldlm_canceld", 
1125                                 ldlm_svc_proc_dir);
1126
1127         if (!ldlm->ldlm_cancel_service) {
1128                 CERROR("failed to start service\n");
1129                 GOTO(out_proc, rc = -ENOMEM);
1130         }
1131
1132         OBD_ALLOC(blp, sizeof(*blp));
1133         if (blp == NULL)
1134                 GOTO(out_proc, rc = -ENOMEM);
1135         ldlm->ldlm_bl_pool = blp;
1136
1137         atomic_set(&blp->blp_num_threads, 0);
1138         init_waitqueue_head(&blp->blp_waitq);
1139         spin_lock_init(&blp->blp_lock);
1140
1141         INIT_LIST_HEAD(&blp->blp_list);
1142
1143 #ifdef __KERNEL__
1144         for (i = 0; i < LDLM_NUM_THREADS; i++) {
1145                 struct ldlm_bl_thread_data bltd = {
1146                         .bltd_num = i,
1147                         .bltd_blp = blp,
1148                 };
1149                 init_completion(&blp->blp_comp);
1150                 if (kernel_thread(ldlm_bl_thread_main, &bltd, 0) < 0) {
1151                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1152                         LBUG();
1153                         GOTO(out_thread, rc);
1154                 }
1155                 wait_for_completion(&blp->blp_comp);
1156         }
1157
1158         rc = ptlrpc_start_n_threads(NULL, ldlm->ldlm_cancel_service,
1159                                     LDLM_NUM_THREADS, "ldlm_cn");
1160         if (rc) {
1161                 LBUG();
1162                 GOTO(out_thread, rc);
1163         }
1164
1165         rc = ptlrpc_start_n_threads(NULL, ldlm->ldlm_cb_service,
1166                                     LDLM_NUM_THREADS, "ldlm_cb");
1167         if (rc) {
1168                 LBUG();
1169                 GOTO(out_thread, rc);
1170         }
1171
1172         INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1173         spin_lock_init(&expired_lock_thread.elt_lock);
1174         expired_lock_thread.elt_state = ELT_STOPPED;
1175         init_waitqueue_head(&expired_lock_thread.elt_waitq);
1176
1177         rc = kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FS);
1178         if (rc < 0) {
1179                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1180                 GOTO(out_thread, rc);
1181         }
1182
1183         wait_event(expired_lock_thread.elt_waitq,
1184                    expired_lock_thread.elt_state == ELT_READY);
1185
1186         INIT_LIST_HEAD(&waiting_locks_list);
1187         spin_lock_init(&waiting_locks_spinlock);
1188         waiting_locks_timer.function = waiting_locks_callback;
1189         waiting_locks_timer.data = 0;
1190         init_timer(&waiting_locks_timer);
1191 #endif
1192
1193         RETURN(0);
1194
1195 #ifdef __KERNEL__
1196  out_thread:
1197         ptlrpc_unregister_service(ldlm->ldlm_cancel_service);
1198         ptlrpc_unregister_service(ldlm->ldlm_cb_service);
1199 #endif
1200
1201  out_proc:
1202 #ifdef __KERNEL__
1203         ldlm_proc_cleanup();
1204  out_free:
1205 #endif
1206         OBD_FREE(ldlm, sizeof(*ldlm));
1207         ldlm = NULL;
1208         return rc;
1209 }
1210
1211 static int ldlm_cleanup(int force)
1212 {
1213 #ifdef __KERNEL__
1214         struct ldlm_bl_pool *blp = ldlm->ldlm_bl_pool;
1215 #endif
1216         ENTRY;
1217
1218         if (!list_empty(&ldlm_namespace_list)) {
1219                 CERROR("ldlm still has namespaces; clean these up first.\n");
1220                 ldlm_dump_all_namespaces();
1221                 RETURN(-EBUSY);
1222         }
1223
1224 #ifdef __KERNEL__
1225         while (atomic_read(&blp->blp_num_threads) > 0) {
1226                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1227
1228                 init_completion(&blp->blp_comp);
1229
1230                 spin_lock(&blp->blp_lock);
1231                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1232                 wake_up(&blp->blp_waitq);
1233                 spin_unlock(&blp->blp_lock);
1234
1235                 wait_for_completion(&blp->blp_comp);
1236         }
1237         OBD_FREE(blp, sizeof(*blp));
1238
1239         ptlrpc_stop_all_threads(ldlm->ldlm_cb_service);
1240         ptlrpc_unregister_service(ldlm->ldlm_cb_service);
1241         ptlrpc_stop_all_threads(ldlm->ldlm_cancel_service);
1242         ptlrpc_unregister_service(ldlm->ldlm_cancel_service);
1243         ldlm_proc_cleanup();
1244
1245         expired_lock_thread.elt_state = ELT_TERMINATE;
1246         wake_up(&expired_lock_thread.elt_waitq);
1247         wait_event(expired_lock_thread.elt_waitq,
1248                    expired_lock_thread.elt_state == ELT_STOPPED);
1249
1250 #endif
1251
1252         OBD_FREE(ldlm, sizeof(*ldlm));
1253         ldlm = NULL;
1254
1255         RETURN(0);
1256 }
1257
1258 int __init ldlm_init(void)
1259 {
1260         ldlm_resource_slab = kmem_cache_create("ldlm_resources",
1261                                                sizeof(struct ldlm_resource), 0,
1262                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
1263         if (ldlm_resource_slab == NULL)
1264                 return -ENOMEM;
1265
1266         ldlm_lock_slab = kmem_cache_create("ldlm_locks",
1267                                            sizeof(struct ldlm_lock), 0,
1268                                            SLAB_HWCACHE_ALIGN, NULL, NULL);
1269         if (ldlm_lock_slab == NULL) {
1270                 kmem_cache_destroy(ldlm_resource_slab);
1271                 return -ENOMEM;
1272         }
1273
1274         l_lock_init(&ldlm_handle_lock);
1275
1276         return 0;
1277 }
1278
1279 void __exit ldlm_exit(void)
1280 {
1281         if ( ldlm_refcount )
1282                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1283         if (kmem_cache_destroy(ldlm_resource_slab) != 0)
1284                 CERROR("couldn't free ldlm resource slab\n");
1285         if (kmem_cache_destroy(ldlm_lock_slab) != 0)
1286                 CERROR("couldn't free ldlm lock slab\n");
1287 }
1288
1289 /* ldlm_flock.c */
1290 EXPORT_SYMBOL(ldlm_flock_completion_ast);
1291
1292 /* ldlm_lock.c */
1293 EXPORT_SYMBOL(ldlm_lock2desc);
1294 EXPORT_SYMBOL(ldlm_register_intent);
1295 EXPORT_SYMBOL(ldlm_unregister_intent);
1296 EXPORT_SYMBOL(ldlm_lockname);
1297 EXPORT_SYMBOL(ldlm_typename);
1298 EXPORT_SYMBOL(ldlm_lock2handle);
1299 EXPORT_SYMBOL(__ldlm_handle2lock);
1300 EXPORT_SYMBOL(ldlm_lock_put);
1301 EXPORT_SYMBOL(ldlm_lock_match);
1302 EXPORT_SYMBOL(ldlm_lock_cancel);
1303 EXPORT_SYMBOL(ldlm_lock_addref);
1304 EXPORT_SYMBOL(ldlm_lock_decref);
1305 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1306 EXPORT_SYMBOL(ldlm_lock_change_resource);
1307 EXPORT_SYMBOL(ldlm_lock_set_data);
1308 EXPORT_SYMBOL(ldlm_it2str);
1309 EXPORT_SYMBOL(ldlm_lock_dump);
1310 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1311 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1312 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1313
1314 /* ldlm_request.c */
1315 EXPORT_SYMBOL(ldlm_completion_ast);
1316 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1317 EXPORT_SYMBOL(ldlm_cli_convert);
1318 EXPORT_SYMBOL(ldlm_cli_enqueue);
1319 EXPORT_SYMBOL(ldlm_cli_cancel);
1320 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1321 EXPORT_SYMBOL(ldlm_replay_locks);
1322 EXPORT_SYMBOL(ldlm_resource_foreach);
1323 EXPORT_SYMBOL(ldlm_namespace_foreach);
1324 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1325 EXPORT_SYMBOL(ldlm_change_cbdata);
1326
1327 /* ldlm_lockd.c */
1328 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1329 EXPORT_SYMBOL(ldlm_server_completion_ast);
1330 EXPORT_SYMBOL(ldlm_handle_enqueue);
1331 EXPORT_SYMBOL(ldlm_handle_cancel);
1332 EXPORT_SYMBOL(ldlm_handle_convert);
1333 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1334 EXPORT_SYMBOL(ldlm_get_ref);
1335 EXPORT_SYMBOL(ldlm_put_ref);
1336
1337 #if 0
1338 /* ldlm_test.c */
1339 EXPORT_SYMBOL(ldlm_test);
1340 EXPORT_SYMBOL(ldlm_regression_start);
1341 EXPORT_SYMBOL(ldlm_regression_stop);
1342 #endif
1343
1344 /* ldlm_resource.c */
1345 EXPORT_SYMBOL(ldlm_namespace_new);
1346 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1347 EXPORT_SYMBOL(ldlm_namespace_free);
1348 EXPORT_SYMBOL(ldlm_namespace_dump);
1349
1350 /* l_lock.c */
1351 EXPORT_SYMBOL(l_lock);
1352 EXPORT_SYMBOL(l_unlock);
1353
1354 /* ldlm_lib.c */
1355 EXPORT_SYMBOL(client_obd_setup);
1356 EXPORT_SYMBOL(client_obd_cleanup);
1357 EXPORT_SYMBOL(client_connect_import);
1358 EXPORT_SYMBOL(client_disconnect_export);
1359 EXPORT_SYMBOL(target_abort_recovery);
1360 EXPORT_SYMBOL(target_handle_connect);
1361 EXPORT_SYMBOL(target_destroy_export);
1362 EXPORT_SYMBOL(target_cancel_recovery_timer);
1363 EXPORT_SYMBOL(target_send_reply);
1364 EXPORT_SYMBOL(target_queue_recovery_request);
1365 EXPORT_SYMBOL(target_handle_ping);
1366 EXPORT_SYMBOL(target_handle_disconnect);
1367 EXPORT_SYMBOL(target_queue_final_reply);
1368 EXPORT_SYMBOL(ldlm_put_lock_into_req);