Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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-2004 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 the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30 #define DEBUG_SUBSYSTEM S_LDLM
31
32 #ifdef __KERNEL__
33 # include <libcfs/libcfs.h>
34 #else
35 # include <liblustre.h>
36 #endif
37
38 #include <lustre_dlm.h>
39 #include <obd_class.h>
40 #include <libcfs/list.h>
41 #include "ldlm_internal.h"
42
43 extern cfs_mem_cache_t *ldlm_resource_slab;
44 extern cfs_mem_cache_t *ldlm_lock_slab;
45 extern struct list_head ldlm_namespace_list;
46
47 extern struct semaphore ldlm_namespace_lock;
48 static struct semaphore ldlm_ref_sem;
49 static int ldlm_refcount;
50
51 /* LDLM state */
52
53 static struct ldlm_state *ldlm_state;
54
55 inline cfs_time_t round_timeout(cfs_time_t timeout)
56 {
57         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
58 }
59
60 /* timeout for initial callback (AST) reply */
61 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
62                                                unsigned int obd_timeout)
63 {
64         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
65
66         return timeout < 1 ? 1 : timeout;
67 }
68
69 #ifdef __KERNEL__
70 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
71 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
72 static struct list_head waiting_locks_list;
73 static cfs_timer_t waiting_locks_timer;
74
75 static struct expired_lock_thread {
76         cfs_waitq_t               elt_waitq;
77         int                       elt_state;
78         int                       elt_dump;
79         struct list_head          elt_expired_locks;
80 } expired_lock_thread;
81 #endif
82
83 #define ELT_STOPPED   0
84 #define ELT_READY     1
85 #define ELT_TERMINATE 2
86
87 struct ldlm_bl_pool {
88         spinlock_t              blp_lock;
89         struct list_head        blp_list;
90         cfs_waitq_t             blp_waitq;
91         atomic_t                blp_num_threads;
92         struct completion       blp_comp;
93 };
94
95 struct ldlm_bl_work_item {
96         struct list_head        blwi_entry;
97         struct ldlm_namespace   *blwi_ns;
98         struct ldlm_lock_desc   blwi_ld;
99         struct ldlm_lock        *blwi_lock;
100 };
101
102 #ifdef __KERNEL__
103
104 static inline int have_expired_locks(void)
105 {
106         int need_to_run;
107
108         ENTRY;
109         spin_lock_bh(&waiting_locks_spinlock);
110         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
111         spin_unlock_bh(&waiting_locks_spinlock);
112
113         RETURN(need_to_run);
114 }
115
116 static int expired_lock_main(void *arg)
117 {
118         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
119         struct l_wait_info lwi = { 0 };
120         int do_dump;
121
122         ENTRY;
123         cfs_daemonize("ldlm_elt");
124
125         expired_lock_thread.elt_state = ELT_READY;
126         cfs_waitq_signal(&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(&waiting_locks_spinlock);
135                 if (expired_lock_thread.elt_dump) {
136                         spin_unlock_bh(&waiting_locks_spinlock);
137
138                         /* from waiting_locks_callback, but not in timer */
139                         libcfs_debug_dumplog();
140                         libcfs_run_lbug_upcall(__FILE__,
141                                                 "waiting_locks_callback",
142                                                 expired_lock_thread.elt_dump);
143
144                         spin_lock_bh(&waiting_locks_spinlock);
145                         expired_lock_thread.elt_dump = 0;
146                 }
147
148                 do_dump = 0;
149
150                 while (!list_empty(expired)) {
151                         struct obd_export *export;
152                         struct ldlm_lock *lock;
153
154                         lock = list_entry(expired->next, struct ldlm_lock,
155                                           l_pending_chain);
156                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
157                             (void *)lock >= LP_POISON) {
158                                 spin_unlock_bh(&waiting_locks_spinlock);
159                                 CERROR("free lock on elt list %p\n", lock);
160                                 LBUG();
161                         }
162                         list_del_init(&lock->l_pending_chain);
163                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
164                             (void *)lock->l_export >= LP_POISON) {
165                                 CERROR("lock with free export on elt list %p\n",
166                                        lock->l_export);
167                                 lock->l_export = NULL;
168                                 LDLM_ERROR(lock, "free export");
169                                 continue;
170                         }
171                         export = class_export_get(lock->l_export);
172                         spin_unlock_bh(&waiting_locks_spinlock);
173
174                         do_dump++;
175                         class_fail_export(export);
176                         class_export_put(export);
177                         spin_lock_bh(&waiting_locks_spinlock);
178                 }
179                 spin_unlock_bh(&waiting_locks_spinlock);
180
181                 if (do_dump && obd_dump_on_eviction) {
182                         CERROR("dump the log upon eviction\n");
183                         libcfs_debug_dumplog();
184                 }
185
186                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
187                         break;
188         }
189
190         expired_lock_thread.elt_state = ELT_STOPPED;
191         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
192         RETURN(0);
193 }
194
195 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
196
197 /* This is called from within a timer interrupt and cannot schedule */
198 static void waiting_locks_callback(unsigned long unused)
199 {
200         struct ldlm_lock *lock, *last = NULL;
201
202 repeat:
203         spin_lock_bh(&waiting_locks_spinlock);
204         while (!list_empty(&waiting_locks_list)) {
205                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
206                                   l_pending_chain);
207
208                 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
209                     (lock->l_req_mode == LCK_GROUP))
210                         break;
211
212                 if (ptlrpc_check_suspend()) {
213                         /* there is a case when we talk to one mds, holding
214                          * lock from another mds. this way we easily can get
215                          * here, if second mds is being recovered. so, we
216                          * suspend timeouts. bug 6019 */
217
218                         LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
219                                    lock->l_export->exp_client_uuid.uuid,
220                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
221                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
222
223                         list_del_init(&lock->l_pending_chain);
224                         spin_unlock_bh(&waiting_locks_spinlock);
225                         ldlm_add_waiting_lock(lock);
226                         goto repeat;
227                 }
228
229                 /* if timeout overlaps the activation time of suspended timeouts
230                  * then extend it to give a chance for client to reconnect */
231                 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
232                                                  cfs_time_seconds(obd_timeout)/2),
233                                     ptlrpc_suspend_wakeup_time())) {
234                         LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
235                                    lock->l_export->exp_client_uuid.uuid,
236                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
237                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
238
239                         list_del_init(&lock->l_pending_chain);
240                         spin_unlock_bh(&waiting_locks_spinlock);
241                         ldlm_add_waiting_lock(lock);
242                         goto repeat;
243                 }
244
245                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
246                            "%s@%s nid %s\n",
247                            lock->l_export->exp_client_uuid.uuid,
248                            lock->l_export->exp_connection->c_remote_uuid.uuid,
249                            libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
250
251                 last = lock;
252
253                 list_del(&lock->l_pending_chain);
254                 list_add(&lock->l_pending_chain,
255                          &expired_lock_thread.elt_expired_locks);
256         }
257
258         if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
259                 if (obd_dump_on_timeout)
260                         expired_lock_thread.elt_dump = __LINE__;
261
262                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
263         }
264
265         /*
266          * Make sure the timer will fire again if we have any locks
267          * left.
268          */
269         if (!list_empty(&waiting_locks_list)) {
270                 cfs_time_t timeout_rounded;
271                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
272                                   l_pending_chain);
273                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
274                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
275         }
276         spin_unlock_bh(&waiting_locks_spinlock);
277 }
278
279 /*
280  * Indicate that we're waiting for a client to call us back cancelling a given
281  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
282  * timer to fire appropriately.  (We round up to the next second, to avoid
283  * floods of timer firings during periods of high lock contention and traffic).
284  *
285  * Called with the namespace lock held.
286  */
287 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
288 {
289         cfs_time_t timeout_rounded;
290
291         if (!list_empty(&lock->l_pending_chain))
292                 return 0;
293
294         lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
295                                                cfs_time_seconds(obd_timeout)/2);
296
297         timeout_rounded = round_timeout(lock->l_callback_timeout);
298
299         if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
300             !cfs_timer_is_armed(&waiting_locks_timer)) {
301                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
302
303         }
304         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
305         return 1;
306 }
307
308 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
309 {
310         int ret;
311
312         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
313
314         spin_lock_bh(&waiting_locks_spinlock);
315         if (lock->l_destroyed) {
316                 static cfs_time_t next;
317                 spin_unlock_bh(&waiting_locks_spinlock);
318                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
319                 if (cfs_time_after(cfs_time_current(), next)) {
320                         next = cfs_time_shift(14400);
321                         libcfs_debug_dumpstack(NULL);
322                 }
323                 return 0;
324         }
325
326         ret = __ldlm_add_waiting_lock(lock);
327         spin_unlock_bh(&waiting_locks_spinlock);
328
329         LDLM_DEBUG(lock, "%sadding to wait list",
330                    ret == 0 ? "not re-" : "");
331         return ret;
332 }
333
334 /*
335  * Remove a lock from the pending list, likely because it had its cancellation
336  * callback arrive without incident.  This adjusts the lock-timeout timer if
337  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
338  *
339  * Called with namespace lock held.
340  */
341 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
342 {
343         struct list_head *list_next;
344
345         if (list_empty(&lock->l_pending_chain))
346                 return 0;
347
348         list_next = lock->l_pending_chain.next;
349         if (lock->l_pending_chain.prev == &waiting_locks_list) {
350                 /* Removing the head of the list, adjust timer. */
351                 if (list_next == &waiting_locks_list) {
352                         /* No more, just cancel. */
353                         cfs_timer_disarm(&waiting_locks_timer);
354                 } else {
355                         struct ldlm_lock *next;
356                         next = list_entry(list_next, struct ldlm_lock,
357                                           l_pending_chain);
358                         cfs_timer_arm(&waiting_locks_timer,
359                                       round_timeout(next->l_callback_timeout));
360                 }
361         }
362         list_del_init(&lock->l_pending_chain);
363
364         return 1;
365 }
366
367 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
368 {
369         int ret;
370
371         if (lock->l_export == NULL) {
372                 /* We don't have a "waiting locks list" on clients. */
373                 LDLM_DEBUG(lock, "client lock: no-op");
374                 return 0;
375         }
376
377         spin_lock_bh(&waiting_locks_spinlock);
378         ret = __ldlm_del_waiting_lock(lock);
379         spin_unlock_bh(&waiting_locks_spinlock);
380
381         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
382         return ret;
383 }
384
385 /*
386  * Prolong the lock
387  *
388  * Called with namespace lock held.
389  */
390 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
391 {
392         if (lock->l_export == NULL) {
393                 /* We don't have a "waiting locks list" on clients. */
394                 LDLM_DEBUG(lock, "client lock: no-op");
395                 return 0;
396         }
397
398         spin_lock_bh(&waiting_locks_spinlock);
399
400         if (list_empty(&lock->l_pending_chain)) {
401                 spin_unlock_bh(&waiting_locks_spinlock);
402                 LDLM_DEBUG(lock, "wasn't waiting");
403                 return 0;
404         }
405
406         __ldlm_del_waiting_lock(lock);
407         __ldlm_add_waiting_lock(lock);
408         spin_unlock_bh(&waiting_locks_spinlock);
409
410         LDLM_DEBUG(lock, "refreshed");
411         return 1;
412 }
413
414 #else /* !__KERNEL__ */
415
416 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
417 {
418         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
419         RETURN(1);
420 }
421
422 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
423 {
424         RETURN(0);
425 }
426
427 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
428 {
429         RETURN(0);
430 }
431 #endif /* __KERNEL__ */
432
433 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
434                             const char *ast_type)
435 {
436         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
437         char                     *str = libcfs_nid2str(conn->c_peer.nid);
438
439         LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
440                            "service %s.\n", str, 
441                            lock->l_export->exp_obd->obd_name);
442
443         LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
444                            "resource %d\n", ast_type, 
445                            obd_export_nid2str(lock->l_export), rc);
446
447         if (obd_dump_on_timeout)
448                 libcfs_debug_dumplog();
449         class_fail_export(lock->l_export);
450 }
451
452 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
453                                  struct ptlrpc_request *req, int rc,
454                                  const char *ast_type)
455 {
456         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
457
458         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
459                 LASSERT(lock->l_export);
460                 if (lock->l_export->exp_libclient) {
461                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
462                                    " timeout, just cancelling lock", ast_type,
463                                    libcfs_nid2str(peer.nid));
464                         ldlm_lock_cancel(lock);
465                         rc = -ERESTART;
466                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
467                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
468                                    "cancel was received (AST reply lost?)",
469                                    ast_type, libcfs_nid2str(peer.nid));
470                         ldlm_lock_cancel(lock);
471                         rc = -ERESTART;
472                 } else {
473                         ldlm_del_waiting_lock(lock);
474                         ldlm_failed_ast(lock, rc, ast_type);
475                 }
476         } else if (rc) {
477                 if (rc == -EINVAL)
478                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
479                                " from %s AST - normal race",
480                                libcfs_nid2str(peer.nid),
481                                req->rq_repmsg ?
482                                lustre_msg_get_status(req->rq_repmsg) : -1,
483                                ast_type);
484                 else
485                         LDLM_ERROR(lock, "client (nid %s) returned %d "
486                                    "from %s AST", libcfs_nid2str(peer.nid),
487                                    (req->rq_repmsg != NULL) ?
488                                    lustre_msg_get_status(req->rq_repmsg) : 0,
489                                    ast_type);
490                 ldlm_lock_cancel(lock);
491                 /* Server-side AST functions are called from ldlm_reprocess_all,
492                  * which needs to be told to please restart its reprocessing. */
493                 rc = -ERESTART;
494         }
495
496         return rc;
497 }
498
499 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
500                              struct ldlm_lock_desc *desc,
501                              void *data, int flag)
502 {
503         struct ldlm_request *body;
504         struct ptlrpc_request *req;
505         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
506                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
507         int instant_cancel = 0, rc = 0;
508         ENTRY;
509
510         if (flag == LDLM_CB_CANCELING) {
511                 /* Don't need to do anything here. */
512                 RETURN(0);
513         }
514
515         LASSERT(lock);
516         if (lock->l_export->exp_obd->obd_recovering != 0) {
517                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
518                 ldlm_lock_dump(D_ERROR, lock, 0);
519         }
520
521         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
522                               LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
523                               NULL);
524         if (req == NULL)
525                 RETURN(-ENOMEM);
526
527         lock_res(lock->l_resource);
528         if (lock->l_granted_mode != lock->l_req_mode) {
529                 /* this blocking AST will be communicated as part of the
530                  * completion AST instead */
531                 unlock_res(lock->l_resource);
532                 ptlrpc_req_finished(req);
533                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
534                 RETURN(0);
535         }
536
537         if (lock->l_destroyed) {
538                 /* What's the point? */
539                 unlock_res(lock->l_resource);
540                 ptlrpc_req_finished(req);
541                 RETURN(0);
542         }
543
544         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
545                 instant_cancel = 1;
546
547         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
548         body->lock_handle1 = lock->l_remote_handle;
549         body->lock_desc = *desc;
550         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
551
552         LDLM_DEBUG(lock, "server preparing blocking AST");
553
554         ptlrpc_req_set_repsize(req, 1, NULL);
555         if (instant_cancel) {
556                 unlock_res(lock->l_resource);
557                 ldlm_lock_cancel(lock);
558         } else if (lock->l_granted_mode == lock->l_req_mode) {
559                 ldlm_add_waiting_lock(lock);
560                 unlock_res(lock->l_resource);
561         }
562
563         req->rq_send_state = LUSTRE_IMP_FULL;
564         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
565
566         if (lock->l_export && lock->l_export->exp_ldlm_stats)
567                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
568                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
569
570         if (unlikely(instant_cancel)) {
571                 rc = ptl_send_rpc(req, 1);
572         } else {
573                 rc = ptlrpc_queue_wait(req);
574                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
575         }
576
577         if (rc != 0)
578                 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
579
580         ptlrpc_req_finished(req);
581
582         /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
583         if (!rc && instant_cancel)
584                 rc = -ERESTART;
585
586         RETURN(rc);
587 }
588
589 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
590 {
591         struct ldlm_request *body;
592         struct ptlrpc_request *req;
593         struct timeval granted_time;
594         long total_enqueue_wait;
595         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
596                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
597         int rc = 0, buffers = 2, instant_cancel = 0;
598         ENTRY;
599
600         LASSERT(lock != NULL);
601
602         do_gettimeofday(&granted_time);
603         total_enqueue_wait = cfs_timeval_sub(&granted_time,
604                                              &lock->l_enqueued_time, NULL);
605
606         if (total_enqueue_wait / 1000000 > obd_timeout)
607                 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
608                            total_enqueue_wait, lock->l_enqueued_time.tv_sec);
609
610         lock_res_and_lock(lock);
611         if (lock->l_resource->lr_lvb_len) {
612                 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
613                 buffers = 3;
614         }
615         unlock_res_and_lock(lock);
616
617         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
618                               LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
619                               size, NULL);
620         if (req == NULL)
621                 RETURN(-ENOMEM);
622
623         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
624         body->lock_handle1 = lock->l_remote_handle;
625         body->lock_flags = flags;
626         ldlm_lock2desc(lock, &body->lock_desc);
627
628         if (buffers == 3) {
629                 void *lvb;
630
631                 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
632                                      lock->l_resource->lr_lvb_len);
633                 lock_res_and_lock(lock);
634                 memcpy(lvb, lock->l_resource->lr_lvb_data,
635                        lock->l_resource->lr_lvb_len);
636                 unlock_res_and_lock(lock);
637         }
638
639         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
640                    total_enqueue_wait);
641         
642         ptlrpc_req_set_repsize(req, 1, NULL);
643
644         req->rq_send_state = LUSTRE_IMP_FULL;
645         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
646
647         /* We only send real blocking ASTs after the lock is granted */
648         lock_res_and_lock(lock);
649         if (lock->l_flags & LDLM_FL_AST_SENT) {
650                 body->lock_flags |= LDLM_FL_AST_SENT;
651
652                 /* We might get here prior to ldlm_handle_enqueue setting
653                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
654                  * into waiting list, but this is safe and similar code in
655                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
656                  * that would not only cancel the lock, but will also remove
657                  * it from waiting list */
658                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
659                         unlock_res_and_lock(lock);
660                         ldlm_lock_cancel(lock);
661                         instant_cancel = 1;
662                         lock_res_and_lock(lock);
663                 } else {
664                         /* start the lock-timeout clock */
665                         ldlm_add_waiting_lock(lock);
666                 }
667         }
668         unlock_res_and_lock(lock);
669
670         if (lock->l_export && lock->l_export->exp_ldlm_stats)
671                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
672                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
673
674         rc = ptlrpc_queue_wait(req);
675         if (rc != 0)
676                 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
677
678         ptlrpc_req_finished(req);
679
680         /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
681         if (!rc && instant_cancel)
682                 rc = -ERESTART;
683
684         RETURN(rc);
685 }
686
687 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
688 {
689         struct ldlm_resource *res = lock->l_resource;
690         struct ldlm_request *body;
691         struct ptlrpc_request *req;
692         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
693                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
694         int rc = 0;
695         ENTRY;
696
697         LASSERT(lock != NULL);
698
699         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
700                               LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
701                               NULL);
702         if (req == NULL)
703                 RETURN(-ENOMEM);
704
705         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
706         body->lock_handle1 = lock->l_remote_handle;
707         ldlm_lock2desc(lock, &body->lock_desc);
708
709         lock_res_and_lock(lock);
710         size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
711         unlock_res_and_lock(lock);
712         res = lock->l_resource;
713         ptlrpc_req_set_repsize(req, 2, size);
714
715         req->rq_send_state = LUSTRE_IMP_FULL;
716         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
717
718         if (lock->l_export && lock->l_export->exp_ldlm_stats)
719                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
720                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
721
722         rc = ptlrpc_queue_wait(req);
723         if (rc == -ELDLM_NO_LOCK_DATA)
724                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
725         else if (rc != 0)
726                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
727         else
728                 rc = res->lr_namespace->ns_lvbo->lvbo_update
729                         (res, req->rq_repmsg, REPLY_REC_OFF, 1);
730         ptlrpc_req_finished(req);
731         RETURN(rc);
732 }
733
734 static struct ldlm_lock *
735 find_existing_lock(struct obd_export *exp,
736                    const struct lustre_handle *remote_hdl)
737 {
738         struct list_head *iter;
739
740         spin_lock(&exp->exp_ldlm_data.led_lock);
741         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
742                 struct ldlm_lock *lock;
743                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
744                 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
745                         LDLM_LOCK_GET(lock);
746                         spin_unlock(&exp->exp_ldlm_data.led_lock);
747                         return lock;
748                 }
749         }
750         spin_unlock(&exp->exp_ldlm_data.led_lock);
751         return NULL;
752 }
753
754 #ifdef __KERNEL__
755 extern unsigned long long lu_time_stamp_get(void);
756 #else
757 #define lu_time_stamp_get() time(NULL)
758 #endif
759
760 /*
761  * Main server-side entry point into LDLM. This is called by ptlrpc service
762  * threads to carry out client lock enqueueing requests.
763  */
764 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
765                          struct ptlrpc_request *req,
766                          const struct ldlm_request *dlm_req,
767                          const struct ldlm_callback_suite *cbs)
768 {
769         struct ldlm_reply *dlm_rep;
770         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
771                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
772         int rc = 0;
773         __u32 flags;
774         ldlm_error_t err = ELDLM_OK;
775         struct ldlm_lock *lock = NULL;
776         void *cookie = NULL;
777         ENTRY;
778
779         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
780
781         flags = dlm_req->lock_flags;
782
783         LASSERT(req->rq_export);
784
785         if (req->rq_export->exp_ldlm_stats)
786                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
787                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
788
789         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
790                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
791                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
792                           dlm_req->lock_desc.l_resource.lr_type);
793                 GOTO(out, rc = -EFAULT);
794         }
795
796         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
797                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
798                      dlm_req->lock_desc.l_req_mode &
799                      (dlm_req->lock_desc.l_req_mode-1))) {
800                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
801                           dlm_req->lock_desc.l_req_mode);
802                 GOTO(out, rc = -EFAULT);
803         }
804
805         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
806                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
807                              LDLM_PLAIN)) {
808                         DEBUG_REQ(D_ERROR, req,
809                                   "PLAIN lock request from IBITS client?");
810                         GOTO(out, rc = -EPROTO);
811                 }
812         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
813                             LDLM_IBITS)) {
814                 DEBUG_REQ(D_ERROR, req,
815                           "IBITS lock request from unaware client?");
816                 GOTO(out, rc = -EPROTO);
817         }
818
819 #if 0
820         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
821            against server's _CONNECT_SUPPORTED flags? (I don't want to use
822            ibits for mgc/mgs) */
823
824         /* INODEBITS_INTEROP: Perform conversion from plain lock to
825          * inodebits lock if client does not support them. */
826         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
827             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
828                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
829                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
830                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
831                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
832                         dlm_req->lock_desc.l_req_mode = LCK_CR;
833         }
834 #endif
835
836         if (unlikely(flags & LDLM_FL_REPLAY)) {
837                 lock = find_existing_lock(req->rq_export,
838                                           &dlm_req->lock_handle1);
839                 if (lock != NULL) {
840                         DEBUG_REQ(D_HA, req, "found existing lock cookie "LPX64,
841                                   lock->l_handle.h_cookie);
842                         GOTO(existing_lock, rc = 0);
843                 }
844         }
845
846         /* The lock's callback data might be set in the policy function */
847         lock = ldlm_lock_create(ns, &dlm_req->lock_handle2,
848                                 &dlm_req->lock_desc.l_resource.lr_name,
849                                 dlm_req->lock_desc.l_resource.lr_type,
850                                 dlm_req->lock_desc.l_req_mode,
851                                 cbs->lcs_blocking, cbs->lcs_completion,
852                                 cbs->lcs_glimpse, NULL, 0);
853
854         if (!lock)
855                 GOTO(out, rc = -ENOMEM);
856
857         do_gettimeofday(&lock->l_enqueued_time);
858         lock->l_remote_handle = dlm_req->lock_handle1;
859         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
860
861         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
862         /* Don't enqueue a lock onto the export if it has already
863          * been evicted.  Cancel it now instead. (bug 3822) */
864         if (req->rq_export->exp_failed) {
865                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
866                 GOTO(out, rc = -ENOTCONN);
867         }
868         lock->l_export = class_export_get(req->rq_export);
869         spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
870         list_add(&lock->l_export_chain,
871                  &lock->l_export->exp_ldlm_data.led_held_locks);
872         spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
873
874 existing_lock:
875
876         if (flags & LDLM_FL_HAS_INTENT) {
877                 /* In this case, the reply buffer is allocated deep in
878                  * local_lock_enqueue by the policy function. */
879                 cookie = req;
880         } else {
881                 int buffers = 2;
882
883                 lock_res_and_lock(lock);
884                 if (lock->l_resource->lr_lvb_len) {
885                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
886                         buffers = 3;
887                 }
888                 unlock_res_and_lock(lock);
889
890                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
891                         GOTO(out, rc = -ENOMEM);
892
893                 rc = lustre_pack_reply(req, buffers, size, NULL);
894                 if (rc)
895                         GOTO(out, rc);
896         }
897
898         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
899                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
900         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
901                 lock->l_req_extent = lock->l_policy_data.l_extent;
902
903         err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
904         if (err)
905                 GOTO(out, err);
906
907         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
908                                  sizeof(*dlm_rep));
909         dlm_rep->lock_flags = flags;
910
911         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
912         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
913
914         /* We never send a blocking AST until the lock is granted, but
915          * we can tell it right now */
916         lock_res_and_lock(lock);
917
918         /* Now take into account flags to be inherited from original lock
919            request both in reply to client and in our own lock flags. */
920         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
921         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
922
923         /* Don't move a pending lock onto the export if it has already
924          * been evicted.  Cancel it now instead. (bug 5683) */
925         if (unlikely(req->rq_export->exp_failed ||
926                      OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
927                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
928                 rc = -ENOTCONN;
929         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
930                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
931                 if (lock->l_granted_mode == lock->l_req_mode) {
932                         /*
933                          * Only cancel lock if it was granted, because it would
934                          * be destroyed immediatelly and would never be granted
935                          * in the future, causing timeouts on client.  Not
936                          * granted lock will be cancelled immediatelly after
937                          * sending completion AST.
938                          */
939                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
940                                 unlock_res_and_lock(lock);
941                                 ldlm_lock_cancel(lock);
942                                 lock_res_and_lock(lock);
943                         } else if (lock->l_granted_mode == lock->l_req_mode)
944                                 ldlm_add_waiting_lock(lock);
945                 }
946         }
947         /* Make sure we never ever grant usual metadata locks to liblustre
948            clients */
949         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
950             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
951              req->rq_export->exp_libclient) {
952                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
953                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
954                         CERROR("Granting sync lock to libclient. "
955                                "req fl %d, rep fl %d, lock fl %d\n",
956                                dlm_req->lock_flags, dlm_rep->lock_flags,
957                                lock->l_flags);
958                         LDLM_ERROR(lock, "sync lock");
959                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
960                                 struct ldlm_intent *it;
961                                 it = lustre_msg_buf(req->rq_reqmsg,
962                                                     DLM_INTENT_IT_OFF,
963                                                     sizeof(*it));
964                                 if (it != NULL) {
965                                         CERROR("This is intent %s ("LPU64")\n",
966                                                ldlm_it2str(it->opc), it->opc);
967                                 }
968                         }
969                 }
970         }
971
972         unlock_res_and_lock(lock);
973
974         EXIT;
975  out:
976         req->rq_status = err;
977         if (req->rq_reply_state == NULL) {
978                 err = lustre_pack_reply(req, 1, NULL, NULL);
979                 if (rc == 0)
980                         rc = err;
981                 req->rq_status = rc;
982         }
983
984         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
985          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
986         if (lock) {
987                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
988                            "(err=%d, rc=%d)", err, rc);
989
990                 if (rc == 0) {
991                         lock_res_and_lock(lock);
992                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
993                         if (size[DLM_REPLY_REC_OFF] > 0) {
994                                 void *lvb = lustre_msg_buf(req->rq_repmsg,
995                                                        DLM_REPLY_REC_OFF,
996                                                        size[DLM_REPLY_REC_OFF]);
997                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
998                                          req, lock);
999
1000                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1001                                        size[DLM_REPLY_REC_OFF]);
1002                         }
1003                         unlock_res_and_lock(lock);
1004                 } else {
1005                         lock_res_and_lock(lock);
1006                         ldlm_resource_unlink_lock(lock);
1007                         ldlm_lock_destroy_nolock(lock);
1008                         unlock_res_and_lock(lock);
1009                 }
1010
1011                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1012                         ldlm_reprocess_all(lock->l_resource);
1013
1014                 LDLM_LOCK_PUT(lock);
1015         }
1016
1017         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1018                           lock, rc);
1019
1020         return rc;
1021 }
1022
1023 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1024                         ldlm_completion_callback completion_callback,
1025                         ldlm_blocking_callback blocking_callback,
1026                         ldlm_glimpse_callback glimpse_callback)
1027 {
1028         int rc;
1029         struct ldlm_request *dlm_req;
1030         struct ldlm_callback_suite cbs = {
1031                 .lcs_completion = completion_callback,
1032                 .lcs_blocking   = blocking_callback,
1033                 .lcs_glimpse    = glimpse_callback
1034         };
1035
1036
1037         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1038                                      sizeof *dlm_req, lustre_swab_ldlm_request);
1039         if (dlm_req != NULL) {
1040                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1041                                           req, dlm_req, &cbs);
1042         } else {
1043                 CERROR ("Can't unpack dlm_req\n");
1044                 rc = -EFAULT;
1045         }
1046         return rc;
1047 }
1048
1049 int ldlm_handle_convert0(struct ptlrpc_request *req,
1050                          const struct ldlm_request *dlm_req)
1051 {
1052         struct ldlm_reply *dlm_rep;
1053         struct ldlm_lock *lock;
1054         int rc;
1055         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1056                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
1057         ENTRY;
1058
1059         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1060                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1061                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1062
1063         rc = lustre_pack_reply(req, 2, size, NULL);
1064         if (rc) {
1065                 CERROR("out of memory\n");
1066                 RETURN(-ENOMEM);
1067         }
1068         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1069                                  sizeof(*dlm_rep));
1070         dlm_rep->lock_flags = dlm_req->lock_flags;
1071
1072         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1073         if (!lock) {
1074                 req->rq_status = EINVAL;
1075         } else {
1076                 void *res = NULL;
1077
1078                 LDLM_DEBUG(lock, "server-side convert handler START");
1079
1080                 do_gettimeofday(&lock->l_enqueued_time);
1081                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1082                                         &dlm_rep->lock_flags);
1083                 if (res) {
1084                         if (ldlm_del_waiting_lock(lock))
1085                                 LDLM_DEBUG(lock, "converted waiting lock");
1086                         req->rq_status = 0;
1087                 } else {
1088                         req->rq_status = EDEADLOCK;
1089                 }
1090         }
1091
1092         if (lock) {
1093                 if (!req->rq_status)
1094                         ldlm_reprocess_all(lock->l_resource);
1095                 LDLM_DEBUG(lock, "server-side convert handler END");
1096                 LDLM_LOCK_PUT(lock);
1097         } else
1098                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1099
1100         RETURN(0);
1101 }
1102
1103 int ldlm_handle_convert(struct ptlrpc_request *req)
1104 {
1105         int rc;
1106         struct ldlm_request *dlm_req;
1107
1108         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof *dlm_req,
1109                                      lustre_swab_ldlm_request);
1110         if (dlm_req != NULL) {
1111                 rc = ldlm_handle_convert0(req, dlm_req);
1112         } else {
1113                 CERROR ("Can't unpack dlm_req\n");
1114                 rc = -EFAULT;
1115         }
1116         return rc;
1117 }
1118
1119 int ldlm_handle_cancel(struct ptlrpc_request *req)
1120 {
1121         struct ldlm_request *dlm_req;
1122         struct ldlm_lock *lock;
1123         struct ldlm_resource *res;
1124         int rc;
1125         ENTRY;
1126
1127         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1128                                      lustre_swab_ldlm_request);
1129         if (dlm_req == NULL) {
1130                 CERROR("bad request buffer for cancel\n");
1131                 RETURN(-EFAULT);
1132         }
1133
1134         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1135                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1136                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1137
1138         rc = lustre_pack_reply(req, 1, NULL, NULL);
1139         if (rc) {
1140                 CERROR("out of memory\n");
1141                 RETURN(-ENOMEM);
1142         }
1143
1144         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1145         if (!lock) {
1146                 CERROR("received cancel for unknown lock cookie "LPX64
1147                        " from client %s id %s\n",
1148                        dlm_req->lock_handle1.cookie,
1149                        req->rq_export->exp_client_uuid.uuid,
1150                        libcfs_id2str(req->rq_peer));
1151                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
1152                                   "(cookie "LPU64")",
1153                                   dlm_req->lock_handle1.cookie);
1154                 req->rq_status = ESTALE;
1155         } else {
1156                 LDLM_DEBUG(lock, "server-side cancel handler START");
1157                 res = lock->l_resource;
1158                 if (res && res->lr_namespace->ns_lvbo &&
1159                     res->lr_namespace->ns_lvbo->lvbo_update) {
1160                         (void)res->lr_namespace->ns_lvbo->lvbo_update
1161                                 (res, NULL, 0, 0);
1162                 }
1163
1164                 ldlm_lock_cancel(lock);
1165                 if (ldlm_del_waiting_lock(lock))
1166                         CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
1167                 req->rq_status = rc;
1168         }
1169
1170         if (ptlrpc_reply(req) != 0)
1171                 LBUG();
1172
1173         if (lock) {
1174                 ldlm_reprocess_all(lock->l_resource);
1175                 LDLM_DEBUG(lock, "server-side cancel handler END");
1176                 LDLM_LOCK_PUT(lock);
1177         }
1178
1179         RETURN(0);
1180 }
1181
1182 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1183                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1184 {
1185         int do_ast;
1186         ENTRY;
1187
1188         LDLM_DEBUG(lock, "client blocking AST callback handler START");
1189
1190         lock_res_and_lock(lock);
1191         lock->l_flags |= LDLM_FL_CBPENDING;
1192
1193         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1194                 lock->l_flags |= LDLM_FL_CANCEL;
1195
1196         do_ast = (!lock->l_readers && !lock->l_writers);
1197         unlock_res_and_lock(lock);
1198
1199         if (do_ast) {
1200                 LDLM_DEBUG(lock, "already unused, calling "
1201                            "callback (%p)", lock->l_blocking_ast);
1202                 if (lock->l_blocking_ast != NULL)
1203                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1204                                              LDLM_CB_BLOCKING);
1205         } else {
1206                 LDLM_DEBUG(lock, "Lock still has references, will be"
1207                            " cancelled later");
1208         }
1209
1210         LDLM_DEBUG(lock, "client blocking callback handler END");
1211         LDLM_LOCK_PUT(lock);
1212         EXIT;
1213 }
1214
1215 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1216                                     struct ldlm_namespace *ns,
1217                                     struct ldlm_request *dlm_req,
1218                                     struct ldlm_lock *lock)
1219 {
1220         CFS_LIST_HEAD(ast_list);
1221         ENTRY;
1222
1223         LDLM_DEBUG(lock, "client completion callback handler START");
1224
1225         lock_res_and_lock(lock);
1226
1227         /* If we receive the completion AST before the actual enqueue returned,
1228          * then we might need to switch lock modes, resources, or extents. */
1229         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1230                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1231                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1232         }
1233
1234         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1235                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1236                 LDLM_DEBUG(lock, "completion AST, new policy data");
1237         }
1238
1239         ldlm_resource_unlink_lock(lock);
1240         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1241                    &lock->l_resource->lr_name,
1242                    sizeof(lock->l_resource->lr_name)) != 0) {
1243                 unlock_res_and_lock(lock);
1244                 ldlm_lock_change_resource(ns, lock,
1245                                         &dlm_req->lock_desc.l_resource.lr_name);
1246                 LDLM_DEBUG(lock, "completion AST, new resource");
1247                 CERROR("change resource!\n");
1248                 lock_res_and_lock(lock);
1249         }
1250
1251         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1252                 lock->l_flags |= LDLM_FL_CBPENDING;
1253                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1254         }
1255
1256         if (lock->l_lvb_len) {
1257                 void *lvb;
1258                 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1259                                          lock->l_lvb_swabber);
1260                 if (lvb == NULL) {
1261                         LDLM_ERROR(lock, "completion AST did not contain "
1262                                    "expected LVB!");
1263                 } else {
1264                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1265                 }
1266         }
1267
1268         ldlm_grant_lock(lock, &ast_list);
1269         unlock_res_and_lock(lock);
1270
1271         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1272
1273         ldlm_run_cp_ast_work(&ast_list);
1274
1275         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1276                           lock);
1277         LDLM_LOCK_PUT(lock);
1278         EXIT;
1279 }
1280
1281 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1282                                     struct ldlm_namespace *ns,
1283                                     struct ldlm_request *dlm_req,
1284                                     struct ldlm_lock *lock)
1285 {
1286         int rc = -ENOSYS;
1287         ENTRY;
1288
1289         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1290
1291         if (lock->l_glimpse_ast != NULL)
1292                 rc = lock->l_glimpse_ast(lock, req);
1293
1294         if (req->rq_repmsg != NULL) {
1295                 ptlrpc_reply(req);
1296         } else {
1297                 req->rq_status = rc;
1298                 ptlrpc_error(req);
1299         }
1300
1301         lock_res_and_lock(lock);
1302         if (lock->l_granted_mode == LCK_PW &&
1303             !lock->l_readers && !lock->l_writers &&
1304             cfs_time_after(cfs_time_current(),
1305                            cfs_time_add(lock->l_last_used, cfs_time_seconds(10)))) {
1306                 unlock_res_and_lock(lock);
1307                 if (ldlm_bl_to_thread(ns, NULL, lock))
1308                         ldlm_handle_bl_callback(ns, NULL, lock);
1309
1310                 EXIT;
1311                 return;
1312         }
1313         unlock_res_and_lock(lock);
1314         LDLM_LOCK_PUT(lock);
1315         EXIT;
1316 }
1317
1318 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1319 {
1320         req->rq_status = rc;
1321         if (req->rq_reply_state == NULL) {
1322                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1323                 if (rc)
1324                         return rc;
1325         }
1326         return ptlrpc_reply(req);
1327 }
1328
1329 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1330                       struct ldlm_lock *lock)
1331 {
1332 #ifdef __KERNEL__
1333         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1334         struct ldlm_bl_work_item *blwi;
1335         ENTRY;
1336
1337         OBD_ALLOC(blwi, sizeof(*blwi));
1338         if (blwi == NULL)
1339                 RETURN(-ENOMEM);
1340
1341         blwi->blwi_ns = ns;
1342         if (ld != NULL)
1343                 blwi->blwi_ld = *ld;
1344         blwi->blwi_lock = lock;
1345
1346         spin_lock(&blp->blp_lock);
1347         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1348         cfs_waitq_signal(&blp->blp_waitq);
1349         spin_unlock(&blp->blp_lock);
1350
1351         RETURN(0);
1352 #else
1353         RETURN(-ENOSYS);
1354 #endif
1355 }
1356
1357 static int ldlm_callback_handler(struct ptlrpc_request *req)
1358 {
1359         struct ldlm_namespace *ns;
1360         struct ldlm_request *dlm_req;
1361         struct ldlm_lock *lock;
1362         int rc;
1363         ENTRY;
1364
1365         /* Requests arrive in sender's byte order.  The ptlrpc service
1366          * handler has already checked and, if necessary, byte-swapped the
1367          * incoming request message body, but I am responsible for the
1368          * message buffers. */
1369
1370         if (req->rq_export == NULL) {
1371                 struct ldlm_request *dlm_req;
1372
1373                 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1374                        "export cookie "LPX64"; this is "
1375                        "normal if this node rebooted with a lock held\n",
1376                        lustre_msg_get_opc(req->rq_reqmsg),
1377                        libcfs_id2str(req->rq_peer),
1378                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1379
1380                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1381                                              sizeof(*dlm_req),
1382                                              lustre_swab_ldlm_request);
1383                 if (dlm_req != NULL)
1384                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1385                                dlm_req->lock_handle1.cookie);
1386
1387                 ldlm_callback_reply(req, -ENOTCONN);
1388                 RETURN(0);
1389         }
1390
1391         LASSERT(req->rq_export != NULL);
1392         LASSERT(req->rq_export->exp_obd != NULL);
1393
1394         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1395         case LDLM_BL_CALLBACK:
1396                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1397                 break;
1398         case LDLM_CP_CALLBACK:
1399                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1400                 break;
1401         case LDLM_GL_CALLBACK:
1402                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1403                 break;
1404         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1405                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1406                 rc = llog_origin_handle_cancel(req);
1407                 ldlm_callback_reply(req, rc);
1408                 RETURN(0);
1409         case OBD_QC_CALLBACK:
1410                 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1411                 rc = target_handle_qc_callback(req);
1412                 ldlm_callback_reply(req, rc);
1413                 RETURN(0);
1414         case QUOTA_DQACQ:
1415         case QUOTA_DQREL:
1416                 /* reply in handler */
1417                 rc = target_handle_dqacq_callback(req);
1418                 RETURN(0);
1419         case LLOG_ORIGIN_HANDLE_CREATE:
1420                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1421                 rc = llog_origin_handle_create(req);
1422                 ldlm_callback_reply(req, rc);
1423                 RETURN(0);
1424         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1425                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1426                 rc = llog_origin_handle_next_block(req);
1427                 ldlm_callback_reply(req, rc);
1428                 RETURN(0);
1429         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1430                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1431                 rc = llog_origin_handle_read_header(req);
1432                 ldlm_callback_reply(req, rc);
1433                 RETURN(0);
1434         case LLOG_ORIGIN_HANDLE_CLOSE:
1435                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1436                 rc = llog_origin_handle_close(req);
1437                 ldlm_callback_reply(req, rc);
1438                 RETURN(0);
1439         default:
1440                 CERROR("unknown opcode %u\n",
1441                        lustre_msg_get_opc(req->rq_reqmsg));
1442                 ldlm_callback_reply(req, -EPROTO);
1443                 RETURN(0);
1444         }
1445
1446         ns = req->rq_export->exp_obd->obd_namespace;
1447         LASSERT(ns != NULL);
1448
1449         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1450                                      lustre_swab_ldlm_request);
1451         if (dlm_req == NULL) {
1452                 CERROR ("can't unpack dlm_req\n");
1453                 ldlm_callback_reply(req, -EPROTO);
1454                 RETURN (0);
1455         }
1456
1457         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle1);
1458         if (!lock) {
1459                 CDEBUG(D_INODE, "callback on lock "LPX64" - lock disappeared\n",
1460                        dlm_req->lock_handle1.cookie);
1461                 ldlm_callback_reply(req, -EINVAL);
1462                 RETURN(0);
1463         }
1464
1465         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1466         lock_res_and_lock(lock);
1467         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1468         unlock_res_and_lock(lock);
1469
1470         /* We want the ost thread to get this reply so that it can respond
1471          * to ost requests (write cache writeback) that might be triggered
1472          * in the callback.
1473          *
1474          * But we'd also like to be able to indicate in the reply that we're
1475          * cancelling right now, because it's unused, or have an intent result
1476          * in the reply, so we might have to push the responsibility for sending
1477          * the reply down into the AST handlers, alas. */
1478
1479         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1480         case LDLM_BL_CALLBACK:
1481                 CDEBUG(D_INODE, "blocking ast\n");
1482                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1483                         ldlm_callback_reply(req, 0);
1484                 if (ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock))
1485                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1486                 break;
1487         case LDLM_CP_CALLBACK:
1488                 CDEBUG(D_INODE, "completion ast\n");
1489                 ldlm_callback_reply(req, 0);
1490                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1491                 break;
1492         case LDLM_GL_CALLBACK:
1493                 CDEBUG(D_INODE, "glimpse ast\n");
1494                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1495                 break;
1496         default:
1497                 LBUG();                         /* checked above */
1498         }
1499
1500         RETURN(0);
1501 }
1502
1503 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1504 {
1505         int rc;
1506         ENTRY;
1507
1508         /* Requests arrive in sender's byte order.  The ptlrpc service
1509          * handler has already checked and, if necessary, byte-swapped the
1510          * incoming request message body, but I am responsible for the
1511          * message buffers. */
1512
1513         if (req->rq_export == NULL) {
1514                 struct ldlm_request *dlm_req;
1515
1516                 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1517                        lustre_msg_get_opc(req->rq_reqmsg),
1518                        libcfs_id2str(req->rq_peer),
1519                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1520
1521                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1522                                              sizeof(*dlm_req),
1523                                              lustre_swab_ldlm_request);
1524                 if (dlm_req != NULL)
1525                         ldlm_lock_dump_handle(D_ERROR, &dlm_req->lock_handle1);
1526
1527                 ldlm_callback_reply(req, -ENOTCONN);
1528                 RETURN(0);
1529         }
1530
1531         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1532
1533         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1534         case LDLM_CANCEL:
1535                 CDEBUG(D_INODE, "cancel\n");
1536                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1537                 rc = ldlm_handle_cancel(req);
1538                 if (rc)
1539                         break;
1540                 RETURN(0);
1541         case OBD_LOG_CANCEL:
1542                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1543                 rc = llog_origin_handle_cancel(req);
1544                 ldlm_callback_reply(req, rc);
1545                 RETURN(0);
1546         default:
1547                 CERROR("invalid opcode %d\n",
1548                        lustre_msg_get_opc(req->rq_reqmsg));
1549                 ldlm_callback_reply(req, -EINVAL);
1550         }
1551
1552         RETURN(0);
1553 }
1554
1555 void ldlm_revoke_export_locks(struct obd_export *exp)
1556 {
1557         struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
1558         struct list_head  rpc_list;
1559         struct ldlm_lock *lock, *next;
1560         struct ldlm_lock_desc desc;
1561
1562         ENTRY;
1563         INIT_LIST_HEAD(&rpc_list);
1564
1565         spin_lock(&exp->exp_ldlm_data.led_lock);
1566         list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
1567                 lock_res_and_lock(lock);
1568
1569                 if (lock->l_req_mode != lock->l_granted_mode) {
1570                         unlock_res_and_lock(lock);
1571                         continue;
1572                 }
1573
1574                 LASSERT(lock->l_resource);
1575                 if (lock->l_resource->lr_type != LDLM_IBITS &&
1576                     lock->l_resource->lr_type != LDLM_PLAIN) {
1577                         unlock_res_and_lock(lock);
1578                         continue;
1579                 }
1580
1581                 if (lock->l_flags & LDLM_FL_AST_SENT) {
1582                         unlock_res_and_lock(lock);
1583                         continue;
1584                 }
1585
1586                 LASSERT(lock->l_blocking_ast);
1587                 LASSERT(!lock->l_blocking_lock);
1588
1589                 lock->l_flags |= LDLM_FL_AST_SENT;
1590                 list_move(&lock->l_export_chain, &rpc_list);
1591
1592                 unlock_res_and_lock(lock);
1593         }
1594         spin_unlock(&exp->exp_ldlm_data.led_lock);
1595
1596         while (!list_empty(&rpc_list)) {
1597                 lock = list_entry(rpc_list.next, struct ldlm_lock,
1598                                   l_export_chain);
1599                 list_del_init(&lock->l_export_chain);
1600
1601                 /* the desc just pretend to exclusive */
1602                 ldlm_lock2desc(lock, &desc);
1603                 desc.l_req_mode = LCK_EX;
1604                 desc.l_granted_mode = 0;
1605
1606                 LDLM_LOCK_GET(lock);
1607                 lock->l_blocking_ast(lock, &desc, lock->l_ast_data,
1608                                      LDLM_CB_BLOCKING);
1609                 LDLM_LOCK_PUT(lock);
1610         }
1611         EXIT;
1612 }
1613
1614 #ifdef __KERNEL__
1615 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1616 {
1617         struct ldlm_bl_work_item *blwi = NULL;
1618
1619         spin_lock(&blp->blp_lock);
1620         if (!list_empty(&blp->blp_list)) {
1621                 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
1622                                   blwi_entry);
1623                 list_del(&blwi->blwi_entry);
1624         }
1625         spin_unlock(&blp->blp_lock);
1626
1627         return blwi;
1628 }
1629
1630 struct ldlm_bl_thread_data {
1631         int                     bltd_num;
1632         struct ldlm_bl_pool     *bltd_blp;
1633 };
1634
1635 static int ldlm_bl_thread_main(void *arg)
1636 {
1637         struct ldlm_bl_thread_data *bltd = arg;
1638         struct ldlm_bl_pool *blp = bltd->bltd_blp;
1639         ENTRY;
1640
1641         {
1642                 char name[CFS_CURPROC_COMM_MAX];
1643                 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1644                          bltd->bltd_num);
1645                 cfs_daemonize(name);
1646         }
1647
1648         atomic_inc(&blp->blp_num_threads);
1649         complete(&blp->blp_comp);
1650
1651         while(1) {
1652                 struct l_wait_info lwi = { 0 };
1653                 struct ldlm_bl_work_item *blwi = NULL;
1654
1655                 l_wait_event_exclusive(blp->blp_waitq,
1656                                        (blwi = ldlm_bl_get_work(blp)) != NULL,
1657                                        &lwi);
1658
1659                 if (blwi->blwi_ns == NULL)
1660                         break;
1661
1662                 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1663                                         blwi->blwi_lock);
1664                 OBD_FREE(blwi, sizeof(*blwi));
1665         }
1666
1667         atomic_dec(&blp->blp_num_threads);
1668         complete(&blp->blp_comp);
1669         RETURN(0);
1670 }
1671
1672 #endif
1673
1674 static int ldlm_setup(void);
1675 static int ldlm_cleanup(int force);
1676
1677 int ldlm_get_ref(void)
1678 {
1679         int rc = 0;
1680         ENTRY;
1681         mutex_down(&ldlm_ref_sem);
1682         if (++ldlm_refcount == 1) {
1683                 rc = ldlm_setup();
1684                 if (rc)
1685                         ldlm_refcount--;
1686         }
1687         mutex_up(&ldlm_ref_sem);
1688
1689         RETURN(rc);
1690 }
1691
1692 void ldlm_put_ref(int force)
1693 {
1694         ENTRY;
1695         mutex_down(&ldlm_ref_sem);
1696         if (ldlm_refcount == 1) {
1697                 int rc = ldlm_cleanup(force);
1698                 if (rc)
1699                         CERROR("ldlm_cleanup failed: %d\n", rc);
1700                 else
1701                         ldlm_refcount--;
1702         } else {
1703                 ldlm_refcount--;
1704         }
1705         mutex_up(&ldlm_ref_sem);
1706
1707         EXIT;
1708 }
1709
1710 static int ldlm_setup(void)
1711 {
1712         struct ldlm_bl_pool *blp;
1713         int rc = 0;
1714 #ifdef __KERNEL__
1715         int i;
1716 #endif
1717         ENTRY;
1718
1719         if (ldlm_state != NULL)
1720                 RETURN(-EALREADY);
1721
1722         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1723         if (ldlm_state == NULL)
1724                 RETURN(-ENOMEM);
1725
1726 #ifdef LPROCFS
1727         rc = ldlm_proc_setup();
1728         if (rc != 0)
1729                 GOTO(out_free, rc);
1730 #endif
1731
1732         ldlm_state->ldlm_cb_service =
1733                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1734                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
1735                                 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
1736                                 ldlm_callback_handler, "ldlm_cbd",
1737                                 ldlm_svc_proc_dir, NULL,
1738                                 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1739                                 "ldlm_cb",
1740                                 LCT_MD_THREAD|LCT_DT_THREAD);
1741
1742         if (!ldlm_state->ldlm_cb_service) {
1743                 CERROR("failed to start service\n");
1744                 GOTO(out_proc, rc = -ENOMEM);
1745         }
1746
1747         ldlm_state->ldlm_cancel_service =
1748                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1749                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
1750                                 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
1751                                 ldlm_cancel_handler, "ldlm_canceld",
1752                                 ldlm_svc_proc_dir, NULL,
1753                                 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1754                                 "ldlm_cn",
1755                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
1756
1757         if (!ldlm_state->ldlm_cancel_service) {
1758                 CERROR("failed to start service\n");
1759                 GOTO(out_proc, rc = -ENOMEM);
1760         }
1761
1762         OBD_ALLOC(blp, sizeof(*blp));
1763         if (blp == NULL)
1764                 GOTO(out_proc, rc = -ENOMEM);
1765         ldlm_state->ldlm_bl_pool = blp;
1766
1767         atomic_set(&blp->blp_num_threads, 0);
1768         cfs_waitq_init(&blp->blp_waitq);
1769         spin_lock_init(&blp->blp_lock);
1770
1771         CFS_INIT_LIST_HEAD(&blp->blp_list);
1772
1773 #ifdef __KERNEL__
1774         for (i = 0; i < LDLM_BL_THREADS; i++) {
1775                 struct ldlm_bl_thread_data bltd = {
1776                         .bltd_num = i,
1777                         .bltd_blp = blp,
1778                 };
1779                 init_completion(&blp->blp_comp);
1780                 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1781                 if (rc < 0) {
1782                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1783                         GOTO(out_thread, rc);
1784                 }
1785                 wait_for_completion(&blp->blp_comp);
1786         }
1787
1788         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
1789         if (rc)
1790                 GOTO(out_thread, rc);
1791
1792         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
1793         if (rc)
1794                 GOTO(out_thread, rc);
1795
1796         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1797         expired_lock_thread.elt_state = ELT_STOPPED;
1798         cfs_waitq_init(&expired_lock_thread.elt_waitq);
1799
1800         CFS_INIT_LIST_HEAD(&waiting_locks_list);
1801         spin_lock_init(&waiting_locks_spinlock);
1802         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
1803
1804         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
1805         if (rc < 0) {
1806                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1807                 GOTO(out_thread, rc);
1808         }
1809
1810         wait_event(expired_lock_thread.elt_waitq,
1811                    expired_lock_thread.elt_state == ELT_READY);
1812 #endif
1813
1814         RETURN(0);
1815
1816 #ifdef __KERNEL__
1817  out_thread:
1818         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1819         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1820 #endif
1821
1822  out_proc:
1823 #ifdef LPROCFS
1824         ldlm_proc_cleanup();
1825  out_free:
1826 #endif
1827         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1828         ldlm_state = NULL;
1829         return rc;
1830 }
1831
1832 static int ldlm_cleanup(int force)
1833 {
1834 #ifdef __KERNEL__
1835         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1836 #endif
1837         ENTRY;
1838
1839         if (!list_empty(&ldlm_namespace_list)) {
1840                 CERROR("ldlm still has namespaces; clean these up first.\n");
1841                 ldlm_dump_all_namespaces(D_DLMTRACE);
1842                 RETURN(-EBUSY);
1843         }
1844
1845 #ifdef __KERNEL__
1846         while (atomic_read(&blp->blp_num_threads) > 0) {
1847                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1848
1849                 init_completion(&blp->blp_comp);
1850
1851                 spin_lock(&blp->blp_lock);
1852                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1853                 cfs_waitq_signal(&blp->blp_waitq);
1854                 spin_unlock(&blp->blp_lock);
1855
1856                 wait_for_completion(&blp->blp_comp);
1857         }
1858         OBD_FREE(blp, sizeof(*blp));
1859
1860         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1861         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1862         ldlm_proc_cleanup();
1863
1864         expired_lock_thread.elt_state = ELT_TERMINATE;
1865         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
1866         wait_event(expired_lock_thread.elt_waitq,
1867                    expired_lock_thread.elt_state == ELT_STOPPED);
1868 #else
1869         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1870         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1871 #endif
1872
1873         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1874         ldlm_state = NULL;
1875
1876         RETURN(0);
1877 }
1878
1879 int __init ldlm_init(void)
1880 {
1881         init_mutex(&ldlm_ref_sem);
1882         init_mutex(&ldlm_namespace_lock);
1883         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
1884                                                sizeof(struct ldlm_resource), 0,
1885                                                SLAB_HWCACHE_ALIGN);
1886         if (ldlm_resource_slab == NULL)
1887                 return -ENOMEM;
1888
1889         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
1890                                            sizeof(struct ldlm_lock), 0,
1891                                            SLAB_HWCACHE_ALIGN);
1892         if (ldlm_lock_slab == NULL) {
1893                 cfs_mem_cache_destroy(ldlm_resource_slab);
1894                 return -ENOMEM;
1895         }
1896
1897         return 0;
1898 }
1899
1900 void __exit ldlm_exit(void)
1901 {
1902         int rc;
1903         if (ldlm_refcount)
1904                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1905         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
1906         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
1907         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
1908         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
1909 }
1910
1911 /* ldlm_extent.c */
1912 EXPORT_SYMBOL(ldlm_extent_shift_kms);
1913
1914 /* ldlm_lock.c */
1915 EXPORT_SYMBOL(ldlm_get_processing_policy);
1916 EXPORT_SYMBOL(ldlm_lock2desc);
1917 EXPORT_SYMBOL(ldlm_register_intent);
1918 EXPORT_SYMBOL(ldlm_lockname);
1919 EXPORT_SYMBOL(ldlm_typename);
1920 EXPORT_SYMBOL(ldlm_lock2handle);
1921 EXPORT_SYMBOL(__ldlm_handle2lock);
1922 EXPORT_SYMBOL(ldlm_lock_get);
1923 EXPORT_SYMBOL(ldlm_lock_put);
1924 EXPORT_SYMBOL(ldlm_lock_match);
1925 EXPORT_SYMBOL(ldlm_lock_cancel);
1926 EXPORT_SYMBOL(ldlm_lock_addref);
1927 EXPORT_SYMBOL(ldlm_lock_decref);
1928 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1929 EXPORT_SYMBOL(ldlm_lock_change_resource);
1930 EXPORT_SYMBOL(ldlm_lock_set_data);
1931 EXPORT_SYMBOL(ldlm_it2str);
1932 EXPORT_SYMBOL(ldlm_lock_dump);
1933 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1934 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1935 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1936 EXPORT_SYMBOL(ldlm_lock_allow_match);
1937
1938 /* ldlm_request.c */
1939 EXPORT_SYMBOL(ldlm_completion_ast);
1940 EXPORT_SYMBOL(ldlm_blocking_ast);
1941 EXPORT_SYMBOL(ldlm_glimpse_ast);
1942 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1943 EXPORT_SYMBOL(ldlm_cli_convert);
1944 EXPORT_SYMBOL(ldlm_cli_enqueue);
1945 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
1946 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
1947 EXPORT_SYMBOL(ldlm_cli_cancel);
1948 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1949 EXPORT_SYMBOL(ldlm_cli_join_lru);
1950 EXPORT_SYMBOL(ldlm_replay_locks);
1951 EXPORT_SYMBOL(ldlm_resource_foreach);
1952 EXPORT_SYMBOL(ldlm_namespace_foreach);
1953 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1954 EXPORT_SYMBOL(ldlm_resource_iterate);
1955
1956 /* ldlm_lockd.c */
1957 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1958 EXPORT_SYMBOL(ldlm_server_completion_ast);
1959 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1960 EXPORT_SYMBOL(ldlm_handle_enqueue);
1961 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1962 EXPORT_SYMBOL(ldlm_handle_cancel);
1963 EXPORT_SYMBOL(ldlm_handle_convert);
1964 EXPORT_SYMBOL(ldlm_handle_convert0);
1965 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1966 EXPORT_SYMBOL(ldlm_get_ref);
1967 EXPORT_SYMBOL(ldlm_put_ref);
1968 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
1969 EXPORT_SYMBOL(ldlm_revoke_export_locks);
1970
1971 /* ldlm_resource.c */
1972 EXPORT_SYMBOL(ldlm_namespace_new);
1973 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1974 EXPORT_SYMBOL(ldlm_namespace_free);
1975 EXPORT_SYMBOL(ldlm_namespace_dump);
1976 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1977 EXPORT_SYMBOL(ldlm_resource_get);
1978 EXPORT_SYMBOL(ldlm_resource_putref);
1979 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1980
1981 /* ldlm_lib.c */
1982 EXPORT_SYMBOL(client_import_add_conn);
1983 EXPORT_SYMBOL(client_import_del_conn);
1984 EXPORT_SYMBOL(client_obd_setup);
1985 EXPORT_SYMBOL(client_obd_cleanup);
1986 EXPORT_SYMBOL(client_connect_import);
1987 EXPORT_SYMBOL(client_disconnect_export);
1988 EXPORT_SYMBOL(target_start_recovery_thread);
1989 EXPORT_SYMBOL(target_stop_recovery_thread);
1990 EXPORT_SYMBOL(target_handle_connect);
1991 EXPORT_SYMBOL(target_cleanup_recovery);
1992 EXPORT_SYMBOL(target_destroy_export);
1993 EXPORT_SYMBOL(target_cancel_recovery_timer);
1994 EXPORT_SYMBOL(target_send_reply);
1995 EXPORT_SYMBOL(target_queue_recovery_request);
1996 EXPORT_SYMBOL(target_handle_ping);
1997 EXPORT_SYMBOL(target_handle_disconnect);
1998
1999 /* l_lock.c */
2000 EXPORT_SYMBOL(lock_res_and_lock);
2001 EXPORT_SYMBOL(unlock_res_and_lock);
2002