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