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