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