Whamcloud - gitweb
Don't crash in expired_lock_main() with racing client eviction/lock completion.
[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                 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 obd_export *export;
137                         struct ldlm_lock *lock;
138
139                         lock = list_entry(expired->next, struct ldlm_lock,
140                                           l_pending_chain);
141                         if ((void *)lock < LP_POISON + PAGE_SIZE &&
142                             (void *)lock >= LP_POISON) {
143                                 CERROR("free lock on elt list %p\n", lock);
144                                 LBUG();
145                         }
146                         list_del_init(&lock->l_pending_chain);
147                         if ((void *)lock->l_export < LP_POISON + PAGE_SIZE &&
148                             (void *)lock->l_export >= LP_POISON + PAGE_SIZE) {
149                                 CERROR("lock with free export on elt list %p\n",
150                                        export);
151                                 lock->l_export = NULL;
152                                 LDLM_ERROR(lock, "free export\n");
153                                 continue;
154                         }
155                         export = class_export_get(lock->l_export);
156                         spin_unlock_bh(&expired_lock_thread.elt_lock);
157
158                         ptlrpc_fail_export(export);
159                         class_export_put(export);
160                         spin_lock_bh(&expired_lock_thread.elt_lock);
161                 }
162                 spin_unlock_bh(&expired_lock_thread.elt_lock);
163
164                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
165                         break;
166         }
167
168         expired_lock_thread.elt_state = ELT_STOPPED;
169         wake_up(&expired_lock_thread.elt_waitq);
170         RETURN(0);
171 }
172
173 static void waiting_locks_callback(unsigned long unused)
174 {
175         struct ldlm_lock *lock;
176         char str[PTL_NALFMT_SIZE];
177
178         spin_lock_bh(&waiting_locks_spinlock);
179         while (!list_empty(&waiting_locks_list)) {
180                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
181                                   l_pending_chain);
182
183                 if ((lock->l_callback_timeout > jiffies) ||
184                     (lock->l_req_mode == LCK_GROUP))
185                         break;
186
187                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
188                            "%s@%s nid "LPX64" (%s) ",
189                            lock->l_export->exp_client_uuid.uuid,
190                            lock->l_export->exp_connection->c_remote_uuid.uuid,
191                            lock->l_export->exp_connection->c_peer.peer_nid,
192                            portals_nid2str(lock->l_export->exp_connection->c_peer.peer_ni->pni_number,
193                                            lock->l_export->exp_connection->c_peer.peer_nid,
194                                            str));
195
196                 spin_lock_bh(&expired_lock_thread.elt_lock);
197                 list_del(&lock->l_pending_chain);
198                 list_add(&lock->l_pending_chain,
199                          &expired_lock_thread.elt_expired_locks);
200                 spin_unlock_bh(&expired_lock_thread.elt_lock);
201                 wake_up(&expired_lock_thread.elt_waitq);
202         }
203
204         /*
205          * Make sure the timer will fire again if we have any locks
206          * left.
207          */
208         if (!list_empty(&waiting_locks_list)) {
209                 unsigned long timeout_rounded;
210                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
211                                   l_pending_chain);
212                 timeout_rounded = round_timeout(lock->l_callback_timeout);
213                 mod_timer(&waiting_locks_timer, timeout_rounded);
214         }
215         spin_unlock_bh(&waiting_locks_spinlock);
216 }
217
218 /*
219  * Indicate that we're waiting for a client to call us back cancelling a given
220  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
221  * timer to fire appropriately.  (We round up to the next second, to avoid
222  * floods of timer firings during periods of high lock contention and traffic).
223  */
224 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
225 {
226         unsigned long timeout_rounded;
227
228         spin_lock_bh(&waiting_locks_spinlock);
229         if (!list_empty(&lock->l_pending_chain)) {
230                 LDLM_DEBUG(lock, "not re-adding to wait list");
231                 spin_unlock_bh(&waiting_locks_spinlock);
232                 return 0;
233         }
234         LDLM_DEBUG(lock, "adding to wait list");
235
236         lock->l_callback_timeout = jiffies + (obd_timeout * HZ / 2);
237
238         timeout_rounded = round_timeout(lock->l_callback_timeout);
239
240         if (timeout_rounded < waiting_locks_timer.expires ||
241             !timer_pending(&waiting_locks_timer)) {
242                 mod_timer(&waiting_locks_timer, timeout_rounded);
243         }
244         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
245         spin_unlock_bh(&waiting_locks_spinlock);
246         return 1;
247 }
248
249 /*
250  * Remove a lock from the pending list, likely because it had its cancellation
251  * callback arrive without incident.  This adjusts the lock-timeout timer if
252  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
253  */
254 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
255 {
256         struct list_head *list_next;
257
258         if (lock->l_export == NULL) {
259                 /* We don't have a "waiting locks list" on clients. */
260                 LDLM_DEBUG(lock, "client lock: no-op");
261                 return 0;
262         }
263
264         spin_lock_bh(&waiting_locks_spinlock);
265
266         if (list_empty(&lock->l_pending_chain)) {
267                 spin_unlock_bh(&waiting_locks_spinlock);
268                 LDLM_DEBUG(lock, "wasn't waiting");
269                 return 0;
270         }
271
272         list_next = lock->l_pending_chain.next;
273         if (lock->l_pending_chain.prev == &waiting_locks_list) {
274                 /* Removing the head of the list, adjust timer. */
275                 if (list_next == &waiting_locks_list) {
276                         /* No more, just cancel. */
277                         del_timer(&waiting_locks_timer);
278                 } else {
279                         struct ldlm_lock *next;
280                         next = list_entry(list_next, struct ldlm_lock,
281                                           l_pending_chain);
282                         mod_timer(&waiting_locks_timer,
283                                   round_timeout(next->l_callback_timeout));
284                 }
285         }
286
287         spin_lock_bh(&expired_lock_thread.elt_lock);
288         list_del_init(&lock->l_pending_chain);
289         spin_unlock_bh(&expired_lock_thread.elt_lock);
290
291         spin_unlock_bh(&waiting_locks_spinlock);
292         LDLM_DEBUG(lock, "removed");
293         return 1;
294 }
295
296 #else /* !__KERNEL__ */
297
298 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
299 {
300         RETURN(1);
301 }
302
303 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
304 {
305         RETURN(0);
306 }
307
308 #endif /* __KERNEL__ */
309
310 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc, char *ast_type)
311 {
312         const struct ptlrpc_connection *conn = lock->l_export->exp_connection;
313         char str[PTL_NALFMT_SIZE];
314
315         CERROR("%s AST failed (%d) for res "LPU64"/"LPU64
316                ", mode %s: evicting client %s@%s NID "LPX64" (%s)\n",
317                ast_type, rc,
318                lock->l_resource->lr_name.name[0],
319                lock->l_resource->lr_name.name[1],
320                ldlm_lockname[lock->l_granted_mode],
321                lock->l_export->exp_client_uuid.uuid,
322                conn->c_remote_uuid.uuid, conn->c_peer.peer_nid,
323                portals_nid2str(conn->c_peer.peer_ni->pni_number,
324                                conn->c_peer.peer_nid, str));
325         ptlrpc_fail_export(lock->l_export);
326 }
327
328 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
329                              struct ldlm_lock_desc *desc,
330                              void *data, int flag)
331 {
332         struct ldlm_request *body;
333         struct ptlrpc_request *req;
334         int rc = 0, size = sizeof(*body);
335         ENTRY;
336
337         if (flag == LDLM_CB_CANCELING) {
338                 /* Don't need to do anything here. */
339                 RETURN(0);
340         }
341
342         LASSERT(lock);
343
344         l_lock(&lock->l_resource->lr_namespace->ns_lock);
345         if (lock->l_granted_mode != lock->l_req_mode) {
346                 /* this blocking AST will be communicated as part of the
347                  * completion AST instead */
348                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
349                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");                 RETURN(0);
350         }
351
352         if (lock->l_destroyed) {
353                 /* What's the point? */
354                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
355                 RETURN(0);
356         }
357
358 #if 0
359         if (LTIME_S(CURRENT_TIME) - lock->l_export->exp_last_request_time > 30){
360                 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
361                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
362                 RETURN(-ETIMEDOUT);
363         }
364 #endif
365
366         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
367                               LDLM_BL_CALLBACK, 1, &size, NULL);
368         if (req == NULL) {
369                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
370                 RETURN(-ENOMEM);
371         }
372
373         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
374         memcpy(&body->lock_handle1, &lock->l_remote_handle,
375                sizeof(body->lock_handle1));
376         memcpy(&body->lock_desc, desc, sizeof(*desc));
377         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
378
379         LDLM_DEBUG(lock, "server preparing blocking AST");
380         req->rq_replen = lustre_msg_size(0, NULL);
381
382         if (lock->l_granted_mode == lock->l_req_mode)
383                 ldlm_add_waiting_lock(lock);
384         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
385
386         req->rq_send_state = LUSTRE_IMP_FULL;
387         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
388         rc = ptlrpc_queue_wait(req);
389         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
390                 LASSERT(lock->l_export);
391                 if (lock->l_export->exp_libclient) {
392                         CDEBUG(D_HA, "BLOCKING AST to liblustre client (nid "
393                                LPU64") timeout, simply cancel lock 0x%p\n",
394                                req->rq_peer.peer_nid, lock);
395                         ldlm_lock_cancel(lock);
396                         rc = -ERESTART;
397                 } else {
398                         ldlm_del_waiting_lock(lock);
399                         ldlm_failed_ast(lock, rc, "blocking");
400                 }
401         } else if (rc) {
402                 if (rc == -EINVAL)
403                         CDEBUG(D_DLMTRACE, "client (nid "LPU64") returned %d "
404                                "from blocking AST for lock %p--normal race\n",
405                                req->rq_peer.peer_nid,
406                                req->rq_repmsg->status, lock);
407                 else if (rc == -ENOTCONN)
408                         CDEBUG(D_DLMTRACE, "client (nid "LPU64") returned %d "
409                                "from blocking AST for lock %p--this client was "
410                                "probably rebooted while it held a lock, nothing"
411                                " serious\n",req->rq_peer.peer_nid,
412                                req->rq_repmsg->status, lock);
413                 else
414                         CDEBUG(D_ERROR, "client (nid "LPU64") returned %d "
415                                "from blocking AST for lock %p\n",
416                                req->rq_peer.peer_nid,
417                                (req->rq_repmsg != NULL)?
418                                req->rq_repmsg->status : 0,
419                                lock);
420                 LDLM_DEBUG(lock, "client sent rc %d rq_status %d from blocking "
421                            "AST", rc, req->rq_status);
422                 ldlm_lock_cancel(lock);
423                 /* Server-side AST functions are called from ldlm_reprocess_all,
424                  * which needs to be told to please restart its reprocessing. */
425                 rc = -ERESTART;
426         }
427
428         ptlrpc_req_finished(req);
429
430         RETURN(rc);
431 }
432
433 /* XXX copied from ptlrpc/service.c */
434 static long timeval_sub(struct timeval *large, struct timeval *small)
435 {
436         return (large->tv_sec - small->tv_sec) * 1000000 +
437                 (large->tv_usec - small->tv_usec);
438 }
439
440 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
441 {
442         struct ldlm_request *body;
443         struct ptlrpc_request *req;
444         struct timeval granted_time;
445         long total_enqueue_wait;
446         int rc = 0, size[2] = {sizeof(*body)}, buffers = 1;
447         ENTRY;
448
449         LASSERT(lock != NULL);
450
451         do_gettimeofday(&granted_time);
452         total_enqueue_wait = timeval_sub(&granted_time, &lock->l_enqueued_time);
453
454         if (total_enqueue_wait / 1000000 > obd_timeout)
455                 LDLM_ERROR(lock, "enqueue wait took %ldus", total_enqueue_wait);
456
457         if (lock->l_resource->lr_lvb_len) {
458                 buffers = 2;
459                 size[1] = lock->l_resource->lr_lvb_len;
460         }
461
462         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
463                               LDLM_CP_CALLBACK, buffers, size, NULL);
464         if (req == NULL)
465                 RETURN(-ENOMEM);
466
467         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
468         memcpy(&body->lock_handle1, &lock->l_remote_handle,
469                sizeof(body->lock_handle1));
470         body->lock_flags = flags;
471         ldlm_lock2desc(lock, &body->lock_desc);
472
473         if (buffers == 2) {
474                 void *lvb = lustre_msg_buf(req->rq_reqmsg, 1,
475                                            lock->l_resource->lr_lvb_len);
476                 memcpy(lvb, lock->l_resource->lr_lvb_data,
477                        lock->l_resource->lr_lvb_len);
478         }
479
480         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
481                    total_enqueue_wait);
482         req->rq_replen = lustre_msg_size(0, NULL);
483
484         req->rq_send_state = LUSTRE_IMP_FULL;
485         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
486
487         /* We only send real blocking ASTs after the lock is granted */
488         l_lock(&lock->l_resource->lr_namespace->ns_lock);
489         if (lock->l_flags & LDLM_FL_AST_SENT) {
490                 body->lock_flags |= LDLM_FL_AST_SENT;
491                 ldlm_add_waiting_lock(lock); /* start the lock-timeout clock */
492         }
493         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
494
495         rc = ptlrpc_queue_wait(req);
496         if ((rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) &&
497              !lock->l_export->exp_libclient) {
498                 ldlm_del_waiting_lock(lock);
499                 ldlm_failed_ast(lock, rc, "completion");
500         } else if (rc == -EINVAL) {
501                 LDLM_DEBUG(lock, "lost the race -- client no longer has this "
502                            "lock");
503         } else if (rc) {
504                 LDLM_ERROR(lock, "client sent rc %d rq_status %d from "
505                            "completion AST", rc, req->rq_status);
506                 ldlm_lock_cancel(lock);
507                 /* Server-side AST functions are called from ldlm_reprocess_all,
508                  * which needs to be told to please restart its reprocessing. */
509                 rc = -ERESTART;
510         }
511         ptlrpc_req_finished(req);
512
513         RETURN(rc);
514 }
515
516 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
517 {
518         struct ldlm_resource *res = lock->l_resource;
519         struct ldlm_request *body;
520         struct ptlrpc_request *req;
521         int rc = 0, size = sizeof(*body);
522         ENTRY;
523
524         LASSERT(lock != NULL);
525
526         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
527                               LDLM_GL_CALLBACK, 1, &size, NULL);
528         if (req == NULL)
529                 RETURN(-ENOMEM);
530
531         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
532         memcpy(&body->lock_handle1, &lock->l_remote_handle,
533                sizeof(body->lock_handle1));
534         ldlm_lock2desc(lock, &body->lock_desc);
535
536         size = lock->l_resource->lr_lvb_len;
537         req->rq_replen = lustre_msg_size(1, &size);
538
539         req->rq_send_state = LUSTRE_IMP_FULL;
540         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
541
542         rc = ptlrpc_queue_wait(req);
543         if ((rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) &&
544             !lock->l_export->exp_libclient) {
545                 ldlm_del_waiting_lock(lock);
546                 ldlm_failed_ast(lock, rc, "glimpse");
547         } else if (rc == -EINVAL) {
548                 LDLM_DEBUG(lock, "lost the race -- client no longer has this "
549                            "lock");
550         } else if (rc == -ELDLM_NO_LOCK_DATA) {
551                 LDLM_DEBUG(lock, "lost a race -- client has a lock, but no "
552                            "inode");
553         } else if (rc) {
554                 LDLM_ERROR(lock, "client sent rc %d rq_status %d from "
555                            "glimpse AST", rc, req->rq_status);
556         } else {
557                 rc = res->lr_namespace->ns_lvbo->lvbo_update
558                         (res, req->rq_repmsg, 0, 1);
559         }
560         ptlrpc_req_finished(req);
561         RETURN(rc);
562 }
563
564 int ldlm_handle_enqueue(struct ptlrpc_request *req,
565                         ldlm_completion_callback completion_callback,
566                         ldlm_blocking_callback blocking_callback,
567                         ldlm_glimpse_callback glimpse_callback)
568 {
569         struct obd_device *obddev = req->rq_export->exp_obd;
570         struct ldlm_reply *dlm_rep;
571         struct ldlm_request *dlm_req;
572         int rc = 0, size[2] = {sizeof(*dlm_rep)};
573         __u32 flags;
574         ldlm_error_t err = ELDLM_OK;
575         struct ldlm_lock *lock = NULL;
576         void *cookie = NULL;
577         ENTRY;
578
579         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
580
581         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
582                                       lustre_swab_ldlm_request);
583         if (dlm_req == NULL) {
584                 CERROR ("Can't unpack dlm_req\n");
585                 GOTO(out, rc = -EFAULT);
586         }
587
588         flags = dlm_req->lock_flags;
589
590         /* The lock's callback data might be set in the policy function */
591         lock = ldlm_lock_create(obddev->obd_namespace, &dlm_req->lock_handle2,
592                                 dlm_req->lock_desc.l_resource.lr_name,
593                                 dlm_req->lock_desc.l_resource.lr_type,
594                                 dlm_req->lock_desc.l_req_mode,
595                                 blocking_callback, completion_callback,
596                                 glimpse_callback, NULL, 0);
597         if (!lock)
598                 GOTO(out, rc = -ENOMEM);
599
600         do_gettimeofday(&lock->l_enqueued_time);
601         memcpy(&lock->l_remote_handle, &dlm_req->lock_handle1,
602                sizeof(lock->l_remote_handle));
603         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
604
605         LASSERT(req->rq_export);
606         lock->l_export = class_export_get(req->rq_export);
607         l_lock(&lock->l_resource->lr_namespace->ns_lock);
608         list_add(&lock->l_export_chain,
609                  &lock->l_export->exp_ldlm_data.led_held_locks);
610         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
611
612         if (flags & LDLM_FL_HAS_INTENT) {
613                 /* In this case, the reply buffer is allocated deep in
614                  * local_lock_enqueue by the policy function. */
615                 cookie = req;
616         } else {
617                 int buffers = 1;
618                 if (lock->l_resource->lr_lvb_len) {
619                         size[1] = lock->l_resource->lr_lvb_len;
620                         buffers = 2;
621                 }
622
623                 rc = lustre_pack_reply(req, buffers, size, NULL);
624                 if (rc)
625                         GOTO(out, rc);
626         }
627
628         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
629                 memcpy(&lock->l_policy_data, &dlm_req->lock_desc.l_policy_data,
630                        sizeof(ldlm_policy_data_t));
631         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
632                 memcpy(&lock->l_req_extent, &lock->l_policy_data.l_extent,
633                        sizeof(lock->l_req_extent));
634
635         err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, &flags);
636         if (err)
637                 GOTO(out, err);
638
639         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
640         dlm_rep->lock_flags = flags;
641
642         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
643         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
644
645         /* We never send a blocking AST until the lock is granted, but
646          * we can tell it right now */
647         l_lock(&lock->l_resource->lr_namespace->ns_lock);
648         if (lock->l_flags & LDLM_FL_AST_SENT) {
649                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
650                 if (lock->l_granted_mode == lock->l_req_mode)
651                         ldlm_add_waiting_lock(lock);
652         }
653         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
654
655         EXIT;
656  out:
657         req->rq_status = err;
658         if (req->rq_reply_state == NULL) {
659                 err = lustre_pack_reply(req, 0, NULL, NULL);
660                 if (rc == 0)
661                         rc = err;
662         }
663
664         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
665          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
666         if (lock) {
667                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
668                            "(err=%d, rc=%d)", err, rc);
669
670                 if (lock->l_resource->lr_lvb_len > 0) {
671                         void *lvb = lustre_msg_buf(req->rq_repmsg, 1,
672                                                   lock->l_resource->lr_lvb_len);
673                         memcpy(lvb, lock->l_resource->lr_lvb_data,
674                                lock->l_resource->lr_lvb_len);
675                 }
676
677                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
678                         ldlm_reprocess_all(lock->l_resource);
679                 LDLM_LOCK_PUT(lock);
680         }
681         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
682                           lock, rc);
683
684         return rc;
685 }
686
687 int ldlm_handle_convert(struct ptlrpc_request *req)
688 {
689         struct ldlm_request *dlm_req;
690         struct ldlm_reply *dlm_rep;
691         struct ldlm_lock *lock;
692         int rc, size = sizeof(*dlm_rep);
693         ENTRY;
694
695         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
696                                       lustre_swab_ldlm_request);
697         if (dlm_req == NULL) {
698                 CERROR ("Can't unpack dlm_req\n");
699                 RETURN (-EFAULT);
700         }
701
702         rc = lustre_pack_reply(req, 1, &size, NULL);
703         if (rc) {
704                 CERROR("out of memory\n");
705                 RETURN(-ENOMEM);
706         }
707         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
708         dlm_rep->lock_flags = dlm_req->lock_flags;
709
710         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
711         if (!lock) {
712                 req->rq_status = EINVAL;
713         } else {
714                 LDLM_DEBUG(lock, "server-side convert handler START");
715                 ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
716                                   &dlm_rep->lock_flags);
717                 if (ldlm_del_waiting_lock(lock))
718                         CDEBUG(D_DLMTRACE, "converted waiting lock %p\n", lock);
719                 req->rq_status = 0;
720         }
721
722         if (lock) {
723                 ldlm_reprocess_all(lock->l_resource);
724                 LDLM_DEBUG(lock, "server-side convert handler END");
725                 LDLM_LOCK_PUT(lock);
726         } else
727                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
728
729         RETURN(0);
730 }
731
732 int ldlm_handle_cancel(struct ptlrpc_request *req)
733 {
734         struct ldlm_request *dlm_req;
735         struct ldlm_lock *lock;
736         struct ldlm_resource *res;
737         char str[PTL_NALFMT_SIZE];
738         int rc;
739         ENTRY;
740
741         dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
742                                       lustre_swab_ldlm_request);
743         if (dlm_req == NULL) {
744                 CERROR("bad request buffer for cancel\n");
745                 RETURN(-EFAULT);
746         }
747
748         rc = lustre_pack_reply(req, 0, NULL, NULL);
749         if (rc) {
750                 CERROR("out of memory\n");
751                 RETURN(-ENOMEM);
752         }
753
754         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
755         if (!lock) {
756                 CERROR("received cancel for unknown lock cookie "LPX64
757                        " from client %s nid "LPX64" (%s)\n",
758                        dlm_req->lock_handle1.cookie,
759                        req->rq_export->exp_client_uuid.uuid,
760                        req->rq_peer.peer_nid,
761                        portals_nid2str(req->rq_peer.peer_ni->pni_number,
762                                        req->rq_peer.peer_nid, str));
763                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
764                                   "(cookie "LPU64")",
765                                   dlm_req->lock_handle1.cookie);
766                 req->rq_status = ESTALE;
767         } else {
768                 LDLM_DEBUG(lock, "server-side cancel handler START");
769                 res = lock->l_resource;
770                 if (res && res->lr_namespace->ns_lvbo &&
771                     res->lr_namespace->ns_lvbo->lvbo_update) {
772                         (void)res->lr_namespace->ns_lvbo->lvbo_update
773                                 (res, NULL, 0, 0);
774                                 //(res, req->rq_reqmsg, 1);
775                 }
776
777                 ldlm_lock_cancel(lock);
778                 if (ldlm_del_waiting_lock(lock))
779                         CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
780                 req->rq_status = rc;
781         }
782
783         if (ptlrpc_reply(req) != 0)
784                 LBUG();
785
786         if (lock) {
787                 ldlm_reprocess_all(lock->l_resource);
788                 LDLM_DEBUG(lock, "server-side cancel handler END");
789                 LDLM_LOCK_PUT(lock);
790         }
791
792         RETURN(0);
793 }
794
795 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
796                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
797 {
798         int do_ast;
799         ENTRY;
800
801         l_lock(&ns->ns_lock);
802         LDLM_DEBUG(lock, "client blocking AST callback handler START");
803
804         lock->l_flags |= LDLM_FL_CBPENDING;
805         do_ast = (!lock->l_readers && !lock->l_writers);
806
807         if (do_ast) {
808                 LDLM_DEBUG(lock, "already unused, calling "
809                            "callback (%p)", lock->l_blocking_ast);
810                 if (lock->l_blocking_ast != NULL) {
811                         l_unlock(&ns->ns_lock);
812                         l_check_no_ns_lock(ns);
813                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
814                                              LDLM_CB_BLOCKING);
815                         l_lock(&ns->ns_lock);
816                 }
817         } else {
818                 LDLM_DEBUG(lock, "Lock still has references, will be"
819                            " cancelled later");
820         }
821
822         LDLM_DEBUG(lock, "client blocking callback handler END");
823         l_unlock(&ns->ns_lock);
824         LDLM_LOCK_PUT(lock);
825         EXIT;
826 }
827
828 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
829                                     struct ldlm_namespace *ns,
830                                     struct ldlm_request *dlm_req,
831                                     struct ldlm_lock *lock)
832 {
833         LIST_HEAD(ast_list);
834         ENTRY;
835
836         l_lock(&ns->ns_lock);
837         LDLM_DEBUG(lock, "client completion callback handler START");
838
839         /* If we receive the completion AST before the actual enqueue returned,
840          * then we might need to switch lock modes, resources, or extents. */
841         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
842                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
843                 LDLM_DEBUG(lock, "completion AST, new lock mode");
844         }
845
846         if (lock->l_resource->lr_type != LDLM_PLAIN) {
847                 memcpy(&lock->l_policy_data, &dlm_req->lock_desc.l_policy_data,
848                        sizeof(lock->l_policy_data));
849                 LDLM_DEBUG(lock, "completion AST, new policy data");
850         }
851
852         ldlm_resource_unlink_lock(lock);
853         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
854                    &lock->l_resource->lr_name,
855                    sizeof(lock->l_resource->lr_name)) != 0) {
856                 ldlm_lock_change_resource(ns, lock,
857                                          dlm_req->lock_desc.l_resource.lr_name);
858                 LDLM_DEBUG(lock, "completion AST, new resource");
859         }
860
861         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
862                 lock->l_flags |= LDLM_FL_CBPENDING;
863                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
864         }
865
866         if (lock->l_lvb_len) {
867                 void *lvb;
868                 lvb = lustre_swab_reqbuf(req, 1, lock->l_lvb_len,
869                                          lock->l_lvb_swabber);
870                 if (lvb == NULL) {
871                         LDLM_ERROR(lock, "completion AST did not contain "
872                                    "expected LVB!");
873                 } else {
874                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
875                 }
876         }
877
878         lock->l_resource->lr_tmp = &ast_list;
879         ldlm_grant_lock(lock, req, sizeof(*req), 1);
880         lock->l_resource->lr_tmp = NULL;
881         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
882         l_unlock(&ns->ns_lock);
883         LDLM_LOCK_PUT(lock);
884
885         ldlm_run_ast_work(ns, &ast_list);
886
887         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
888                           lock);
889         EXIT;
890 }
891
892 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
893                                     struct ldlm_namespace *ns,
894                                     struct ldlm_request *dlm_req,
895                                     struct ldlm_lock *lock)
896 {
897         int rc = -ENOSYS;
898         ENTRY;
899
900         l_lock(&ns->ns_lock);
901         LDLM_DEBUG(lock, "client glimpse AST callback handler");
902
903         if (lock->l_glimpse_ast != NULL) {
904                 l_unlock(&ns->ns_lock);
905                 l_check_no_ns_lock(ns);
906                 rc = lock->l_glimpse_ast(lock, req);
907                 l_lock(&ns->ns_lock);
908         }
909
910         if (req->rq_repmsg != NULL) {
911                 ptlrpc_reply(req);
912         } else {
913                 req->rq_status = rc;
914                 ptlrpc_error(req);
915         }
916
917         if (lock->l_granted_mode == LCK_PW &&
918             !lock->l_readers && !lock->l_writers &&
919             time_after(jiffies, lock->l_last_used + 10 * HZ)) {
920                 l_unlock(&ns->ns_lock);
921                 ldlm_handle_bl_callback(ns, NULL, lock);
922                 EXIT;
923                 return;
924         }
925
926         l_unlock(&ns->ns_lock);
927         LDLM_LOCK_PUT(lock);
928         EXIT;
929 }
930
931 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
932 {
933         req->rq_status = rc;
934         if (req->rq_reply_state == NULL) {
935                 rc = lustre_pack_reply(req, 0, NULL, NULL);
936                 if (rc)
937                         return rc;
938         }
939         return ptlrpc_reply(req);
940 }
941
942 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
943                       struct ldlm_lock *lock)
944 {
945 #ifdef __KERNEL__
946         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
947         struct ldlm_bl_work_item *blwi;
948         ENTRY;
949
950         OBD_ALLOC(blwi, sizeof(*blwi));
951         if (blwi == NULL)
952                 RETURN(-ENOMEM);
953
954         blwi->blwi_ns = ns;
955         if (ld != NULL)
956                 blwi->blwi_ld = *ld;
957         blwi->blwi_lock = lock;
958
959         spin_lock(&blp->blp_lock);
960         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
961         wake_up(&blp->blp_waitq);
962         spin_unlock(&blp->blp_lock);
963 #else
964         LBUG();
965 #endif
966
967         RETURN(0);
968 }
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);