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