Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_lockd.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45 #define DEBUG_SUBSYSTEM S_LDLM
46
47 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 #else
50 # include <liblustre.h>
51 #endif
52
53 #include <lustre_dlm.h>
54 #include <obd_class.h>
55 #include <libcfs/list.h>
56 #include "ldlm_internal.h"
57
58 #ifdef __KERNEL__
59 static int ldlm_num_threads;
60 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
61                 "number of DLM service threads to start");
62 #endif
63
64 extern cfs_mem_cache_t *ldlm_resource_slab;
65 extern cfs_mem_cache_t *ldlm_lock_slab;
66 static struct semaphore ldlm_ref_sem;
67 static int ldlm_refcount;
68
69 /* LDLM state */
70
71 static struct ldlm_state *ldlm_state;
72
73 inline cfs_time_t round_timeout(cfs_time_t timeout)
74 {
75         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
76 }
77
78 /* timeout for initial callback (AST) reply (bz10399) */
79 static inline unsigned int ldlm_get_rq_timeout(void)
80 {
81         /* Non-AT value */
82         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
83
84         return timeout < 1 ? 1 : timeout;
85 }
86
87 #ifdef __KERNEL__
88 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
89 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
90 static struct list_head waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
92
93 static struct expired_lock_thread {
94         cfs_waitq_t               elt_waitq;
95         int                       elt_state;
96         int                       elt_dump;
97         struct list_head          elt_expired_locks;
98 } expired_lock_thread;
99 #endif
100
101 #define ELT_STOPPED   0
102 #define ELT_READY     1
103 #define ELT_TERMINATE 2
104
105 struct ldlm_bl_pool {
106         spinlock_t              blp_lock;
107
108         /*
109          * blp_prio_list is used for callbacks that should be handled
110          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
111          * see bug 13843
112          */
113         struct list_head        blp_prio_list;
114
115         /*
116          * blp_list is used for all other callbacks which are likely
117          * to take longer to process.
118          */
119         struct list_head        blp_list;
120
121         cfs_waitq_t             blp_waitq;
122         struct completion       blp_comp;
123         atomic_t                blp_num_threads;
124         atomic_t                blp_busy_threads;
125         int                     blp_min_threads;
126         int                     blp_max_threads;
127 };
128
129 struct ldlm_bl_work_item {
130         struct list_head        blwi_entry;
131         struct ldlm_namespace   *blwi_ns;
132         struct ldlm_lock_desc   blwi_ld;
133         struct ldlm_lock        *blwi_lock;
134         struct list_head        blwi_head;
135         int                     blwi_count;
136 };
137
138 #ifdef __KERNEL__
139
140 static inline int have_expired_locks(void)
141 {
142         int need_to_run;
143
144         ENTRY;
145         spin_lock_bh(&waiting_locks_spinlock);
146         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
147         spin_unlock_bh(&waiting_locks_spinlock);
148
149         RETURN(need_to_run);
150 }
151
152 static int expired_lock_main(void *arg)
153 {
154         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
155         struct l_wait_info lwi = { 0 };
156         int do_dump;
157
158         ENTRY;
159         cfs_daemonize("ldlm_elt");
160
161         expired_lock_thread.elt_state = ELT_READY;
162         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
163
164         while (1) {
165                 l_wait_event(expired_lock_thread.elt_waitq,
166                              have_expired_locks() ||
167                              expired_lock_thread.elt_state == ELT_TERMINATE,
168                              &lwi);
169
170                 spin_lock_bh(&waiting_locks_spinlock);
171                 if (expired_lock_thread.elt_dump) {
172                         spin_unlock_bh(&waiting_locks_spinlock);
173
174                         /* from waiting_locks_callback, but not in timer */
175                         libcfs_debug_dumplog();
176                         libcfs_run_lbug_upcall(__FILE__,
177                                                 "waiting_locks_callback",
178                                                 expired_lock_thread.elt_dump);
179
180                         spin_lock_bh(&waiting_locks_spinlock);
181                         expired_lock_thread.elt_dump = 0;
182                 }
183
184                 do_dump = 0;
185
186                 while (!list_empty(expired)) {
187                         struct obd_export *export;
188                         struct ldlm_lock *lock;
189
190                         lock = list_entry(expired->next, struct ldlm_lock,
191                                           l_pending_chain);
192                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
193                             (void *)lock >= LP_POISON) {
194                                 spin_unlock_bh(&waiting_locks_spinlock);
195                                 CERROR("free lock on elt list %p\n", lock);
196                                 LBUG();
197                         }
198                         list_del_init(&lock->l_pending_chain);
199                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
200                             (void *)lock->l_export >= LP_POISON) {
201                                 CERROR("lock with free export on elt list %p\n",
202                                        lock->l_export);
203                                 lock->l_export = NULL;
204                                 LDLM_ERROR(lock, "free export");
205                                 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_nid_stats &&
676             lock->l_export->exp_nid_stats->nid_ldlm_stats)
677                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
678                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
679
680         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
681
682         RETURN(rc);
683 }
684
685 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
686 {
687         struct ldlm_cb_set_arg *arg = data;
688         struct ldlm_request    *body;
689         struct ptlrpc_request  *req;
690         struct timeval          granted_time;
691         long                    total_enqueue_wait;
692         int                     instant_cancel = 0;
693         int                     rc = 0;
694         ENTRY;
695
696         LASSERT(lock != NULL);
697         LASSERT(data != NULL);
698
699         do_gettimeofday(&granted_time);
700         total_enqueue_wait = cfs_timeval_sub(&granted_time,
701                                              &lock->l_enqueued_time, NULL);
702
703         if (total_enqueue_wait / ONE_MILLION > obd_timeout)
704                 /* non-fatal with AT - change to LDLM_DEBUG? */
705                 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
706                            total_enqueue_wait, lock->l_enqueued_time.tv_sec);
707
708          req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
709                                     &RQF_LDLM_CP_CALLBACK);
710          if (req == NULL)
711                  RETURN(-ENOMEM);
712
713         lock_res_and_lock(lock);
714         if (lock->l_resource->lr_lvb_len)
715                  req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
716                                       lock->l_resource->lr_lvb_len);
717         unlock_res_and_lock(lock);
718
719         rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
720         if (rc) {
721                 ptlrpc_request_free(req);
722                 RETURN(rc);
723         }
724
725         req->rq_async_args.pointer_arg[0] = arg;
726         req->rq_async_args.pointer_arg[1] = lock;
727         req->rq_interpret_reply = ldlm_cb_interpret;
728         req->rq_no_resend = 1;
729         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
730
731         body->lock_handle[0] = lock->l_remote_handle;
732         body->lock_flags = flags;
733         ldlm_lock2desc(lock, &body->lock_desc);
734         if (lock->l_resource->lr_lvb_len) {
735                 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
736
737                 lock_res_and_lock(lock);
738                 memcpy(lvb, lock->l_resource->lr_lvb_data,
739                        lock->l_resource->lr_lvb_len);
740                 unlock_res_and_lock(lock);
741         }
742
743         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
744                    total_enqueue_wait);
745
746         /* Server-side enqueue wait time estimate, used in
747             __ldlm_add_waiting_lock to set future enqueue timers */
748         at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
749                total_enqueue_wait / ONE_MILLION);
750
751         ptlrpc_request_set_replen(req);
752
753         req->rq_send_state = LUSTRE_IMP_FULL;
754         /* ptlrpc_prep_req already set timeout */
755         if (AT_OFF)
756                 req->rq_timeout = ldlm_get_rq_timeout();
757
758         /* We only send real blocking ASTs after the lock is granted */
759         lock_res_and_lock(lock);
760         if (lock->l_flags & LDLM_FL_AST_SENT) {
761                 body->lock_flags |= LDLM_FL_AST_SENT;
762
763                 /* We might get here prior to ldlm_handle_enqueue setting
764                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
765                  * into waiting list, but this is safe and similar code in
766                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
767                  * that would not only cancel the lock, but will also remove
768                  * it from waiting list */
769                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
770                         unlock_res_and_lock(lock);
771                         ldlm_lock_cancel(lock);
772                         instant_cancel = 1;
773                         lock_res_and_lock(lock);
774                 } else {
775                         /* start the lock-timeout clock */
776                         ldlm_add_waiting_lock(lock);
777                 }
778         }
779         unlock_res_and_lock(lock);
780
781         if (lock->l_export && lock->l_export->exp_nid_stats &&
782             lock->l_export->exp_nid_stats->nid_ldlm_stats)
783                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
784                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
785
786         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
787
788         RETURN(rc);
789 }
790
791 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
792 {
793         struct ldlm_resource  *res = lock->l_resource;
794         struct ldlm_request   *body;
795         struct ptlrpc_request *req;
796         int                    rc;
797         ENTRY;
798
799         LASSERT(lock != NULL);
800
801         req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
802                                         &RQF_LDLM_GL_CALLBACK,
803                                         LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
804
805         if (req == NULL)
806                 RETURN(-ENOMEM);
807
808         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
809         body->lock_handle[0] = lock->l_remote_handle;
810         ldlm_lock2desc(lock, &body->lock_desc);
811
812         lock_res_and_lock(lock);
813         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
814                              lock->l_resource->lr_lvb_len);
815         unlock_res_and_lock(lock);
816         res = lock->l_resource;
817         ptlrpc_request_set_replen(req);
818
819
820         req->rq_send_state = LUSTRE_IMP_FULL;
821         /* ptlrpc_prep_req already set timeout */
822         if (AT_OFF)
823                 req->rq_timeout = ldlm_get_rq_timeout();
824
825         if (lock->l_export && lock->l_export->exp_nid_stats &&
826             lock->l_export->exp_nid_stats->nid_ldlm_stats)
827                 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
828                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
829
830         rc = ptlrpc_queue_wait(req);
831         if (rc == -ELDLM_NO_LOCK_DATA)
832                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
833         else if (rc != 0)
834                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
835         else
836                 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
837                                           REPLY_REC_OFF, 1);
838         ptlrpc_req_finished(req);
839         if (rc == -ERESTART)
840                 ldlm_reprocess_all(res);
841
842         RETURN(rc);
843 }
844
845 #ifdef __KERNEL__
846 extern unsigned long long lu_time_stamp_get(void);
847 #else
848 #define lu_time_stamp_get() time(NULL)
849 #endif
850
851 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
852                        struct lprocfs_stats *srv_stats)
853 {
854         int lock_type = 0, op = 0;
855
856         lock_type = dlm_req->lock_desc.l_resource.lr_type;
857
858         switch (lock_type) {
859         case LDLM_PLAIN:
860                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
861                 break;
862         case LDLM_EXTENT:
863                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
864                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
865                 else
866                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
867                 break;
868         case LDLM_FLOCK:
869                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
870                 break;
871         case LDLM_IBITS:
872                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
873                 break;
874         default:
875                 op = 0;
876                 break;
877         }
878
879         if (op)
880                 lprocfs_counter_incr(srv_stats, op);
881
882         return ;
883 }
884
885 /*
886  * Main server-side entry point into LDLM. This is called by ptlrpc service
887  * threads to carry out client lock enqueueing requests.
888  */
889 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
890                          struct ptlrpc_request *req,
891                          const struct ldlm_request *dlm_req,
892                          const struct ldlm_callback_suite *cbs)
893 {
894         struct ldlm_reply *dlm_rep;
895         __u32 flags;
896         ldlm_error_t err = ELDLM_OK;
897         struct ldlm_lock *lock = NULL;
898         void *cookie = NULL;
899         int rc = 0;
900         ENTRY;
901
902         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
903
904         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
905         flags = dlm_req->lock_flags;
906
907         LASSERT(req->rq_export);
908
909         if (req->rq_rqbd->rqbd_service->srv_stats)
910                 ldlm_svc_get_eopc(dlm_req,
911                                   req->rq_rqbd->rqbd_service->srv_stats);
912
913         if (req->rq_export && req->rq_export->exp_nid_stats &&
914             req->rq_export->exp_nid_stats->nid_ldlm_stats)
915                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
916                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
917
918         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
919                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
920                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
921                           dlm_req->lock_desc.l_resource.lr_type);
922                 GOTO(out, rc = -EFAULT);
923         }
924
925         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
926                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
927                      dlm_req->lock_desc.l_req_mode &
928                      (dlm_req->lock_desc.l_req_mode-1))) {
929                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
930                           dlm_req->lock_desc.l_req_mode);
931                 GOTO(out, rc = -EFAULT);
932         }
933
934         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
935                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
936                              LDLM_PLAIN)) {
937                         DEBUG_REQ(D_ERROR, req,
938                                   "PLAIN lock request from IBITS client?");
939                         GOTO(out, rc = -EPROTO);
940                 }
941         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
942                             LDLM_IBITS)) {
943                 DEBUG_REQ(D_ERROR, req,
944                           "IBITS lock request from unaware client?");
945                 GOTO(out, rc = -EPROTO);
946         }
947
948 #if 0
949         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
950            against server's _CONNECT_SUPPORTED flags? (I don't want to use
951            ibits for mgc/mgs) */
952
953         /* INODEBITS_INTEROP: Perform conversion from plain lock to
954          * inodebits lock if client does not support them. */
955         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
956             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
957                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
958                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
959                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
960                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
961                         dlm_req->lock_desc.l_req_mode = LCK_CR;
962         }
963 #endif
964
965         if (unlikely(flags & LDLM_FL_REPLAY)) {
966                 /* Find an existing lock in the per-export lock hash */
967                 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
968                                           (void *)&dlm_req->lock_handle[0]);
969                 if (lock != NULL) {
970                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
971                                   LPX64, lock->l_handle.h_cookie);
972                         GOTO(existing_lock, rc = 0);
973                 }
974         }
975
976         /* The lock's callback data might be set in the policy function */
977         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
978                                 dlm_req->lock_desc.l_resource.lr_type,
979                                 dlm_req->lock_desc.l_req_mode,
980                                 cbs->lcs_blocking, cbs->lcs_completion,
981                                 cbs->lcs_glimpse, NULL, 0);
982
983         if (!lock)
984                 GOTO(out, rc = -ENOMEM);
985
986         do_gettimeofday(&lock->l_enqueued_time);
987         lock->l_remote_handle = dlm_req->lock_handle[0];
988         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
989
990         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
991         /* Don't enqueue a lock onto the export if it has already
992          * been evicted.  Cancel it now instead. (bug 3822) */
993         if (req->rq_export->exp_failed) {
994                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
995                 GOTO(out, rc = -ENOTCONN);
996         }
997         lock->l_export = class_export_get(req->rq_export);
998
999         if (lock->l_export->exp_lock_hash)
1000                 lustre_hash_add(lock->l_export->exp_lock_hash,
1001                                 &lock->l_remote_handle,
1002                                 &lock->l_exp_hash);
1003
1004 existing_lock:
1005
1006         if (flags & LDLM_FL_HAS_INTENT) {
1007                 /* In this case, the reply buffer is allocated deep in
1008                  * local_lock_enqueue by the policy function. */
1009                 cookie = req;
1010         } else {
1011                 lock_res_and_lock(lock);
1012                 if (lock->l_resource->lr_lvb_len) {
1013                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1014                                              RCL_SERVER,
1015                                              lock->l_resource->lr_lvb_len);
1016                 }
1017                 unlock_res_and_lock(lock);
1018
1019                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1020                         GOTO(out, rc = -ENOMEM);
1021
1022                 rc = req_capsule_server_pack(&req->rq_pill);
1023                 if (rc)
1024                         GOTO(out, rc);
1025         }
1026
1027         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1028                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1029         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1030                 lock->l_req_extent = lock->l_policy_data.l_extent;
1031
1032         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1033         if (err)
1034                 GOTO(out, err);
1035
1036         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1037         dlm_rep->lock_flags = flags;
1038
1039         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1040         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1041
1042         /* We never send a blocking AST until the lock is granted, but
1043          * we can tell it right now */
1044         lock_res_and_lock(lock);
1045
1046         /* Now take into account flags to be inherited from original lock
1047            request both in reply to client and in our own lock flags. */
1048         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1049         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1050
1051         /* Don't move a pending lock onto the export if it has already
1052          * been evicted.  Cancel it now instead. (bug 5683) */
1053         if (unlikely(req->rq_export->exp_failed ||
1054                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1055                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1056                 rc = -ENOTCONN;
1057         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1058                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1059                 if (lock->l_granted_mode == lock->l_req_mode) {
1060                         /*
1061                          * Only cancel lock if it was granted, because it would
1062                          * be destroyed immediatelly and would never be granted
1063                          * in the future, causing timeouts on client.  Not
1064                          * granted lock will be cancelled immediatelly after
1065                          * sending completion AST.
1066                          */
1067                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1068                                 unlock_res_and_lock(lock);
1069                                 ldlm_lock_cancel(lock);
1070                                 lock_res_and_lock(lock);
1071                         } else
1072                                 ldlm_add_waiting_lock(lock);
1073                 }
1074         }
1075         /* Make sure we never ever grant usual metadata locks to liblustre
1076            clients */
1077         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1078             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1079              req->rq_export->exp_libclient) {
1080                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1081                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1082                         CERROR("Granting sync lock to libclient. "
1083                                "req fl %d, rep fl %d, lock fl %d\n",
1084                                dlm_req->lock_flags, dlm_rep->lock_flags,
1085                                lock->l_flags);
1086                         LDLM_ERROR(lock, "sync lock");
1087                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1088                                 struct ldlm_intent *it;
1089
1090                                 it = req_capsule_client_get(&req->rq_pill,
1091                                                             &RMF_LDLM_INTENT);
1092                                 if (it != NULL) {
1093                                         CERROR("This is intent %s ("LPU64")\n",
1094                                                ldlm_it2str(it->opc), it->opc);
1095                                 }
1096                         }
1097                 }
1098         }
1099
1100         unlock_res_and_lock(lock);
1101
1102         EXIT;
1103  out:
1104         req->rq_status = rc ?: err; /* return either error - bug 11190 */
1105         if (!req->rq_packed_final) {
1106                 err = lustre_pack_reply(req, 1, NULL, NULL);
1107                 if (rc == 0)
1108                         rc = err;
1109         }
1110
1111         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1112          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1113         if (lock) {
1114                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1115                            "(err=%d, rc=%d)", err, rc);
1116
1117                 lock_res_and_lock(lock);
1118                 if (rc == 0) {
1119                         if (lock->l_resource->lr_lvb_len > 0) {
1120                                 void *lvb;
1121
1122                                 lvb = req_capsule_server_get(&req->rq_pill,
1123                                                              &RMF_DLM_LVB);
1124                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1125                                          req, lock);
1126
1127                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1128                                        lock->l_resource->lr_lvb_len);
1129                         }
1130                 } else {
1131                         ldlm_resource_unlink_lock(lock);
1132                         ldlm_lock_destroy_nolock(lock);
1133                 }
1134                 unlock_res_and_lock(lock);
1135
1136                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1137                         ldlm_reprocess_all(lock->l_resource);
1138
1139                 LDLM_LOCK_PUT(lock);
1140         }
1141
1142         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1143                           lock, rc);
1144
1145         return rc;
1146 }
1147
1148 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1149                         ldlm_completion_callback completion_callback,
1150                         ldlm_blocking_callback blocking_callback,
1151                         ldlm_glimpse_callback glimpse_callback)
1152 {
1153         struct ldlm_request *dlm_req;
1154         struct ldlm_callback_suite cbs = {
1155                 .lcs_completion = completion_callback,
1156                 .lcs_blocking   = blocking_callback,
1157                 .lcs_glimpse    = glimpse_callback
1158         };
1159         int rc;
1160
1161         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1162         if (dlm_req != NULL) {
1163                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1164                                           req, dlm_req, &cbs);
1165         } else {
1166                 rc = -EFAULT;
1167         }
1168         return rc;
1169 }
1170
1171 int ldlm_handle_convert0(struct ptlrpc_request *req,
1172                          const struct ldlm_request *dlm_req)
1173 {
1174         struct ldlm_reply *dlm_rep;
1175         struct ldlm_lock *lock;
1176         int rc;
1177         ENTRY;
1178
1179         if (req->rq_export && req->rq_export->exp_nid_stats &&
1180             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1181                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1182                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1183
1184         rc = req_capsule_server_pack(&req->rq_pill);
1185         if (rc)
1186                 RETURN(rc);
1187
1188         dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1189         dlm_rep->lock_flags = dlm_req->lock_flags;
1190
1191         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1192         if (!lock) {
1193                 req->rq_status = EINVAL;
1194         } else {
1195                 void *res = NULL;
1196
1197                 LDLM_DEBUG(lock, "server-side convert handler START");
1198
1199                 do_gettimeofday(&lock->l_enqueued_time);
1200                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1201                                         &dlm_rep->lock_flags);
1202                 if (res) {
1203                         if (ldlm_del_waiting_lock(lock))
1204                                 LDLM_DEBUG(lock, "converted waiting lock");
1205                         req->rq_status = 0;
1206                 } else {
1207                         req->rq_status = EDEADLOCK;
1208                 }
1209         }
1210
1211         if (lock) {
1212                 if (!req->rq_status)
1213                         ldlm_reprocess_all(lock->l_resource);
1214                 LDLM_DEBUG(lock, "server-side convert handler END");
1215                 LDLM_LOCK_PUT(lock);
1216         } else
1217                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1218
1219         RETURN(0);
1220 }
1221
1222 int ldlm_handle_convert(struct ptlrpc_request *req)
1223 {
1224         int rc;
1225         struct ldlm_request *dlm_req;
1226
1227         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1228         if (dlm_req != NULL) {
1229                 rc = ldlm_handle_convert0(req, dlm_req);
1230         } else {
1231                 CERROR ("Can't unpack dlm_req\n");
1232                 rc = -EFAULT;
1233         }
1234         return rc;
1235 }
1236
1237 /* Cancel all the locks whos handles are packed into ldlm_request */
1238 int ldlm_request_cancel(struct ptlrpc_request *req,
1239                         const struct ldlm_request *dlm_req, int first)
1240 {
1241         struct ldlm_resource *res, *pres = NULL;
1242         struct ldlm_lock *lock;
1243         int i, count, done = 0;
1244         ENTRY;
1245
1246         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1247         if (first >= count)
1248                 RETURN(0);
1249
1250         /* There is no lock on the server at the replay time,
1251          * skip lock cancelling to make replay tests to pass. */
1252         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1253                 RETURN(0);
1254
1255         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1256                           "starting at %d", count, first);
1257
1258         for (i = first; i < count; i++) {
1259                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1260                 if (!lock) {
1261                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1262                                           "lock (cookie "LPU64")",
1263                                           dlm_req->lock_handle[i].cookie);
1264                         continue;
1265                 }
1266
1267                 res = lock->l_resource;
1268                 done++;
1269
1270                 if (res != pres) {
1271                         if (pres != NULL) {
1272                                 ldlm_reprocess_all(pres);
1273                                 ldlm_resource_putref(pres);
1274                         }
1275                         if (res != NULL) {
1276                                 ldlm_resource_getref(res);
1277                                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1278                         }
1279                         pres = res;
1280                 }
1281                 ldlm_lock_cancel(lock);
1282                 LDLM_LOCK_PUT(lock);
1283         }
1284         if (pres != NULL) {
1285                 ldlm_reprocess_all(pres);
1286                 ldlm_resource_putref(pres);
1287         }
1288         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1289         RETURN(done);
1290 }
1291
1292 int ldlm_handle_cancel(struct ptlrpc_request *req)
1293 {
1294         struct ldlm_request *dlm_req;
1295         int rc;
1296         ENTRY;
1297
1298         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1299         if (dlm_req == NULL) {
1300                 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1301                 RETURN(-EFAULT);
1302         }
1303
1304         if (req->rq_export && req->rq_export->exp_nid_stats &&
1305             req->rq_export->exp_nid_stats->nid_ldlm_stats)
1306                 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1307                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1308
1309         rc = req_capsule_server_pack(&req->rq_pill);
1310         if (rc)
1311                 RETURN(rc);
1312
1313         if (!ldlm_request_cancel(req, dlm_req, 0))
1314                 req->rq_status = ESTALE;
1315
1316         if (ptlrpc_reply(req) != 0)
1317                 LBUG();
1318
1319         RETURN(0);
1320 }
1321
1322 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1323                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1324 {
1325         int do_ast;
1326         ENTRY;
1327
1328         LDLM_DEBUG(lock, "client blocking AST callback handler START");
1329
1330         lock_res_and_lock(lock);
1331         lock->l_flags |= LDLM_FL_CBPENDING;
1332
1333         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1334                 lock->l_flags |= LDLM_FL_CANCEL;
1335
1336         do_ast = (!lock->l_readers && !lock->l_writers);
1337         unlock_res_and_lock(lock);
1338
1339         if (do_ast) {
1340                 LDLM_DEBUG(lock, "already unused, calling "
1341                            "callback (%p)", lock->l_blocking_ast);
1342                 if (lock->l_blocking_ast != NULL)
1343                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1344                                              LDLM_CB_BLOCKING);
1345         } else {
1346                 LDLM_DEBUG(lock, "Lock still has references, will be"
1347                            " cancelled later");
1348         }
1349
1350         LDLM_DEBUG(lock, "client blocking callback handler END");
1351         LDLM_LOCK_PUT(lock);
1352         EXIT;
1353 }
1354
1355 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1356                                     struct ldlm_namespace *ns,
1357                                     struct ldlm_request *dlm_req,
1358                                     struct ldlm_lock *lock)
1359 {
1360         CFS_LIST_HEAD(ast_list);
1361         ENTRY;
1362
1363         LDLM_DEBUG(lock, "client completion callback handler START");
1364
1365         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1366                 int to = cfs_time_seconds(1);
1367                 while (to > 0) {
1368                         cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1369                         if (lock->l_granted_mode == lock->l_req_mode ||
1370                             lock->l_destroyed)
1371                                 break;
1372                 }
1373         }
1374
1375         lock_res_and_lock(lock);
1376         if (lock->l_destroyed ||
1377             lock->l_granted_mode == lock->l_req_mode) {
1378                 /* bug 11300: the lock has already been granted */
1379                 unlock_res_and_lock(lock);
1380                 LDLM_DEBUG(lock, "Double grant race happened");
1381                 LDLM_LOCK_PUT(lock);
1382                 EXIT;
1383                 return;
1384         }
1385
1386         /* If we receive the completion AST before the actual enqueue returned,
1387          * then we might need to switch lock modes, resources, or extents. */
1388         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1389                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1390                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1391         }
1392
1393         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1394                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1395                 LDLM_DEBUG(lock, "completion AST, new policy data");
1396         }
1397
1398         ldlm_resource_unlink_lock(lock);
1399         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1400                    &lock->l_resource->lr_name,
1401                    sizeof(lock->l_resource->lr_name)) != 0) {
1402                 unlock_res_and_lock(lock);
1403                 if (ldlm_lock_change_resource(ns, lock,
1404                                 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1405                         LDLM_ERROR(lock, "Failed to allocate resource");
1406                         LDLM_LOCK_PUT(lock);
1407                         EXIT;
1408                         return;
1409                 }
1410                 LDLM_DEBUG(lock, "completion AST, new resource");
1411                 CERROR("change resource!\n");
1412                 lock_res_and_lock(lock);
1413         }
1414
1415         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1416                 /* BL_AST locks are not needed in lru.
1417                  * let ldlm_cancel_lru() be fast. */
1418                 ldlm_lock_remove_from_lru(lock);
1419                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1420                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1421         }
1422
1423         if (lock->l_lvb_len) {
1424                 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1425                                          RCL_CLIENT) < lock->l_lvb_len) {
1426                         LDLM_ERROR(lock, "completion AST did not contain "
1427                                    "expected LVB!");
1428                 } else {
1429                         void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1430                                                                 &RMF_DLM_LVB,
1431                                                   (void *)lock->l_lvb_swabber);
1432                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1433                 }
1434         }
1435
1436         ldlm_grant_lock(lock, &ast_list);
1437         unlock_res_and_lock(lock);
1438
1439         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1440
1441         ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1442
1443         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1444                           lock);
1445         LDLM_LOCK_PUT(lock);
1446         EXIT;
1447 }
1448
1449 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1450                                     struct ldlm_namespace *ns,
1451                                     struct ldlm_request *dlm_req,
1452                                     struct ldlm_lock *lock)
1453 {
1454         int rc = -ENOSYS;
1455         ENTRY;
1456
1457         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1458
1459         if (lock->l_glimpse_ast != NULL)
1460                 rc = lock->l_glimpse_ast(lock, req);
1461
1462         if (req->rq_repmsg != NULL) {
1463                 ptlrpc_reply(req);
1464         } else {
1465                 req->rq_status = rc;
1466                 ptlrpc_error(req);
1467         }
1468
1469         lock_res_and_lock(lock);
1470         if (lock->l_granted_mode == LCK_PW &&
1471             !lock->l_readers && !lock->l_writers &&
1472             cfs_time_after(cfs_time_current(),
1473                            cfs_time_add(lock->l_last_used,
1474                                         cfs_time_seconds(10)))) {
1475                 unlock_res_and_lock(lock);
1476                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1477                         ldlm_handle_bl_callback(ns, NULL, lock);
1478
1479                 EXIT;
1480                 return;
1481         }
1482         unlock_res_and_lock(lock);
1483         LDLM_LOCK_PUT(lock);
1484         EXIT;
1485 }
1486
1487 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1488 {
1489         if (req->rq_no_reply)
1490                 return 0;
1491
1492         req->rq_status = rc;
1493         if (!req->rq_packed_final) {
1494                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1495                 if (rc)
1496                         return rc;
1497         }
1498         return ptlrpc_reply(req);
1499 }
1500
1501 #ifdef __KERNEL__
1502 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1503                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1504                              struct list_head *cancels, int count)
1505 {
1506         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1507         struct ldlm_bl_work_item *blwi;
1508         ENTRY;
1509
1510         if (cancels && count == 0)
1511                 RETURN(0);
1512
1513         OBD_ALLOC(blwi, sizeof(*blwi));
1514         if (blwi == NULL)
1515                 RETURN(-ENOMEM);
1516
1517         blwi->blwi_ns = ns;
1518         if (ld != NULL)
1519                 blwi->blwi_ld = *ld;
1520         if (count) {
1521                 list_add(&blwi->blwi_head, cancels);
1522                 list_del_init(cancels);
1523                 blwi->blwi_count = count;
1524         } else {
1525                 blwi->blwi_lock = lock;
1526         }
1527         spin_lock(&blp->blp_lock);
1528         if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1529                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1530                 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1531         } else {
1532                 /* other blocking callbacks are added to the regular list */
1533                 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1534         }
1535         cfs_waitq_signal(&blp->blp_waitq);
1536         spin_unlock(&blp->blp_lock);
1537
1538         RETURN(0);
1539 }
1540 #endif
1541
1542 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1543                            struct ldlm_lock *lock)
1544 {
1545 #ifdef __KERNEL__
1546         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1547 #else
1548         RETURN(-ENOSYS);
1549 #endif
1550 }
1551
1552 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1553                            struct list_head *cancels, int count)
1554 {
1555 #ifdef __KERNEL__
1556         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1557 #else
1558         RETURN(-ENOSYS);
1559 #endif
1560 }
1561
1562 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1563 static int ldlm_callback_handler(struct ptlrpc_request *req)
1564 {
1565         struct ldlm_namespace *ns;
1566         struct ldlm_request *dlm_req;
1567         struct ldlm_lock *lock;
1568         int rc;
1569         ENTRY;
1570
1571         /* Requests arrive in sender's byte order.  The ptlrpc service
1572          * handler has already checked and, if necessary, byte-swapped the
1573          * incoming request message body, but I am responsible for the
1574          * message buffers. */
1575
1576         /* do nothing for sec context finalize */
1577         if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1578                 RETURN(0);
1579
1580         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1581
1582         if (req->rq_export == NULL) {
1583                 struct ldlm_request *dlm_req;
1584
1585                 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1586                        "export cookie "LPX64"; this is "
1587                        "normal if this node rebooted with a lock held\n",
1588                        lustre_msg_get_opc(req->rq_reqmsg),
1589                        libcfs_id2str(req->rq_peer),
1590                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1591
1592                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1593                 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1594                 if (dlm_req != NULL)
1595                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1596                                dlm_req->lock_handle[0].cookie);
1597
1598                 ldlm_callback_reply(req, -ENOTCONN);
1599                 RETURN(0);
1600         }
1601
1602         LASSERT(req->rq_export != NULL);
1603         LASSERT(req->rq_export->exp_obd != NULL);
1604
1605         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1606         case LDLM_BL_CALLBACK:
1607                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1608                         RETURN(0);
1609                 break;
1610         case LDLM_CP_CALLBACK:
1611                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1612                         RETURN(0);
1613                 break;
1614         case LDLM_GL_CALLBACK:
1615                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1616                         RETURN(0);
1617                 break;
1618         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1619                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1620                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1621                         RETURN(0);
1622                 rc = llog_origin_handle_cancel(req);
1623                 ldlm_callback_reply(req, rc);
1624                 RETURN(0);
1625         case OBD_QC_CALLBACK:
1626                 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1627                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1628                         RETURN(0);
1629                 rc = target_handle_qc_callback(req);
1630                 ldlm_callback_reply(req, rc);
1631                 RETURN(0);
1632         case QUOTA_DQACQ:
1633         case QUOTA_DQREL:
1634                 /* reply in handler */
1635                 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1636                 rc = target_handle_dqacq_callback(req);
1637                 RETURN(0);
1638         case LLOG_ORIGIN_HANDLE_CREATE:
1639                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1640                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1641                         RETURN(0);
1642                 rc = llog_origin_handle_create(req);
1643                 ldlm_callback_reply(req, rc);
1644                 RETURN(0);
1645         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1646                 req_capsule_set(&req->rq_pill,
1647                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1648                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1649                         RETURN(0);
1650                 rc = llog_origin_handle_next_block(req);
1651                 ldlm_callback_reply(req, rc);
1652                 RETURN(0);
1653         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1654                 req_capsule_set(&req->rq_pill,
1655                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1656                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1657                         RETURN(0);
1658                 rc = llog_origin_handle_read_header(req);
1659                 ldlm_callback_reply(req, rc);
1660                 RETURN(0);
1661         case LLOG_ORIGIN_HANDLE_CLOSE:
1662                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1663                         RETURN(0);
1664                 rc = llog_origin_handle_close(req);
1665                 ldlm_callback_reply(req, rc);
1666                 RETURN(0);
1667         default:
1668                 CERROR("unknown opcode %u\n",
1669                        lustre_msg_get_opc(req->rq_reqmsg));
1670                 ldlm_callback_reply(req, -EPROTO);
1671                 RETURN(0);
1672         }
1673
1674         ns = req->rq_export->exp_obd->obd_namespace;
1675         LASSERT(ns != NULL);
1676
1677         req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1678
1679         dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1680         if (dlm_req == NULL) {
1681                 ldlm_callback_reply(req, -EPROTO);
1682                 RETURN(0);
1683         }
1684
1685         /* Force a known safe race, send a cancel to the server for a lock
1686          * which the server has already started a blocking callback on. */
1687         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1688             lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1689                 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1690                 if (rc < 0)
1691                         CERROR("ldlm_cli_cancel: %d\n", rc);
1692         }
1693
1694         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1695         if (!lock) {
1696                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1697                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1698                 ldlm_callback_reply(req, -EINVAL);
1699                 RETURN(0);
1700         }
1701
1702         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1703         lock_res_and_lock(lock);
1704         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1705         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1706                 /* If somebody cancels locks and cache is already droped,
1707                  * we can tell the server we have no lock. Otherwise, we
1708                  * should send cancel after dropping the cache. */
1709                 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1710                     (lock->l_flags & LDLM_FL_BL_DONE)) {
1711                         LDLM_DEBUG(lock, "callback on lock "
1712                                    LPX64" - lock disappeared\n",
1713                                    dlm_req->lock_handle[0].cookie);
1714                         unlock_res_and_lock(lock);
1715                         LDLM_LOCK_PUT(lock);
1716                         ldlm_callback_reply(req, -EINVAL);
1717                         RETURN(0);
1718                 }
1719                 /* BL_AST locks are not needed in lru.
1720                  * let ldlm_cancel_lru() be fast. */
1721                 ldlm_lock_remove_from_lru(lock);
1722                 lock->l_flags |= LDLM_FL_BL_AST;
1723         }
1724         unlock_res_and_lock(lock);
1725
1726         /* We want the ost thread to get this reply so that it can respond
1727          * to ost requests (write cache writeback) that might be triggered
1728          * in the callback.
1729          *
1730          * But we'd also like to be able to indicate in the reply that we're
1731          * cancelling right now, because it's unused, or have an intent result
1732          * in the reply, so we might have to push the responsibility for sending
1733          * the reply down into the AST handlers, alas. */
1734
1735         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1736         case LDLM_BL_CALLBACK:
1737                 CDEBUG(D_INODE, "blocking ast\n");
1738                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1739                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1740                         ldlm_callback_reply(req, 0);
1741                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1742                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1743                 break;
1744         case LDLM_CP_CALLBACK:
1745                 CDEBUG(D_INODE, "completion ast\n");
1746                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1747                 ldlm_callback_reply(req, 0);
1748                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1749                 break;
1750         case LDLM_GL_CALLBACK:
1751                 CDEBUG(D_INODE, "glimpse ast\n");
1752                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1753                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1754                 break;
1755         default:
1756                 LBUG();                         /* checked above */
1757         }
1758
1759         RETURN(0);
1760 }
1761
1762 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1763 {
1764         int rc;
1765         ENTRY;
1766
1767         /* Requests arrive in sender's byte order.  The ptlrpc service
1768          * handler has already checked and, if necessary, byte-swapped the
1769          * incoming request message body, but I am responsible for the
1770          * message buffers. */
1771
1772         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1773
1774         if (req->rq_export == NULL) {
1775                 struct ldlm_request *dlm_req;
1776
1777                 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1778                        lustre_msg_get_opc(req->rq_reqmsg),
1779                        libcfs_id2str(req->rq_peer),
1780                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1781
1782                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1783                 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1784                 if (dlm_req != NULL)
1785                         ldlm_lock_dump_handle(D_ERROR,
1786                                               &dlm_req->lock_handle[0]);
1787                 ldlm_callback_reply(req, -ENOTCONN);
1788                 RETURN(0);
1789         }
1790
1791         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1792
1793         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1794         case LDLM_CANCEL:
1795                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1796                 CDEBUG(D_INODE, "cancel\n");
1797                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1798                         RETURN(0);
1799                 rc = ldlm_handle_cancel(req);
1800                 if (rc)
1801                         break;
1802                 RETURN(0);
1803         case OBD_LOG_CANCEL:
1804                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1805                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1806                         RETURN(0);
1807                 rc = llog_origin_handle_cancel(req);
1808                 ldlm_callback_reply(req, rc);
1809                 RETURN(0);
1810         default:
1811                 CERROR("invalid opcode %d\n",
1812                        lustre_msg_get_opc(req->rq_reqmsg));
1813                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1814                 ldlm_callback_reply(req, -EINVAL);
1815         }
1816
1817         RETURN(0);
1818 }
1819
1820 void ldlm_revoke_lock_cb(void *obj, void *data)
1821 {
1822         struct list_head   *rpc_list = data;
1823         struct ldlm_lock   *lock = obj;
1824
1825         lock_res_and_lock(lock);
1826
1827         if (lock->l_req_mode != lock->l_granted_mode) {
1828                 unlock_res_and_lock(lock);
1829                 return;
1830         }
1831
1832         LASSERT(lock->l_resource);
1833         if (lock->l_resource->lr_type != LDLM_IBITS &&
1834             lock->l_resource->lr_type != LDLM_PLAIN) {
1835                 unlock_res_and_lock(lock);
1836                 return;
1837         }
1838
1839         if (lock->l_flags & LDLM_FL_AST_SENT) {
1840                 unlock_res_and_lock(lock);
1841                 return;
1842         }
1843
1844         LASSERT(lock->l_blocking_ast);
1845         LASSERT(!lock->l_blocking_lock);
1846
1847         lock->l_flags |= LDLM_FL_AST_SENT;
1848         if (lock->l_export && lock->l_export->exp_lock_hash)
1849                 lustre_hash_del(lock->l_export->exp_lock_hash,
1850                                 &lock->l_remote_handle, &lock->l_exp_hash);
1851         list_add_tail(&lock->l_rk_ast, rpc_list);
1852         LDLM_LOCK_GET(lock);
1853
1854         unlock_res_and_lock(lock);
1855 }
1856
1857 void ldlm_revoke_export_locks(struct obd_export *exp)
1858 {
1859         struct list_head  rpc_list;
1860         ENTRY;
1861
1862         CFS_INIT_LIST_HEAD(&rpc_list);
1863         lustre_hash_for_each_empty(exp->exp_lock_hash,
1864                                    ldlm_revoke_lock_cb, &rpc_list);
1865         ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1866
1867         EXIT;
1868 }
1869
1870 #ifdef __KERNEL__
1871 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1872 {
1873         struct ldlm_bl_work_item *blwi = NULL;
1874         static unsigned int num_bl = 0;
1875
1876         spin_lock(&blp->blp_lock);
1877         /* process a request from the blp_list at least every blp_num_threads */
1878         if (!list_empty(&blp->blp_list) &&
1879             (list_empty(&blp->blp_prio_list) || num_bl == 0))
1880                 blwi = list_entry(blp->blp_list.next,
1881                                   struct ldlm_bl_work_item, blwi_entry);
1882         else
1883                 if (!list_empty(&blp->blp_prio_list))
1884                         blwi = list_entry(blp->blp_prio_list.next,
1885                                           struct ldlm_bl_work_item, blwi_entry);
1886
1887         if (blwi) {
1888                 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1889                         num_bl = 0;
1890                 list_del(&blwi->blwi_entry);
1891         }
1892         spin_unlock(&blp->blp_lock);
1893
1894         return blwi;
1895 }
1896
1897 /* This only contains temporary data until the thread starts */
1898 struct ldlm_bl_thread_data {
1899         char                    bltd_name[CFS_CURPROC_COMM_MAX];
1900         struct ldlm_bl_pool     *bltd_blp;
1901         struct completion       bltd_comp;
1902         int                     bltd_num;
1903 };
1904
1905 static int ldlm_bl_thread_main(void *arg);
1906
1907 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1908 {
1909         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1910         int rc;
1911
1912         init_completion(&bltd.bltd_comp);
1913         rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1914         if (rc < 0) {
1915                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1916                        atomic_read(&blp->blp_num_threads), rc);
1917                 return rc;
1918         }
1919         wait_for_completion(&bltd.bltd_comp);
1920
1921         return 0;
1922 }
1923
1924 static int ldlm_bl_thread_main(void *arg)
1925 {
1926         struct ldlm_bl_pool *blp;
1927         ENTRY;
1928
1929         {
1930                 struct ldlm_bl_thread_data *bltd = arg;
1931
1932                 blp = bltd->bltd_blp;
1933
1934                 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1935                 atomic_inc(&blp->blp_busy_threads);
1936
1937                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1938                         "ldlm_bl_%02d", bltd->bltd_num);
1939                 cfs_daemonize(bltd->bltd_name);
1940
1941                 complete(&bltd->bltd_comp);
1942                 /* cannot use bltd after this, it is only on caller's stack */
1943         }
1944
1945         while (1) {
1946                 struct l_wait_info lwi = { 0 };
1947                 struct ldlm_bl_work_item *blwi = NULL;
1948
1949                 blwi = ldlm_bl_get_work(blp);
1950
1951                 if (blwi == NULL) {
1952                         int busy;
1953
1954                         atomic_dec(&blp->blp_busy_threads);
1955                         l_wait_event_exclusive(blp->blp_waitq,
1956                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
1957                                          &lwi);
1958                         busy = atomic_inc_return(&blp->blp_busy_threads);
1959
1960                         if (blwi->blwi_ns == NULL)
1961                                 /* added by ldlm_cleanup() */
1962                                 break;
1963
1964                         /* Not fatal if racy and have a few too many threads */
1965                         if (unlikely(busy < blp->blp_max_threads &&
1966                                     busy >= atomic_read(&blp->blp_num_threads)))
1967                                 /* discard the return value, we tried */
1968                                 ldlm_bl_thread_start(blp);
1969                 } else {
1970                         if (blwi->blwi_ns == NULL)
1971                                 /* added by ldlm_cleanup() */
1972                                 break;
1973                 }
1974
1975                 if (blwi->blwi_count) {
1976                         /* The special case when we cancel locks in lru
1977                          * asynchronously, we pass the list of locks here.
1978                          * Thus lock is marked LDLM_FL_CANCELING, and already
1979                          * canceled locally. */
1980                         ldlm_cli_cancel_list(&blwi->blwi_head,
1981                                              blwi->blwi_count, NULL, 0);
1982                 } else {
1983                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1984                                                 blwi->blwi_lock);
1985                 }
1986                 OBD_FREE(blwi, sizeof(*blwi));
1987         }
1988
1989         atomic_dec(&blp->blp_busy_threads);
1990         atomic_dec(&blp->blp_num_threads);
1991         complete(&blp->blp_comp);
1992         RETURN(0);
1993 }
1994
1995 #endif
1996
1997 static int ldlm_setup(void);
1998 static int ldlm_cleanup(void);
1999
2000 int ldlm_get_ref(void)
2001 {
2002         int rc = 0;
2003         ENTRY;
2004         mutex_down(&ldlm_ref_sem);
2005         if (++ldlm_refcount == 1) {
2006                 rc = ldlm_setup();
2007                 if (rc)
2008                         ldlm_refcount--;
2009         }
2010         mutex_up(&ldlm_ref_sem);
2011
2012         RETURN(rc);
2013 }
2014
2015 void ldlm_put_ref(void)
2016 {
2017         ENTRY;
2018         mutex_down(&ldlm_ref_sem);
2019         if (ldlm_refcount == 1) {
2020                 int rc = ldlm_cleanup();
2021                 if (rc)
2022                         CERROR("ldlm_cleanup failed: %d\n", rc);
2023                 else
2024                         ldlm_refcount--;
2025         } else {
2026                 ldlm_refcount--;
2027         }
2028         mutex_up(&ldlm_ref_sem);
2029
2030         EXIT;
2031 }
2032
2033 /*
2034  * Export handle<->lock hash operations.
2035  */
2036 static unsigned
2037 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2038 {
2039         return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2040 }
2041
2042 static void *
2043 ldlm_export_lock_key(struct hlist_node *hnode)
2044 {
2045         struct ldlm_lock *lock;
2046         ENTRY;
2047
2048         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2049         RETURN(&lock->l_remote_handle);
2050 }
2051
2052 static int
2053 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2054 {
2055         ENTRY;
2056         RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2057 }
2058
2059 static void *
2060 ldlm_export_lock_get(struct hlist_node *hnode)
2061 {
2062         struct ldlm_lock *lock;
2063         ENTRY;
2064
2065         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2066         LDLM_LOCK_GET(lock);
2067
2068         RETURN(lock);
2069 }
2070
2071 static void *
2072 ldlm_export_lock_put(struct hlist_node *hnode)
2073 {
2074         struct ldlm_lock *lock;
2075         ENTRY;
2076
2077         lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2078         LDLM_LOCK_PUT(lock);
2079
2080         RETURN(lock);
2081 }
2082
2083 static lustre_hash_ops_t ldlm_export_lock_ops = {
2084         .lh_hash    = ldlm_export_lock_hash,
2085         .lh_key     = ldlm_export_lock_key,
2086         .lh_compare = ldlm_export_lock_compare,
2087         .lh_get     = ldlm_export_lock_get,
2088         .lh_put     = ldlm_export_lock_put
2089 };
2090
2091 int ldlm_init_export(struct obd_export *exp)
2092 {
2093         ENTRY;
2094
2095         exp->exp_lock_hash =
2096                 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2097                                  128, 65536, &ldlm_export_lock_ops, LH_REHASH);
2098
2099         if (!exp->exp_lock_hash)
2100                 RETURN(-ENOMEM);
2101
2102         RETURN(0);
2103 }
2104 EXPORT_SYMBOL(ldlm_init_export);
2105
2106 void ldlm_destroy_export(struct obd_export *exp)
2107 {
2108         ENTRY;
2109         lustre_hash_exit(exp->exp_lock_hash);
2110         exp->exp_lock_hash = NULL;
2111         EXIT;
2112 }
2113 EXPORT_SYMBOL(ldlm_destroy_export);
2114
2115 static int ldlm_setup(void)
2116 {
2117         struct ldlm_bl_pool *blp;
2118         int rc = 0;
2119         int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2120         int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2121 #ifdef __KERNEL__
2122         int i;
2123 #endif
2124         ENTRY;
2125
2126         if (ldlm_state != NULL)
2127                 RETURN(-EALREADY);
2128
2129         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2130         if (ldlm_state == NULL)
2131                 RETURN(-ENOMEM);
2132
2133 #ifdef LPROCFS
2134         rc = ldlm_proc_setup();
2135         if (rc != 0)
2136                 GOTO(out_free, rc);
2137 #endif
2138
2139 #ifdef __KERNEL__
2140         if (ldlm_num_threads) {
2141                 /* If ldlm_num_threads is set, it is the min and the max. */
2142                 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2143                         ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2144                 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2145                         ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2146                 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2147         }
2148 #endif
2149
2150         ldlm_state->ldlm_cb_service =
2151                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2152                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2153                                 LDLM_CB_REPLY_PORTAL, 1800,
2154                                 ldlm_callback_handler, "ldlm_cbd",
2155                                 ldlm_svc_proc_dir, NULL,
2156                                 ldlm_min_threads, ldlm_max_threads,
2157                                 "ldlm_cb",
2158                                 LCT_MD_THREAD|LCT_DT_THREAD);
2159
2160         if (!ldlm_state->ldlm_cb_service) {
2161                 CERROR("failed to start service\n");
2162                 GOTO(out_proc, rc = -ENOMEM);
2163         }
2164
2165         ldlm_state->ldlm_cancel_service =
2166                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2167                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2168                                 LDLM_CANCEL_REPLY_PORTAL, 6000,
2169                                 ldlm_cancel_handler, "ldlm_canceld",
2170                                 ldlm_svc_proc_dir, NULL,
2171                                 ldlm_min_threads, ldlm_max_threads,
2172                                 "ldlm_cn",
2173                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2174
2175         if (!ldlm_state->ldlm_cancel_service) {
2176                 CERROR("failed to start service\n");
2177                 GOTO(out_proc, rc = -ENOMEM);
2178         }
2179
2180         OBD_ALLOC(blp, sizeof(*blp));
2181         if (blp == NULL)
2182                 GOTO(out_proc, rc = -ENOMEM);
2183         ldlm_state->ldlm_bl_pool = blp;
2184
2185         spin_lock_init(&blp->blp_lock);
2186         CFS_INIT_LIST_HEAD(&blp->blp_list);
2187         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2188         cfs_waitq_init(&blp->blp_waitq);
2189         atomic_set(&blp->blp_num_threads, 0);
2190         atomic_set(&blp->blp_busy_threads, 0);
2191         blp->blp_min_threads = ldlm_min_threads;
2192         blp->blp_max_threads = ldlm_max_threads;
2193
2194 #ifdef __KERNEL__
2195         for (i = 0; i < blp->blp_min_threads; i++) {
2196                 rc = ldlm_bl_thread_start(blp);
2197                 if (rc < 0)
2198                         GOTO(out_thread, rc);
2199         }
2200
2201         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2202         if (rc)
2203                 GOTO(out_thread, rc);
2204
2205         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2206         if (rc)
2207                 GOTO(out_thread, rc);
2208
2209         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2210         expired_lock_thread.elt_state = ELT_STOPPED;
2211         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2212
2213         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2214         spin_lock_init(&waiting_locks_spinlock);
2215         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2216
2217         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2218         if (rc < 0) {
2219                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2220                 GOTO(out_thread, rc);
2221         }
2222
2223         wait_event(expired_lock_thread.elt_waitq,
2224                    expired_lock_thread.elt_state == ELT_READY);
2225 #endif
2226
2227 #ifdef __KERNEL__
2228         rc = ldlm_pools_init();
2229         if (rc)
2230                 GOTO(out_thread, rc);
2231 #endif
2232         RETURN(0);
2233
2234 #ifdef __KERNEL__
2235  out_thread:
2236         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2237         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2238 #endif
2239
2240  out_proc:
2241 #ifdef LPROCFS
2242         ldlm_proc_cleanup();
2243  out_free:
2244 #endif
2245         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2246         ldlm_state = NULL;
2247         return rc;
2248 }
2249
2250 static int ldlm_cleanup(void)
2251 {
2252 #ifdef __KERNEL__
2253         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2254 #endif
2255         ENTRY;
2256
2257         if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2258             !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2259                 CERROR("ldlm still has namespaces; clean these up first.\n");
2260                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2261                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2262                 RETURN(-EBUSY);
2263         }
2264
2265 #ifdef __KERNEL__
2266         ldlm_pools_fini();
2267 #endif
2268
2269 #ifdef __KERNEL__
2270         while (atomic_read(&blp->blp_num_threads) > 0) {
2271                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2272
2273                 init_completion(&blp->blp_comp);
2274
2275                 spin_lock(&blp->blp_lock);
2276                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2277                 cfs_waitq_signal(&blp->blp_waitq);
2278                 spin_unlock(&blp->blp_lock);
2279
2280                 wait_for_completion(&blp->blp_comp);
2281         }
2282         OBD_FREE(blp, sizeof(*blp));
2283
2284         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2285         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2286         ldlm_proc_cleanup();
2287
2288         expired_lock_thread.elt_state = ELT_TERMINATE;
2289         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2290         wait_event(expired_lock_thread.elt_waitq,
2291                    expired_lock_thread.elt_state == ELT_STOPPED);
2292 #else
2293         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2294         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2295 #endif
2296
2297         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2298         ldlm_state = NULL;
2299
2300         RETURN(0);
2301 }
2302
2303 int __init ldlm_init(void)
2304 {
2305         init_mutex(&ldlm_ref_sem);
2306         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2307         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2308         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2309                                                sizeof(struct ldlm_resource), 0,
2310                                                SLAB_HWCACHE_ALIGN);
2311         if (ldlm_resource_slab == NULL)
2312                 return -ENOMEM;
2313
2314         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2315                                            sizeof(struct ldlm_lock), 0,
2316                                            SLAB_HWCACHE_ALIGN);
2317         if (ldlm_lock_slab == NULL) {
2318                 cfs_mem_cache_destroy(ldlm_resource_slab);
2319                 return -ENOMEM;
2320         }
2321
2322         ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2323                                         sizeof(struct ldlm_interval),
2324                                         0, SLAB_HWCACHE_ALIGN);
2325         if (ldlm_interval_slab == NULL) {
2326                 cfs_mem_cache_destroy(ldlm_resource_slab);
2327                 cfs_mem_cache_destroy(ldlm_lock_slab);
2328                 return -ENOMEM;
2329         }
2330
2331         return 0;
2332 }
2333
2334 void __exit ldlm_exit(void)
2335 {
2336         int rc;
2337         if (ldlm_refcount)
2338                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2339         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2340         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2341         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2342         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2343         rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2344         LASSERTF(rc == 0, "couldn't free interval node slab\n");
2345 }
2346
2347 /* ldlm_extent.c */
2348 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2349
2350 /* ldlm_lock.c */
2351 EXPORT_SYMBOL(ldlm_get_processing_policy);
2352 EXPORT_SYMBOL(ldlm_lock2desc);
2353 EXPORT_SYMBOL(ldlm_register_intent);
2354 EXPORT_SYMBOL(ldlm_lockname);
2355 EXPORT_SYMBOL(ldlm_typename);
2356 EXPORT_SYMBOL(ldlm_lock2handle);
2357 EXPORT_SYMBOL(__ldlm_handle2lock);
2358 EXPORT_SYMBOL(ldlm_lock_get);
2359 EXPORT_SYMBOL(ldlm_lock_put);
2360 EXPORT_SYMBOL(ldlm_lock_fast_match);
2361 EXPORT_SYMBOL(ldlm_lock_fast_release);
2362 EXPORT_SYMBOL(ldlm_lock_match);
2363 EXPORT_SYMBOL(ldlm_lock_cancel);
2364 EXPORT_SYMBOL(ldlm_lock_addref);
2365 EXPORT_SYMBOL(ldlm_lock_decref);
2366 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2367 EXPORT_SYMBOL(ldlm_lock_change_resource);
2368 EXPORT_SYMBOL(ldlm_lock_set_data);
2369 EXPORT_SYMBOL(ldlm_it2str);
2370 EXPORT_SYMBOL(ldlm_lock_dump);
2371 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2372 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2373 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2374 EXPORT_SYMBOL(ldlm_lock_allow_match);
2375
2376 /* ldlm_request.c */
2377 EXPORT_SYMBOL(ldlm_completion_ast);
2378 EXPORT_SYMBOL(ldlm_blocking_ast);
2379 EXPORT_SYMBOL(ldlm_glimpse_ast);
2380 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2381 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2382 EXPORT_SYMBOL(ldlm_prep_elc_req);
2383 EXPORT_SYMBOL(ldlm_cli_convert);
2384 EXPORT_SYMBOL(ldlm_cli_enqueue);
2385 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2386 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2387 EXPORT_SYMBOL(ldlm_cli_cancel);
2388 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2389 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2390 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2391 EXPORT_SYMBOL(ldlm_cli_join_lru);
2392 EXPORT_SYMBOL(ldlm_replay_locks);
2393 EXPORT_SYMBOL(ldlm_resource_foreach);
2394 EXPORT_SYMBOL(ldlm_namespace_foreach);
2395 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2396 EXPORT_SYMBOL(ldlm_resource_iterate);
2397 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2398 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2399
2400 /* ldlm_lockd.c */
2401 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2402 EXPORT_SYMBOL(ldlm_server_completion_ast);
2403 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2404 EXPORT_SYMBOL(ldlm_handle_enqueue);
2405 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2406 EXPORT_SYMBOL(ldlm_handle_cancel);
2407 EXPORT_SYMBOL(ldlm_request_cancel);
2408 EXPORT_SYMBOL(ldlm_handle_convert);
2409 EXPORT_SYMBOL(ldlm_handle_convert0);
2410 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2411 EXPORT_SYMBOL(ldlm_get_ref);
2412 EXPORT_SYMBOL(ldlm_put_ref);
2413 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2414 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2415
2416 /* ldlm_resource.c */
2417 EXPORT_SYMBOL(ldlm_namespace_new);
2418 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2419 EXPORT_SYMBOL(ldlm_namespace_free);
2420 EXPORT_SYMBOL(ldlm_namespace_dump);
2421 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2422 EXPORT_SYMBOL(ldlm_resource_get);
2423 EXPORT_SYMBOL(ldlm_resource_putref);
2424 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2425
2426 /* ldlm_lib.c */
2427 EXPORT_SYMBOL(client_import_add_conn);
2428 EXPORT_SYMBOL(client_import_del_conn);
2429 EXPORT_SYMBOL(client_obd_setup);
2430 EXPORT_SYMBOL(client_obd_cleanup);
2431 EXPORT_SYMBOL(client_connect_import);
2432 EXPORT_SYMBOL(client_disconnect_export);
2433 EXPORT_SYMBOL(target_start_recovery_thread);
2434 EXPORT_SYMBOL(target_stop_recovery_thread);
2435 EXPORT_SYMBOL(target_handle_connect);
2436 EXPORT_SYMBOL(target_cleanup_recovery);
2437 EXPORT_SYMBOL(target_destroy_export);
2438 EXPORT_SYMBOL(target_cancel_recovery_timer);
2439 EXPORT_SYMBOL(target_send_reply);
2440 EXPORT_SYMBOL(target_queue_recovery_request);
2441 EXPORT_SYMBOL(target_handle_ping);
2442 EXPORT_SYMBOL(target_pack_pool_reply);
2443 EXPORT_SYMBOL(target_handle_disconnect);
2444
2445 /* l_lock.c */
2446 EXPORT_SYMBOL(lock_res_and_lock);
2447 EXPORT_SYMBOL(unlock_res_and_lock);