Whamcloud - gitweb
LU-1522 recovery: rework LU-1166 patch in different way
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #define DEBUG_SUBSYSTEM S_LDLM
43
44 #ifdef __KERNEL__
45 # include <libcfs/libcfs.h>
46 #else
47 # include <liblustre.h>
48 #endif
49
50 #include <lustre_dlm.h>
51 #include <obd_class.h>
52 #include <libcfs/list.h>
53 #include "ldlm_internal.h"
54
55 static int ldlm_num_threads;
56 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
57                 "number of DLM service threads to start");
58
59 static char *ldlm_cpts;
60 CFS_MODULE_PARM(ldlm_cpts, "s", charp, 0444,
61                 "CPU partitions ldlm threads should run on");
62
63 extern cfs_mem_cache_t *ldlm_resource_slab;
64 extern cfs_mem_cache_t *ldlm_lock_slab;
65 static cfs_mutex_t      ldlm_ref_mutex;
66 static int ldlm_refcount;
67
68 struct ldlm_cb_async_args {
69         struct ldlm_cb_set_arg *ca_set_arg;
70         struct ldlm_lock       *ca_lock;
71 };
72
73 /* LDLM state */
74
75 static struct ldlm_state *ldlm_state;
76
77 inline cfs_time_t round_timeout(cfs_time_t timeout)
78 {
79         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
80 }
81
82 /* timeout for initial callback (AST) reply (bz10399) */
83 static inline unsigned int ldlm_get_rq_timeout(void)
84 {
85         /* Non-AT value */
86         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
87
88         return timeout < 1 ? 1 : timeout;
89 }
90
91 #define ELT_STOPPED   0
92 #define ELT_READY     1
93 #define ELT_TERMINATE 2
94
95 struct ldlm_bl_pool {
96         cfs_spinlock_t          blp_lock;
97
98         /*
99          * blp_prio_list is used for callbacks that should be handled
100          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
101          * see bug 13843
102          */
103         cfs_list_t              blp_prio_list;
104
105         /*
106          * blp_list is used for all other callbacks which are likely
107          * to take longer to process.
108          */
109         cfs_list_t              blp_list;
110
111         cfs_waitq_t             blp_waitq;
112         cfs_completion_t        blp_comp;
113         cfs_atomic_t            blp_num_threads;
114         cfs_atomic_t            blp_busy_threads;
115         int                     blp_min_threads;
116         int                     blp_max_threads;
117 };
118
119 struct ldlm_bl_work_item {
120         cfs_list_t              blwi_entry;
121         struct ldlm_namespace  *blwi_ns;
122         struct ldlm_lock_desc   blwi_ld;
123         struct ldlm_lock       *blwi_lock;
124         cfs_list_t              blwi_head;
125         int                     blwi_count;
126         cfs_completion_t        blwi_comp;
127         int                     blwi_mode;
128         int                     blwi_mem_pressure;
129 };
130
131 #if defined(HAVE_SERVER_SUPPORT) && defined(__KERNEL__)
132
133 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
134 static cfs_spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
135 static cfs_list_t waiting_locks_list;
136 static cfs_timer_t waiting_locks_timer;
137
138 static struct expired_lock_thread {
139         cfs_waitq_t             elt_waitq;
140         int                     elt_state;
141         int                     elt_dump;
142         cfs_list_t              elt_expired_locks;
143 } expired_lock_thread;
144
145 static inline int have_expired_locks(void)
146 {
147         int need_to_run;
148
149         ENTRY;
150         cfs_spin_lock_bh(&waiting_locks_spinlock);
151         need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
152         cfs_spin_unlock_bh(&waiting_locks_spinlock);
153
154         RETURN(need_to_run);
155 }
156
157 static int expired_lock_main(void *arg)
158 {
159         cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
160         struct l_wait_info lwi = { 0 };
161         int do_dump;
162
163         ENTRY;
164         cfs_daemonize("ldlm_elt");
165
166         expired_lock_thread.elt_state = ELT_READY;
167         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
168
169         while (1) {
170                 l_wait_event(expired_lock_thread.elt_waitq,
171                              have_expired_locks() ||
172                              expired_lock_thread.elt_state == ELT_TERMINATE,
173                              &lwi);
174
175                 cfs_spin_lock_bh(&waiting_locks_spinlock);
176                 if (expired_lock_thread.elt_dump) {
177                         struct libcfs_debug_msg_data msgdata = {
178                                 .msg_file = __FILE__,
179                                 .msg_fn = "waiting_locks_callback",
180                                 .msg_line = expired_lock_thread.elt_dump };
181                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
182
183                         /* from waiting_locks_callback, but not in timer */
184                         libcfs_debug_dumplog();
185                         libcfs_run_lbug_upcall(&msgdata);
186
187                         cfs_spin_lock_bh(&waiting_locks_spinlock);
188                         expired_lock_thread.elt_dump = 0;
189                 }
190
191                 do_dump = 0;
192
193                 while (!cfs_list_empty(expired)) {
194                         struct obd_export *export;
195                         struct ldlm_lock *lock;
196
197                         lock = cfs_list_entry(expired->next, struct ldlm_lock,
198                                           l_pending_chain);
199                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
200                             (void *)lock >= LP_POISON) {
201                                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
202                                 CERROR("free lock on elt list %p\n", lock);
203                                 LBUG();
204                         }
205                         cfs_list_del_init(&lock->l_pending_chain);
206                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
207                             (void *)lock->l_export >= LP_POISON) {
208                                 CERROR("lock with free export on elt list %p\n",
209                                        lock->l_export);
210                                 lock->l_export = NULL;
211                                 LDLM_ERROR(lock, "free export");
212                                 /* release extra ref grabbed by
213                                  * ldlm_add_waiting_lock() or
214                                  * ldlm_failed_ast() */
215                                 LDLM_LOCK_RELEASE(lock);
216                                 continue;
217                         }
218
219                         if (lock->l_destroyed) {
220                                 /* release the lock refcount where
221                                  * waiting_locks_callback() founds */
222                                 LDLM_LOCK_RELEASE(lock);
223                                 continue;
224                         }
225                         export = class_export_lock_get(lock->l_export, lock);
226                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
227
228                         do_dump++;
229                         class_fail_export(export);
230                         class_export_lock_put(export, lock);
231
232                         /* release extra ref grabbed by ldlm_add_waiting_lock()
233                          * or ldlm_failed_ast() */
234                         LDLM_LOCK_RELEASE(lock);
235
236                         cfs_spin_lock_bh(&waiting_locks_spinlock);
237                 }
238                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
239
240                 if (do_dump && obd_dump_on_eviction) {
241                         CERROR("dump the log upon eviction\n");
242                         libcfs_debug_dumplog();
243                 }
244
245                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
246                         break;
247         }
248
249         expired_lock_thread.elt_state = ELT_STOPPED;
250         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
251         RETURN(0);
252 }
253
254 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
255 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
256
257 /**
258  * Check if there is a request in the export request list
259  * which prevents the lock canceling.
260  */
261 static int ldlm_lock_busy(struct ldlm_lock *lock)
262 {
263         struct ptlrpc_request *req;
264         int match = 0;
265         ENTRY;
266
267         if (lock->l_export == NULL)
268                 return 0;
269
270         cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
271         cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
272                                 rq_exp_list) {
273                 if (req->rq_ops->hpreq_lock_match) {
274                         match = req->rq_ops->hpreq_lock_match(req, lock);
275                         if (match)
276                                 break;
277                 }
278         }
279         cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
280         RETURN(match);
281 }
282
283 /* This is called from within a timer interrupt and cannot schedule */
284 static void waiting_locks_callback(unsigned long unused)
285 {
286         struct ldlm_lock        *lock;
287         int                     need_dump = 0;
288
289         cfs_spin_lock_bh(&waiting_locks_spinlock);
290         while (!cfs_list_empty(&waiting_locks_list)) {
291                 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
292                                       l_pending_chain);
293                 if (cfs_time_after(lock->l_callback_timeout,
294                                    cfs_time_current()) ||
295                     (lock->l_req_mode == LCK_GROUP))
296                         break;
297
298                 if (ptlrpc_check_suspend()) {
299                         /* there is a case when we talk to one mds, holding
300                          * lock from another mds. this way we easily can get
301                          * here, if second mds is being recovered. so, we
302                          * suspend timeouts. bug 6019 */
303
304                         LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
305                                    lock->l_export->exp_client_uuid.uuid,
306                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
307                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
308
309                         cfs_list_del_init(&lock->l_pending_chain);
310                         if (lock->l_destroyed) {
311                                 /* relay the lock refcount decrease to
312                                  * expired lock thread */
313                                 cfs_list_add(&lock->l_pending_chain,
314                                         &expired_lock_thread.elt_expired_locks);
315                         } else {
316                                 __ldlm_add_waiting_lock(lock,
317                                                 ldlm_get_enq_timeout(lock));
318                         }
319                         continue;
320                 }
321
322                 /* if timeout overlaps the activation time of suspended timeouts
323                  * then extend it to give a chance for client to reconnect */
324                 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
325                                                  cfs_time_seconds(obd_timeout)/2),
326                                     ptlrpc_suspend_wakeup_time())) {
327                         LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
328                                    lock->l_export->exp_client_uuid.uuid,
329                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
330                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
331
332                         cfs_list_del_init(&lock->l_pending_chain);
333                         if (lock->l_destroyed) {
334                                 /* relay the lock refcount decrease to
335                                  * expired lock thread */
336                                 cfs_list_add(&lock->l_pending_chain,
337                                         &expired_lock_thread.elt_expired_locks);
338                         } else {
339                                 __ldlm_add_waiting_lock(lock,
340                                                 ldlm_get_enq_timeout(lock));
341                         }
342                         continue;
343                 }
344
345                 /* Check if we need to prolong timeout */
346                 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
347                     ldlm_lock_busy(lock)) {
348                         int cont = 1;
349
350                         if (lock->l_pending_chain.next == &waiting_locks_list)
351                                 cont = 0;
352
353                         LDLM_LOCK_GET(lock);
354
355                         cfs_spin_unlock_bh(&waiting_locks_spinlock);
356                         LDLM_DEBUG(lock, "prolong the busy lock");
357                         ldlm_refresh_waiting_lock(lock,
358                                                   ldlm_get_enq_timeout(lock));
359                         cfs_spin_lock_bh(&waiting_locks_spinlock);
360
361                         if (!cont) {
362                                 LDLM_LOCK_RELEASE(lock);
363                                 break;
364                         }
365
366                         LDLM_LOCK_RELEASE(lock);
367                         continue;
368                 }
369                 ldlm_lock_to_ns(lock)->ns_timeouts++;
370                 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
371                            "evicting client at %s ",
372                            cfs_time_current_sec()- lock->l_last_activity,
373                            libcfs_nid2str(
374                                    lock->l_export->exp_connection->c_peer.nid));
375
376                 /* no needs to take an extra ref on the lock since it was in
377                  * the waiting_locks_list and ldlm_add_waiting_lock()
378                  * already grabbed a ref */
379                 cfs_list_del(&lock->l_pending_chain);
380                 cfs_list_add(&lock->l_pending_chain,
381                              &expired_lock_thread.elt_expired_locks);
382                 need_dump = 1;
383         }
384
385         if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
386                 if (obd_dump_on_timeout && need_dump)
387                         expired_lock_thread.elt_dump = __LINE__;
388
389                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
390         }
391
392         /*
393          * Make sure the timer will fire again if we have any locks
394          * left.
395          */
396         if (!cfs_list_empty(&waiting_locks_list)) {
397                 cfs_time_t timeout_rounded;
398                 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
399                                       l_pending_chain);
400                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
401                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
402         }
403         cfs_spin_unlock_bh(&waiting_locks_spinlock);
404 }
405
406 /*
407  * Indicate that we're waiting for a client to call us back cancelling a given
408  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
409  * timer to fire appropriately.  (We round up to the next second, to avoid
410  * floods of timer firings during periods of high lock contention and traffic).
411  * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
412  * if it has been added to the waiting list (1 is returned).
413  *
414  * Called with the namespace lock held.
415  */
416 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
417 {
418         cfs_time_t timeout;
419         cfs_time_t timeout_rounded;
420
421         if (!cfs_list_empty(&lock->l_pending_chain))
422                 return 0;
423
424         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
425             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
426                 seconds = 1;
427
428         timeout = cfs_time_shift(seconds);
429         if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
430                 lock->l_callback_timeout = timeout;
431
432         timeout_rounded = round_timeout(lock->l_callback_timeout);
433
434         if (cfs_time_before(timeout_rounded,
435                             cfs_timer_deadline(&waiting_locks_timer)) ||
436             !cfs_timer_is_armed(&waiting_locks_timer)) {
437                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
438         }
439         /* if the new lock has a shorter timeout than something earlier on
440            the list, we'll wait the longer amount of time; no big deal. */
441         /* FIFO */
442         cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
443         return 1;
444 }
445
446 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
447 {
448         int ret;
449         int timeout = ldlm_get_enq_timeout(lock);
450
451         /* NB: must be called with hold of lock_res_and_lock() */
452         LASSERT(lock->l_res_locked);
453         lock->l_waited = 1;
454
455         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
456
457         cfs_spin_lock_bh(&waiting_locks_spinlock);
458         if (lock->l_destroyed) {
459                 static cfs_time_t next;
460                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
461                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
462                 if (cfs_time_after(cfs_time_current(), next)) {
463                         next = cfs_time_shift(14400);
464                         libcfs_debug_dumpstack(NULL);
465                 }
466                 return 0;
467         }
468
469         ret = __ldlm_add_waiting_lock(lock, timeout);
470         if (ret) {
471                 /* grab ref on the lock if it has been added to the
472                  * waiting list */
473                 LDLM_LOCK_GET(lock);
474         }
475         cfs_spin_unlock_bh(&waiting_locks_spinlock);
476
477         if (ret) {
478                 cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
479                 if (cfs_list_empty(&lock->l_exp_list))
480                         cfs_list_add(&lock->l_exp_list,
481                                      &lock->l_export->exp_bl_list);
482                 cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
483         }
484
485         LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
486                    ret == 0 ? "not re-" : "", timeout,
487                    AT_OFF ? "off" : "on");
488         return ret;
489 }
490
491 /*
492  * Remove a lock from the pending list, likely because it had its cancellation
493  * callback arrive without incident.  This adjusts the lock-timeout timer if
494  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
495  * As done by ldlm_del_waiting_lock(), the caller must release the lock
496  * reference when the lock is removed from any list (1 is returned).
497  *
498  * Called with namespace lock held.
499  */
500 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
501 {
502         cfs_list_t *list_next;
503
504         if (cfs_list_empty(&lock->l_pending_chain))
505                 return 0;
506
507         list_next = lock->l_pending_chain.next;
508         if (lock->l_pending_chain.prev == &waiting_locks_list) {
509                 /* Removing the head of the list, adjust timer. */
510                 if (list_next == &waiting_locks_list) {
511                         /* No more, just cancel. */
512                         cfs_timer_disarm(&waiting_locks_timer);
513                 } else {
514                         struct ldlm_lock *next;
515                         next = cfs_list_entry(list_next, struct ldlm_lock,
516                                               l_pending_chain);
517                         cfs_timer_arm(&waiting_locks_timer,
518                                       round_timeout(next->l_callback_timeout));
519                 }
520         }
521         cfs_list_del_init(&lock->l_pending_chain);
522
523         return 1;
524 }
525
526 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
527 {
528         int ret;
529
530         if (lock->l_export == NULL) {
531                 /* We don't have a "waiting locks list" on clients. */
532                 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
533                 return 0;
534         }
535
536         cfs_spin_lock_bh(&waiting_locks_spinlock);
537         ret = __ldlm_del_waiting_lock(lock);
538         cfs_spin_unlock_bh(&waiting_locks_spinlock);
539
540         /* remove the lock out of export blocking list */
541         cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
542         cfs_list_del_init(&lock->l_exp_list);
543         cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
544
545         if (ret) {
546                 /* release lock ref if it has indeed been removed
547                  * from a list */
548                 LDLM_LOCK_RELEASE(lock);
549         }
550
551         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
552         return ret;
553 }
554
555 /*
556  * Prolong the lock
557  *
558  * Called with namespace lock held.
559  */
560 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
561 {
562         if (lock->l_export == NULL) {
563                 /* We don't have a "waiting locks list" on clients. */
564                 LDLM_DEBUG(lock, "client lock: no-op");
565                 return 0;
566         }
567
568         cfs_spin_lock_bh(&waiting_locks_spinlock);
569
570         if (cfs_list_empty(&lock->l_pending_chain)) {
571                 cfs_spin_unlock_bh(&waiting_locks_spinlock);
572                 LDLM_DEBUG(lock, "wasn't waiting");
573                 return 0;
574         }
575
576         /* we remove/add the lock to the waiting list, so no needs to
577          * release/take a lock reference */
578         __ldlm_del_waiting_lock(lock);
579         __ldlm_add_waiting_lock(lock, timeout);
580         cfs_spin_unlock_bh(&waiting_locks_spinlock);
581
582         LDLM_DEBUG(lock, "refreshed");
583         return 1;
584 }
585
586 #else /* !HAVE_SERVER_SUPPORT ||  !__KERNEL__ */
587
588 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
589 {
590         RETURN(0);
591 }
592
593 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
594 {
595         RETURN(0);
596 }
597
598 # ifdef HAVE_SERVER_SUPPORT
599 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
600 {
601         LASSERT(lock->l_res_locked);
602         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
603         RETURN(1);
604 }
605
606 # endif
607 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
608
609 #ifdef HAVE_SERVER_SUPPORT
610
611 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
612                             const char *ast_type)
613 {
614         LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
615                            "to a lock %s callback time out: rc %d\n",
616                            lock->l_export->exp_obd->obd_name,
617                            obd_export_nid2str(lock->l_export), ast_type, rc);
618
619         if (obd_dump_on_timeout)
620                 libcfs_debug_dumplog();
621 #ifdef __KERNEL__
622         cfs_spin_lock_bh(&waiting_locks_spinlock);
623         if (__ldlm_del_waiting_lock(lock) == 0)
624                 /* the lock was not in any list, grab an extra ref before adding
625                  * the lock to the expired list */
626                 LDLM_LOCK_GET(lock);
627         cfs_list_add(&lock->l_pending_chain,
628                      &expired_lock_thread.elt_expired_locks);
629         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
630         cfs_spin_unlock_bh(&waiting_locks_spinlock);
631 #else
632         class_fail_export(lock->l_export);
633 #endif
634 }
635
636 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
637                                  struct ptlrpc_request *req, int rc,
638                                  const char *ast_type)
639 {
640         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
641
642         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
643                 LASSERT(lock->l_export);
644                 if (lock->l_export->exp_libclient) {
645                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
646                                    " timeout, just cancelling lock", ast_type,
647                                    libcfs_nid2str(peer.nid));
648                         ldlm_lock_cancel(lock);
649                         rc = -ERESTART;
650                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
651                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
652                                    "cancel was received (AST reply lost?)",
653                                    ast_type, libcfs_nid2str(peer.nid));
654                         ldlm_lock_cancel(lock);
655                         rc = -ERESTART;
656                 } else {
657                         ldlm_del_waiting_lock(lock);
658                         ldlm_failed_ast(lock, rc, ast_type);
659                 }
660         } else if (rc) {
661                 if (rc == -EINVAL) {
662                         struct ldlm_resource *res = lock->l_resource;
663                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
664                                " from %s AST - normal race",
665                                libcfs_nid2str(peer.nid),
666                                req->rq_repmsg ?
667                                lustre_msg_get_status(req->rq_repmsg) : -1,
668                                ast_type);
669                         if (res) {
670                                 /* update lvbo to return proper attributes.
671                                  * see bug 23174 */
672                                 ldlm_resource_getref(res);
673                                 ldlm_res_lvbo_update(res, NULL, 1);
674                                 ldlm_resource_putref(res);
675                         }
676
677                 } else {
678                         LDLM_ERROR(lock, "client (nid %s) returned %d "
679                                    "from %s AST", libcfs_nid2str(peer.nid),
680                                    (req->rq_repmsg != NULL) ?
681                                    lustre_msg_get_status(req->rq_repmsg) : 0,
682                                    ast_type);
683                 }
684                 ldlm_lock_cancel(lock);
685                 /* Server-side AST functions are called from ldlm_reprocess_all,
686                  * which needs to be told to please restart its reprocessing. */
687                 rc = -ERESTART;
688         }
689
690         return rc;
691 }
692
693 static int ldlm_cb_interpret(const struct lu_env *env,
694                              struct ptlrpc_request *req, void *data, int rc)
695 {
696         struct ldlm_cb_async_args *ca   = data;
697         struct ldlm_lock          *lock = ca->ca_lock;
698         struct ldlm_cb_set_arg    *arg  = ca->ca_set_arg;
699         ENTRY;
700
701         LASSERT(lock != NULL);
702         if (rc != 0) {
703                 rc = ldlm_handle_ast_error(lock, req, rc,
704                                            arg->type == LDLM_BL_CALLBACK
705                                            ? "blocking" : "completion");
706                 if (rc == -ERESTART)
707                         cfs_atomic_inc(&arg->restart);
708         }
709         LDLM_LOCK_RELEASE(lock);
710
711         RETURN(0);
712 }
713
714 static inline int ldlm_bl_and_cp_ast_tail(struct ptlrpc_request *req,
715                                           struct ldlm_cb_set_arg *arg,
716                                           struct ldlm_lock *lock,
717                                           int instant_cancel)
718 {
719         int rc = 0;
720         ENTRY;
721
722         if (unlikely(instant_cancel)) {
723                 rc = ptl_send_rpc(req, 1);
724                 ptlrpc_req_finished(req);
725                 if (rc == 0)
726                         cfs_atomic_inc(&arg->restart);
727         } else {
728                 LDLM_LOCK_GET(lock);
729                 ptlrpc_set_add_req(arg->set, req);
730         }
731
732         RETURN(rc);
733 }
734
735 /**
736  * Check if there are requests in the export request list which prevent
737  * the lock canceling and make these requests high priority ones.
738  */
739 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
740 {
741         struct ptlrpc_request *req;
742         ENTRY;
743
744         if (lock->l_export == NULL) {
745                 LDLM_DEBUG(lock, "client lock: no-op");
746                 RETURN_EXIT;
747         }
748
749         cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
750         cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
751                                 rq_exp_list) {
752                 /* Do not process requests that were not yet added to there
753                  * incoming queue or were already removed from there for
754                  * processing */
755                 if (!req->rq_hp && !cfs_list_empty(&req->rq_list) &&
756                     req->rq_ops->hpreq_lock_match &&
757                     req->rq_ops->hpreq_lock_match(req, lock))
758                         ptlrpc_hpreq_reorder(req);
759         }
760         cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
761         EXIT;
762 }
763
764 /*
765  * ->l_blocking_ast() method for server-side locks. This is invoked when newly
766  * enqueued server lock conflicts with given one.
767  *
768  * Sends blocking ast rpc to the client owning that lock; arms timeout timer
769  * to wait for client response.
770  */
771 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
772                              struct ldlm_lock_desc *desc,
773                              void *data, int flag)
774 {
775         struct ldlm_cb_async_args *ca;
776         struct ldlm_cb_set_arg *arg = data;
777         struct ldlm_request    *body;
778         struct ptlrpc_request  *req;
779         int                     instant_cancel = 0;
780         int                     rc = 0;
781         ENTRY;
782
783         if (flag == LDLM_CB_CANCELING)
784                 /* Don't need to do anything here. */
785                 RETURN(0);
786
787         LASSERT(lock);
788         LASSERT(data != NULL);
789         if (lock->l_export->exp_obd->obd_recovering != 0)
790                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
791
792         ldlm_lock_reorder_req(lock);
793
794         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
795                                         &RQF_LDLM_BL_CALLBACK,
796                                         LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
797         if (req == NULL)
798                 RETURN(-ENOMEM);
799
800         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
801         ca = ptlrpc_req_async_args(req);
802         ca->ca_set_arg = arg;
803         ca->ca_lock = lock;
804
805         req->rq_interpret_reply = ldlm_cb_interpret;
806         req->rq_no_resend = 1;
807
808         lock_res_and_lock(lock);
809         if (lock->l_granted_mode != lock->l_req_mode) {
810                 /* this blocking AST will be communicated as part of the
811                  * completion AST instead */
812                 unlock_res_and_lock(lock);
813
814                 ptlrpc_req_finished(req);
815                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
816                 RETURN(0);
817         }
818
819         if (lock->l_destroyed) {
820                 /* What's the point? */
821                 unlock_res_and_lock(lock);
822                 ptlrpc_req_finished(req);
823                 RETURN(0);
824         }
825
826         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
827                 instant_cancel = 1;
828
829         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
830         body->lock_handle[0] = lock->l_remote_handle;
831         body->lock_desc = *desc;
832         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
833
834         LDLM_DEBUG(lock, "server preparing blocking AST");
835
836         ptlrpc_request_set_replen(req);
837         if (instant_cancel) {
838                 unlock_res_and_lock(lock);
839                 ldlm_lock_cancel(lock);
840         } else {
841                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
842                 ldlm_add_waiting_lock(lock);
843                 unlock_res_and_lock(lock);
844         }
845
846         req->rq_send_state = LUSTRE_IMP_FULL;
847         /* ptlrpc_request_alloc_pack already set timeout */
848         if (AT_OFF)
849                 req->rq_timeout = ldlm_get_rq_timeout();
850
851         if (lock->l_export && lock->l_export->exp_nid_stats &&
852             lock->l_export->exp_nid_stats->nid_ldlm_stats)
853                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
854                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
855
856         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
857
858         RETURN(rc);
859 }
860
861 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
862 {
863         struct ldlm_cb_set_arg *arg = data;
864         struct ldlm_request    *body;
865         struct ptlrpc_request  *req;
866         struct ldlm_cb_async_args *ca;
867         long                    total_enqueue_wait;
868         int                     instant_cancel = 0;
869         int                     rc = 0;
870         ENTRY;
871
872         LASSERT(lock != NULL);
873         LASSERT(data != NULL);
874
875         total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
876                                           lock->l_last_activity);
877
878         req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
879                                     &RQF_LDLM_CP_CALLBACK);
880         if (req == NULL)
881                 RETURN(-ENOMEM);
882
883         /* server namespace, doesn't need lock */
884         if (lock->l_resource->lr_lvb_len) {
885                  req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
886                                       lock->l_resource->lr_lvb_len);
887         }
888
889         rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
890         if (rc) {
891                 ptlrpc_request_free(req);
892                 RETURN(rc);
893         }
894
895         CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
896         ca = ptlrpc_req_async_args(req);
897         ca->ca_set_arg = arg;
898         ca->ca_lock = lock;
899
900         req->rq_interpret_reply = ldlm_cb_interpret;
901         req->rq_no_resend = 1;
902         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
903
904         body->lock_handle[0] = lock->l_remote_handle;
905         body->lock_flags = flags;
906         ldlm_lock2desc(lock, &body->lock_desc);
907         if (lock->l_resource->lr_lvb_len) {
908                 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
909
910                 lock_res(lock->l_resource);
911                 memcpy(lvb, lock->l_resource->lr_lvb_data,
912                        lock->l_resource->lr_lvb_len);
913                 unlock_res(lock->l_resource);
914         }
915
916         LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
917                    total_enqueue_wait);
918
919         /* Server-side enqueue wait time estimate, used in
920             __ldlm_add_waiting_lock to set future enqueue timers */
921         if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
922                 at_measured(ldlm_lock_to_ns_at(lock),
923                             total_enqueue_wait);
924         else
925                 /* bz18618. Don't add lock enqueue time we spend waiting for a
926                    previous callback to fail. Locks waiting legitimately will
927                    get extended by ldlm_refresh_waiting_lock regardless of the
928                    estimate, so it's okay to underestimate here. */
929                 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
930                        "It is likely that a previous callback timed out.",
931                        total_enqueue_wait,
932                        at_get(ldlm_lock_to_ns_at(lock)));
933
934         ptlrpc_request_set_replen(req);
935
936         req->rq_send_state = LUSTRE_IMP_FULL;
937         /* ptlrpc_request_pack already set timeout */
938         if (AT_OFF)
939                 req->rq_timeout = ldlm_get_rq_timeout();
940
941         /* We only send real blocking ASTs after the lock is granted */
942         lock_res_and_lock(lock);
943         if (lock->l_flags & LDLM_FL_AST_SENT) {
944                 body->lock_flags |= LDLM_FL_AST_SENT;
945                 /* copy ast flags like LDLM_FL_DISCARD_DATA */
946                 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
947
948                 /* We might get here prior to ldlm_handle_enqueue setting
949                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
950                  * into waiting list, but this is safe and similar code in
951                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
952                  * that would not only cancel the lock, but will also remove
953                  * it from waiting list */
954                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
955                         unlock_res_and_lock(lock);
956                         ldlm_lock_cancel(lock);
957                         instant_cancel = 1;
958                         lock_res_and_lock(lock);
959                 } else {
960                         /* start the lock-timeout clock */
961                         ldlm_add_waiting_lock(lock);
962                 }
963         }
964         unlock_res_and_lock(lock);
965
966         if (lock->l_export && lock->l_export->exp_nid_stats &&
967             lock->l_export->exp_nid_stats->nid_ldlm_stats)
968                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
969                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
970
971         rc = ldlm_bl_and_cp_ast_tail(req, arg, lock, instant_cancel);
972
973         RETURN(rc);
974 }
975
976 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
977 {
978         struct ldlm_resource  *res = lock->l_resource;
979         struct ldlm_request   *body;
980         struct ptlrpc_request *req;
981         int                    rc;
982         ENTRY;
983
984         LASSERT(lock != NULL);
985
986         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
987                                         &RQF_LDLM_GL_CALLBACK,
988                                         LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
989
990         if (req == NULL)
991                 RETURN(-ENOMEM);
992
993         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
994         body->lock_handle[0] = lock->l_remote_handle;
995         ldlm_lock2desc(lock, &body->lock_desc);
996
997         /* server namespace, doesn't need lock */
998         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
999                              lock->l_resource->lr_lvb_len);
1000         res = lock->l_resource;
1001         ptlrpc_request_set_replen(req);
1002
1003
1004         req->rq_send_state = LUSTRE_IMP_FULL;
1005         /* ptlrpc_request_alloc_pack already set timeout */
1006         if (AT_OFF)
1007                 req->rq_timeout = ldlm_get_rq_timeout();
1008
1009         if (lock->l_export && lock->l_export->exp_nid_stats &&
1010             lock->l_export->exp_nid_stats->nid_ldlm_stats)
1011                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1012                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1013
1014         rc = ptlrpc_queue_wait(req);
1015         /* Update the LVB from disk if the AST failed (this is a legal race)
1016          *
1017          * - Glimpse callback of local lock just return -ELDLM_NO_LOCK_DATA.
1018          * - Glimpse callback of remote lock might return -ELDLM_NO_LOCK_DATA
1019          *   when inode is cleared. LU-274
1020          */
1021         if (rc == -ELDLM_NO_LOCK_DATA) {
1022                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
1023                 ldlm_res_lvbo_update(res, NULL, 1);
1024         } else if (rc != 0) {
1025                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
1026         } else {
1027                 rc = ldlm_res_lvbo_update(res, req, 1);
1028         }
1029
1030         ptlrpc_req_finished(req);
1031         if (rc == -ERESTART)
1032                 ldlm_reprocess_all(res);
1033
1034         RETURN(rc);
1035 }
1036
1037 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1038                        struct lprocfs_stats *srv_stats)
1039 {
1040         int lock_type = 0, op = 0;
1041
1042         lock_type = dlm_req->lock_desc.l_resource.lr_type;
1043
1044         switch (lock_type) {
1045         case LDLM_PLAIN:
1046                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1047                 break;
1048         case LDLM_EXTENT:
1049                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1050                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1051                 else
1052                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1053                 break;
1054         case LDLM_FLOCK:
1055                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1056                 break;
1057         case LDLM_IBITS:
1058                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1059                 break;
1060         default:
1061                 op = 0;
1062                 break;
1063         }
1064
1065         if (op)
1066                 lprocfs_counter_incr(srv_stats, op);
1067
1068         return;
1069 }
1070
1071 /*
1072  * Main server-side entry point into LDLM. This is called by ptlrpc service
1073  * threads to carry out client lock enqueueing requests.
1074  */
1075 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1076                          struct ptlrpc_request *req,
1077                          const struct ldlm_request *dlm_req,
1078                          const struct ldlm_callback_suite *cbs)
1079 {
1080         struct ldlm_reply *dlm_rep;
1081         __u32 flags;
1082         ldlm_error_t err = ELDLM_OK;
1083         struct ldlm_lock *lock = NULL;
1084         void *cookie = NULL;
1085         int rc = 0;
1086         ENTRY;
1087
1088         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1089
1090         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1091         flags = dlm_req->lock_flags;
1092
1093         LASSERT(req->rq_export);
1094
1095         if (ptlrpc_req2svc(req)->srv_stats != NULL)
1096                 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1097
1098         if (req->rq_export && req->rq_export->exp_nid_stats &&
1099             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1100                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1101                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
1102
1103         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1104                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1105                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1106                           dlm_req->lock_desc.l_resource.lr_type);
1107                 GOTO(out, rc = -EFAULT);
1108         }
1109
1110         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1111                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1112                      dlm_req->lock_desc.l_req_mode &
1113                      (dlm_req->lock_desc.l_req_mode-1))) {
1114                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1115                           dlm_req->lock_desc.l_req_mode);
1116                 GOTO(out, rc = -EFAULT);
1117         }
1118
1119         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1120                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1121                              LDLM_PLAIN)) {
1122                         DEBUG_REQ(D_ERROR, req,
1123                                   "PLAIN lock request from IBITS client?");
1124                         GOTO(out, rc = -EPROTO);
1125                 }
1126         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1127                             LDLM_IBITS)) {
1128                 DEBUG_REQ(D_ERROR, req,
1129                           "IBITS lock request from unaware client?");
1130                 GOTO(out, rc = -EPROTO);
1131         }
1132
1133 #if 0
1134         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1135            against server's _CONNECT_SUPPORTED flags? (I don't want to use
1136            ibits for mgc/mgs) */
1137
1138         /* INODEBITS_INTEROP: Perform conversion from plain lock to
1139          * inodebits lock if client does not support them. */
1140         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1141             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1142                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1143                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1144                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1145                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1146                         dlm_req->lock_desc.l_req_mode = LCK_CR;
1147         }
1148 #endif
1149
1150         if (unlikely(flags & LDLM_FL_REPLAY)) {
1151                 /* Find an existing lock in the per-export lock hash */
1152                 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1153                                        (void *)&dlm_req->lock_handle[0]);
1154                 if (lock != NULL) {
1155                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1156                                   LPX64, lock->l_handle.h_cookie);
1157                         GOTO(existing_lock, rc = 0);
1158                 }
1159         }
1160
1161         /* The lock's callback data might be set in the policy function */
1162         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1163                                 dlm_req->lock_desc.l_resource.lr_type,
1164                                 dlm_req->lock_desc.l_req_mode,
1165                                 cbs, NULL, 0);
1166
1167         if (!lock)
1168                 GOTO(out, rc = -ENOMEM);
1169
1170         lock->l_last_activity = cfs_time_current_sec();
1171         lock->l_remote_handle = dlm_req->lock_handle[0];
1172         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1173
1174         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1175         /* Don't enqueue a lock onto the export if it is been disonnected
1176          * due to eviction (bug 3822) or server umount (bug 24324).
1177          * Cancel it now instead. */
1178         if (req->rq_export->exp_disconnected) {
1179                 LDLM_ERROR(lock, "lock on disconnected export %p",
1180                            req->rq_export);
1181                 GOTO(out, rc = -ENOTCONN);
1182         }
1183
1184         lock->l_export = class_export_lock_get(req->rq_export, lock);
1185         if (lock->l_export->exp_lock_hash)
1186                 cfs_hash_add(lock->l_export->exp_lock_hash,
1187                              &lock->l_remote_handle,
1188                              &lock->l_exp_hash);
1189
1190 existing_lock:
1191
1192         if (flags & LDLM_FL_HAS_INTENT) {
1193                 /* In this case, the reply buffer is allocated deep in
1194                  * local_lock_enqueue by the policy function. */
1195                 cookie = req;
1196         } else {
1197                 /* based on the assumption that lvb size never changes during
1198                  * resource life time otherwise it need resource->lr_lock's
1199                  * protection */
1200                 if (lock->l_resource->lr_lvb_len) {
1201                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1202                                              RCL_SERVER,
1203                                              lock->l_resource->lr_lvb_len);
1204                 }
1205
1206                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1207                         GOTO(out, rc = -ENOMEM);
1208
1209                 rc = req_capsule_server_pack(&req->rq_pill);
1210                 if (rc)
1211                         GOTO(out, rc);
1212         }
1213
1214         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1215                 ldlm_convert_policy_to_local(req->rq_export,
1216                                           dlm_req->lock_desc.l_resource.lr_type,
1217                                           &dlm_req->lock_desc.l_policy_data,
1218                                           &lock->l_policy_data);
1219         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1220                 lock->l_req_extent = lock->l_policy_data.l_extent;
1221
1222         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1223         if (err)
1224                 GOTO(out, err);
1225
1226         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1227         dlm_rep->lock_flags = flags;
1228
1229         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1230         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1231
1232         /* We never send a blocking AST until the lock is granted, but
1233          * we can tell it right now */
1234         lock_res_and_lock(lock);
1235
1236         /* Now take into account flags to be inherited from original lock
1237            request both in reply to client and in our own lock flags. */
1238         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1239         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1240
1241         /* Don't move a pending lock onto the export if it has already been
1242          * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1243          * Cancel it now instead. */
1244         if (unlikely(req->rq_export->exp_disconnected ||
1245                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1246                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1247                 rc = -ENOTCONN;
1248         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1249                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1250                 if (lock->l_granted_mode == lock->l_req_mode) {
1251                         /*
1252                          * Only cancel lock if it was granted, because it would
1253                          * be destroyed immediately and would never be granted
1254                          * in the future, causing timeouts on client.  Not
1255                          * granted lock will be cancelled immediately after
1256                          * sending completion AST.
1257                          */
1258                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1259                                 unlock_res_and_lock(lock);
1260                                 ldlm_lock_cancel(lock);
1261                                 lock_res_and_lock(lock);
1262                         } else
1263                                 ldlm_add_waiting_lock(lock);
1264                 }
1265         }
1266         /* Make sure we never ever grant usual metadata locks to liblustre
1267            clients */
1268         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1269             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1270              req->rq_export->exp_libclient) {
1271                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1272                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1273                         CERROR("Granting sync lock to libclient. "
1274                                "req fl %d, rep fl %d, lock fl "LPX64"\n",
1275                                dlm_req->lock_flags, dlm_rep->lock_flags,
1276                                lock->l_flags);
1277                         LDLM_ERROR(lock, "sync lock");
1278                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1279                                 struct ldlm_intent *it;
1280
1281                                 it = req_capsule_client_get(&req->rq_pill,
1282                                                             &RMF_LDLM_INTENT);
1283                                 if (it != NULL) {
1284                                         CERROR("This is intent %s ("LPU64")\n",
1285                                                ldlm_it2str(it->opc), it->opc);
1286                                 }
1287                         }
1288                 }
1289         }
1290
1291         unlock_res_and_lock(lock);
1292
1293         EXIT;
1294  out:
1295         req->rq_status = rc ?: err; /* return either error - bug 11190 */
1296         if (!req->rq_packed_final) {
1297                 err = lustre_pack_reply(req, 1, NULL, NULL);
1298                 if (rc == 0)
1299                         rc = err;
1300         }
1301
1302         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1303          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1304         if (lock) {
1305                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1306                            "(err=%d, rc=%d)", err, rc);
1307
1308                 if (rc == 0) {
1309                         if (lock->l_resource->lr_lvb_len > 0) {
1310                                 /* MDT path won't handle lr_lvb_data, so
1311                                  * lock/unlock better be contained in the
1312                                  * if block */
1313                                 void *lvb;
1314
1315                                 lvb = req_capsule_server_get(&req->rq_pill,
1316                                                              &RMF_DLM_LVB);
1317                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1318                                          req, lock);
1319                                 lock_res(lock->l_resource);
1320                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1321                                        lock->l_resource->lr_lvb_len);
1322                                 unlock_res(lock->l_resource);
1323                         }
1324                 } else {
1325                         lock_res_and_lock(lock);
1326                         ldlm_resource_unlink_lock(lock);
1327                         ldlm_lock_destroy_nolock(lock);
1328                         unlock_res_and_lock(lock);
1329                 }
1330
1331                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1332                         ldlm_reprocess_all(lock->l_resource);
1333
1334                 LDLM_LOCK_RELEASE(lock);
1335         }
1336
1337         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1338                           lock, rc);
1339
1340         return rc;
1341 }
1342
1343 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1344                         ldlm_completion_callback completion_callback,
1345                         ldlm_blocking_callback blocking_callback,
1346                         ldlm_glimpse_callback glimpse_callback)
1347 {
1348         struct ldlm_request *dlm_req;
1349         struct ldlm_callback_suite cbs = {
1350                 .lcs_completion = completion_callback,
1351                 .lcs_blocking   = blocking_callback,
1352                 .lcs_glimpse    = glimpse_callback
1353         };
1354         int rc;
1355
1356         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1357         if (dlm_req != NULL) {
1358                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1359                                           req, dlm_req, &cbs);
1360         } else {
1361                 rc = -EFAULT;
1362         }
1363         return rc;
1364 }
1365
1366 int ldlm_handle_convert0(struct ptlrpc_request *req,
1367                          const struct ldlm_request *dlm_req)
1368 {
1369         struct ldlm_reply *dlm_rep;
1370         struct ldlm_lock *lock;
1371         int rc;
1372         ENTRY;
1373
1374         if (req->rq_export && req->rq_export->exp_nid_stats &&
1375             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1376                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1377                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1378
1379         rc = req_capsule_server_pack(&req->rq_pill);
1380         if (rc)
1381                 RETURN(rc);
1382
1383         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1384         dlm_rep->lock_flags = dlm_req->lock_flags;
1385
1386         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1387         if (!lock) {
1388                 req->rq_status = EINVAL;
1389         } else {
1390                 void *res = NULL;
1391
1392                 LDLM_DEBUG(lock, "server-side convert handler START");
1393
1394                 lock->l_last_activity = cfs_time_current_sec();
1395                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1396                                         &dlm_rep->lock_flags);
1397                 if (res) {
1398                         if (ldlm_del_waiting_lock(lock))
1399                                 LDLM_DEBUG(lock, "converted waiting lock");
1400                         req->rq_status = 0;
1401                 } else {
1402                         req->rq_status = EDEADLOCK;
1403                 }
1404         }
1405
1406         if (lock) {
1407                 if (!req->rq_status)
1408                         ldlm_reprocess_all(lock->l_resource);
1409                 LDLM_DEBUG(lock, "server-side convert handler END");
1410                 LDLM_LOCK_PUT(lock);
1411         } else
1412                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1413
1414         RETURN(0);
1415 }
1416
1417 int ldlm_handle_convert(struct ptlrpc_request *req)
1418 {
1419         int rc;
1420         struct ldlm_request *dlm_req;
1421
1422         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1423         if (dlm_req != NULL) {
1424                 rc = ldlm_handle_convert0(req, dlm_req);
1425         } else {
1426                 CERROR ("Can't unpack dlm_req\n");
1427                 rc = -EFAULT;
1428         }
1429         return rc;
1430 }
1431
1432 /* Cancel all the locks whos handles are packed into ldlm_request */
1433 int ldlm_request_cancel(struct ptlrpc_request *req,
1434                         const struct ldlm_request *dlm_req, int first)
1435 {
1436         struct ldlm_resource *res, *pres = NULL;
1437         struct ldlm_lock *lock;
1438         int i, count, done = 0;
1439         ENTRY;
1440
1441         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1442         if (first >= count)
1443                 RETURN(0);
1444
1445         /* There is no lock on the server at the replay time,
1446          * skip lock cancelling to make replay tests to pass. */
1447         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1448                 RETURN(0);
1449
1450         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1451                           "starting at %d", count, first);
1452
1453         for (i = first; i < count; i++) {
1454                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1455                 if (!lock) {
1456                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1457                                           "lock (cookie "LPU64")",
1458                                           dlm_req->lock_handle[i].cookie);
1459                         continue;
1460                 }
1461
1462                 res = lock->l_resource;
1463                 done++;
1464
1465                 if (res != pres) {
1466                         if (pres != NULL) {
1467                                 ldlm_reprocess_all(pres);
1468                                 LDLM_RESOURCE_DELREF(pres);
1469                                 ldlm_resource_putref(pres);
1470                         }
1471                         if (res != NULL) {
1472                                 ldlm_resource_getref(res);
1473                                 LDLM_RESOURCE_ADDREF(res);
1474                                 ldlm_res_lvbo_update(res, NULL, 1);
1475                         }
1476                         pres = res;
1477                 }
1478                 ldlm_lock_cancel(lock);
1479                 LDLM_LOCK_PUT(lock);
1480         }
1481         if (pres != NULL) {
1482                 ldlm_reprocess_all(pres);
1483                 LDLM_RESOURCE_DELREF(pres);
1484                 ldlm_resource_putref(pres);
1485         }
1486         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1487         RETURN(done);
1488 }
1489
1490 int ldlm_handle_cancel(struct ptlrpc_request *req)
1491 {
1492         struct ldlm_request *dlm_req;
1493         int rc;
1494         ENTRY;
1495
1496         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1497         if (dlm_req == NULL) {
1498                 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1499                 RETURN(-EFAULT);
1500         }
1501
1502         if (req->rq_export && req->rq_export->exp_nid_stats &&
1503             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1504                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1505                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1506
1507         rc = req_capsule_server_pack(&req->rq_pill);
1508         if (rc)
1509                 RETURN(rc);
1510
1511         if (!ldlm_request_cancel(req, dlm_req, 0))
1512                 req->rq_status = ESTALE;
1513
1514         RETURN(ptlrpc_reply(req));
1515 }
1516 #endif /* HAVE_SERVER_SUPPORT */
1517
1518 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1519                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1520 {
1521         int do_ast;
1522         ENTRY;
1523
1524         LDLM_DEBUG(lock, "client blocking AST callback handler");
1525
1526         lock_res_and_lock(lock);
1527         lock->l_flags |= LDLM_FL_CBPENDING;
1528
1529         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1530                 lock->l_flags |= LDLM_FL_CANCEL;
1531
1532         do_ast = (!lock->l_readers && !lock->l_writers);
1533         unlock_res_and_lock(lock);
1534
1535         if (do_ast) {
1536                 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1537                        lock, lock->l_blocking_ast);
1538                 if (lock->l_blocking_ast != NULL)
1539                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1540                                              LDLM_CB_BLOCKING);
1541         } else {
1542                 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1543                        lock);
1544         }
1545
1546         LDLM_DEBUG(lock, "client blocking callback handler END");
1547         LDLM_LOCK_RELEASE(lock);
1548         EXIT;
1549 }
1550
1551 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1552                                     struct ldlm_namespace *ns,
1553                                     struct ldlm_request *dlm_req,
1554                                     struct ldlm_lock *lock)
1555 {
1556         int lvb_len;
1557         CFS_LIST_HEAD(ast_list);
1558         ENTRY;
1559
1560         LDLM_DEBUG(lock, "client completion callback handler START");
1561
1562         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1563                 int to = cfs_time_seconds(1);
1564                 while (to > 0) {
1565                         cfs_schedule_timeout_and_set_state(
1566                                 CFS_TASK_INTERRUPTIBLE, to);
1567                         if (lock->l_granted_mode == lock->l_req_mode ||
1568                             lock->l_destroyed)
1569                                 break;
1570                 }
1571         }
1572
1573         lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1574         if (lvb_len > 0) {
1575                 if (lock->l_lvb_len > 0) {
1576                         /* for extent lock, lvb contains ost_lvb{}. */
1577                         LASSERT(lock->l_lvb_data != NULL);
1578                         LASSERTF(lock->l_lvb_len == lvb_len,
1579                                 "preallocated %d, actual %d.\n",
1580                                 lock->l_lvb_len, lvb_len);
1581                 } else { /* for layout lock, lvb has variable length */
1582                         void *lvb_data;
1583
1584                         OBD_ALLOC(lvb_data, lvb_len);
1585                         if (lvb_data == NULL)
1586                                 LDLM_ERROR(lock, "no memory.\n");
1587
1588                         lock_res_and_lock(lock);
1589                         if (lvb_data == NULL) {
1590                                 lock->l_flags |= LDLM_FL_FAILED;
1591                         } else {
1592                                 LASSERT(lock->l_lvb_data == NULL);
1593                                 lock->l_lvb_data = lvb_data;
1594                                 lock->l_lvb_len = lvb_len;
1595                         }
1596                         unlock_res_and_lock(lock);
1597                 }
1598         }
1599
1600         lock_res_and_lock(lock);
1601         if (lock->l_destroyed ||
1602             lock->l_granted_mode == lock->l_req_mode) {
1603                 /* bug 11300: the lock has already been granted */
1604                 unlock_res_and_lock(lock);
1605                 LDLM_DEBUG(lock, "Double grant race happened");
1606                 LDLM_LOCK_RELEASE(lock);
1607                 EXIT;
1608                 return;
1609         }
1610
1611         /* If we receive the completion AST before the actual enqueue returned,
1612          * then we might need to switch lock modes, resources, or extents. */
1613         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1614                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1615                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1616         }
1617
1618         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1619                 ldlm_convert_policy_to_local(req->rq_export,
1620                                           dlm_req->lock_desc.l_resource.lr_type,
1621                                           &dlm_req->lock_desc.l_policy_data,
1622                                           &lock->l_policy_data);
1623                 LDLM_DEBUG(lock, "completion AST, new policy data");
1624         }
1625
1626         ldlm_resource_unlink_lock(lock);
1627         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1628                    &lock->l_resource->lr_name,
1629                    sizeof(lock->l_resource->lr_name)) != 0) {
1630                 unlock_res_and_lock(lock);
1631                 if (ldlm_lock_change_resource(ns, lock,
1632                                 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1633                         LDLM_ERROR(lock, "Failed to allocate resource");
1634                         LDLM_LOCK_RELEASE(lock);
1635                         EXIT;
1636                         return;
1637                 }
1638                 LDLM_DEBUG(lock, "completion AST, new resource");
1639                 CERROR("change resource!\n");
1640                 lock_res_and_lock(lock);
1641         }
1642
1643         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1644                 /* BL_AST locks are not needed in lru.
1645                  * let ldlm_cancel_lru() be fast. */
1646                 ldlm_lock_remove_from_lru(lock);
1647                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1648                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1649         }
1650
1651         if (lock->l_lvb_len) {
1652                 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1653                                          RCL_CLIENT) < lock->l_lvb_len) {
1654                         LDLM_ERROR(lock, "completion AST did not contain "
1655                                    "expected LVB!");
1656                 } else {
1657                         void *lvb = req_capsule_client_get(&req->rq_pill,
1658                                                            &RMF_DLM_LVB);
1659                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1660                 }
1661         }
1662
1663         ldlm_grant_lock(lock, &ast_list);
1664         unlock_res_and_lock(lock);
1665
1666         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1667
1668         /* Let Enqueue to call osc_lock_upcall() and initialize
1669          * l_ast_data */
1670         OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1671
1672         ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1673
1674         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1675                           lock);
1676         LDLM_LOCK_RELEASE(lock);
1677         EXIT;
1678 }
1679
1680 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1681                                     struct ldlm_namespace *ns,
1682                                     struct ldlm_request *dlm_req,
1683                                     struct ldlm_lock *lock)
1684 {
1685         int rc = -ENOSYS;
1686         ENTRY;
1687
1688         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1689
1690         if (lock->l_glimpse_ast != NULL)
1691                 rc = lock->l_glimpse_ast(lock, req);
1692
1693         if (req->rq_repmsg != NULL) {
1694                 ptlrpc_reply(req);
1695         } else {
1696                 req->rq_status = rc;
1697                 ptlrpc_error(req);
1698         }
1699
1700         lock_res_and_lock(lock);
1701         if (lock->l_granted_mode == LCK_PW &&
1702             !lock->l_readers && !lock->l_writers &&
1703             cfs_time_after(cfs_time_current(),
1704                            cfs_time_add(lock->l_last_used,
1705                                         cfs_time_seconds(10)))) {
1706                 unlock_res_and_lock(lock);
1707                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1708                         ldlm_handle_bl_callback(ns, NULL, lock);
1709
1710                 EXIT;
1711                 return;
1712         }
1713         unlock_res_and_lock(lock);
1714         LDLM_LOCK_RELEASE(lock);
1715         EXIT;
1716 }
1717
1718 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1719 {
1720         if (req->rq_no_reply)
1721                 return 0;
1722
1723         req->rq_status = rc;
1724         if (!req->rq_packed_final) {
1725                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1726                 if (rc)
1727                         return rc;
1728         }
1729         return ptlrpc_reply(req);
1730 }
1731
1732 #ifdef __KERNEL__
1733 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1734 {
1735         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1736         ENTRY;
1737
1738         cfs_spin_lock(&blp->blp_lock);
1739         if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1740                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1741                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1742         } else {
1743                 /* other blocking callbacks are added to the regular list */
1744                 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1745         }
1746         cfs_spin_unlock(&blp->blp_lock);
1747
1748         cfs_waitq_signal(&blp->blp_waitq);
1749
1750         /* can not use blwi->blwi_mode as blwi could be already freed in
1751            LDLM_ASYNC mode */
1752         if (mode == LDLM_SYNC)
1753                 cfs_wait_for_completion(&blwi->blwi_comp);
1754
1755         RETURN(0);
1756 }
1757
1758 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1759                              struct ldlm_namespace *ns,
1760                              struct ldlm_lock_desc *ld,
1761                              cfs_list_t *cancels, int count,
1762                              struct ldlm_lock *lock,
1763                              int mode)
1764 {
1765         cfs_init_completion(&blwi->blwi_comp);
1766         CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1767
1768         if (cfs_memory_pressure_get())
1769                 blwi->blwi_mem_pressure = 1;
1770
1771         blwi->blwi_ns = ns;
1772         blwi->blwi_mode = mode;
1773         if (ld != NULL)
1774                 blwi->blwi_ld = *ld;
1775         if (count) {
1776                 cfs_list_add(&blwi->blwi_head, cancels);
1777                 cfs_list_del_init(cancels);
1778                 blwi->blwi_count = count;
1779         } else {
1780                 blwi->blwi_lock = lock;
1781         }
1782 }
1783
1784 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1785                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1786                              cfs_list_t *cancels, int count, int mode)
1787 {
1788         ENTRY;
1789
1790         if (cancels && count == 0)
1791                 RETURN(0);
1792
1793         if (mode == LDLM_SYNC) {
1794                 /* if it is synchronous call do minimum mem alloc, as it could
1795                  * be triggered from kernel shrinker
1796                  */
1797                 struct ldlm_bl_work_item blwi;
1798                 memset(&blwi, 0, sizeof(blwi));
1799                 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1800                 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1801         } else {
1802                 struct ldlm_bl_work_item *blwi;
1803                 OBD_ALLOC(blwi, sizeof(*blwi));
1804                 if (blwi == NULL)
1805                         RETURN(-ENOMEM);
1806                 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1807
1808                 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1809         }
1810 }
1811
1812 #endif
1813
1814 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1815                            struct ldlm_lock *lock)
1816 {
1817 #ifdef __KERNEL__
1818         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1819 #else
1820         RETURN(-ENOSYS);
1821 #endif
1822 }
1823
1824 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1825                            cfs_list_t *cancels, int count, int mode)
1826 {
1827 #ifdef __KERNEL__
1828         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1829 #else
1830         RETURN(-ENOSYS);
1831 #endif
1832 }
1833
1834 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1835 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1836 {
1837         struct obd_device *obd = req->rq_export->exp_obd;
1838         char *key;
1839         void *val;
1840         int keylen, vallen;
1841         int rc = -ENOSYS;
1842         ENTRY;
1843
1844         DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1845
1846         req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1847
1848         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1849         if (key == NULL) {
1850                 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1851                 RETURN(-EFAULT);
1852         }
1853         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1854                                       RCL_CLIENT);
1855         val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1856         if (val == NULL) {
1857                 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1858                 RETURN(-EFAULT);
1859         }
1860         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1861                                       RCL_CLIENT);
1862
1863         /* We are responsible for swabbing contents of val */
1864
1865         if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1866                 /* Pass it on to mdc (the "export" in this case) */
1867                 rc = obd_set_info_async(req->rq_svc_thread->t_env,
1868                                         req->rq_export,
1869                                         sizeof(KEY_HSM_COPYTOOL_SEND),
1870                                         KEY_HSM_COPYTOOL_SEND,
1871                                         vallen, val, NULL);
1872         else
1873                 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1874
1875         return rc;
1876 }
1877
1878 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1879                                         const char *msg, int rc,
1880                                         struct lustre_handle *handle)
1881 {
1882         DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1883                   "%s: [nid %s] [rc %d] [lock "LPX64"]",
1884                   msg, libcfs_id2str(req->rq_peer), rc,
1885                   handle ? handle->cookie : 0);
1886         if (req->rq_no_reply)
1887                 CWARN("No reply was sent, maybe cause bug 21636.\n");
1888         else if (rc)
1889                 CWARN("Send reply failed, maybe cause bug 21636.\n");
1890 }
1891
1892 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1893 static int ldlm_callback_handler(struct ptlrpc_request *req)
1894 {
1895         struct ldlm_namespace *ns;
1896         struct ldlm_request *dlm_req;
1897         struct ldlm_lock *lock;
1898         int rc;
1899         ENTRY;
1900
1901         /* Requests arrive in sender's byte order.  The ptlrpc service
1902          * handler has already checked and, if necessary, byte-swapped the
1903          * incoming request message body, but I am responsible for the
1904          * message buffers. */
1905
1906         /* do nothing for sec context finalize */
1907         if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1908                 RETURN(0);
1909
1910         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1911
1912         if (req->rq_export == NULL) {
1913                 rc = ldlm_callback_reply(req, -ENOTCONN);
1914                 ldlm_callback_errmsg(req, "Operate on unconnected server",
1915                                      rc, NULL);
1916                 RETURN(0);
1917         }
1918
1919         LASSERT(req->rq_export != NULL);
1920         LASSERT(req->rq_export->exp_obd != NULL);
1921
1922         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1923         case LDLM_BL_CALLBACK:
1924                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1925                         RETURN(0);
1926                 break;
1927         case LDLM_CP_CALLBACK:
1928                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1929                         RETURN(0);
1930                 break;
1931         case LDLM_GL_CALLBACK:
1932                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1933                         RETURN(0);
1934                 break;
1935         case LDLM_SET_INFO:
1936                 rc = ldlm_handle_setinfo(req);
1937                 ldlm_callback_reply(req, rc);
1938                 RETURN(0);
1939         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1940                 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1941                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1942                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1943                         RETURN(0);
1944                 rc = llog_origin_handle_cancel(req);
1945                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1946                         RETURN(0);
1947                 ldlm_callback_reply(req, rc);
1948                 RETURN(0);
1949         case OBD_QC_CALLBACK:
1950                 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1951                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1952                         RETURN(0);
1953                 rc = target_handle_qc_callback(req);
1954                 ldlm_callback_reply(req, rc);
1955                 RETURN(0);
1956         case QUOTA_DQACQ:
1957         case QUOTA_DQREL:
1958                 /* reply in handler */
1959                 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1960                 rc = target_handle_dqacq_callback(req);
1961                 RETURN(0);
1962         case LLOG_ORIGIN_HANDLE_CREATE:
1963                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1964                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1965                         RETURN(0);
1966                 rc = llog_origin_handle_create(req);
1967                 ldlm_callback_reply(req, rc);
1968                 RETURN(0);
1969         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1970                 req_capsule_set(&req->rq_pill,
1971                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1972                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1973                         RETURN(0);
1974                 rc = llog_origin_handle_next_block(req);
1975                 ldlm_callback_reply(req, rc);
1976                 RETURN(0);
1977         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1978                 req_capsule_set(&req->rq_pill,
1979                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1980                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1981                         RETURN(0);
1982                 rc = llog_origin_handle_read_header(req);
1983                 ldlm_callback_reply(req, rc);
1984                 RETURN(0);
1985         case LLOG_ORIGIN_HANDLE_CLOSE:
1986                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1987                         RETURN(0);
1988                 rc = llog_origin_handle_close(req);
1989                 ldlm_callback_reply(req, rc);
1990                 RETURN(0);
1991         default:
1992                 CERROR("unknown opcode %u\n",
1993                        lustre_msg_get_opc(req->rq_reqmsg));
1994                 ldlm_callback_reply(req, -EPROTO);
1995                 RETURN(0);
1996         }
1997
1998         ns = req->rq_export->exp_obd->obd_namespace;
1999         LASSERT(ns != NULL);
2000
2001         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2002
2003         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2004         if (dlm_req == NULL) {
2005                 rc = ldlm_callback_reply(req, -EPROTO);
2006                 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2007                                      NULL);
2008                 RETURN(0);
2009         }
2010
2011         /* Force a known safe race, send a cancel to the server for a lock
2012          * which the server has already started a blocking callback on. */
2013         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2014             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2015                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
2016                 if (rc < 0)
2017                         CERROR("ldlm_cli_cancel: %d\n", rc);
2018         }
2019
2020         lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2021         if (!lock) {
2022                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2023                        "disappeared\n", dlm_req->lock_handle[0].cookie);
2024                 rc = ldlm_callback_reply(req, -EINVAL);
2025                 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2026                                      &dlm_req->lock_handle[0]);
2027                 RETURN(0);
2028         }
2029
2030         if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2031             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2032                 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2033
2034         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2035         lock_res_and_lock(lock);
2036         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
2037         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2038                 /* If somebody cancels lock and cache is already dropped,
2039                  * or lock is failed before cp_ast received on client,
2040                  * we can tell the server we have no lock. Otherwise, we
2041                  * should send cancel after dropping the cache. */
2042                 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2043                     (lock->l_flags & LDLM_FL_BL_DONE)) ||
2044                     (lock->l_flags & LDLM_FL_FAILED)) {
2045                         LDLM_DEBUG(lock, "callback on lock "
2046                                    LPX64" - lock disappeared\n",
2047                                    dlm_req->lock_handle[0].cookie);
2048                         unlock_res_and_lock(lock);
2049                         LDLM_LOCK_RELEASE(lock);
2050                         rc = ldlm_callback_reply(req, -EINVAL);
2051                         ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2052                                              &dlm_req->lock_handle[0]);
2053                         RETURN(0);
2054                 }
2055                 /* BL_AST locks are not needed in lru.
2056                  * let ldlm_cancel_lru() be fast. */
2057                 ldlm_lock_remove_from_lru(lock);
2058                 lock->l_flags |= LDLM_FL_BL_AST;
2059         }
2060         unlock_res_and_lock(lock);
2061
2062         /* We want the ost thread to get this reply so that it can respond
2063          * to ost requests (write cache writeback) that might be triggered
2064          * in the callback.
2065          *
2066          * But we'd also like to be able to indicate in the reply that we're
2067          * cancelling right now, because it's unused, or have an intent result
2068          * in the reply, so we might have to push the responsibility for sending
2069          * the reply down into the AST handlers, alas. */
2070
2071         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2072         case LDLM_BL_CALLBACK:
2073                 CDEBUG(D_INODE, "blocking ast\n");
2074                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2075                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2076                         rc = ldlm_callback_reply(req, 0);
2077                         if (req->rq_no_reply || rc)
2078                                 ldlm_callback_errmsg(req, "Normal process", rc,
2079                                                      &dlm_req->lock_handle[0]);
2080                 }
2081                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2082                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2083                 break;
2084         case LDLM_CP_CALLBACK:
2085                 CDEBUG(D_INODE, "completion ast\n");
2086                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2087                 ldlm_callback_reply(req, 0);
2088                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2089                 break;
2090         case LDLM_GL_CALLBACK:
2091                 CDEBUG(D_INODE, "glimpse ast\n");
2092                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2093                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2094                 break;
2095         default:
2096                 LBUG();                         /* checked above */
2097         }
2098
2099         RETURN(0);
2100 }
2101
2102 #ifdef HAVE_SERVER_SUPPORT
2103 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2104 {
2105         int rc;
2106         ENTRY;
2107
2108         /* Requests arrive in sender's byte order.  The ptlrpc service
2109          * handler has already checked and, if necessary, byte-swapped the
2110          * incoming request message body, but I am responsible for the
2111          * message buffers. */
2112
2113         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2114
2115         if (req->rq_export == NULL) {
2116                 struct ldlm_request *dlm_req;
2117
2118                 CERROR("%s from %s arrived at %lu with bad export cookie "
2119                        LPU64"\n",
2120                        ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2121                        libcfs_nid2str(req->rq_peer.nid),
2122                        req->rq_arrival_time.tv_sec,
2123                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2124
2125                 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2126                         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2127                         dlm_req = req_capsule_client_get(&req->rq_pill,
2128                                                          &RMF_DLM_REQ);
2129                         if (dlm_req != NULL)
2130                                 ldlm_lock_dump_handle(D_ERROR,
2131                                                       &dlm_req->lock_handle[0]);
2132                 }
2133                 ldlm_callback_reply(req, -ENOTCONN);
2134                 RETURN(0);
2135         }
2136
2137         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2138
2139         /* XXX FIXME move this back to mds/handler.c, bug 249 */
2140         case LDLM_CANCEL:
2141                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2142                 CDEBUG(D_INODE, "cancel\n");
2143                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2144                         RETURN(0);
2145                 rc = ldlm_handle_cancel(req);
2146                 if (rc)
2147                         break;
2148                 RETURN(0);
2149         case OBD_LOG_CANCEL:
2150                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2151                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2152                         RETURN(0);
2153                 rc = llog_origin_handle_cancel(req);
2154                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2155                         RETURN(0);
2156                 ldlm_callback_reply(req, rc);
2157                 RETURN(0);
2158         default:
2159                 CERROR("invalid opcode %d\n",
2160                        lustre_msg_get_opc(req->rq_reqmsg));
2161                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2162                 ldlm_callback_reply(req, -EINVAL);
2163         }
2164
2165         RETURN(0);
2166 }
2167
2168 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2169                                         struct ldlm_lock *lock)
2170 {
2171         struct ldlm_request *dlm_req;
2172         struct lustre_handle lockh;
2173         int rc = 0;
2174         int i;
2175         ENTRY;
2176
2177         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2178         if (dlm_req == NULL)
2179                 RETURN(0);
2180
2181         ldlm_lock2handle(lock, &lockh);
2182         for (i = 0; i < dlm_req->lock_count; i++) {
2183                 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2184                                         &lockh)) {
2185                         DEBUG_REQ(D_RPCTRACE, req,
2186                                   "Prio raised by lock "LPX64".", lockh.cookie);
2187
2188                         rc = 1;
2189                         break;
2190                 }
2191         }
2192
2193         RETURN(rc);
2194
2195 }
2196
2197 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2198 {
2199         struct ldlm_request *dlm_req;
2200         int rc = 0;
2201         int i;
2202         ENTRY;
2203
2204         /* no prolong in recovery */
2205         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2206                 RETURN(0);
2207
2208         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2209         if (dlm_req == NULL)
2210                 RETURN(-EFAULT);
2211
2212         for (i = 0; i < dlm_req->lock_count; i++) {
2213                 struct ldlm_lock *lock;
2214
2215                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2216                 if (lock == NULL)
2217                         continue;
2218
2219                 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2220                 if (rc)
2221                         LDLM_DEBUG(lock, "hpreq cancel lock");
2222                 LDLM_LOCK_PUT(lock);
2223
2224                 if (rc)
2225                         break;
2226         }
2227
2228         RETURN(rc);
2229 }
2230
2231 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2232         .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2233         .hpreq_check      = ldlm_cancel_hpreq_check,
2234         .hpreq_fini       = NULL,
2235 };
2236
2237 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2238 {
2239         ENTRY;
2240
2241         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2242
2243         if (req->rq_export == NULL)
2244                 RETURN(0);
2245
2246         if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2247                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2248                 req->rq_ops = &ldlm_cancel_hpreq_ops;
2249         }
2250         RETURN(0);
2251 }
2252
2253 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2254                         cfs_hlist_node_t *hnode, void *data)
2255
2256 {
2257         cfs_list_t         *rpc_list = data;
2258         struct ldlm_lock   *lock = cfs_hash_object(hs, hnode);
2259
2260         lock_res_and_lock(lock);
2261
2262         if (lock->l_req_mode != lock->l_granted_mode) {
2263                 unlock_res_and_lock(lock);
2264                 return 0;
2265         }
2266
2267         LASSERT(lock->l_resource);
2268         if (lock->l_resource->lr_type != LDLM_IBITS &&
2269             lock->l_resource->lr_type != LDLM_PLAIN) {
2270                 unlock_res_and_lock(lock);
2271                 return 0;
2272         }
2273
2274         if (lock->l_flags & LDLM_FL_AST_SENT) {
2275                 unlock_res_and_lock(lock);
2276                 return 0;
2277         }
2278
2279         LASSERT(lock->l_blocking_ast);
2280         LASSERT(!lock->l_blocking_lock);
2281
2282         lock->l_flags |= LDLM_FL_AST_SENT;
2283         if (lock->l_export && lock->l_export->exp_lock_hash) {
2284                 /* NB: it's safe to call cfs_hash_del() even lock isn't
2285                  * in exp_lock_hash. */
2286                 cfs_hash_del(lock->l_export->exp_lock_hash,
2287                              &lock->l_remote_handle, &lock->l_exp_hash);
2288         }
2289
2290         cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2291         LDLM_LOCK_GET(lock);
2292
2293         unlock_res_and_lock(lock);
2294         return 0;
2295 }
2296
2297 void ldlm_revoke_export_locks(struct obd_export *exp)
2298 {
2299         cfs_list_t  rpc_list;
2300         ENTRY;
2301
2302         CFS_INIT_LIST_HEAD(&rpc_list);
2303         cfs_hash_for_each_empty(exp->exp_lock_hash,
2304                                 ldlm_revoke_lock_cb, &rpc_list);
2305         ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2306                           LDLM_WORK_REVOKE_AST);
2307
2308         EXIT;
2309 }
2310 #endif /* HAVE_SERVER_SUPPORT */
2311
2312 #ifdef __KERNEL__
2313 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2314 {
2315         struct ldlm_bl_work_item *blwi = NULL;
2316         static unsigned int num_bl = 0;
2317
2318         cfs_spin_lock(&blp->blp_lock);
2319         /* process a request from the blp_list at least every blp_num_threads */
2320         if (!cfs_list_empty(&blp->blp_list) &&
2321             (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2322                 blwi = cfs_list_entry(blp->blp_list.next,
2323                                       struct ldlm_bl_work_item, blwi_entry);
2324         else
2325                 if (!cfs_list_empty(&blp->blp_prio_list))
2326                         blwi = cfs_list_entry(blp->blp_prio_list.next,
2327                                               struct ldlm_bl_work_item,
2328                                               blwi_entry);
2329
2330         if (blwi) {
2331                 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2332                         num_bl = 0;
2333                 cfs_list_del(&blwi->blwi_entry);
2334         }
2335         cfs_spin_unlock(&blp->blp_lock);
2336
2337         return blwi;
2338 }
2339
2340 /* This only contains temporary data until the thread starts */
2341 struct ldlm_bl_thread_data {
2342         char                    bltd_name[CFS_CURPROC_COMM_MAX];
2343         struct ldlm_bl_pool     *bltd_blp;
2344         cfs_completion_t        bltd_comp;
2345         int                     bltd_num;
2346 };
2347
2348 static int ldlm_bl_thread_main(void *arg);
2349
2350 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2351 {
2352         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2353         int rc;
2354
2355         cfs_init_completion(&bltd.bltd_comp);
2356         rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2357         if (rc < 0) {
2358                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2359                        cfs_atomic_read(&blp->blp_num_threads), rc);
2360                 return rc;
2361         }
2362         cfs_wait_for_completion(&bltd.bltd_comp);
2363
2364         return 0;
2365 }
2366
2367 static int ldlm_bl_thread_main(void *arg)
2368 {
2369         struct ldlm_bl_pool *blp;
2370         ENTRY;
2371
2372         {
2373                 struct ldlm_bl_thread_data *bltd = arg;
2374
2375                 blp = bltd->bltd_blp;
2376
2377                 bltd->bltd_num =
2378                         cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2379                 cfs_atomic_inc(&blp->blp_busy_threads);
2380
2381                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2382                         "ldlm_bl_%02d", bltd->bltd_num);
2383                 cfs_daemonize(bltd->bltd_name);
2384
2385                 cfs_complete(&bltd->bltd_comp);
2386                 /* cannot use bltd after this, it is only on caller's stack */
2387         }
2388
2389         while (1) {
2390                 struct l_wait_info lwi = { 0 };
2391                 struct ldlm_bl_work_item *blwi = NULL;
2392                 int busy;
2393
2394                 blwi = ldlm_bl_get_work(blp);
2395
2396                 if (blwi == NULL) {
2397                         cfs_atomic_dec(&blp->blp_busy_threads);
2398                         l_wait_event_exclusive(blp->blp_waitq,
2399                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
2400                                          &lwi);
2401                         busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2402                 } else {
2403                         busy = cfs_atomic_read(&blp->blp_busy_threads);
2404                 }
2405
2406                 if (blwi->blwi_ns == NULL)
2407                         /* added by ldlm_cleanup() */
2408                         break;
2409
2410                 /* Not fatal if racy and have a few too many threads */
2411                 if (unlikely(busy < blp->blp_max_threads &&
2412                              busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2413                              !blwi->blwi_mem_pressure))
2414                         /* discard the return value, we tried */
2415                         ldlm_bl_thread_start(blp);
2416
2417                 if (blwi->blwi_mem_pressure)
2418                         cfs_memory_pressure_set();
2419
2420                 if (blwi->blwi_count) {
2421                         int count;
2422                         /* The special case when we cancel locks in lru
2423                          * asynchronously, we pass the list of locks here.
2424                          * Thus locks are marked LDLM_FL_CANCELING, but NOT
2425                          * canceled locally yet. */
2426                         count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2427                                                            blwi->blwi_count,
2428                                                            LCF_BL_AST);
2429                         ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2430                 } else {
2431                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2432                                                 blwi->blwi_lock);
2433                 }
2434                 if (blwi->blwi_mem_pressure)
2435                         cfs_memory_pressure_clr();
2436
2437                 if (blwi->blwi_mode == LDLM_ASYNC)
2438                         OBD_FREE(blwi, sizeof(*blwi));
2439                 else
2440                         cfs_complete(&blwi->blwi_comp);
2441         }
2442
2443         cfs_atomic_dec(&blp->blp_busy_threads);
2444         cfs_atomic_dec(&blp->blp_num_threads);
2445         cfs_complete(&blp->blp_comp);
2446         RETURN(0);
2447 }
2448
2449 #endif
2450
2451 static int ldlm_setup(void);
2452 static int ldlm_cleanup(void);
2453
2454 int ldlm_get_ref(void)
2455 {
2456         int rc = 0;
2457         ENTRY;
2458         cfs_mutex_lock(&ldlm_ref_mutex);
2459         if (++ldlm_refcount == 1) {
2460                 rc = ldlm_setup();
2461                 if (rc)
2462                         ldlm_refcount--;
2463         }
2464         cfs_mutex_unlock(&ldlm_ref_mutex);
2465
2466         RETURN(rc);
2467 }
2468
2469 void ldlm_put_ref(void)
2470 {
2471         ENTRY;
2472         cfs_mutex_lock(&ldlm_ref_mutex);
2473         if (ldlm_refcount == 1) {
2474                 int rc = ldlm_cleanup();
2475                 if (rc)
2476                         CERROR("ldlm_cleanup failed: %d\n", rc);
2477                 else
2478                         ldlm_refcount--;
2479         } else {
2480                 ldlm_refcount--;
2481         }
2482         cfs_mutex_unlock(&ldlm_ref_mutex);
2483
2484         EXIT;
2485 }
2486
2487 /*
2488  * Export handle<->lock hash operations.
2489  */
2490 static unsigned
2491 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2492 {
2493         return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2494 }
2495
2496 static void *
2497 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2498 {
2499         struct ldlm_lock *lock;
2500
2501         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2502         return &lock->l_remote_handle;
2503 }
2504
2505 static void
2506 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2507 {
2508         struct ldlm_lock     *lock;
2509
2510         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2511         lock->l_remote_handle = *(struct lustre_handle *)key;
2512 }
2513
2514 static int
2515 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2516 {
2517         return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2518 }
2519
2520 static void *
2521 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2522 {
2523         return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2524 }
2525
2526 static void
2527 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2528 {
2529         struct ldlm_lock *lock;
2530
2531         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2532         LDLM_LOCK_GET(lock);
2533 }
2534
2535 static void
2536 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2537 {
2538         struct ldlm_lock *lock;
2539
2540         lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2541         LDLM_LOCK_RELEASE(lock);
2542 }
2543
2544 static cfs_hash_ops_t ldlm_export_lock_ops = {
2545         .hs_hash        = ldlm_export_lock_hash,
2546         .hs_key         = ldlm_export_lock_key,
2547         .hs_keycmp      = ldlm_export_lock_keycmp,
2548         .hs_keycpy      = ldlm_export_lock_keycpy,
2549         .hs_object      = ldlm_export_lock_object,
2550         .hs_get         = ldlm_export_lock_get,
2551         .hs_put         = ldlm_export_lock_put,
2552         .hs_put_locked  = ldlm_export_lock_put,
2553 };
2554
2555 int ldlm_init_export(struct obd_export *exp)
2556 {
2557         ENTRY;
2558
2559         exp->exp_lock_hash =
2560                 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2561                                 HASH_EXP_LOCK_CUR_BITS,
2562                                 HASH_EXP_LOCK_MAX_BITS,
2563                                 HASH_EXP_LOCK_BKT_BITS, 0,
2564                                 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2565                                 &ldlm_export_lock_ops,
2566                                 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2567                                 CFS_HASH_NBLK_CHANGE);
2568
2569         if (!exp->exp_lock_hash)
2570                 RETURN(-ENOMEM);
2571
2572         RETURN(0);
2573 }
2574 EXPORT_SYMBOL(ldlm_init_export);
2575
2576 void ldlm_destroy_export(struct obd_export *exp)
2577 {
2578         ENTRY;
2579         cfs_hash_putref(exp->exp_lock_hash);
2580         exp->exp_lock_hash = NULL;
2581
2582         ldlm_destroy_flock_export(exp);
2583         EXIT;
2584 }
2585 EXPORT_SYMBOL(ldlm_destroy_export);
2586
2587 static int ldlm_setup(void)
2588 {
2589         static struct ptlrpc_service_conf       conf;
2590         struct ldlm_bl_pool                     *blp = NULL;
2591         int rc = 0;
2592 #ifdef __KERNEL__
2593         int i;
2594 #endif
2595         ENTRY;
2596
2597         if (ldlm_state != NULL)
2598                 RETURN(-EALREADY);
2599
2600         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2601         if (ldlm_state == NULL)
2602                 RETURN(-ENOMEM);
2603
2604 #ifdef LPROCFS
2605         rc = ldlm_proc_setup();
2606         if (rc != 0)
2607                 GOTO(out, rc);
2608 #endif
2609
2610         memset(&conf, 0, sizeof(conf));
2611         conf = (typeof(conf)) {
2612                 .psc_name               = "ldlm_cbd",
2613                 .psc_watchdog_factor    = 2,
2614                 .psc_buf                = {
2615                         .bc_nbufs               = LDLM_NBUFS,
2616                         .bc_buf_size            = LDLM_BUFSIZE,
2617                         .bc_req_max_size        = LDLM_MAXREQSIZE,
2618                         .bc_rep_max_size        = LDLM_MAXREPSIZE,
2619                         .bc_req_portal          = LDLM_CB_REQUEST_PORTAL,
2620                         .bc_rep_portal          = LDLM_CB_REPLY_PORTAL,
2621                 },
2622                 .psc_thr                = {
2623                         .tc_thr_name            = "ldlm_cb",
2624                         .tc_thr_factor          = LDLM_THR_FACTOR,
2625                         .tc_nthrs_init          = LDLM_NTHRS_INIT,
2626                         .tc_nthrs_base          = LDLM_NTHRS_BASE,
2627                         .tc_nthrs_max           = LDLM_NTHRS_MAX,
2628                         .tc_nthrs_user          = ldlm_num_threads,
2629                         .tc_cpu_affinity        = 1,
2630                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD,
2631                 },
2632                 .psc_cpt                = {
2633                         .cc_pattern             = ldlm_cpts,
2634                 },
2635                 .psc_ops                = {
2636                         .so_req_handler         = ldlm_callback_handler,
2637                 },
2638         };
2639         ldlm_state->ldlm_cb_service = \
2640                         ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2641         if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2642                 CERROR("failed to start service\n");
2643                 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2644                 ldlm_state->ldlm_cb_service = NULL;
2645                 GOTO(out, rc);
2646         }
2647
2648 #ifdef HAVE_SERVER_SUPPORT
2649         memset(&conf, 0, sizeof(conf));
2650         conf = (typeof(conf)) {
2651                 .psc_name               = "ldlm_canceld",
2652                 .psc_watchdog_factor    = 6,
2653                 .psc_buf                = {
2654                         .bc_nbufs               = LDLM_NBUFS,
2655                         .bc_buf_size            = LDLM_BUFSIZE,
2656                         .bc_req_max_size        = LDLM_MAXREQSIZE,
2657                         .bc_rep_max_size        = LDLM_MAXREPSIZE,
2658                         .bc_req_portal          = LDLM_CANCEL_REQUEST_PORTAL,
2659                         .bc_rep_portal          = LDLM_CANCEL_REPLY_PORTAL,
2660
2661                 },
2662                 .psc_thr                = {
2663                         .tc_thr_name            = "ldlm_cn",
2664                         .tc_thr_factor          = LDLM_THR_FACTOR,
2665                         .tc_nthrs_init          = LDLM_NTHRS_INIT,
2666                         .tc_nthrs_base          = LDLM_NTHRS_BASE,
2667                         .tc_nthrs_max           = LDLM_NTHRS_MAX,
2668                         .tc_nthrs_user          = ldlm_num_threads,
2669                         .tc_cpu_affinity        = 1,
2670                         .tc_ctx_tags            = LCT_MD_THREAD | \
2671                                                   LCT_DT_THREAD | \
2672                                                   LCT_CL_THREAD,
2673                 },
2674                 .psc_cpt                = {
2675                         .cc_pattern             = ldlm_cpts,
2676                 },
2677                 .psc_ops                = {
2678                         .so_req_handler         = ldlm_cancel_handler,
2679                         .so_hpreq_handler       = ldlm_hpreq_handler,
2680                 },
2681         };
2682         ldlm_state->ldlm_cancel_service = \
2683                         ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2684         if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2685                 CERROR("failed to start service\n");
2686                 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2687                 ldlm_state->ldlm_cancel_service = NULL;
2688                 GOTO(out, rc);
2689         }
2690 #endif
2691
2692         OBD_ALLOC(blp, sizeof(*blp));
2693         if (blp == NULL)
2694                 GOTO(out, rc = -ENOMEM);
2695         ldlm_state->ldlm_bl_pool = blp;
2696
2697         cfs_spin_lock_init(&blp->blp_lock);
2698         CFS_INIT_LIST_HEAD(&blp->blp_list);
2699         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2700         cfs_waitq_init(&blp->blp_waitq);
2701         cfs_atomic_set(&blp->blp_num_threads, 0);
2702         cfs_atomic_set(&blp->blp_busy_threads, 0);
2703
2704 #ifdef __KERNEL__
2705         if (ldlm_num_threads == 0) {
2706                 blp->blp_min_threads = LDLM_NTHRS_INIT;
2707                 blp->blp_max_threads = LDLM_NTHRS_MAX;
2708         } else {
2709                 blp->blp_min_threads = blp->blp_max_threads = \
2710                         min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2711                                                          ldlm_num_threads));
2712         }
2713
2714         for (i = 0; i < blp->blp_min_threads; i++) {
2715                 rc = ldlm_bl_thread_start(blp);
2716                 if (rc < 0)
2717                         GOTO(out, rc);
2718         }
2719
2720 # ifdef HAVE_SERVER_SUPPORT
2721         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2722         expired_lock_thread.elt_state = ELT_STOPPED;
2723         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2724
2725         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2726         cfs_spin_lock_init(&waiting_locks_spinlock);
2727         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2728
2729         rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2730         if (rc < 0) {
2731                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2732                 GOTO(out, rc);
2733         }
2734
2735         cfs_wait_event(expired_lock_thread.elt_waitq,
2736                        expired_lock_thread.elt_state == ELT_READY);
2737 # endif /* HAVE_SERVER_SUPPORT */
2738
2739         rc = ldlm_pools_init();
2740         if (rc) {
2741                 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2742                 GOTO(out, rc);
2743         }
2744 #endif
2745         RETURN(0);
2746
2747  out:
2748         ldlm_cleanup();
2749         RETURN(rc);
2750 }
2751
2752 static int ldlm_cleanup(void)
2753 {
2754         ENTRY;
2755
2756         if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2757             !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2758                 CERROR("ldlm still has namespaces; clean these up first.\n");
2759                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2760                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2761                 RETURN(-EBUSY);
2762         }
2763
2764 #ifdef __KERNEL__
2765         ldlm_pools_fini();
2766
2767         if (ldlm_state->ldlm_bl_pool != NULL) {
2768                 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2769
2770                 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2771                         struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2772
2773                         cfs_init_completion(&blp->blp_comp);
2774
2775                         cfs_spin_lock(&blp->blp_lock);
2776                         cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2777                         cfs_waitq_signal(&blp->blp_waitq);
2778                         cfs_spin_unlock(&blp->blp_lock);
2779
2780                         cfs_wait_for_completion(&blp->blp_comp);
2781                 }
2782
2783                 OBD_FREE(blp, sizeof(*blp));
2784         }
2785 #endif /* __KERNEL__ */
2786
2787         if (ldlm_state->ldlm_cb_service != NULL)
2788                 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2789 # ifdef HAVE_SERVER_SUPPORT
2790         if (ldlm_state->ldlm_cancel_service != NULL)
2791                 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2792 # endif
2793
2794 #ifdef __KERNEL__
2795         ldlm_proc_cleanup();
2796
2797 # ifdef HAVE_SERVER_SUPPORT
2798         if (expired_lock_thread.elt_state != ELT_STOPPED) {
2799                 expired_lock_thread.elt_state = ELT_TERMINATE;
2800                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2801                 cfs_wait_event(expired_lock_thread.elt_waitq,
2802                                expired_lock_thread.elt_state == ELT_STOPPED);
2803         }
2804 # endif
2805 #endif /* __KERNEL__ */
2806
2807         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2808         ldlm_state = NULL;
2809
2810         RETURN(0);
2811 }
2812
2813 int ldlm_init(void)
2814 {
2815         cfs_mutex_init(&ldlm_ref_mutex);
2816         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2817         cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2818         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2819                                                sizeof(struct ldlm_resource), 0,
2820                                                CFS_SLAB_HWCACHE_ALIGN);
2821         if (ldlm_resource_slab == NULL)
2822                 return -ENOMEM;
2823
2824         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2825                               sizeof(struct ldlm_lock), 0,
2826                               CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2827         if (ldlm_lock_slab == NULL) {
2828                 cfs_mem_cache_destroy(ldlm_resource_slab);
2829                 return -ENOMEM;
2830         }
2831
2832         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2833                                         sizeof(struct ldlm_interval),
2834                                         0, CFS_SLAB_HWCACHE_ALIGN);
2835         if (ldlm_interval_slab == NULL) {
2836                 cfs_mem_cache_destroy(ldlm_resource_slab);
2837                 cfs_mem_cache_destroy(ldlm_lock_slab);
2838                 return -ENOMEM;
2839         }
2840 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2841         class_export_dump_hook = ldlm_dump_export_locks;
2842 #endif
2843         return 0;
2844 }
2845
2846 void ldlm_exit(void)
2847 {
2848         int rc;
2849         if (ldlm_refcount)
2850                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2851         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2852         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2853 #ifdef __KERNEL__
2854         /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2855          * synchronize_rcu() to wait a grace period elapsed, so that
2856          * ldlm_lock_free() get a chance to be called. */
2857         synchronize_rcu();
2858 #endif
2859         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2860         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2861         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2862         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2863 }
2864
2865 /* ldlm_extent.c */
2866 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2867
2868 /* ldlm_lock.c */
2869 #ifdef HAVE_SERVER_SUPPORT
2870 EXPORT_SYMBOL(ldlm_get_processing_policy);
2871 #endif
2872 EXPORT_SYMBOL(ldlm_lock2desc);
2873 EXPORT_SYMBOL(ldlm_register_intent);
2874 EXPORT_SYMBOL(ldlm_lockname);
2875 EXPORT_SYMBOL(ldlm_typename);
2876 EXPORT_SYMBOL(ldlm_lock2handle);
2877 EXPORT_SYMBOL(__ldlm_handle2lock);
2878 EXPORT_SYMBOL(ldlm_lock_get);
2879 EXPORT_SYMBOL(ldlm_lock_put);
2880 EXPORT_SYMBOL(ldlm_lock_match);
2881 EXPORT_SYMBOL(ldlm_lock_cancel);
2882 EXPORT_SYMBOL(ldlm_lock_addref);
2883 EXPORT_SYMBOL(ldlm_lock_addref_try);
2884 EXPORT_SYMBOL(ldlm_lock_decref);
2885 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2886 EXPORT_SYMBOL(ldlm_lock_change_resource);
2887 EXPORT_SYMBOL(ldlm_it2str);
2888 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2889 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2890 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2891 EXPORT_SYMBOL(ldlm_lock_allow_match);
2892 EXPORT_SYMBOL(ldlm_lock_downgrade);
2893 EXPORT_SYMBOL(ldlm_lock_convert);
2894
2895 /* ldlm_request.c */
2896 EXPORT_SYMBOL(ldlm_completion_ast_async);
2897 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2898 EXPORT_SYMBOL(ldlm_completion_ast);
2899 EXPORT_SYMBOL(ldlm_blocking_ast);
2900 EXPORT_SYMBOL(ldlm_glimpse_ast);
2901 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2902 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2903 EXPORT_SYMBOL(ldlm_prep_elc_req);
2904 EXPORT_SYMBOL(ldlm_cli_convert);
2905 EXPORT_SYMBOL(ldlm_cli_enqueue);
2906 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2907 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2908 EXPORT_SYMBOL(ldlm_cli_cancel);
2909 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2910 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2911 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2912 EXPORT_SYMBOL(ldlm_replay_locks);
2913 EXPORT_SYMBOL(ldlm_resource_foreach);
2914 EXPORT_SYMBOL(ldlm_namespace_foreach);
2915 EXPORT_SYMBOL(ldlm_resource_iterate);
2916 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2917 EXPORT_SYMBOL(ldlm_cli_cancel_list_local);
2918 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2919
2920 /* ldlm_lockd.c */
2921 #ifdef HAVE_SERVER_SUPPORT
2922 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2923 EXPORT_SYMBOL(ldlm_server_completion_ast);
2924 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2925 EXPORT_SYMBOL(ldlm_handle_enqueue);
2926 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2927 EXPORT_SYMBOL(ldlm_handle_cancel);
2928 EXPORT_SYMBOL(ldlm_request_cancel);
2929 EXPORT_SYMBOL(ldlm_handle_convert);
2930 EXPORT_SYMBOL(ldlm_handle_convert0);
2931 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2932 #endif
2933 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2934 EXPORT_SYMBOL(ldlm_get_ref);
2935 EXPORT_SYMBOL(ldlm_put_ref);
2936 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2937
2938 /* ldlm_resource.c */
2939 EXPORT_SYMBOL(ldlm_namespace_new);
2940 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2941 EXPORT_SYMBOL(ldlm_namespace_free);
2942 EXPORT_SYMBOL(ldlm_namespace_dump);
2943 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2944 EXPORT_SYMBOL(ldlm_resource_get);
2945 EXPORT_SYMBOL(ldlm_resource_putref);
2946 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2947
2948 /* ldlm_lib.c */
2949 EXPORT_SYMBOL(client_import_add_conn);
2950 EXPORT_SYMBOL(client_import_del_conn);
2951 EXPORT_SYMBOL(client_obd_setup);
2952 EXPORT_SYMBOL(client_obd_cleanup);
2953 EXPORT_SYMBOL(client_connect_import);
2954 EXPORT_SYMBOL(client_disconnect_export);
2955 EXPORT_SYMBOL(target_send_reply);
2956 EXPORT_SYMBOL(target_pack_pool_reply);
2957
2958 #ifdef HAVE_SERVER_SUPPORT
2959 EXPORT_SYMBOL(server_disconnect_export);
2960 EXPORT_SYMBOL(target_stop_recovery_thread);
2961 EXPORT_SYMBOL(target_handle_connect);
2962 EXPORT_SYMBOL(target_cleanup_recovery);
2963 EXPORT_SYMBOL(target_destroy_export);
2964 EXPORT_SYMBOL(target_cancel_recovery_timer);
2965 EXPORT_SYMBOL(target_queue_recovery_request);
2966 EXPORT_SYMBOL(target_handle_ping);
2967 EXPORT_SYMBOL(target_handle_disconnect);
2968 #endif
2969
2970 /* l_lock.c */
2971 EXPORT_SYMBOL(lock_res_and_lock);
2972 EXPORT_SYMBOL(unlock_res_and_lock);