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