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