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