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