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