Whamcloud - gitweb
b=23596 account direct i/o inflight separately from non-direct i/o
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_lockd.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45 #define DEBUG_SUBSYSTEM S_LDLM
46
47 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 #else
50 # include <liblustre.h>
51 #endif
52
53 #include <lustre_dlm.h>
54 #include <obd_class.h>
55 #include <libcfs/list.h>
56 #include "ldlm_internal.h"
57
58 #ifdef __KERNEL__
59 static int ldlm_num_threads;
60 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
61                 "number of DLM service threads to start");
62 #endif
63
64 extern cfs_mem_cache_t *ldlm_resource_slab;
65 extern cfs_mem_cache_t *ldlm_lock_slab;
66 extern struct lustre_lock ldlm_handle_lock;
67
68 static struct semaphore ldlm_ref_sem;
69 static int ldlm_refcount;
70
71 static struct ldlm_state *ldlm_state;
72
73 inline cfs_time_t round_timeout(cfs_time_t timeout)
74 {
75         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
76 }
77
78 /* timeout for initial callback (AST) reply (bz10399) */
79 static inline unsigned int ldlm_get_rq_timeout(void)
80 {
81         /* Non-AT value */
82         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
83
84         return timeout < 1 ? 1 : timeout;
85 }
86
87 #ifdef __KERNEL__
88 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
89 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
90 static struct list_head waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
92
93 static struct expired_lock_thread {
94         cfs_waitq_t               elt_waitq;
95         int                       elt_state;
96         int                       elt_dump;
97         struct list_head          elt_expired_locks;
98 } expired_lock_thread;
99 #endif
100
101 #define ELT_STOPPED   0
102 #define ELT_READY     1
103 #define ELT_TERMINATE 2
104
105 struct ldlm_bl_pool {
106         spinlock_t              blp_lock;
107
108         /*
109          * blp_prio_list is used for callbacks that should be handled
110          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
111          * see bug 13843
112          */
113         struct list_head        blp_prio_list;
114
115         /*
116          * blp_list is used for all other callbacks which are likely
117          * to take longer to process.
118          */
119         struct list_head        blp_list;
120
121         cfs_waitq_t             blp_waitq;
122         struct completion       blp_comp;
123         atomic_t                blp_num_threads;
124         atomic_t                blp_busy_threads;
125         int                     blp_min_threads;
126         int                     blp_max_threads;
127 };
128
129 struct ldlm_bl_work_item {
130         struct list_head        blwi_entry;
131         struct ldlm_namespace   *blwi_ns;
132         struct ldlm_lock_desc   blwi_ld;
133         struct ldlm_lock        *blwi_lock;
134         struct list_head        blwi_head;
135         int                     blwi_count;
136         struct completion       blwi_comp;
137         int                     blwi_mode;
138         int                     blwi_mem_pressure;
139 };
140
141 #ifdef __KERNEL__
142
143 static inline int have_expired_locks(void)
144 {
145         int need_to_run;
146         ENTRY;
147
148         spin_lock_bh(&waiting_locks_spinlock);
149         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
150         spin_unlock_bh(&waiting_locks_spinlock);
151
152         RETURN(need_to_run);
153 }
154
155 static int expired_lock_main(void *arg)
156 {
157         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
158         struct l_wait_info lwi = { 0 };
159         int do_dump;
160
161         ENTRY;
162         cfs_daemonize("ldlm_elt");
163
164         expired_lock_thread.elt_state = ELT_READY;
165         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
166
167         while (1) {
168                 l_wait_event(expired_lock_thread.elt_waitq,
169                              have_expired_locks() ||
170                              expired_lock_thread.elt_state == ELT_TERMINATE,
171                              &lwi);
172
173                 spin_lock_bh(&waiting_locks_spinlock);
174                 if (expired_lock_thread.elt_dump) {
175                         spin_unlock_bh(&waiting_locks_spinlock);
176
177                         /* from waiting_locks_callback, but not in timer */
178                         libcfs_debug_dumplog();
179                         libcfs_run_lbug_upcall(__FILE__,
180                                                 "waiting_locks_callback",
181                                                 expired_lock_thread.elt_dump);
182
183                         spin_lock_bh(&waiting_locks_spinlock);
184                         expired_lock_thread.elt_dump = 0;
185                 }
186
187                 do_dump = 0;
188
189                 while (!list_empty(expired)) {
190                         struct obd_export *export;
191                         struct ldlm_lock *lock;
192
193                         lock = list_entry(expired->next, struct ldlm_lock,
194                                           l_pending_chain);
195                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
196                             (void *)lock >= LP_POISON) {
197                                 spin_unlock_bh(&waiting_locks_spinlock);
198                                 CERROR("free lock on elt list %p\n", lock);
199                                 LBUG();
200                         }
201                         list_del_init(&lock->l_pending_chain);
202                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
203                             (void *)lock->l_export >= LP_POISON) {
204                                 CERROR("lock with free export on elt list %p\n",
205                                        lock->l_export);
206                                 lock->l_export = NULL;
207                                 LDLM_ERROR(lock, "free export");
208                                 /* release extra ref grabbed by
209                                  * ldlm_add_waiting_lock() or
210                                  * ldlm_failed_ast() */
211                                 LDLM_LOCK_PUT(lock);
212                                 continue;
213                         }
214                         export = class_export_get(lock->l_export);
215                         spin_unlock_bh(&waiting_locks_spinlock);
216
217                         /* release extra ref grabbed by ldlm_add_waiting_lock()
218                          * or ldlm_failed_ast() */
219                         LDLM_LOCK_PUT(lock);
220
221                         do_dump++;
222                         class_fail_export(export);
223                         class_export_put(export);
224                         spin_lock_bh(&waiting_locks_spinlock);
225                 }
226                 spin_unlock_bh(&waiting_locks_spinlock);
227
228                 if (do_dump && obd_dump_on_eviction) {
229                         CERROR("dump the log upon eviction\n");
230                         libcfs_debug_dumplog();
231                 }
232
233                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
234                         break;
235         }
236
237         expired_lock_thread.elt_state = ELT_STOPPED;
238         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
239         RETURN(0);
240 }
241
242 /**
243  * Check if there is a request in the export request list
244  * which prevents the lock canceling.
245  */
246 static int ldlm_lock_busy(struct ldlm_lock *lock)
247 {
248         struct ptlrpc_request *req;
249         int match = 0;
250         ENTRY;
251
252         if (lock->l_export == NULL)
253                 return 0;
254
255         spin_lock(&lock->l_export->exp_lock);
256         list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
257                 if (req->rq_ops->hpreq_lock_match) {
258                         match = req->rq_ops->hpreq_lock_match(req, lock);
259                         if (match)
260                                 break;
261                 }
262         }
263         spin_unlock(&lock->l_export->exp_lock);
264         RETURN(match);
265 }
266
267 /* This is called from within a timer interrupt and cannot schedule */
268 static void waiting_locks_callback(unsigned long unused)
269 {
270         struct ldlm_lock *lock, *last = NULL;
271
272         spin_lock_bh(&waiting_locks_spinlock);
273         while (!list_empty(&waiting_locks_list)) {
274                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
275                                   l_pending_chain);
276                 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current())
277                     || (lock->l_req_mode == LCK_GROUP))
278                         break;
279
280                 /* Check if we need to prolong timeout */
281                 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
282                     ldlm_lock_busy(lock)) {
283                         int cont = 1;
284
285                         if (lock->l_pending_chain.next == &waiting_locks_list)
286                                 cont = 0;
287
288                         LDLM_LOCK_GET(lock);
289                         spin_unlock_bh(&waiting_locks_spinlock);
290                         LDLM_DEBUG(lock, "prolong the busy lock");
291                         ldlm_refresh_waiting_lock(lock,
292                                                   ldlm_get_enq_timeout(lock));
293                         spin_lock_bh(&waiting_locks_spinlock);
294
295                         if (!cont) {
296                                 LDLM_LOCK_PUT(lock);
297                                 break;
298                         }
299
300                         LDLM_LOCK_PUT(lock);
301                         continue;
302                 }
303                 lock->l_resource->lr_namespace->ns_timeouts++;
304                 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
305                            "evicting client at %s ",
306                            cfs_time_current_sec()- lock->l_last_activity,
307                            libcfs_nid2str(
308                                    lock->l_export->exp_connection->c_peer.nid));
309                 if (lock == last) {
310                         LDLM_ERROR(lock, "waiting on lock multiple times");
311                         CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
312                                &waiting_locks_list,
313                                waiting_locks_list.next, waiting_locks_list.prev,
314                                &lock->l_pending_chain,
315                                lock->l_pending_chain.next,
316                                lock->l_pending_chain.prev);
317
318                         CFS_INIT_LIST_HEAD(&waiting_locks_list);    /* HACK */
319                         expired_lock_thread.elt_dump = __LINE__;
320
321                         /* LBUG(); */
322                         CEMERG("would be an LBUG, but isn't (bug 5653)\n");
323                         libcfs_debug_dumpstack(NULL);
324                         /*blocks* libcfs_debug_dumplog(); */
325                         /*blocks* libcfs_run_lbug_upcall(file, func, line); */
326                         break;
327                 }
328                 last = lock;
329
330                 /* no needs to take an extra ref on the lock since it was in
331                  * the waiting_locks_list and ldlm_add_waiting_lock()
332                  * already grabbed a ref */
333                 list_del(&lock->l_pending_chain);
334                 list_add(&lock->l_pending_chain,
335                          &expired_lock_thread.elt_expired_locks);
336         }
337
338         if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
339                 if (obd_dump_on_timeout)
340                         expired_lock_thread.elt_dump = __LINE__;
341
342                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
343         }
344
345         /*
346          * Make sure the timer will fire again if we have any locks
347          * left.
348          */
349         if (!list_empty(&waiting_locks_list)) {
350                 cfs_time_t timeout_rounded;
351                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
352                                   l_pending_chain);
353                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
354                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
355         }
356         spin_unlock_bh(&waiting_locks_spinlock);
357 }
358
359 /*
360  * Indicate that we're waiting for a client to call us back cancelling a given
361  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
362  * timer to fire appropriately.  (We round up to the next second, to avoid
363  * floods of timer firings during periods of high lock contention and traffic).
364  * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
365  * if it has been added to the waiting list (1 is returned).
366  *
367  * Called with the namespace lock held.
368  */
369 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
370 {
371         cfs_time_t timeout;
372         cfs_time_t timeout_rounded;
373
374         if (!list_empty(&lock->l_pending_chain))
375                 return 0;
376
377         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
378             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
379                 seconds = 1;
380
381         timeout = cfs_time_shift(seconds);
382         if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
383                 lock->l_callback_timeout = timeout;
384
385         timeout_rounded = round_timeout(lock->l_callback_timeout);
386
387         if (cfs_time_before(timeout_rounded,
388                             cfs_timer_deadline(&waiting_locks_timer)) ||
389             !cfs_timer_is_armed(&waiting_locks_timer)) {
390                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
391         }
392         /* if the new lock has a shorter timeout than something earlier on
393            the list, we'll wait the longer amount of time; no big deal. */
394         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
395         return 1;
396 }
397
398 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
399 {
400         int ret;
401         int timeout = ldlm_get_enq_timeout(lock);
402
403         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
404
405         spin_lock_bh(&waiting_locks_spinlock);
406         if (lock->l_destroyed) {
407                 static cfs_time_t next;
408                 spin_unlock_bh(&waiting_locks_spinlock);
409                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
410                 if (cfs_time_after(cfs_time_current(), next)) {
411                         next = cfs_time_shift(14400);
412                         libcfs_debug_dumpstack(NULL);
413                 }
414                 return 0;
415         }
416
417         ret = __ldlm_add_waiting_lock(lock, timeout);
418         if (ret)
419                 /* grab ref on the lock if it has been added to the
420                  * waiting list */
421                 LDLM_LOCK_GET(lock);
422         spin_unlock_bh(&waiting_locks_spinlock);
423
424         LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
425                    ret == 0 ? "not re-" : "", timeout,
426                    AT_OFF ? "off" : "on");
427         return ret;
428 }
429
430 /*
431  * Remove a lock from the pending list, likely because it had its cancellation
432  * callback arrive without incident.  This adjusts the lock-timeout timer if
433  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
434  * As done by ldlm_del_waiting_lock(), the caller must release the lock
435  * reference when the lock is removed from any list (1 is returned).
436  *
437  * Called with namespace lock held.
438  */
439 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
440 {
441         struct list_head *list_next;
442
443         if (list_empty(&lock->l_pending_chain))
444                 return 0;
445
446         list_next = lock->l_pending_chain.next;
447         if (lock->l_pending_chain.prev == &waiting_locks_list) {
448                 /* Removing the head of the list, adjust timer. */
449                 if (list_next == &waiting_locks_list) {
450                         /* No more, just cancel. */
451                         cfs_timer_disarm(&waiting_locks_timer);
452                 } else {
453                         struct ldlm_lock *next;
454                         next = list_entry(list_next, struct ldlm_lock,
455                                           l_pending_chain);
456                         cfs_timer_arm(&waiting_locks_timer,
457                                       round_timeout(next->l_callback_timeout));
458                 }
459         }
460         list_del_init(&lock->l_pending_chain);
461
462         return 1;
463 }
464
465 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
466 {
467         int ret;
468
469         if (lock->l_export == NULL) {
470                 /* We don't have a "waiting locks list" on clients. */
471                 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
472                 return 0;
473         }
474
475         spin_lock_bh(&waiting_locks_spinlock);
476         ret = __ldlm_del_waiting_lock(lock);
477         spin_unlock_bh(&waiting_locks_spinlock);
478         if (ret)
479                 /* release lock ref if it has indeed been removed
480                  * from a list */
481                 LDLM_LOCK_PUT(lock);
482
483         return ret;
484 }
485
486 /*
487  * Prolong the lock
488  *
489  * Called with namespace lock held.
490  */
491 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
492 {
493         if (lock->l_export == NULL) {
494                 /* We don't have a "waiting locks list" on clients. */
495                 LDLM_DEBUG(lock, "client lock: no-op");
496                 return 0;
497         }
498
499         spin_lock_bh(&waiting_locks_spinlock);
500
501         if (list_empty(&lock->l_pending_chain)) {
502                 spin_unlock_bh(&waiting_locks_spinlock);
503                 LDLM_DEBUG(lock, "wasn't waiting");
504                 return 0;
505         }
506
507         /* we remove/add the lock to the waiting list, so no needs to
508          * release/take a lock reference */
509         __ldlm_del_waiting_lock(lock);
510         __ldlm_add_waiting_lock(lock, timeout);
511         spin_unlock_bh(&waiting_locks_spinlock);
512
513         LDLM_DEBUG(lock, "refreshed");
514         return 1;
515 }
516 #else /* !__KERNEL__ */
517
518 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
519 {
520         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
521         RETURN(1);
522 }
523
524 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
525 {
526         RETURN(0);
527 }
528
529 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
530 {
531         RETURN(0);
532 }
533 #endif /* __KERNEL__ */
534
535 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
536                             const char *ast_type)
537 {
538         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
539         char                     *str = libcfs_nid2str(conn->c_peer.nid);
540
541         LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
542                              "to a lock %s callback to %s timed out: rc %d\n",
543                              lock->l_export->exp_obd->obd_name, str,
544                              ast_type, obd_export_nid2str(lock->l_export), rc);
545
546         if (obd_dump_on_timeout)
547                 libcfs_debug_dumplog();
548 #ifdef __KERNEL__
549         spin_lock_bh(&waiting_locks_spinlock);
550         if (__ldlm_del_waiting_lock(lock) == 0)
551                 /* the lock was not in any list, grab an extra ref before adding
552                  * the lock to the expired list */
553                 LDLM_LOCK_GET(lock);
554         list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
555         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
556         spin_unlock_bh(&waiting_locks_spinlock);
557 #else
558         class_fail_export(lock->l_export);
559 #endif
560 }
561
562 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
563                                  struct ptlrpc_request *req, int rc,
564                                  const char *ast_type)
565 {
566         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
567
568         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
569                 LASSERT(lock->l_export);
570                 if (lock->l_export->exp_libclient) {
571                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
572                                    " timeout, just cancelling lock", ast_type,
573                                    libcfs_nid2str(peer.nid));
574                         ldlm_lock_cancel(lock);
575                         rc = -ERESTART;
576                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
577                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
578                                    "cancel was received (AST reply lost?)",
579                                    ast_type, libcfs_nid2str(peer.nid));
580                         ldlm_lock_cancel(lock);
581                         rc = -ERESTART;
582                 } else {
583                         ldlm_del_waiting_lock(lock);
584                         ldlm_failed_ast(lock, rc, ast_type);
585                 }
586         } else if (rc) {
587                 if (rc == -EINVAL) {
588                         struct ldlm_resource *res = lock->l_resource;
589
590                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
591                                    " from %s AST - normal race",
592                                    libcfs_nid2str(peer.nid),
593                                    lustre_msg_get_status(req->rq_repmsg),
594                                    ast_type);
595                         if (res) {
596                                 /* update lvbo to return proper attributes.
597                                  * see bug 23174 */
598                                 ldlm_resource_getref(res);
599                                 ldlm_res_lvbo_update(res, NULL, 0, 1);
600                                 ldlm_resource_putref(res);
601                         }
602                 } else {
603                         LDLM_ERROR(lock, "client (nid %s) returned %d "
604                                    "from %s AST", libcfs_nid2str(peer.nid),
605                                    (req->rq_repmsg != NULL) ?
606                                    lustre_msg_get_status(req->rq_repmsg) : 0,
607                                    ast_type);
608                 }
609                 ldlm_lock_cancel(lock);
610                 /* Server-side AST functions are called from ldlm_reprocess_all,
611                  * which needs to be told to please restart its reprocessing. */
612                 rc = -ERESTART;
613         }
614
615         return rc;
616 }
617
618 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
619 {
620         struct ldlm_cb_set_arg *arg;
621         struct ldlm_lock *lock;
622         ENTRY;
623
624         LASSERT(data != NULL);
625
626         arg = req->rq_async_args.pointer_arg[0];
627         lock = req->rq_async_args.pointer_arg[1];
628         LASSERT(lock != NULL);
629         if (rc != 0) {
630                 rc = ldlm_handle_ast_error(lock, req, rc,
631                                            arg->type == LDLM_BL_CALLBACK
632                                            ? "blocking" : "completion");
633         }
634
635         LDLM_LOCK_PUT(lock);
636
637         if (rc == -ERESTART)
638                 atomic_set(&arg->restart, 1);
639
640         RETURN(0);
641 }
642
643 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
644                                           struct ldlm_cb_set_arg *arg,
645                                           struct ldlm_lock *lock,
646                                           int instant_cancel)
647 {
648         int rc = 0;
649         ENTRY;
650
651         if (unlikely(instant_cancel)) {
652                 rc = ptl_send_rpc(req, 1);
653                 ptlrpc_req_finished(req);
654                 if (rc == 0)
655                         /* If we cancelled the lock, we need to restart
656                          * ldlm_reprocess_queue */
657                         atomic_set(&arg->restart, 1);
658         } else {
659                 LDLM_LOCK_GET(lock);
660                 ptlrpc_set_add_req(arg->set, req);
661         }
662
663         RETURN(rc);
664 }
665
666 /**
667  * Check if there are requests in the export request list which prevent
668  * the lock canceling and make these requests high priority ones.
669  */
670 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
671 {
672         struct ptlrpc_request *req;
673         ENTRY;
674
675         if (lock->l_export == NULL) {
676                 LDLM_DEBUG(lock, "client lock: no-op");
677                 RETURN_EXIT;
678         }
679
680         spin_lock(&lock->l_export->exp_lock);
681         list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
682                 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
683                     req->rq_ops->hpreq_lock_match(req, lock))
684                         ptlrpc_hpreq_reorder(req);
685         }
686         spin_unlock(&lock->l_export->exp_lock);
687         EXIT;
688 }
689
690 /*
691  * ->l_blocking_ast() method for server-side locks. This is invoked when newly
692  * enqueued server lock conflicts with given one.
693  *
694  * Sends blocking ast rpc to the client owning that lock; arms timeout timer
695  * to wait for client response.
696  */
697 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
698                              struct ldlm_lock_desc *desc,
699                              void *data, int flag)
700 {
701         struct ldlm_cb_set_arg *arg = data;
702         struct ldlm_request *body;
703         struct ptlrpc_request *req;
704         __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
705                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
706         int instant_cancel = 0, rc;
707         ENTRY;
708
709         if (flag == LDLM_CB_CANCELING) {
710                 /* Don't need to do anything here. */
711                 RETURN(0);
712         }
713
714         LASSERT(lock);
715         LASSERT(data != NULL);
716
717         ldlm_lock_reorder_req(lock);
718
719         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
720                               LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
721                               NULL);
722         if (req == NULL)
723                 RETURN(-ENOMEM);
724
725         req->rq_async_args.pointer_arg[0] = arg;
726         req->rq_async_args.pointer_arg[1] = lock;
727         req->rq_interpret_reply = ldlm_cb_interpret;
728         req->rq_no_resend = 1;
729
730         lock_res(lock->l_resource);
731         if (lock->l_granted_mode != lock->l_req_mode) {
732                 /* this blocking AST will be communicated as part of the
733                  * completion AST instead */
734                 unlock_res(lock->l_resource);
735                 ptlrpc_req_finished(req);
736                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
737                 RETURN(0);
738         }
739
740         if (lock->l_destroyed) {
741                 /* What's the point? */
742                 unlock_res(lock->l_resource);
743                 ptlrpc_req_finished(req);
744                 RETURN(0);
745         }
746
747 #if 0
748         if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
749                 unlock_res(lock->l_resource);
750                 ptlrpc_req_finished(req);
751                 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
752                 RETURN(-ETIMEDOUT);
753         }
754 #endif
755
756         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
757                 instant_cancel = 1;
758
759         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
760         body->lock_handle[0] = lock->l_remote_handle;
761         body->lock_desc = *desc;
762         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
763
764         LDLM_DEBUG(lock, "server preparing blocking AST");
765
766         lock->l_last_activity = cfs_time_current_sec();
767
768         ptlrpc_req_set_repsize(req, 1, NULL);
769         if (instant_cancel) {
770                 unlock_res(lock->l_resource);
771                 ldlm_lock_cancel(lock);
772         } else {
773                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
774                 ldlm_add_waiting_lock(lock);
775                 unlock_res(lock->l_resource);
776         }
777
778         req->rq_send_state = LUSTRE_IMP_FULL;
779         /* ptlrpc_prep_req already set timeout */
780         if (AT_OFF)
781                 req->rq_timeout = ldlm_get_rq_timeout();
782
783         if (lock->l_export && lock->l_export->exp_nid_stats &&
784             lock->l_export->exp_nid_stats->nid_ldlm_stats) {
785                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
786                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
787         }
788
789         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
790
791         RETURN(rc);
792 }
793
794 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
795 {
796         struct ldlm_cb_set_arg *arg = data;
797         struct ldlm_request *body;
798         struct ptlrpc_request *req;
799         long total_enqueue_wait;
800         __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
801                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
802         int rc, buffers = 2, instant_cancel = 0;
803         ENTRY;
804
805         LASSERT(lock != NULL);
806         LASSERT(data != NULL);
807
808         total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
809                                           lock->l_last_activity);
810
811         lock_res_and_lock(lock);
812         if (lock->l_resource->lr_lvb_len) {
813                 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
814                 buffers = 3;
815         }
816         unlock_res_and_lock(lock);
817
818         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
819                               LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
820                               size, NULL);
821         if (req == NULL)
822                 RETURN(-ENOMEM);
823
824         req->rq_async_args.pointer_arg[0] = arg;
825         req->rq_async_args.pointer_arg[1] = lock;
826         req->rq_interpret_reply = ldlm_cb_interpret;
827         req->rq_no_resend = 1;
828
829         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
830         body->lock_handle[0] = lock->l_remote_handle;
831         body->lock_flags = flags;
832         ldlm_lock2desc(lock, &body->lock_desc);
833
834         if (buffers == 3) {
835                 void *lvb;
836
837                 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
838                                      lock->l_resource->lr_lvb_len);
839                 lock_res_and_lock(lock);
840                 memcpy(lvb, lock->l_resource->lr_lvb_data,
841                        lock->l_resource->lr_lvb_len);
842                 unlock_res_and_lock(lock);
843         }
844
845         LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
846                    total_enqueue_wait);
847
848         /* Server-side enqueue wait time estimate, used in
849             __ldlm_add_waiting_lock to set future enqueue timers */
850         if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
851                 at_measured(&lock->l_resource->lr_namespace->ns_at_estimate,
852                             total_enqueue_wait);
853         else
854                 /* bz18618. Don't add lock enqueue time we spend waiting for a
855                    previous callback to fail. Locks waiting legitimately will
856                    get extended by ldlm_refresh_waiting_lock regardless of the
857                    estimate, so it's okay to underestimate here. */
858                 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
859                        "It is likely that a previous callback timed out.",
860                        total_enqueue_wait,
861                        at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
862
863         ptlrpc_req_set_repsize(req, 1, NULL);
864
865         req->rq_send_state = LUSTRE_IMP_FULL;
866         /* ptlrpc_prep_req already set timeout */
867         if (AT_OFF)
868                 req->rq_timeout = ldlm_get_rq_timeout();
869
870         /* We only send real blocking ASTs after the lock is granted */
871         lock_res_and_lock(lock);
872         if (lock->l_flags & LDLM_FL_AST_SENT) {
873                 body->lock_flags |= LDLM_FL_AST_SENT;
874                 /* copy ast flags like LDLM_FL_DISCARD_DATA */
875                 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
876
877                 /* We might get here prior to ldlm_handle_enqueue setting
878                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
879                  * into waiting list, but this is safe and similar code in
880                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
881                  * that would not only cancel the lock, but will also remove
882                  * it from waiting list */
883                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
884                         unlock_res_and_lock(lock);
885                         ldlm_lock_cancel(lock);
886                         instant_cancel = 1;
887                         lock_res_and_lock(lock);
888                 } else {
889                         ldlm_add_waiting_lock(lock); /* start the lock-timeout
890                                                          clock */
891                 }
892         }
893         unlock_res_and_lock(lock);
894
895         if (lock->l_export && lock->l_export->exp_nid_stats &&
896             lock->l_export->exp_nid_stats->nid_ldlm_stats) {
897                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
898                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
899         }
900
901         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
902
903         RETURN(rc);
904 }
905
906 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
907 {
908         struct ldlm_resource *res = lock->l_resource;
909         struct ldlm_request *body;
910         struct ptlrpc_request *req;
911         __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
912                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
913         int rc = 0;
914         ENTRY;
915
916         LASSERT(lock != NULL && lock->l_export != NULL);
917
918         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
919                               LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
920                               NULL);
921         if (req == NULL)
922                 RETURN(-ENOMEM);
923
924         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
925         body->lock_handle[0] = lock->l_remote_handle;
926         ldlm_lock2desc(lock, &body->lock_desc);
927
928         lock_res_and_lock(lock);
929         size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
930         unlock_res_and_lock(lock);
931         res = lock->l_resource;
932         ptlrpc_req_set_repsize(req, 2, size);
933
934         req->rq_send_state = LUSTRE_IMP_FULL;
935         /* ptlrpc_prep_req already set timeout */
936         if (AT_OFF)
937                 req->rq_timeout = ldlm_get_rq_timeout();
938
939         if (lock->l_export && lock->l_export->exp_nid_stats &&
940             lock->l_export->exp_nid_stats->nid_ldlm_stats) {
941                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
942                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
943         }
944
945         rc = ptlrpc_queue_wait(req);
946         if (rc == -ELDLM_NO_LOCK_DATA)
947                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
948         else if (rc != 0)
949                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
950         else
951                 rc = ldlm_res_lvbo_update(res, req,
952                                           REPLY_REC_OFF, 1);
953         ptlrpc_req_finished(req);
954         if (rc == -ERESTART)
955                 ldlm_reprocess_all(res);
956
957         RETURN(rc);
958 }
959
960 static void ldlm_svc_get_eopc(struct ldlm_request *dlm_req,
961                        struct lprocfs_stats *srv_stats)
962 {
963         int lock_type = 0, op = 0;
964
965         lock_type = dlm_req->lock_desc.l_resource.lr_type;
966
967         switch (lock_type) {
968         case LDLM_PLAIN:
969                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
970                 break;
971         case LDLM_EXTENT:
972                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
973                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
974                 else
975                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
976                 break;
977         case LDLM_FLOCK:
978                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
979                 break;
980         case LDLM_IBITS:
981                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
982                 break;
983         default:
984                 op = 0;
985                 break;
986         }
987
988         if (op)
989                 lprocfs_counter_incr(srv_stats, op);
990
991         return ;
992 }
993
994 /*
995  * Main server-side entry point into LDLM. This is called by ptlrpc service
996  * threads to carry out client lock enqueueing requests.
997  */
998 int ldlm_handle_enqueue(struct ptlrpc_request *req,
999                         ldlm_completion_callback completion_callback,
1000                         ldlm_blocking_callback blocking_callback,
1001                         ldlm_glimpse_callback glimpse_callback)
1002 {
1003         struct obd_device *obddev = req->rq_export->exp_obd;
1004         struct ldlm_reply *dlm_rep;
1005         struct ldlm_request *dlm_req;
1006         __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1007                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
1008         int rc = 0;
1009         __u32 flags;
1010         ldlm_error_t err = ELDLM_OK;
1011         struct ldlm_lock *lock = NULL;
1012         void *cookie = NULL;
1013         ENTRY;
1014
1015         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1016
1017         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1018                                      lustre_swab_ldlm_request);
1019         if (dlm_req == NULL) {
1020                 CERROR ("Can't unpack dlm_req\n");
1021                 GOTO(out, rc = -EFAULT);
1022         }
1023
1024         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1025         flags = dlm_req->lock_flags;
1026
1027         LASSERT(req->rq_export);
1028
1029         if (req->rq_rqbd->rqbd_service->srv_stats)
1030                 ldlm_svc_get_eopc(dlm_req,
1031                                   req->rq_rqbd->rqbd_service->srv_stats);
1032
1033         if (req->rq_export && req->rq_export->exp_nid_stats &&
1034             req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1035                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1036                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
1037         }
1038
1039         if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1040             dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
1041                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1042                           dlm_req->lock_desc.l_resource.lr_type);
1043                 GOTO(out, rc = -EFAULT);
1044         }
1045
1046         if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1047             dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1048             dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
1049                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1050                           dlm_req->lock_desc.l_req_mode);
1051                 GOTO(out, rc = -EFAULT);
1052         }
1053
1054         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1055                 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
1056                         DEBUG_REQ(D_ERROR, req,
1057                                   "PLAIN lock request from IBITS client?");
1058                         GOTO(out, rc = -EPROTO);
1059                 }
1060         } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
1061                 DEBUG_REQ(D_ERROR, req,
1062                           "IBITS lock request from unaware client?");
1063                 GOTO(out, rc = -EPROTO);
1064         }
1065
1066 #if 0
1067         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1068            against server's _CONNECT_SUPPORTED flags? (I don't want to use
1069            ibits for mgc/mgs) */
1070
1071         /* INODEBITS_INTEROP: Perform conversion from plain lock to
1072          * inodebits lock if client does not support them. */
1073         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1074             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1075                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1076                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1077                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1078                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1079                         dlm_req->lock_desc.l_req_mode = LCK_CR;
1080         }
1081 #endif
1082
1083         if (flags & LDLM_FL_REPLAY) {
1084                 /* Find an existing lock in the per-export lock hash */
1085                 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
1086                                           (void *)&dlm_req->lock_handle[0]);
1087                 if (lock != NULL) {
1088                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1089                                   LPX64, lock->l_handle.h_cookie);
1090                         GOTO(existing_lock, rc = 0);
1091                 }
1092         }
1093
1094         /* The lock's callback data might be set in the policy function */
1095         lock = ldlm_lock_create(obddev->obd_namespace,
1096                                 dlm_req->lock_desc.l_resource.lr_name,
1097                                 dlm_req->lock_desc.l_resource.lr_type,
1098                                 dlm_req->lock_desc.l_req_mode,
1099                                 blocking_callback, completion_callback,
1100                                 glimpse_callback, NULL, 0);
1101         if (!lock)
1102                 GOTO(out, rc = -ENOMEM);
1103
1104         lock->l_last_activity = cfs_time_current_sec();
1105         lock->l_remote_handle = dlm_req->lock_handle[0];
1106         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1107
1108         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1109         /* Don't enqueue a lock onto the export if it has already
1110          * been evicted.  Cancel it now instead. (bug 3822) */
1111         if (req->rq_export->exp_failed) {
1112                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1113                 GOTO(out, rc = -ENOTCONN);
1114         }
1115         lock->l_export = class_export_get(req->rq_export);
1116
1117         if (lock->l_export->exp_lock_hash)
1118                 lustre_hash_add(lock->l_export->exp_lock_hash,
1119                                 &lock->l_remote_handle, &lock->l_exp_hash);
1120
1121 existing_lock:
1122
1123         if (flags & LDLM_FL_HAS_INTENT) {
1124                 /* In this case, the reply buffer is allocated deep in
1125                  * local_lock_enqueue by the policy function. */
1126                 cookie = req;
1127         } else {
1128                 int buffers = 2;
1129
1130                 lock_res_and_lock(lock);
1131                 if (lock->l_resource->lr_lvb_len) {
1132                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1133                         buffers = 3;
1134                 }
1135                 unlock_res_and_lock(lock);
1136
1137                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1138                         GOTO(out, rc = -ENOMEM);
1139
1140                 rc = lustre_pack_reply(req, buffers, size, NULL);
1141                 if (rc)
1142                         GOTO(out, rc);
1143         }
1144
1145         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1146                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1147         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1148                 lock->l_req_extent = lock->l_policy_data.l_extent;
1149
1150         err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, (int *)&flags);
1151         if (err)
1152                 GOTO(out, err);
1153
1154         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1155                                  sizeof(*dlm_rep));
1156         dlm_rep->lock_flags = flags;
1157
1158         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1159         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1160
1161         /* We never send a blocking AST until the lock is granted, but
1162          * we can tell it right now */
1163         lock_res_and_lock(lock);
1164
1165         /* Now take into account flags to be inherited from original lock
1166            request both in reply to client and in our own lock flags. */
1167         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1168         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1169
1170         /* Don't move a pending lock onto the export if it has already
1171          * been evicted.  Cancel it now instead. (bug 5683) */
1172         if (req->rq_export->exp_failed ||
1173             OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
1174                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1175                 rc = -ENOTCONN;
1176         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1177                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1178                 if (lock->l_granted_mode == lock->l_req_mode) {
1179                         /* Only cancel lock if it was granted, because it
1180                          * would be destroyed immediatelly and would never
1181                          * be granted in the future, causing timeouts on client.
1182                          * Not granted lock will be cancelled immediatelly after
1183                          * sending completion AST.
1184                          */
1185                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1186                                 unlock_res_and_lock(lock);
1187                                 ldlm_lock_cancel(lock);
1188                                 lock_res_and_lock(lock);
1189                         } else
1190                                 ldlm_add_waiting_lock(lock);
1191                 }
1192         }
1193         /* Make sure we never ever grant usual metadata locks to liblustre
1194            clients */
1195         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1196             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1197              req->rq_export->exp_libclient) {
1198                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1199                     !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1200                         CERROR("Granting sync lock to libclient. "
1201                                "req fl %d, rep fl %d, lock fl "LPX64"\n",
1202                                dlm_req->lock_flags, dlm_rep->lock_flags,
1203                                lock->l_flags);
1204                         LDLM_ERROR(lock, "sync lock");
1205                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1206                                 struct ldlm_intent *it;
1207                                 it = lustre_msg_buf(req->rq_reqmsg,
1208                                                     DLM_INTENT_IT_OFF,
1209                                                     sizeof(*it));
1210                                 if (it != NULL) {
1211                                         CERROR("This is intent %s ("LPU64")\n",
1212                                                ldlm_it2str(it->opc), it->opc);
1213                                 }
1214                         }
1215                 }
1216         }
1217
1218         unlock_res_and_lock(lock);
1219
1220         EXIT;
1221  out:
1222         req->rq_status = rc ?: err;  /* return either error - bug 11190 */
1223         if (!req->rq_packed_final) {
1224                 err = lustre_pack_reply(req, 1, NULL, NULL);
1225                 if (rc == 0)
1226                         rc = err;
1227         }
1228
1229         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1230          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1231         if (lock) {
1232                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1233                            "(err=%d, rc=%d)", err, rc);
1234
1235                 if (rc == 0 && obddev->obd_fail)
1236                         rc = -ENOTCONN;
1237
1238                 if (rc == 0) {
1239                         lock_res_and_lock(lock);
1240                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1241                         if (size[DLM_REPLY_REC_OFF] > 0) {
1242                                 void *lvb = lustre_msg_buf(req->rq_repmsg,
1243                                                        DLM_REPLY_REC_OFF,
1244                                                        size[DLM_REPLY_REC_OFF]);
1245                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1246                                          req, lock);
1247
1248                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1249                                        size[DLM_REPLY_REC_OFF]);
1250                         }
1251                         unlock_res_and_lock(lock);
1252                 } else {
1253                         lock_res_and_lock(lock);
1254                         ldlm_resource_unlink_lock(lock);
1255                         ldlm_lock_destroy_nolock(lock);
1256                         unlock_res_and_lock(lock);
1257                 }
1258
1259                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1260                         ldlm_reprocess_all(lock->l_resource);
1261
1262                 LDLM_LOCK_PUT(lock);
1263         }
1264
1265         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1266                           lock, rc);
1267
1268         return rc;
1269 }
1270
1271 int ldlm_handle_convert(struct ptlrpc_request *req)
1272 {
1273         struct ldlm_request *dlm_req;
1274         struct ldlm_reply *dlm_rep;
1275         struct ldlm_lock *lock;
1276         int rc;
1277         __u32 size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1278                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
1279         ENTRY;
1280
1281         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1282                                      lustre_swab_ldlm_request);
1283         if (dlm_req == NULL) {
1284                 CERROR ("Can't unpack dlm_req\n");
1285                 RETURN (-EFAULT);
1286         }
1287
1288         if (req->rq_export && req->rq_export->exp_nid_stats &&
1289             req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1290                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1291                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1292         }
1293
1294         rc = lustre_pack_reply(req, 2, size, NULL);
1295         if (rc)
1296                 RETURN(rc);
1297
1298         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1299                                  sizeof(*dlm_rep));
1300         dlm_rep->lock_flags = dlm_req->lock_flags;
1301
1302         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1303         if (!lock) {
1304                 req->rq_status = EINVAL;
1305         } else {
1306                 void *res = NULL;
1307
1308                 LDLM_DEBUG(lock, "server-side convert handler START");
1309
1310                 lock->l_last_activity = cfs_time_current_sec();
1311                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1312                                         &dlm_rep->lock_flags);
1313                 if (res) {
1314                         if (ldlm_del_waiting_lock(lock))
1315                                 LDLM_DEBUG(lock, "converted waiting lock");
1316                         req->rq_status = 0;
1317                 } else {
1318                         req->rq_status = EDEADLOCK;
1319                 }
1320         }
1321
1322         if (lock) {
1323                 if (!req->rq_status)
1324                         ldlm_reprocess_all(lock->l_resource);
1325                 LDLM_DEBUG(lock, "server-side convert handler END");
1326                 LDLM_LOCK_PUT(lock);
1327         } else
1328                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1329
1330         RETURN(0);
1331 }
1332
1333 /* Cancel all the locks whos handles are packed into ldlm_request */
1334 int ldlm_request_cancel(struct ptlrpc_request *req,
1335                         struct ldlm_request *dlm_req, int first)
1336 {
1337         struct ldlm_resource *res, *pres = NULL;
1338         struct ldlm_lock *lock;
1339         int i, count, done = 0;
1340         ENTRY;
1341
1342         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1343         if (first >= count)
1344                 RETURN(0);
1345
1346         /* There is no lock on the server at the replay time,
1347          * skip lock cancelling to make replay tests to pass. */
1348         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1349                 RETURN(0);
1350
1351         for (i = first; i < count; i++) {
1352                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1353                 if (!lock) {
1354                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1355                                           "lock (cookie "LPU64")",
1356                                           dlm_req->lock_handle[i].cookie);
1357                         continue;
1358                 }
1359
1360                 done++;
1361                 res = lock->l_resource;
1362                 if (res != pres) {
1363                         if (pres != NULL) {
1364                                 ldlm_reprocess_all(pres);
1365                                 ldlm_resource_putref(pres);
1366                         }
1367                         if (res != NULL) {
1368                                 ldlm_resource_getref(res);
1369                                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1370                         }
1371                         pres = res;
1372                 }
1373                 ldlm_lock_cancel(lock);
1374                 LDLM_LOCK_PUT(lock);
1375         }
1376         if (pres != NULL) {
1377                 ldlm_reprocess_all(pres);
1378                 ldlm_resource_putref(pres);
1379         }
1380         RETURN(done);
1381 }
1382
1383 int ldlm_handle_cancel(struct ptlrpc_request *req)
1384 {
1385         struct ldlm_request *dlm_req;
1386         int rc;
1387         ENTRY;
1388
1389         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1390                                      lustre_swab_ldlm_request);
1391         if (dlm_req == NULL) {
1392                 CERROR("bad request buffer for cancel\n");
1393                 RETURN(-EFAULT);
1394         }
1395
1396         if (req->rq_export && req->rq_export->exp_nid_stats &&
1397             req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1398                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1399                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1400         }
1401
1402         rc = lustre_pack_reply(req, 1, NULL, NULL);
1403         if (rc)
1404                 RETURN(rc);
1405
1406         if (!ldlm_request_cancel(req, dlm_req, 0))
1407                 req->rq_status = ESTALE;
1408
1409         if (ptlrpc_reply(req) != 0)
1410                 LBUG();
1411
1412         RETURN(0);
1413 }
1414
1415 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1416                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1417 {
1418         int do_ast;
1419         ENTRY;
1420
1421         LDLM_DEBUG(lock, "client blocking AST callback handler");
1422
1423         lock_res_and_lock(lock);
1424         lock->l_flags |= LDLM_FL_CBPENDING;
1425
1426         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1427                 lock->l_flags |= LDLM_FL_CANCEL;
1428
1429         do_ast = (!lock->l_readers && !lock->l_writers);
1430         unlock_res_and_lock(lock);
1431
1432         if (do_ast) {
1433                 CDEBUG(D_DLMTRACE, "Lock %p is already unused, calling callback (%p)\n",
1434                        lock, lock->l_blocking_ast);
1435                 if (lock->l_blocking_ast != NULL)
1436                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1437                                              LDLM_CB_BLOCKING);
1438         } else {
1439                 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1440                        lock);
1441         }
1442
1443         LDLM_LOCK_PUT(lock);
1444         EXIT;
1445 }
1446
1447 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1448                                     struct ldlm_namespace *ns,
1449                                     struct ldlm_request *dlm_req,
1450                                     struct ldlm_lock *lock)
1451 {
1452         CFS_LIST_HEAD(ast_list);
1453         ENTRY;
1454
1455         LDLM_DEBUG(lock, "client completion callback handler START");
1456
1457         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1458                 int to = cfs_time_seconds(1);
1459                 while (to > 0) {
1460                         to = schedule_timeout(to);
1461                         if (lock->l_granted_mode == lock->l_req_mode ||
1462                             lock->l_destroyed)
1463                                 break;
1464                 }
1465         }
1466
1467         lock_res_and_lock(lock);
1468         if (lock->l_destroyed ||
1469             lock->l_granted_mode == lock->l_req_mode) {
1470                 /* bug 11300: the lock has already been granted */
1471                 unlock_res_and_lock(lock);
1472                 LDLM_DEBUG(lock, "Double grant race happened");
1473                 LDLM_LOCK_PUT(lock);
1474                 EXIT;
1475                 return;
1476         }
1477
1478         /* If we receive the completion AST before the actual enqueue returned,
1479          * then we might need to switch lock modes, resources, or extents. */
1480         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1481                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1482                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1483         }
1484
1485         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1486                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1487                 LDLM_DEBUG(lock, "completion AST, new policy data");
1488         }
1489
1490         ldlm_resource_unlink_lock(lock);
1491         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1492                    &lock->l_resource->lr_name,
1493                    sizeof(lock->l_resource->lr_name)) != 0) {
1494                 unlock_res_and_lock(lock);
1495                 if (ldlm_lock_change_resource(ns, lock,
1496                                 dlm_req->lock_desc.l_resource.lr_name)) {
1497                         LDLM_ERROR(lock, "Failed to allocate resource");
1498                         LDLM_LOCK_PUT(lock);
1499                         EXIT;
1500                         return;
1501                 }
1502                 LDLM_DEBUG(lock, "completion AST, new resource");
1503                 CERROR("change resource!\n");
1504                 lock_res_and_lock(lock);
1505         }
1506
1507         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1508                 /* BL_AST locks are not needed in lru.
1509                  * let ldlm_cancel_lru() be fast. */
1510                 ldlm_lock_remove_from_lru(lock);
1511                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1512                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1513         }
1514
1515         if (lock->l_lvb_len) {
1516                 void *lvb;
1517                 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1518                                          lock->l_lvb_swabber);
1519                 if (lvb == NULL) {
1520                         LDLM_ERROR(lock, "completion AST did not contain "
1521                                    "expected LVB!");
1522                 } else {
1523                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1524                 }
1525         }
1526
1527         ldlm_grant_lock(lock, &ast_list);
1528         unlock_res_and_lock(lock);
1529
1530         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1531
1532         ldlm_run_cp_ast_work(&ast_list);
1533
1534         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1535                           lock);
1536         LDLM_LOCK_PUT(lock);
1537         EXIT;
1538 }
1539
1540 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1541                                     struct ldlm_namespace *ns,
1542                                     struct ldlm_request *dlm_req,
1543                                     struct ldlm_lock *lock)
1544 {
1545         int rc = -ENOSYS;
1546         ENTRY;
1547
1548         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1549
1550         if (lock->l_glimpse_ast != NULL)
1551                 rc = lock->l_glimpse_ast(lock, req);
1552
1553         if (req->rq_repmsg != NULL) {
1554                 ptlrpc_reply(req);
1555         } else {
1556                 req->rq_status = rc;
1557                 ptlrpc_error(req);
1558         }
1559
1560         lock_res_and_lock(lock);
1561         if (lock->l_granted_mode == LCK_PW &&
1562             !lock->l_readers && !lock->l_writers &&
1563             cfs_time_after(cfs_time_current(),
1564                            cfs_time_add(lock->l_last_used,
1565                                         cfs_time_seconds(10)))) {
1566                 unlock_res_and_lock(lock);
1567                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1568                         ldlm_handle_bl_callback(ns, NULL, lock);
1569
1570                 EXIT;
1571                 return;
1572         }
1573         unlock_res_and_lock(lock);
1574         LDLM_LOCK_PUT(lock);
1575         EXIT;
1576 }
1577
1578 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1579 {
1580         req->rq_status = rc;
1581         if (!req->rq_packed_final) {
1582                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1583                 if (rc)
1584                         return rc;
1585         }
1586         return ptlrpc_reply(req);
1587 }
1588
1589 #ifdef __KERNEL__
1590 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1591 {
1592         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1593         ENTRY;
1594
1595         spin_lock(&blp->blp_lock);
1596         if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1597                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1598                 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1599         } else {
1600                 /* other blocking callbacks are added to the regular list */
1601                 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1602         }
1603         spin_unlock(&blp->blp_lock);
1604
1605         cfs_waitq_signal(&blp->blp_waitq);
1606
1607         /* can not use blwi->blwi_mode as blwi could be already freed in
1608            LDLM_ASYNC mode */
1609         if (mode == LDLM_SYNC)
1610                 wait_for_completion(&blwi->blwi_comp);
1611
1612         RETURN(0);
1613 }
1614
1615 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1616                              struct ldlm_namespace *ns,
1617                              struct ldlm_lock_desc *ld,
1618                              struct list_head *cancels, int count,
1619                              struct ldlm_lock *lock,
1620                              int mode)
1621 {
1622         init_completion(&blwi->blwi_comp);
1623         INIT_LIST_HEAD(&blwi->blwi_head);
1624
1625         if (libcfs_memory_pressure_get())
1626                 blwi->blwi_mem_pressure = 1;
1627
1628         blwi->blwi_ns = ns;
1629         blwi->blwi_mode = mode;
1630         if (ld != NULL)
1631                 blwi->blwi_ld = *ld;
1632         if (count) {
1633                 list_add(&blwi->blwi_head, cancels);
1634                 list_del_init(cancels);
1635                 blwi->blwi_count = count;
1636         } else {
1637                 blwi->blwi_lock = lock;
1638         }
1639 }
1640
1641 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1642                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1643                              struct list_head *cancels, int count, int mode)
1644 {
1645         ENTRY;
1646
1647         if (cancels && count == 0)
1648                 RETURN(0);
1649
1650         if (mode == LDLM_SYNC) {
1651                 /* if it is synchronous call do minimum mem alloc, as it could
1652                  * be triggered from kernel shrinker
1653                  */
1654                 struct ldlm_bl_work_item blwi;
1655                 memset(&blwi, 0, sizeof(blwi));
1656                 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1657                 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1658         } else {
1659                 struct ldlm_bl_work_item *blwi;
1660                 OBD_ALLOC(blwi, sizeof(*blwi));
1661                 if (blwi == NULL)
1662                         RETURN(-ENOMEM);
1663                 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1664
1665                 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1666         }
1667 }
1668 #endif
1669
1670 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1671                            struct ldlm_lock *lock)
1672 {
1673 #ifdef __KERNEL__
1674         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1675 #else
1676         RETURN(-ENOSYS);
1677 #endif
1678 }
1679
1680 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1681                            struct list_head *cancels, int count, int mode)
1682 {
1683 #ifdef __KERNEL__
1684         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1685 #else
1686         RETURN(-ENOSYS);
1687 #endif
1688 }
1689
1690 static int ldlm_callback_handler(struct ptlrpc_request *req)
1691 {
1692         struct ldlm_namespace *ns;
1693         struct ldlm_request *dlm_req;
1694         struct ldlm_lock *lock;
1695         int rc;
1696         ENTRY;
1697
1698         /* Requests arrive in sender's byte order.  The ptlrpc service
1699          * handler has already checked and, if necessary, byte-swapped the
1700          * incoming request message body, but I am responsible for the
1701          * message buffers. */
1702
1703         if (req->rq_export == NULL) {
1704                 ldlm_callback_reply(req, -ENOTCONN);
1705                 RETURN(0);
1706         }
1707
1708         LASSERT(req->rq_export != NULL);
1709         LASSERT(req->rq_export->exp_obd != NULL);
1710
1711         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1712         case LDLM_BL_CALLBACK:
1713                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1714                 break;
1715         case LDLM_CP_CALLBACK:
1716                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1717                 break;
1718         case LDLM_GL_CALLBACK:
1719                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1720                 break;
1721         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1722                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1723                 rc = llog_origin_handle_cancel(req);
1724                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1725                 ldlm_callback_reply(req, rc);
1726                 RETURN(0);
1727         case OBD_QC_CALLBACK:
1728                 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1729                 rc = target_handle_qc_callback(req);
1730                 ldlm_callback_reply(req, rc);
1731                 RETURN(0);
1732         case QUOTA_DQACQ:
1733         case QUOTA_DQREL:
1734                 /* reply in handler */
1735                 rc = target_handle_dqacq_callback(req);
1736                 RETURN(0);
1737         case LLOG_ORIGIN_HANDLE_CREATE:
1738                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1739                 rc = llog_origin_handle_create(req);
1740                 ldlm_callback_reply(req, rc);
1741                 RETURN(0);
1742         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1743                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1744                 rc = llog_origin_handle_next_block(req);
1745                 ldlm_callback_reply(req, rc);
1746                 RETURN(0);
1747         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1748                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1749                 rc = llog_origin_handle_read_header(req);
1750                 ldlm_callback_reply(req, rc);
1751                 RETURN(0);
1752         case LLOG_ORIGIN_HANDLE_CLOSE:
1753                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1754                 rc = llog_origin_handle_close(req);
1755                 ldlm_callback_reply(req, rc);
1756                 RETURN(0);
1757         default:
1758                 CERROR("unknown opcode %u\n",
1759                        lustre_msg_get_opc(req->rq_reqmsg));
1760                 ldlm_callback_reply(req, -EPROTO);
1761                 RETURN(0);
1762         }
1763
1764         ns = req->rq_export->exp_obd->obd_namespace;
1765         LASSERT(ns != NULL);
1766
1767         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1768                                      lustre_swab_ldlm_request);
1769         if (dlm_req == NULL) {
1770                 CERROR ("can't unpack dlm_req\n");
1771                 ldlm_callback_reply(req, -EPROTO);
1772                 RETURN (0);
1773         }
1774
1775         /* Force a known safe race, send a cancel to the server for a lock
1776          * which the server has already started a blocking callback on. */
1777         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1778             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1779                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1780                 if (rc < 0)
1781                         CERROR("ldlm_cli_cancel: %d\n", rc);
1782         }
1783
1784         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1785         if (!lock) {
1786                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1787                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1788                 ldlm_callback_reply(req, -EINVAL);
1789                 RETURN(0);
1790         }
1791
1792         if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1793             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1794                 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1795
1796         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1797         lock_res_and_lock(lock);
1798         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1799         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1800                 /* If somebody cancels lock and cache is already droped,
1801                  * or lock is failed before cp_ast received on client,
1802                  * we can tell the server we have no lock. Otherwise, we
1803                  * should send cancel after dropping the cache. */
1804                 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1805                     (lock->l_flags & LDLM_FL_BL_DONE)) ||
1806                     (lock->l_flags & LDLM_FL_FAILED)) {
1807                         LDLM_DEBUG(lock, "callback on lock "
1808                                    LPX64" - lock disappeared\n",
1809                                    dlm_req->lock_handle[0].cookie);
1810                         unlock_res_and_lock(lock);
1811                         LDLM_LOCK_PUT(lock);
1812                         ldlm_callback_reply(req, -EINVAL);
1813                         RETURN(0);
1814                 }
1815                 /* BL_AST locks are not needed in lru.
1816                  * let ldlm_cancel_lru() be fast. */
1817                 ldlm_lock_remove_from_lru(lock);
1818                 lock->l_flags |= LDLM_FL_BL_AST;
1819         }
1820         unlock_res_and_lock(lock);
1821
1822         /* We want the ost thread to get this reply so that it can respond
1823          * to ost requests (write cache writeback) that might be triggered
1824          * in the callback.
1825          *
1826          * But we'd also like to be able to indicate in the reply that we're
1827          * cancelling right now, because it's unused, or have an intent result
1828          * in the reply, so we might have to push the responsibility for sending
1829          * the reply down into the AST handlers, alas. */
1830
1831         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1832         case LDLM_BL_CALLBACK:
1833                 CDEBUG(D_INODE, "blocking ast\n");
1834                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1835                         ldlm_callback_reply(req, 0);
1836                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1837                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1838                 break;
1839         case LDLM_CP_CALLBACK:
1840                 CDEBUG(D_INODE, "completion ast\n");
1841                 ldlm_callback_reply(req, 0);
1842                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1843                 break;
1844         case LDLM_GL_CALLBACK:
1845                 CDEBUG(D_INODE, "glimpse ast\n");
1846                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1847                 break;
1848         default:
1849                 LBUG();                         /* checked above */
1850         }
1851
1852         RETURN(0);
1853 }
1854
1855 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1856 {
1857         int rc;
1858         ENTRY;
1859
1860         /* Requests arrive in sender's byte order.  The ptlrpc service
1861          * handler has already checked and, if necessary, byte-swapped the
1862          * incoming request message body, but I am responsible for the
1863          * message buffers. */
1864
1865         if (req->rq_export == NULL) {
1866                 struct ldlm_request *dlm_req;
1867
1868                 CERROR("%s from %s arrived at %lu with bad export cookie "
1869                        LPU64"\n",
1870                        ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
1871                        libcfs_nid2str(req->rq_peer.nid),
1872                        req->rq_arrival_time.tv_sec,
1873                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1874
1875                 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
1876                         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1877                                                      sizeof(*dlm_req),
1878                                                      lustre_swab_ldlm_request);
1879                         if (dlm_req != NULL)
1880                                 ldlm_lock_dump_handle(D_ERROR,
1881                                                       &dlm_req->lock_handle[0]);
1882                 }
1883
1884                 ldlm_callback_reply(req, -ENOTCONN);
1885                 RETURN(0);
1886         }
1887
1888         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1889
1890         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1891         case LDLM_CANCEL:
1892                 CDEBUG(D_INODE, "cancel\n");
1893                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1894                 rc = ldlm_handle_cancel(req);
1895                 if (rc)
1896                         break;
1897                 RETURN(0);
1898         case OBD_LOG_CANCEL:
1899                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1900                 rc = llog_origin_handle_cancel(req);
1901                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1902                 ldlm_callback_reply(req, rc);
1903                 RETURN(0);
1904         default:
1905                 CERROR("invalid opcode %d\n",
1906                        lustre_msg_get_opc(req->rq_reqmsg));
1907                 ldlm_callback_reply(req, -EINVAL);
1908         }
1909
1910         RETURN(0);
1911 }
1912
1913 #ifdef __KERNEL__
1914 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1915 {
1916         struct ldlm_bl_work_item *blwi = NULL;
1917         static unsigned int num_bl = 0;
1918
1919         spin_lock(&blp->blp_lock);
1920         /* process a request from the blp_list at least every blp_num_threads */
1921         if (!list_empty(&blp->blp_list) &&
1922             (list_empty(&blp->blp_prio_list) || num_bl == 0))
1923                 blwi = list_entry(blp->blp_list.next,
1924                                   struct ldlm_bl_work_item, blwi_entry);
1925         else
1926                 if (!list_empty(&blp->blp_prio_list))
1927                         blwi = list_entry(blp->blp_prio_list.next,
1928                                           struct ldlm_bl_work_item, blwi_entry);
1929
1930         if (blwi) {
1931                 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1932                         num_bl = 0;
1933                 list_del(&blwi->blwi_entry);
1934         }
1935         spin_unlock(&blp->blp_lock);
1936
1937         return blwi;
1938 }
1939
1940 /* This only contains temporary data until the thread starts */
1941 struct ldlm_bl_thread_data {
1942         char                    bltd_name[CFS_CURPROC_COMM_MAX];
1943         struct ldlm_bl_pool     *bltd_blp;
1944         struct completion       bltd_comp;
1945         int                     bltd_num;
1946 };
1947
1948 static int ldlm_bl_thread_main(void *arg);
1949
1950 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1951 {
1952         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1953         int rc;
1954
1955         init_completion(&bltd.bltd_comp);
1956         rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1957         if (rc < 0) {
1958                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1959                        atomic_read(&blp->blp_num_threads), rc);
1960                 return rc;
1961         }
1962         wait_for_completion(&bltd.bltd_comp);
1963
1964         return 0;
1965 }
1966
1967 static int ldlm_bl_thread_main(void *arg)
1968 {
1969         struct ldlm_bl_pool *blp;
1970         ENTRY;
1971
1972         {
1973                 struct ldlm_bl_thread_data *bltd = arg;
1974
1975                 blp = bltd->bltd_blp;
1976
1977                 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1978                 atomic_inc(&blp->blp_busy_threads);
1979
1980                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1981                         "ldlm_bl_%02d", bltd->bltd_num);
1982                 cfs_daemonize(bltd->bltd_name);
1983
1984                 complete(&bltd->bltd_comp);
1985                 /* cannot use bltd after this, it is only on caller's stack */
1986         }
1987
1988         while (1) {
1989                 struct l_wait_info lwi = { 0 };
1990                 struct ldlm_bl_work_item *blwi = NULL;
1991
1992                 blwi = ldlm_bl_get_work(blp);
1993
1994                 if (blwi == NULL) {
1995                         int busy;
1996
1997                         atomic_dec(&blp->blp_busy_threads);
1998                         l_wait_event_exclusive(blp->blp_waitq,
1999                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
2000                                          &lwi);
2001                         busy = atomic_inc_return(&blp->blp_busy_threads);
2002
2003                         if (blwi->blwi_ns == NULL)
2004                                 /* added by ldlm_cleanup() */
2005                                 break;
2006
2007                         /* Not fatal if racy and have a few too many threads */
2008                         if (unlikely(busy < blp->blp_max_threads &&
2009                                     busy >= atomic_read(&blp->blp_num_threads)))
2010                                 /* discard the return value, we tried */
2011                                 ldlm_bl_thread_start(blp);
2012                 } else {
2013                         if (blwi->blwi_ns == NULL)
2014                                 /* added by ldlm_cleanup() */
2015                                 break;
2016                 }
2017                 if (blwi->blwi_mem_pressure)
2018                         libcfs_memory_pressure_set();
2019
2020                 if (blwi->blwi_count) {
2021                         /* The special case when we cancel locks in lru
2022                          * asynchronously, we pass the list of locks here.
2023                          * Thus lock is marked LDLM_FL_CANCELING, and already
2024                          * canceled locally. */
2025                         ldlm_cli_cancel_list(&blwi->blwi_head,
2026                                              blwi->blwi_count, NULL, 0);
2027                 } else {
2028                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2029                                                 blwi->blwi_lock);
2030                 }
2031                 if (blwi->blwi_mem_pressure)
2032                         libcfs_memory_pressure_clr();
2033
2034                 if (blwi->blwi_mode == LDLM_ASYNC)
2035                         OBD_FREE(blwi, sizeof(*blwi));
2036                 else
2037                         complete(&blwi->blwi_comp);
2038         }
2039
2040         atomic_dec(&blp->blp_busy_threads);
2041         atomic_dec(&blp->blp_num_threads);
2042         complete(&blp->blp_comp);
2043         RETURN(0);
2044 }
2045
2046 #endif
2047
2048 /*
2049  * Export handle<->lock hash operations.
2050  */
2051 static unsigned
2052 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2053 {
2054         return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2055 }
2056
2057 static void *
2058 ldlm_export_lock_key(struct hlist_node *hnode)
2059 {
2060         struct ldlm_lock *lock;
2061         ENTRY;
2062
2063         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2064         RETURN(&lock->l_remote_handle);
2065 }
2066
2067 static int
2068 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2069 {
2070         ENTRY;
2071         RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2072 }
2073
2074 static void *
2075 ldlm_export_lock_get(struct hlist_node *hnode)
2076 {
2077         struct ldlm_lock *lock;
2078         ENTRY;
2079
2080         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2081         LDLM_LOCK_GET(lock);
2082
2083         RETURN(lock);
2084 }
2085
2086 static void *
2087 ldlm_export_lock_put(struct hlist_node *hnode)
2088 {
2089         struct ldlm_lock *lock;
2090         ENTRY;
2091
2092         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2093         LDLM_LOCK_PUT(lock);
2094
2095         RETURN(lock);
2096 }
2097
2098 static lustre_hash_ops_t ldlm_export_lock_ops = {
2099         .lh_hash    = ldlm_export_lock_hash,
2100         .lh_key     = ldlm_export_lock_key,
2101         .lh_compare = ldlm_export_lock_compare,
2102         .lh_get     = ldlm_export_lock_get,
2103         .lh_put     = ldlm_export_lock_put
2104 };
2105
2106 int ldlm_init_export(struct obd_export *exp)
2107 {
2108         ENTRY;
2109
2110         exp->exp_lock_hash =
2111                 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2112                                  7, 16, &ldlm_export_lock_ops, LH_REHASH);
2113
2114         if (!exp->exp_lock_hash)
2115                 RETURN(-ENOMEM);
2116
2117         RETURN(0);
2118 }
2119 EXPORT_SYMBOL(ldlm_init_export);
2120
2121 void ldlm_destroy_export(struct obd_export *exp)
2122 {
2123         ENTRY;
2124         lustre_hash_exit(exp->exp_lock_hash);
2125         exp->exp_lock_hash = NULL;
2126         EXIT;
2127 }
2128 EXPORT_SYMBOL(ldlm_destroy_export);
2129
2130 static int ldlm_setup(void);
2131 static int ldlm_cleanup(void);
2132
2133 int ldlm_get_ref(void)
2134 {
2135         int rc = 0;
2136         ENTRY;
2137         mutex_down(&ldlm_ref_sem);
2138         if (++ldlm_refcount == 1) {
2139                 rc = ldlm_setup();
2140                 if (rc)
2141                         ldlm_refcount--;
2142         }
2143         mutex_up(&ldlm_ref_sem);
2144
2145         RETURN(rc);
2146 }
2147
2148 void ldlm_put_ref(void)
2149 {
2150         ENTRY;
2151         mutex_down(&ldlm_ref_sem);
2152         if (ldlm_refcount == 1) {
2153                 int rc = ldlm_cleanup();
2154                 if (rc)
2155                         CERROR("ldlm_cleanup failed: %d\n", rc);
2156                 else
2157                         ldlm_refcount--;
2158         } else {
2159                 ldlm_refcount--;
2160         }
2161         mutex_up(&ldlm_ref_sem);
2162
2163         EXIT;
2164 }
2165
2166 static int ldlm_setup(void)
2167 {
2168         struct ldlm_bl_pool *blp;
2169         int rc = 0;
2170         int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2171         int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2172 #ifdef __KERNEL__
2173         int i;
2174 #endif
2175         ENTRY;
2176
2177         if (ldlm_state != NULL)
2178                 RETURN(-EALREADY);
2179
2180         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2181         if (ldlm_state == NULL)
2182                 RETURN(-ENOMEM);
2183
2184 #ifdef LPROCFS
2185         rc = ldlm_proc_setup();
2186         if (rc != 0)
2187                 GOTO(out_free, rc);
2188 #endif
2189
2190 #ifdef __KERNEL__
2191         if (ldlm_num_threads) {
2192                 /* If ldlm_num_threads is set, it is the min and the max. */
2193                 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2194                         ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2195                 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2196                         ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2197                 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2198         }
2199 #endif
2200
2201         ldlm_state->ldlm_cb_service =
2202                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2203                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2204                                 LDLM_CB_REPLY_PORTAL, 2,
2205                                 ldlm_callback_handler, "ldlm_cbd",
2206                                 ldlm_svc_proc_dir, NULL,
2207                                 ldlm_min_threads, ldlm_max_threads,
2208                                 "ldlm_cb", NULL);
2209
2210         if (!ldlm_state->ldlm_cb_service) {
2211                 CERROR("failed to start service\n");
2212                 GOTO(out_proc, rc = -ENOMEM);
2213         }
2214
2215         ldlm_state->ldlm_cancel_service =
2216                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2217                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2218                                 LDLM_CANCEL_REPLY_PORTAL, 6,
2219                                 ldlm_cancel_handler, "ldlm_canceld",
2220                                 ldlm_svc_proc_dir, NULL,
2221                                 ldlm_min_threads, ldlm_max_threads,
2222                                 "ldlm_cn", NULL);
2223
2224         if (!ldlm_state->ldlm_cancel_service) {
2225                 CERROR("failed to start service\n");
2226                 GOTO(out_proc, rc = -ENOMEM);
2227         }
2228
2229         OBD_ALLOC(blp, sizeof(*blp));
2230         if (blp == NULL)
2231                 GOTO(out_proc, rc = -ENOMEM);
2232         ldlm_state->ldlm_bl_pool = blp;
2233
2234         spin_lock_init(&blp->blp_lock);
2235         CFS_INIT_LIST_HEAD(&blp->blp_list);
2236         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2237         cfs_waitq_init(&blp->blp_waitq);
2238         atomic_set(&blp->blp_num_threads, 0);
2239         atomic_set(&blp->blp_busy_threads, 0);
2240         blp->blp_min_threads = ldlm_min_threads;
2241         blp->blp_max_threads = ldlm_max_threads;
2242
2243 #ifdef __KERNEL__
2244         for (i = 0; i < blp->blp_min_threads; i++) {
2245                 rc = ldlm_bl_thread_start(blp);
2246                 if (rc < 0)
2247                         GOTO(out_thread, rc);
2248         }
2249
2250         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2251         if (rc)
2252                 GOTO(out_thread, rc);
2253
2254         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2255         if (rc)
2256                 GOTO(out_thread, rc);
2257
2258         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2259         expired_lock_thread.elt_state = ELT_STOPPED;
2260         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2261
2262         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2263         spin_lock_init(&waiting_locks_spinlock);
2264         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2265
2266         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2267         if (rc < 0) {
2268                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2269                 GOTO(out_thread, rc);
2270         }
2271
2272         wait_event(expired_lock_thread.elt_waitq,
2273                    expired_lock_thread.elt_state == ELT_READY);
2274 #endif
2275
2276 #ifdef __KERNEL__
2277         rc = ldlm_pools_init();
2278         if (rc)
2279                 GOTO(out_thread, rc);
2280 #endif
2281
2282         RETURN(0);
2283
2284 #ifdef __KERNEL__
2285  out_thread:
2286         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2287         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2288 #endif
2289
2290  out_proc:
2291 #ifdef LPROCFS
2292         ldlm_proc_cleanup();
2293  out_free:
2294 #endif
2295         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2296         ldlm_state = NULL;
2297         return rc;
2298 }
2299
2300 static int ldlm_cleanup(void)
2301 {
2302 #ifdef __KERNEL__
2303         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2304 #endif
2305         ENTRY;
2306
2307         if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2308             !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2309                 CERROR("ldlm still has namespaces; clean these up first.\n");
2310                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2311                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2312                 RETURN(-EBUSY);
2313         }
2314
2315 #ifdef __KERNEL__
2316         ldlm_pools_fini();
2317 #endif
2318
2319 #ifdef __KERNEL__
2320         while (atomic_read(&blp->blp_num_threads) > 0) {
2321                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2322
2323                 init_completion(&blp->blp_comp);
2324
2325                 spin_lock(&blp->blp_lock);
2326                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2327                 cfs_waitq_signal(&blp->blp_waitq);
2328                 spin_unlock(&blp->blp_lock);
2329
2330                 wait_for_completion(&blp->blp_comp);
2331         }
2332         OBD_FREE(blp, sizeof(*blp));
2333
2334         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2335         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2336         ldlm_proc_cleanup();
2337
2338         expired_lock_thread.elt_state = ELT_TERMINATE;
2339         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2340         wait_event(expired_lock_thread.elt_waitq,
2341                    expired_lock_thread.elt_state == ELT_STOPPED);
2342 #else
2343         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2344         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2345 #endif
2346
2347         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2348         ldlm_state = NULL;
2349
2350         RETURN(0);
2351 }
2352
2353 int __init ldlm_init(void)
2354 {
2355         init_mutex(&ldlm_ref_sem);
2356         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2357         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2358         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2359                                                sizeof(struct ldlm_resource), 0,
2360                                                SLAB_HWCACHE_ALIGN);
2361         if (ldlm_resource_slab == NULL)
2362                 return -ENOMEM;
2363
2364         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2365                                       sizeof(struct ldlm_lock), 0,
2366                                       SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
2367         if (ldlm_lock_slab == NULL) {
2368                 cfs_mem_cache_destroy(ldlm_resource_slab);
2369                 return -ENOMEM;
2370         }
2371
2372         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2373                                         sizeof(struct ldlm_interval),
2374                                         0, SLAB_HWCACHE_ALIGN);
2375         if (ldlm_interval_slab == NULL) {
2376                 cfs_mem_cache_destroy(ldlm_resource_slab);
2377                 cfs_mem_cache_destroy(ldlm_lock_slab);
2378                 return -ENOMEM;
2379         }
2380
2381         return 0;
2382 }
2383
2384 void __exit ldlm_exit(void)
2385 {
2386         int rc;
2387         if (ldlm_refcount)
2388                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2389         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2390         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2391 #ifdef __KERNEL__
2392         /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2393          * synchronize_rcu() to wait a grace period elapsed, so that
2394          * ldlm_lock_free() get a chance to be called. */
2395         synchronize_rcu();
2396 #endif
2397         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2398         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2399         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2400         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2401 }
2402
2403 /* ldlm_extent.c */
2404 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2405
2406 /* ldlm_lock.c */
2407 EXPORT_SYMBOL(ldlm_get_processing_policy);
2408 EXPORT_SYMBOL(ldlm_lock2desc);
2409 EXPORT_SYMBOL(ldlm_register_intent);
2410 EXPORT_SYMBOL(ldlm_lockname);
2411 EXPORT_SYMBOL(ldlm_typename);
2412 EXPORT_SYMBOL(ldlm_lock2handle);
2413 EXPORT_SYMBOL(__ldlm_handle2lock);
2414 EXPORT_SYMBOL(ldlm_lock_get);
2415 EXPORT_SYMBOL(ldlm_lock_put);
2416 EXPORT_SYMBOL(ldlm_lock_fast_match);
2417 EXPORT_SYMBOL(ldlm_lock_match);
2418 EXPORT_SYMBOL(ldlm_lock_cancel);
2419 EXPORT_SYMBOL(ldlm_lock_addref);
2420 EXPORT_SYMBOL(ldlm_lock_decref);
2421 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2422 EXPORT_SYMBOL(ldlm_lock_change_resource);
2423 EXPORT_SYMBOL(ldlm_lock_set_data);
2424 EXPORT_SYMBOL(ldlm_it2str);
2425 EXPORT_SYMBOL(ldlm_lock_dump);
2426 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2427 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2428 EXPORT_SYMBOL(ldlm_lock_allow_match);
2429
2430 /* ldlm_request.c */
2431 EXPORT_SYMBOL(ldlm_completion_ast);
2432 EXPORT_SYMBOL(ldlm_blocking_ast);
2433 EXPORT_SYMBOL(ldlm_glimpse_ast);
2434 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2435 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2436 EXPORT_SYMBOL(ldlm_prep_elc_req);
2437 EXPORT_SYMBOL(ldlm_cli_convert);
2438 EXPORT_SYMBOL(ldlm_cli_enqueue);
2439 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2440 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2441 EXPORT_SYMBOL(ldlm_cli_cancel);
2442 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2443 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2444 EXPORT_SYMBOL(ldlm_cli_join_lru);
2445 EXPORT_SYMBOL(ldlm_replay_locks);
2446 EXPORT_SYMBOL(ldlm_resource_foreach);
2447 EXPORT_SYMBOL(ldlm_namespace_foreach);
2448 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2449 EXPORT_SYMBOL(ldlm_resource_iterate);
2450 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2451 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2452
2453 /* ldlm_lockd.c */
2454 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2455 EXPORT_SYMBOL(ldlm_server_completion_ast);
2456 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2457 EXPORT_SYMBOL(ldlm_handle_enqueue);
2458 EXPORT_SYMBOL(ldlm_handle_cancel);
2459 EXPORT_SYMBOL(ldlm_request_cancel);
2460 EXPORT_SYMBOL(ldlm_handle_convert);
2461 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2462 EXPORT_SYMBOL(ldlm_get_ref);
2463 EXPORT_SYMBOL(ldlm_put_ref);
2464 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2465
2466 /* ldlm_resource.c */
2467 EXPORT_SYMBOL(ldlm_namespace_new);
2468 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2469 EXPORT_SYMBOL(ldlm_namespace_free);
2470 EXPORT_SYMBOL(ldlm_namespace_dump);
2471 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2472 EXPORT_SYMBOL(ldlm_resource_get);
2473 EXPORT_SYMBOL(ldlm_resource_putref);
2474 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2475
2476 /* ldlm_lib.c */
2477 EXPORT_SYMBOL(client_import_add_conn);
2478 EXPORT_SYMBOL(client_import_del_conn);
2479 EXPORT_SYMBOL(client_obd_setup);
2480 EXPORT_SYMBOL(client_obd_cleanup);
2481 EXPORT_SYMBOL(client_connect_import);
2482 EXPORT_SYMBOL(client_disconnect_export);
2483 EXPORT_SYMBOL(server_disconnect_export);
2484 EXPORT_SYMBOL(target_abort_recovery);
2485 EXPORT_SYMBOL(target_cleanup_recovery);
2486 EXPORT_SYMBOL(target_handle_connect);
2487 EXPORT_SYMBOL(target_destroy_export);
2488 EXPORT_SYMBOL(target_cancel_recovery_timer);
2489 EXPORT_SYMBOL(target_send_reply);
2490 EXPORT_SYMBOL(target_queue_recovery_request);
2491 EXPORT_SYMBOL(target_handle_ping);
2492 EXPORT_SYMBOL(target_pack_pool_reply);
2493 EXPORT_SYMBOL(target_handle_disconnect);
2494 EXPORT_SYMBOL(target_handle_reply);
2495
2496 /* l_lock.c */
2497 EXPORT_SYMBOL(lock_res_and_lock);
2498 EXPORT_SYMBOL(unlock_res_and_lock);