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