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