1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002-2004 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
28 # define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_LDLM
33 # include <libcfs/libcfs.h>
35 # include <liblustre.h>
38 #include <lustre_dlm.h>
39 #include <obd_class.h>
40 #include <libcfs/list.h>
41 #include "ldlm_internal.h"
44 static int ldlm_num_threads;
45 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
46 "number of DLM service threads to start");
49 extern cfs_mem_cache_t *ldlm_resource_slab;
50 extern cfs_mem_cache_t *ldlm_lock_slab;
51 static struct semaphore ldlm_ref_sem;
52 static int ldlm_refcount;
56 static struct ldlm_state *ldlm_state;
58 inline cfs_time_t round_timeout(cfs_time_t timeout)
60 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
63 /* timeout for initial callback (AST) reply */
64 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
65 unsigned int obd_timeout)
67 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
69 return timeout < 1 ? 1 : timeout;
73 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
74 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
75 static struct list_head waiting_locks_list;
76 static cfs_timer_t waiting_locks_timer;
78 static struct expired_lock_thread {
79 cfs_waitq_t elt_waitq;
82 struct list_head elt_expired_locks;
83 } expired_lock_thread;
88 #define ELT_TERMINATE 2
94 * blp_prio_list is used for callbacks that should be handled
95 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
98 struct list_head blp_prio_list;
101 * blp_list is used for all other callbacks which are likely
102 * to take longer to process.
104 struct list_head blp_list;
106 cfs_waitq_t blp_waitq;
107 struct completion blp_comp;
108 atomic_t blp_num_threads;
109 atomic_t blp_busy_threads;
114 struct ldlm_bl_work_item {
115 struct list_head blwi_entry;
116 struct ldlm_namespace *blwi_ns;
117 struct ldlm_lock_desc blwi_ld;
118 struct ldlm_lock *blwi_lock;
119 struct list_head blwi_head;
125 static inline int have_expired_locks(void)
130 spin_lock_bh(&waiting_locks_spinlock);
131 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
132 spin_unlock_bh(&waiting_locks_spinlock);
137 static int expired_lock_main(void *arg)
139 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
140 struct l_wait_info lwi = { 0 };
144 cfs_daemonize("ldlm_elt");
146 expired_lock_thread.elt_state = ELT_READY;
147 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
150 l_wait_event(expired_lock_thread.elt_waitq,
151 have_expired_locks() ||
152 expired_lock_thread.elt_state == ELT_TERMINATE,
155 spin_lock_bh(&waiting_locks_spinlock);
156 if (expired_lock_thread.elt_dump) {
157 spin_unlock_bh(&waiting_locks_spinlock);
159 /* from waiting_locks_callback, but not in timer */
160 libcfs_debug_dumplog();
161 libcfs_run_lbug_upcall(__FILE__,
162 "waiting_locks_callback",
163 expired_lock_thread.elt_dump);
165 spin_lock_bh(&waiting_locks_spinlock);
166 expired_lock_thread.elt_dump = 0;
171 while (!list_empty(expired)) {
172 struct obd_export *export;
173 struct ldlm_lock *lock;
175 lock = list_entry(expired->next, struct ldlm_lock,
177 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
178 (void *)lock >= LP_POISON) {
179 spin_unlock_bh(&waiting_locks_spinlock);
180 CERROR("free lock on elt list %p\n", lock);
183 list_del_init(&lock->l_pending_chain);
184 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
185 (void *)lock->l_export >= LP_POISON) {
186 CERROR("lock with free export on elt list %p\n",
188 lock->l_export = NULL;
189 LDLM_ERROR(lock, "free export");
192 export = class_export_get(lock->l_export);
193 spin_unlock_bh(&waiting_locks_spinlock);
196 class_fail_export(export);
197 class_export_put(export);
198 spin_lock_bh(&waiting_locks_spinlock);
200 spin_unlock_bh(&waiting_locks_spinlock);
202 if (do_dump && obd_dump_on_eviction) {
203 CERROR("dump the log upon eviction\n");
204 libcfs_debug_dumplog();
207 if (expired_lock_thread.elt_state == ELT_TERMINATE)
211 expired_lock_thread.elt_state = ELT_STOPPED;
212 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
216 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
218 /* This is called from within a timer interrupt and cannot schedule */
219 static void waiting_locks_callback(unsigned long unused)
221 struct ldlm_lock *lock, *last = NULL;
224 spin_lock_bh(&waiting_locks_spinlock);
225 while (!list_empty(&waiting_locks_list)) {
226 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
229 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
230 (lock->l_req_mode == LCK_GROUP))
233 if (ptlrpc_check_suspend()) {
234 /* there is a case when we talk to one mds, holding
235 * lock from another mds. this way we easily can get
236 * here, if second mds is being recovered. so, we
237 * suspend timeouts. bug 6019 */
239 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
240 lock->l_export->exp_client_uuid.uuid,
241 lock->l_export->exp_connection->c_remote_uuid.uuid,
242 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
244 list_del_init(&lock->l_pending_chain);
245 spin_unlock_bh(&waiting_locks_spinlock);
246 ldlm_add_waiting_lock(lock);
250 /* if timeout overlaps the activation time of suspended timeouts
251 * then extend it to give a chance for client to reconnect */
252 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
253 cfs_time_seconds(obd_timeout)/2),
254 ptlrpc_suspend_wakeup_time())) {
255 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
256 lock->l_export->exp_client_uuid.uuid,
257 lock->l_export->exp_connection->c_remote_uuid.uuid,
258 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
260 list_del_init(&lock->l_pending_chain);
261 spin_unlock_bh(&waiting_locks_spinlock);
262 ldlm_add_waiting_lock(lock);
266 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
268 lock->l_export->exp_client_uuid.uuid,
269 lock->l_export->exp_connection->c_remote_uuid.uuid,
270 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
274 list_del(&lock->l_pending_chain);
275 list_add(&lock->l_pending_chain,
276 &expired_lock_thread.elt_expired_locks);
279 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
280 if (obd_dump_on_timeout)
281 expired_lock_thread.elt_dump = __LINE__;
283 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
287 * Make sure the timer will fire again if we have any locks
290 if (!list_empty(&waiting_locks_list)) {
291 cfs_time_t timeout_rounded;
292 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
294 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
295 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
297 spin_unlock_bh(&waiting_locks_spinlock);
301 * Indicate that we're waiting for a client to call us back cancelling a given
302 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
303 * timer to fire appropriately. (We round up to the next second, to avoid
304 * floods of timer firings during periods of high lock contention and traffic).
306 * Called with the namespace lock held.
308 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
310 cfs_time_t timeout_rounded;
312 if (!list_empty(&lock->l_pending_chain))
315 lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
316 cfs_time_seconds(obd_timeout)/2);
318 timeout_rounded = round_timeout(lock->l_callback_timeout);
320 if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
321 !cfs_timer_is_armed(&waiting_locks_timer)) {
322 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
325 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
329 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
333 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
335 spin_lock_bh(&waiting_locks_spinlock);
336 if (lock->l_destroyed) {
337 static cfs_time_t next;
338 spin_unlock_bh(&waiting_locks_spinlock);
339 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
340 if (cfs_time_after(cfs_time_current(), next)) {
341 next = cfs_time_shift(14400);
342 libcfs_debug_dumpstack(NULL);
347 ret = __ldlm_add_waiting_lock(lock);
348 spin_unlock_bh(&waiting_locks_spinlock);
350 LDLM_DEBUG(lock, "%sadding to wait list",
351 ret == 0 ? "not re-" : "");
356 * Remove a lock from the pending list, likely because it had its cancellation
357 * callback arrive without incident. This adjusts the lock-timeout timer if
358 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
360 * Called with namespace lock held.
362 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
364 struct list_head *list_next;
366 if (list_empty(&lock->l_pending_chain))
369 list_next = lock->l_pending_chain.next;
370 if (lock->l_pending_chain.prev == &waiting_locks_list) {
371 /* Removing the head of the list, adjust timer. */
372 if (list_next == &waiting_locks_list) {
373 /* No more, just cancel. */
374 cfs_timer_disarm(&waiting_locks_timer);
376 struct ldlm_lock *next;
377 next = list_entry(list_next, struct ldlm_lock,
379 cfs_timer_arm(&waiting_locks_timer,
380 round_timeout(next->l_callback_timeout));
383 list_del_init(&lock->l_pending_chain);
388 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
392 if (lock->l_export == NULL) {
393 /* We don't have a "waiting locks list" on clients. */
394 LDLM_DEBUG(lock, "client lock: no-op");
398 spin_lock_bh(&waiting_locks_spinlock);
399 ret = __ldlm_del_waiting_lock(lock);
400 spin_unlock_bh(&waiting_locks_spinlock);
402 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
409 * Called with namespace lock held.
411 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
413 if (lock->l_export == NULL) {
414 /* We don't have a "waiting locks list" on clients. */
415 LDLM_DEBUG(lock, "client lock: no-op");
419 spin_lock_bh(&waiting_locks_spinlock);
421 if (list_empty(&lock->l_pending_chain)) {
422 spin_unlock_bh(&waiting_locks_spinlock);
423 LDLM_DEBUG(lock, "wasn't waiting");
427 __ldlm_del_waiting_lock(lock);
428 __ldlm_add_waiting_lock(lock);
429 spin_unlock_bh(&waiting_locks_spinlock);
431 LDLM_DEBUG(lock, "refreshed");
435 #else /* !__KERNEL__ */
437 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
439 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
443 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
448 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
452 #endif /* __KERNEL__ */
454 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
455 const char *ast_type)
457 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
458 char *str = libcfs_nid2str(conn->c_peer.nid);
460 LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
461 "service %s.\n", str,
462 lock->l_export->exp_obd->obd_name);
464 LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
465 "resource %d\n", ast_type,
466 obd_export_nid2str(lock->l_export), rc);
468 if (obd_dump_on_timeout)
469 libcfs_debug_dumplog();
470 class_fail_export(lock->l_export);
473 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
474 struct ptlrpc_request *req, int rc,
475 const char *ast_type)
477 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
479 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
480 LASSERT(lock->l_export);
481 if (lock->l_export->exp_libclient) {
482 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
483 " timeout, just cancelling lock", ast_type,
484 libcfs_nid2str(peer.nid));
485 ldlm_lock_cancel(lock);
487 } else if (lock->l_flags & LDLM_FL_CANCEL) {
488 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
489 "cancel was received (AST reply lost?)",
490 ast_type, libcfs_nid2str(peer.nid));
491 ldlm_lock_cancel(lock);
494 ldlm_del_waiting_lock(lock);
495 ldlm_failed_ast(lock, rc, ast_type);
499 LDLM_DEBUG(lock, "client (nid %s) returned %d"
500 " from %s AST - normal race",
501 libcfs_nid2str(peer.nid),
503 lustre_msg_get_status(req->rq_repmsg) : -1,
506 LDLM_ERROR(lock, "client (nid %s) returned %d "
507 "from %s AST", libcfs_nid2str(peer.nid),
508 (req->rq_repmsg != NULL) ?
509 lustre_msg_get_status(req->rq_repmsg) : 0,
511 ldlm_lock_cancel(lock);
512 /* Server-side AST functions are called from ldlm_reprocess_all,
513 * which needs to be told to please restart its reprocessing. */
520 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
522 struct ldlm_cb_set_arg *arg;
523 struct ldlm_lock *lock;
526 LASSERT(data != NULL);
528 arg = req->rq_async_args.pointer_arg[0];
529 lock = req->rq_async_args.pointer_arg[1];
530 LASSERT(lock != NULL);
532 /* If client canceled the lock but the cancel has not
533 * been recieved yet, we need to update lvbo to have the
534 * proper attributes cached. */
535 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
536 ldlm_res_lvbo_update(lock->l_resource, NULL,
538 rc = ldlm_handle_ast_error(lock, req, rc,
539 arg->type == LDLM_BL_CALLBACK
540 ? "blocking" : "completion");
546 atomic_set(&arg->restart, 1);
551 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
552 struct ldlm_cb_set_arg *arg,
553 struct ldlm_lock *lock,
559 if (unlikely(instant_cancel)) {
560 rc = ptl_send_rpc(req, 1);
561 ptlrpc_req_finished(req);
563 /* If we cancelled the lock, we need to restart
564 * ldlm_reprocess_queue */
565 atomic_set(&arg->restart, 1);
568 ptlrpc_set_add_req(arg->set, req);
575 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
576 * enqueued server lock conflicts with given one.
578 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
579 * to wait for client response.
581 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
582 struct ldlm_lock_desc *desc,
583 void *data, int flag)
585 struct ldlm_cb_set_arg *arg = data;
586 struct ldlm_request *body;
587 struct ptlrpc_request *req;
588 int instant_cancel = 0;
592 if (flag == LDLM_CB_CANCELING)
593 /* Don't need to do anything here. */
597 LASSERT(data != NULL);
598 if (lock->l_export->exp_obd->obd_recovering != 0) {
599 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
600 ldlm_lock_dump(D_ERROR, lock, 0);
603 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
604 &RQF_LDLM_BL_CALLBACK,
605 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
609 req->rq_async_args.pointer_arg[0] = arg;
610 req->rq_async_args.pointer_arg[1] = lock;
611 req->rq_interpret_reply = ldlm_cb_interpret;
612 req->rq_no_resend = 1;
614 lock_res(lock->l_resource);
615 if (lock->l_granted_mode != lock->l_req_mode) {
616 /* this blocking AST will be communicated as part of the
617 * completion AST instead */
618 unlock_res(lock->l_resource);
619 ptlrpc_req_finished(req);
620 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
624 if (lock->l_destroyed) {
625 /* What's the point? */
626 unlock_res(lock->l_resource);
627 ptlrpc_req_finished(req);
631 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
634 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
635 body->lock_handle[0] = lock->l_remote_handle;
636 body->lock_desc = *desc;
637 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
639 LDLM_DEBUG(lock, "server preparing blocking AST");
641 ptlrpc_request_set_replen(req);
642 if (instant_cancel) {
643 unlock_res(lock->l_resource);
644 ldlm_lock_cancel(lock);
646 LASSERT(lock->l_granted_mode == lock->l_req_mode);
647 ldlm_add_waiting_lock(lock);
648 unlock_res(lock->l_resource);
651 req->rq_send_state = LUSTRE_IMP_FULL;
652 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
654 if (lock->l_export && lock->l_export->exp_ldlm_stats)
655 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
656 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
658 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
663 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
665 struct ldlm_cb_set_arg *arg = data;
666 struct ldlm_request *body;
667 struct ptlrpc_request *req;
668 struct timeval granted_time;
669 long total_enqueue_wait;
670 int instant_cancel = 0;
674 LASSERT(lock != NULL);
675 LASSERT(data != NULL);
677 do_gettimeofday(&granted_time);
678 total_enqueue_wait = cfs_timeval_sub(&granted_time,
679 &lock->l_enqueued_time, NULL);
681 if (total_enqueue_wait / 1000000 > obd_timeout)
682 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
683 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
685 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
686 &RQF_LDLM_CP_CALLBACK);
690 lock_res_and_lock(lock);
691 if (lock->l_resource->lr_lvb_len)
692 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
693 lock->l_resource->lr_lvb_len);
694 unlock_res_and_lock(lock);
696 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
698 ptlrpc_request_free(req);
702 req->rq_async_args.pointer_arg[0] = arg;
703 req->rq_async_args.pointer_arg[1] = lock;
704 req->rq_interpret_reply = ldlm_cb_interpret;
705 req->rq_no_resend = 1;
706 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
708 body->lock_handle[0] = lock->l_remote_handle;
709 body->lock_flags = flags;
710 ldlm_lock2desc(lock, &body->lock_desc);
711 if (lock->l_resource->lr_lvb_len) {
712 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
714 lock_res_and_lock(lock);
715 memcpy(lvb, lock->l_resource->lr_lvb_data,
716 lock->l_resource->lr_lvb_len);
717 unlock_res_and_lock(lock);
720 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
723 ptlrpc_request_set_replen(req);
724 req->rq_send_state = LUSTRE_IMP_FULL;
725 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
727 /* We only send real blocking ASTs after the lock is granted */
728 lock_res_and_lock(lock);
729 if (lock->l_flags & LDLM_FL_AST_SENT) {
730 body->lock_flags |= LDLM_FL_AST_SENT;
732 /* We might get here prior to ldlm_handle_enqueue setting
733 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
734 * into waiting list, but this is safe and similar code in
735 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
736 * that would not only cancel the lock, but will also remove
737 * it from waiting list */
738 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
739 unlock_res_and_lock(lock);
740 ldlm_lock_cancel(lock);
742 lock_res_and_lock(lock);
744 /* start the lock-timeout clock */
745 ldlm_add_waiting_lock(lock);
748 unlock_res_and_lock(lock);
750 if (lock->l_export && lock->l_export->exp_ldlm_stats)
751 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
752 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
754 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
759 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
761 struct ldlm_resource *res = lock->l_resource;
762 struct ldlm_request *body;
763 struct ptlrpc_request *req;
767 LASSERT(lock != NULL);
769 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
770 &RQF_LDLM_GL_CALLBACK,
771 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
776 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
777 body->lock_handle[0] = lock->l_remote_handle;
778 ldlm_lock2desc(lock, &body->lock_desc);
780 lock_res_and_lock(lock);
781 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
782 lock->l_resource->lr_lvb_len);
783 unlock_res_and_lock(lock);
784 res = lock->l_resource;
785 ptlrpc_request_set_replen(req);
788 req->rq_send_state = LUSTRE_IMP_FULL;
789 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
791 if (lock->l_export && lock->l_export->exp_ldlm_stats)
792 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
793 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
795 rc = ptlrpc_queue_wait(req);
796 if (rc == -ELDLM_NO_LOCK_DATA)
797 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
799 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
801 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
803 ptlrpc_req_finished(req);
807 static struct ldlm_lock *
808 find_existing_lock(struct obd_export *exp,
809 const struct lustre_handle *remote_hdl)
811 struct list_head *iter;
813 spin_lock(&exp->exp_ldlm_data.led_lock);
814 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
815 struct ldlm_lock *lock;
816 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
817 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
819 spin_unlock(&exp->exp_ldlm_data.led_lock);
823 spin_unlock(&exp->exp_ldlm_data.led_lock);
828 extern unsigned long long lu_time_stamp_get(void);
830 #define lu_time_stamp_get() time(NULL)
833 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
834 struct lprocfs_stats *srv_stats)
836 int lock_type = 0, op = 0;
838 lock_type = dlm_req->lock_desc.l_resource.lr_type;
842 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
845 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
846 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
848 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
851 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
854 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
862 lprocfs_counter_incr(srv_stats, op);
868 * Main server-side entry point into LDLM. This is called by ptlrpc service
869 * threads to carry out client lock enqueueing requests.
871 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
872 struct ptlrpc_request *req,
873 const struct ldlm_request *dlm_req,
874 const struct ldlm_callback_suite *cbs)
876 struct ldlm_reply *dlm_rep;
878 ldlm_error_t err = ELDLM_OK;
879 struct ldlm_lock *lock = NULL;
884 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
886 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
887 flags = dlm_req->lock_flags;
889 LASSERT(req->rq_export);
891 if (req->rq_rqbd->rqbd_service->srv_stats)
892 ldlm_svc_get_eopc(dlm_req,
893 req->rq_rqbd->rqbd_service->srv_stats);
895 if (req->rq_export->exp_ldlm_stats)
896 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
897 LDLM_ENQUEUE - LDLM_FIRST_OPC);
899 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
900 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
901 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
902 dlm_req->lock_desc.l_resource.lr_type);
903 GOTO(out, rc = -EFAULT);
906 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
907 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
908 dlm_req->lock_desc.l_req_mode &
909 (dlm_req->lock_desc.l_req_mode-1))) {
910 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
911 dlm_req->lock_desc.l_req_mode);
912 GOTO(out, rc = -EFAULT);
915 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
916 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
918 DEBUG_REQ(D_ERROR, req,
919 "PLAIN lock request from IBITS client?");
920 GOTO(out, rc = -EPROTO);
922 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
924 DEBUG_REQ(D_ERROR, req,
925 "IBITS lock request from unaware client?");
926 GOTO(out, rc = -EPROTO);
930 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
931 against server's _CONNECT_SUPPORTED flags? (I don't want to use
932 ibits for mgc/mgs) */
934 /* INODEBITS_INTEROP: Perform conversion from plain lock to
935 * inodebits lock if client does not support them. */
936 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
937 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
938 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
939 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
940 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
941 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
942 dlm_req->lock_desc.l_req_mode = LCK_CR;
946 if (unlikely(flags & LDLM_FL_REPLAY)) {
947 lock = find_existing_lock(req->rq_export,
948 &dlm_req->lock_handle[0]);
950 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
951 LPX64, lock->l_handle.h_cookie);
952 GOTO(existing_lock, rc = 0);
956 /* The lock's callback data might be set in the policy function */
957 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
958 dlm_req->lock_desc.l_resource.lr_type,
959 dlm_req->lock_desc.l_req_mode,
960 cbs->lcs_blocking, cbs->lcs_completion,
961 cbs->lcs_glimpse, NULL, 0);
964 GOTO(out, rc = -ENOMEM);
966 do_gettimeofday(&lock->l_enqueued_time);
967 lock->l_remote_handle = dlm_req->lock_handle[0];
968 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
970 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
971 /* Don't enqueue a lock onto the export if it has already
972 * been evicted. Cancel it now instead. (bug 3822) */
973 if (req->rq_export->exp_failed) {
974 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
975 GOTO(out, rc = -ENOTCONN);
977 lock->l_export = class_export_get(req->rq_export);
978 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
979 list_add(&lock->l_export_chain,
980 &lock->l_export->exp_ldlm_data.led_held_locks);
981 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
985 if (flags & LDLM_FL_HAS_INTENT) {
986 /* In this case, the reply buffer is allocated deep in
987 * local_lock_enqueue by the policy function. */
990 lock_res_and_lock(lock);
991 if (lock->l_resource->lr_lvb_len) {
992 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
994 lock->l_resource->lr_lvb_len);
996 unlock_res_and_lock(lock);
998 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
999 GOTO(out, rc = -ENOMEM);
1001 rc = req_capsule_server_pack(&req->rq_pill);
1006 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1007 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1008 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1009 lock->l_req_extent = lock->l_policy_data.l_extent;
1011 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1015 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1016 dlm_rep->lock_flags = flags;
1018 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1019 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1021 /* We never send a blocking AST until the lock is granted, but
1022 * we can tell it right now */
1023 lock_res_and_lock(lock);
1025 /* Now take into account flags to be inherited from original lock
1026 request both in reply to client and in our own lock flags. */
1027 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1028 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1030 /* Don't move a pending lock onto the export if it has already
1031 * been evicted. Cancel it now instead. (bug 5683) */
1032 if (unlikely(req->rq_export->exp_failed ||
1033 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1034 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1036 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1037 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1038 if (lock->l_granted_mode == lock->l_req_mode) {
1040 * Only cancel lock if it was granted, because it would
1041 * be destroyed immediatelly and would never be granted
1042 * in the future, causing timeouts on client. Not
1043 * granted lock will be cancelled immediatelly after
1044 * sending completion AST.
1046 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1047 unlock_res_and_lock(lock);
1048 ldlm_lock_cancel(lock);
1049 lock_res_and_lock(lock);
1051 ldlm_add_waiting_lock(lock);
1054 /* Make sure we never ever grant usual metadata locks to liblustre
1056 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1057 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1058 req->rq_export->exp_libclient) {
1059 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1060 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1061 CERROR("Granting sync lock to libclient. "
1062 "req fl %d, rep fl %d, lock fl %d\n",
1063 dlm_req->lock_flags, dlm_rep->lock_flags,
1065 LDLM_ERROR(lock, "sync lock");
1066 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1067 struct ldlm_intent *it;
1069 it = req_capsule_client_get(&req->rq_pill,
1072 CERROR("This is intent %s ("LPU64")\n",
1073 ldlm_it2str(it->opc), it->opc);
1079 unlock_res_and_lock(lock);
1083 req->rq_status = err;
1084 if (req->rq_reply_state == NULL) {
1085 err = lustre_pack_reply(req, 1, NULL, NULL);
1088 req->rq_status = rc;
1091 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1092 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1094 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1095 "(err=%d, rc=%d)", err, rc);
1097 lock_res_and_lock(lock);
1099 if (lock->l_resource->lr_lvb_len > 0) {
1102 lvb = req_capsule_server_get(&req->rq_pill,
1104 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1107 memcpy(lvb, lock->l_resource->lr_lvb_data,
1108 lock->l_resource->lr_lvb_len);
1111 ldlm_resource_unlink_lock(lock);
1112 ldlm_lock_destroy_nolock(lock);
1114 unlock_res_and_lock(lock);
1116 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1117 ldlm_reprocess_all(lock->l_resource);
1119 LDLM_LOCK_PUT(lock);
1122 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1128 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1129 ldlm_completion_callback completion_callback,
1130 ldlm_blocking_callback blocking_callback,
1131 ldlm_glimpse_callback glimpse_callback)
1133 struct ldlm_request *dlm_req;
1134 struct ldlm_callback_suite cbs = {
1135 .lcs_completion = completion_callback,
1136 .lcs_blocking = blocking_callback,
1137 .lcs_glimpse = glimpse_callback
1141 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1142 if (dlm_req != NULL) {
1143 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1144 req, dlm_req, &cbs);
1151 int ldlm_handle_convert0(struct ptlrpc_request *req,
1152 const struct ldlm_request *dlm_req)
1154 struct ldlm_reply *dlm_rep;
1155 struct ldlm_lock *lock;
1159 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1160 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1161 LDLM_CONVERT - LDLM_FIRST_OPC);
1163 rc = req_capsule_server_pack(&req->rq_pill);
1167 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1168 dlm_rep->lock_flags = dlm_req->lock_flags;
1170 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1172 req->rq_status = EINVAL;
1176 LDLM_DEBUG(lock, "server-side convert handler START");
1178 do_gettimeofday(&lock->l_enqueued_time);
1179 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1180 &dlm_rep->lock_flags);
1182 if (ldlm_del_waiting_lock(lock))
1183 LDLM_DEBUG(lock, "converted waiting lock");
1186 req->rq_status = EDEADLOCK;
1191 if (!req->rq_status)
1192 ldlm_reprocess_all(lock->l_resource);
1193 LDLM_DEBUG(lock, "server-side convert handler END");
1194 LDLM_LOCK_PUT(lock);
1196 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1201 int ldlm_handle_convert(struct ptlrpc_request *req)
1204 struct ldlm_request *dlm_req;
1206 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1207 if (dlm_req != NULL) {
1208 rc = ldlm_handle_convert0(req, dlm_req);
1210 CERROR ("Can't unpack dlm_req\n");
1216 /* Cancel all the locks, which handles are packed into ldlm_request */
1217 int ldlm_request_cancel(struct ptlrpc_request *req,
1218 const struct ldlm_request *dlm_req, int first)
1220 struct ldlm_resource *res, *pres = NULL;
1221 struct ldlm_lock *lock;
1222 int i, count, done = 0;
1225 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1229 /* There is no lock on the server at the replay time,
1230 * skip lock cancelling to make replay tests to pass. */
1231 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1234 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1235 "starting at %d", count, first);
1237 for (i = first; i < count; i++) {
1238 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1240 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1241 "lock (cookie "LPU64")",
1242 dlm_req->lock_handle[i].cookie);
1246 res = lock->l_resource;
1251 ldlm_reprocess_all(pres);
1252 ldlm_resource_putref(pres);
1255 ldlm_resource_getref(res);
1256 ldlm_res_lvbo_update(res, NULL, 0, 1);
1260 ldlm_lock_cancel(lock);
1261 LDLM_LOCK_PUT(lock);
1264 ldlm_reprocess_all(pres);
1265 ldlm_resource_putref(pres);
1267 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1271 int ldlm_handle_cancel(struct ptlrpc_request *req)
1273 struct ldlm_request *dlm_req;
1277 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1278 if (dlm_req == NULL) {
1279 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1283 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1284 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1285 LDLM_CANCEL - LDLM_FIRST_OPC);
1287 rc = req_capsule_server_pack(&req->rq_pill);
1291 if (!ldlm_request_cancel(req, dlm_req, 0))
1292 req->rq_status = ESTALE;
1294 if (ptlrpc_reply(req) != 0)
1300 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1301 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1306 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1308 lock_res_and_lock(lock);
1309 lock->l_flags |= LDLM_FL_CBPENDING;
1311 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1312 lock->l_flags |= LDLM_FL_CANCEL;
1314 do_ast = (!lock->l_readers && !lock->l_writers);
1315 unlock_res_and_lock(lock);
1318 LDLM_DEBUG(lock, "already unused, calling "
1319 "callback (%p)", lock->l_blocking_ast);
1320 if (lock->l_blocking_ast != NULL)
1321 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1324 LDLM_DEBUG(lock, "Lock still has references, will be"
1325 " cancelled later");
1328 LDLM_DEBUG(lock, "client blocking callback handler END");
1329 LDLM_LOCK_PUT(lock);
1333 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1334 struct ldlm_namespace *ns,
1335 struct ldlm_request *dlm_req,
1336 struct ldlm_lock *lock)
1338 CFS_LIST_HEAD(ast_list);
1341 LDLM_DEBUG(lock, "client completion callback handler START");
1343 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1344 int to = cfs_time_seconds(1);
1346 to = schedule_timeout(to);
1347 if (lock->l_granted_mode == lock->l_req_mode ||
1353 lock_res_and_lock(lock);
1354 if (lock->l_destroyed ||
1355 lock->l_granted_mode == lock->l_req_mode) {
1356 /* bug 11300: the lock has already been granted */
1357 unlock_res_and_lock(lock);
1358 LDLM_DEBUG(lock, "Double grant race happened");
1359 LDLM_LOCK_PUT(lock);
1364 /* If we receive the completion AST before the actual enqueue returned,
1365 * then we might need to switch lock modes, resources, or extents. */
1366 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1367 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1368 LDLM_DEBUG(lock, "completion AST, new lock mode");
1371 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1372 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1373 LDLM_DEBUG(lock, "completion AST, new policy data");
1376 ldlm_resource_unlink_lock(lock);
1377 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1378 &lock->l_resource->lr_name,
1379 sizeof(lock->l_resource->lr_name)) != 0) {
1380 unlock_res_and_lock(lock);
1381 if (ldlm_lock_change_resource(ns, lock,
1382 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1383 LDLM_ERROR(lock, "Failed to allocate resource");
1384 LDLM_LOCK_PUT(lock);
1388 LDLM_DEBUG(lock, "completion AST, new resource");
1389 CERROR("change resource!\n");
1390 lock_res_and_lock(lock);
1393 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1394 /* BL_AST locks are not needed in lru.
1395 * let ldlm_cancel_lru() be fast. */
1396 ldlm_lock_remove_from_lru(lock);
1397 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1398 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1401 if (lock->l_lvb_len) {
1402 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1403 RCL_CLIENT) < lock->l_lvb_len) {
1404 LDLM_ERROR(lock, "completion AST did not contain "
1407 void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1409 (void *)lock->l_lvb_swabber);
1410 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1414 ldlm_grant_lock(lock, &ast_list);
1415 unlock_res_and_lock(lock);
1417 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1419 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1421 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1423 LDLM_LOCK_PUT(lock);
1427 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1428 struct ldlm_namespace *ns,
1429 struct ldlm_request *dlm_req,
1430 struct ldlm_lock *lock)
1435 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1437 if (lock->l_glimpse_ast != NULL)
1438 rc = lock->l_glimpse_ast(lock, req);
1440 if (req->rq_repmsg != NULL) {
1443 req->rq_status = rc;
1447 lock_res_and_lock(lock);
1448 if (lock->l_granted_mode == LCK_PW &&
1449 !lock->l_readers && !lock->l_writers &&
1450 cfs_time_after(cfs_time_current(),
1451 cfs_time_add(lock->l_last_used,
1452 cfs_time_seconds(10)))) {
1453 unlock_res_and_lock(lock);
1454 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1455 ldlm_handle_bl_callback(ns, NULL, lock);
1460 unlock_res_and_lock(lock);
1461 LDLM_LOCK_PUT(lock);
1465 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1467 if (req->rq_no_reply)
1470 req->rq_status = rc;
1471 if (req->rq_reply_state == NULL) {
1472 rc = lustre_pack_reply(req, 1, NULL, NULL);
1476 return ptlrpc_reply(req);
1480 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1481 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1482 struct list_head *cancels, int count)
1484 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1485 struct ldlm_bl_work_item *blwi;
1488 if (cancels && count == 0)
1491 OBD_ALLOC(blwi, sizeof(*blwi));
1497 blwi->blwi_ld = *ld;
1499 list_add(&blwi->blwi_head, cancels);
1500 list_del_init(cancels);
1501 blwi->blwi_count = count;
1503 blwi->blwi_lock = lock;
1505 spin_lock(&blp->blp_lock);
1506 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1507 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1508 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1510 /* other blocking callbacks are added to the regular list */
1511 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1513 cfs_waitq_signal(&blp->blp_waitq);
1514 spin_unlock(&blp->blp_lock);
1520 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1521 struct ldlm_lock *lock)
1524 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1530 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1531 struct list_head *cancels, int count)
1534 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1540 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1541 static int ldlm_callback_handler(struct ptlrpc_request *req)
1543 struct ldlm_namespace *ns;
1544 struct ldlm_request *dlm_req;
1545 struct ldlm_lock *lock;
1549 /* Requests arrive in sender's byte order. The ptlrpc service
1550 * handler has already checked and, if necessary, byte-swapped the
1551 * incoming request message body, but I am responsible for the
1552 * message buffers. */
1554 /* do nothing for sec context finalize */
1555 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1558 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1560 if (req->rq_export == NULL) {
1561 struct ldlm_request *dlm_req;
1563 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1564 "export cookie "LPX64"; this is "
1565 "normal if this node rebooted with a lock held\n",
1566 lustre_msg_get_opc(req->rq_reqmsg),
1567 libcfs_id2str(req->rq_peer),
1568 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1570 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1571 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1572 if (dlm_req != NULL)
1573 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1574 dlm_req->lock_handle[0].cookie);
1576 ldlm_callback_reply(req, -ENOTCONN);
1580 LASSERT(req->rq_export != NULL);
1581 LASSERT(req->rq_export->exp_obd != NULL);
1583 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1584 case LDLM_BL_CALLBACK:
1585 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1588 case LDLM_CP_CALLBACK:
1589 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1592 case LDLM_GL_CALLBACK:
1593 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1596 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1597 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1598 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1600 rc = llog_origin_handle_cancel(req);
1601 ldlm_callback_reply(req, rc);
1603 case OBD_QC_CALLBACK:
1604 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1605 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1607 rc = target_handle_qc_callback(req);
1608 ldlm_callback_reply(req, rc);
1612 /* reply in handler */
1613 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1614 rc = target_handle_dqacq_callback(req);
1616 case LLOG_ORIGIN_HANDLE_CREATE:
1617 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1618 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1620 rc = llog_origin_handle_create(req);
1621 ldlm_callback_reply(req, rc);
1623 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1624 req_capsule_set(&req->rq_pill,
1625 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1626 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1628 rc = llog_origin_handle_next_block(req);
1629 ldlm_callback_reply(req, rc);
1631 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1632 req_capsule_set(&req->rq_pill,
1633 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1634 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1636 rc = llog_origin_handle_read_header(req);
1637 ldlm_callback_reply(req, rc);
1639 case LLOG_ORIGIN_HANDLE_CLOSE:
1640 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1642 rc = llog_origin_handle_close(req);
1643 ldlm_callback_reply(req, rc);
1646 CERROR("unknown opcode %u\n",
1647 lustre_msg_get_opc(req->rq_reqmsg));
1648 ldlm_callback_reply(req, -EPROTO);
1652 ns = req->rq_export->exp_obd->obd_namespace;
1653 LASSERT(ns != NULL);
1655 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1657 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1658 if (dlm_req == NULL) {
1659 ldlm_callback_reply(req, -EPROTO);
1663 /* Force a known safe race, send a cancel to the server for a lock
1664 * which the server has already started a blocking callback on. */
1665 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1666 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1667 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1669 CERROR("ldlm_cli_cancel: %d\n", rc);
1672 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1674 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1675 "disappeared\n", dlm_req->lock_handle[0].cookie);
1676 ldlm_callback_reply(req, -EINVAL);
1680 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1681 lock_res_and_lock(lock);
1682 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1683 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1684 /* If somebody cancels locks and cache is already droped,
1685 * we can tell the server we have no lock. Otherwise, we
1686 * should send cancel after dropping the cache. */
1687 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1688 (lock->l_flags & LDLM_FL_BL_DONE)) {
1689 LDLM_DEBUG(lock, "callback on lock "
1690 LPX64" - lock disappeared\n",
1691 dlm_req->lock_handle[0].cookie);
1692 unlock_res_and_lock(lock);
1693 LDLM_LOCK_PUT(lock);
1694 ldlm_callback_reply(req, -EINVAL);
1697 /* BL_AST locks are not needed in lru.
1698 * let ldlm_cancel_lru() be fast. */
1699 ldlm_lock_remove_from_lru(lock);
1700 lock->l_flags |= LDLM_FL_BL_AST;
1702 unlock_res_and_lock(lock);
1704 /* We want the ost thread to get this reply so that it can respond
1705 * to ost requests (write cache writeback) that might be triggered
1708 * But we'd also like to be able to indicate in the reply that we're
1709 * cancelling right now, because it's unused, or have an intent result
1710 * in the reply, so we might have to push the responsibility for sending
1711 * the reply down into the AST handlers, alas. */
1713 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1714 case LDLM_BL_CALLBACK:
1715 CDEBUG(D_INODE, "blocking ast\n");
1716 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1717 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1718 ldlm_callback_reply(req, 0);
1719 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1720 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1722 case LDLM_CP_CALLBACK:
1723 CDEBUG(D_INODE, "completion ast\n");
1724 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1725 ldlm_callback_reply(req, 0);
1726 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1728 case LDLM_GL_CALLBACK:
1729 CDEBUG(D_INODE, "glimpse ast\n");
1730 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1731 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1734 LBUG(); /* checked above */
1740 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1745 /* Requests arrive in sender's byte order. The ptlrpc service
1746 * handler has already checked and, if necessary, byte-swapped the
1747 * incoming request message body, but I am responsible for the
1748 * message buffers. */
1750 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1752 if (req->rq_export == NULL) {
1753 struct ldlm_request *dlm_req;
1755 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1756 lustre_msg_get_opc(req->rq_reqmsg),
1757 libcfs_id2str(req->rq_peer),
1758 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1760 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1761 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1762 if (dlm_req != NULL)
1763 ldlm_lock_dump_handle(D_ERROR,
1764 &dlm_req->lock_handle[0]);
1765 ldlm_callback_reply(req, -ENOTCONN);
1769 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1771 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1773 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1774 CDEBUG(D_INODE, "cancel\n");
1775 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1777 rc = ldlm_handle_cancel(req);
1781 case OBD_LOG_CANCEL:
1782 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1783 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1785 rc = llog_origin_handle_cancel(req);
1786 ldlm_callback_reply(req, rc);
1789 CERROR("invalid opcode %d\n",
1790 lustre_msg_get_opc(req->rq_reqmsg));
1791 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1792 ldlm_callback_reply(req, -EINVAL);
1798 void ldlm_revoke_export_locks(struct obd_export *exp)
1800 struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
1801 struct list_head rpc_list;
1802 struct ldlm_lock *lock, *next;
1805 CFS_INIT_LIST_HEAD(&rpc_list);
1807 spin_lock(&exp->exp_ldlm_data.led_lock);
1808 list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
1809 lock_res_and_lock(lock);
1811 if (lock->l_req_mode != lock->l_granted_mode) {
1812 unlock_res_and_lock(lock);
1816 LASSERT(lock->l_resource);
1817 if (lock->l_resource->lr_type != LDLM_IBITS &&
1818 lock->l_resource->lr_type != LDLM_PLAIN) {
1819 unlock_res_and_lock(lock);
1823 if (lock->l_flags & LDLM_FL_AST_SENT) {
1824 unlock_res_and_lock(lock);
1828 LASSERT(lock->l_blocking_ast);
1829 LASSERT(!lock->l_blocking_lock);
1831 lock->l_flags |= LDLM_FL_AST_SENT;
1832 list_move(&lock->l_export_chain, &rpc_list);
1833 LDLM_LOCK_GET(lock);
1835 unlock_res_and_lock(lock);
1837 spin_unlock(&exp->exp_ldlm_data.led_lock);
1839 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1845 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1847 struct ldlm_bl_work_item *blwi = NULL;
1848 static unsigned int num_bl = 0;
1850 spin_lock(&blp->blp_lock);
1851 /* process a request from the blp_list at least every blp_num_threads */
1852 if (!list_empty(&blp->blp_list) &&
1853 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1854 blwi = list_entry(blp->blp_list.next,
1855 struct ldlm_bl_work_item, blwi_entry);
1857 if (!list_empty(&blp->blp_prio_list))
1858 blwi = list_entry(blp->blp_prio_list.next,
1859 struct ldlm_bl_work_item, blwi_entry);
1862 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1864 list_del(&blwi->blwi_entry);
1866 spin_unlock(&blp->blp_lock);
1871 /* This only contains temporary data until the thread starts */
1872 struct ldlm_bl_thread_data {
1873 char bltd_name[CFS_CURPROC_COMM_MAX];
1874 struct ldlm_bl_pool *bltd_blp;
1875 struct completion bltd_comp;
1879 static int ldlm_bl_thread_main(void *arg);
1881 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1883 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1886 init_completion(&bltd.bltd_comp);
1887 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1889 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1890 atomic_read(&blp->blp_num_threads), rc);
1893 wait_for_completion(&bltd.bltd_comp);
1898 static int ldlm_bl_thread_main(void *arg)
1900 struct ldlm_bl_pool *blp;
1904 struct ldlm_bl_thread_data *bltd = arg;
1906 blp = bltd->bltd_blp;
1908 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1909 atomic_inc(&blp->blp_busy_threads);
1911 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1912 "ldlm_bl_%02d", bltd->bltd_num);
1913 cfs_daemonize(bltd->bltd_name);
1915 complete(&bltd->bltd_comp);
1916 /* cannot use bltd after this, it is only on caller's stack */
1920 struct l_wait_info lwi = { 0 };
1921 struct ldlm_bl_work_item *blwi = NULL;
1923 blwi = ldlm_bl_get_work(blp);
1928 atomic_dec(&blp->blp_busy_threads);
1929 l_wait_event_exclusive(blp->blp_waitq,
1930 (blwi = ldlm_bl_get_work(blp)) != NULL,
1932 busy = atomic_inc_return(&blp->blp_busy_threads);
1934 if (blwi->blwi_ns == NULL)
1935 /* added by ldlm_cleanup() */
1938 /* Not fatal if racy and have a few too many threads */
1939 if (unlikely(busy < blp->blp_max_threads &&
1940 busy >= atomic_read(&blp->blp_num_threads)))
1941 /* discard the return value, we tried */
1942 ldlm_bl_thread_start(blp);
1944 if (blwi->blwi_ns == NULL)
1945 /* added by ldlm_cleanup() */
1949 if (blwi->blwi_count) {
1950 /* The special case when we cancel locks in lru
1951 * asynchronously, we pass the list of locks here.
1952 * Thus lock is marked LDLM_FL_CANCELING, and already
1953 * canceled locally. */
1954 ldlm_cli_cancel_list(&blwi->blwi_head,
1955 blwi->blwi_count, NULL, 0);
1957 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1960 OBD_FREE(blwi, sizeof(*blwi));
1963 atomic_dec(&blp->blp_busy_threads);
1964 atomic_dec(&blp->blp_num_threads);
1965 complete(&blp->blp_comp);
1971 static int ldlm_setup(void);
1972 static int ldlm_cleanup(int force);
1974 int ldlm_get_ref(void)
1978 mutex_down(&ldlm_ref_sem);
1979 if (++ldlm_refcount == 1) {
1984 mutex_up(&ldlm_ref_sem);
1989 void ldlm_put_ref(int force)
1992 mutex_down(&ldlm_ref_sem);
1993 if (ldlm_refcount == 1) {
1994 int rc = ldlm_cleanup(force);
1996 CERROR("ldlm_cleanup failed: %d\n", rc);
2002 mutex_up(&ldlm_ref_sem);
2007 static int ldlm_setup(void)
2009 struct ldlm_bl_pool *blp;
2011 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2012 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2018 if (ldlm_state != NULL)
2021 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2022 if (ldlm_state == NULL)
2026 rc = ldlm_proc_setup();
2032 if (ldlm_num_threads) {
2033 /* If ldlm_num_threads is set, it is the min and the max. */
2034 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2035 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2036 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2037 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2038 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2042 ldlm_state->ldlm_cb_service =
2043 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2044 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2045 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
2046 ldlm_callback_handler, "ldlm_cbd",
2047 ldlm_svc_proc_dir, NULL,
2048 ldlm_min_threads, ldlm_max_threads,
2050 LCT_MD_THREAD|LCT_DT_THREAD);
2052 if (!ldlm_state->ldlm_cb_service) {
2053 CERROR("failed to start service\n");
2054 GOTO(out_proc, rc = -ENOMEM);
2057 ldlm_state->ldlm_cancel_service =
2058 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2059 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2060 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
2061 ldlm_cancel_handler, "ldlm_canceld",
2062 ldlm_svc_proc_dir, NULL,
2063 ldlm_min_threads, ldlm_max_threads,
2065 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2067 if (!ldlm_state->ldlm_cancel_service) {
2068 CERROR("failed to start service\n");
2069 GOTO(out_proc, rc = -ENOMEM);
2072 OBD_ALLOC(blp, sizeof(*blp));
2074 GOTO(out_proc, rc = -ENOMEM);
2075 ldlm_state->ldlm_bl_pool = blp;
2077 spin_lock_init(&blp->blp_lock);
2078 CFS_INIT_LIST_HEAD(&blp->blp_list);
2079 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2080 cfs_waitq_init(&blp->blp_waitq);
2081 atomic_set(&blp->blp_num_threads, 0);
2082 atomic_set(&blp->blp_busy_threads, 0);
2083 blp->blp_min_threads = ldlm_min_threads;
2084 blp->blp_max_threads = ldlm_max_threads;
2087 for (i = 0; i < blp->blp_min_threads; i++) {
2088 rc = ldlm_bl_thread_start(blp);
2090 GOTO(out_thread, rc);
2093 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2095 GOTO(out_thread, rc);
2097 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2099 GOTO(out_thread, rc);
2101 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2102 expired_lock_thread.elt_state = ELT_STOPPED;
2103 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2105 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2106 spin_lock_init(&waiting_locks_spinlock);
2107 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2109 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2111 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2112 GOTO(out_thread, rc);
2115 wait_event(expired_lock_thread.elt_waitq,
2116 expired_lock_thread.elt_state == ELT_READY);
2120 rc = ldlm_pools_init();
2122 GOTO(out_thread, rc);
2128 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2129 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2134 ldlm_proc_cleanup();
2137 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2142 static int ldlm_cleanup(int force)
2145 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2149 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2150 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2151 CERROR("ldlm still has namespaces; clean these up first.\n");
2152 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2153 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2162 while (atomic_read(&blp->blp_num_threads) > 0) {
2163 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2165 init_completion(&blp->blp_comp);
2167 spin_lock(&blp->blp_lock);
2168 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2169 cfs_waitq_signal(&blp->blp_waitq);
2170 spin_unlock(&blp->blp_lock);
2172 wait_for_completion(&blp->blp_comp);
2174 OBD_FREE(blp, sizeof(*blp));
2176 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2177 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2178 ldlm_proc_cleanup();
2180 expired_lock_thread.elt_state = ELT_TERMINATE;
2181 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2182 wait_event(expired_lock_thread.elt_waitq,
2183 expired_lock_thread.elt_state == ELT_STOPPED);
2185 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2186 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2189 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2195 int __init ldlm_init(void)
2197 init_mutex(&ldlm_ref_sem);
2198 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2199 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2200 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2201 sizeof(struct ldlm_resource), 0,
2202 SLAB_HWCACHE_ALIGN);
2203 if (ldlm_resource_slab == NULL)
2206 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2207 sizeof(struct ldlm_lock), 0,
2208 SLAB_HWCACHE_ALIGN);
2209 if (ldlm_lock_slab == NULL) {
2210 cfs_mem_cache_destroy(ldlm_resource_slab);
2214 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2215 sizeof(struct ldlm_interval),
2216 0, SLAB_HWCACHE_ALIGN);
2217 if (ldlm_interval_slab == NULL) {
2218 cfs_mem_cache_destroy(ldlm_resource_slab);
2219 cfs_mem_cache_destroy(ldlm_lock_slab);
2226 void __exit ldlm_exit(void)
2230 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2231 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2232 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2233 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2234 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2235 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2236 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2240 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2243 EXPORT_SYMBOL(ldlm_get_processing_policy);
2244 EXPORT_SYMBOL(ldlm_lock2desc);
2245 EXPORT_SYMBOL(ldlm_register_intent);
2246 EXPORT_SYMBOL(ldlm_lockname);
2247 EXPORT_SYMBOL(ldlm_typename);
2248 EXPORT_SYMBOL(ldlm_lock2handle);
2249 EXPORT_SYMBOL(__ldlm_handle2lock);
2250 EXPORT_SYMBOL(ldlm_lock_get);
2251 EXPORT_SYMBOL(ldlm_lock_put);
2252 EXPORT_SYMBOL(ldlm_lock_match);
2253 EXPORT_SYMBOL(ldlm_lock_cancel);
2254 EXPORT_SYMBOL(ldlm_lock_addref);
2255 EXPORT_SYMBOL(ldlm_lock_decref);
2256 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2257 EXPORT_SYMBOL(ldlm_lock_change_resource);
2258 EXPORT_SYMBOL(ldlm_lock_set_data);
2259 EXPORT_SYMBOL(ldlm_it2str);
2260 EXPORT_SYMBOL(ldlm_lock_dump);
2261 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2262 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2263 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2264 EXPORT_SYMBOL(ldlm_lock_allow_match);
2266 /* ldlm_request.c */
2267 EXPORT_SYMBOL(ldlm_completion_ast);
2268 EXPORT_SYMBOL(ldlm_blocking_ast);
2269 EXPORT_SYMBOL(ldlm_glimpse_ast);
2270 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2271 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2272 EXPORT_SYMBOL(ldlm_prep_elc_req);
2273 EXPORT_SYMBOL(ldlm_cli_convert);
2274 EXPORT_SYMBOL(ldlm_cli_enqueue);
2275 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2276 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2277 EXPORT_SYMBOL(ldlm_cli_cancel);
2278 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2279 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2280 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2281 EXPORT_SYMBOL(ldlm_cli_join_lru);
2282 EXPORT_SYMBOL(ldlm_replay_locks);
2283 EXPORT_SYMBOL(ldlm_resource_foreach);
2284 EXPORT_SYMBOL(ldlm_namespace_foreach);
2285 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2286 EXPORT_SYMBOL(ldlm_resource_iterate);
2287 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2288 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2291 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2292 EXPORT_SYMBOL(ldlm_server_completion_ast);
2293 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2294 EXPORT_SYMBOL(ldlm_handle_enqueue);
2295 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2296 EXPORT_SYMBOL(ldlm_handle_cancel);
2297 EXPORT_SYMBOL(ldlm_request_cancel);
2298 EXPORT_SYMBOL(ldlm_handle_convert);
2299 EXPORT_SYMBOL(ldlm_handle_convert0);
2300 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2301 EXPORT_SYMBOL(ldlm_get_ref);
2302 EXPORT_SYMBOL(ldlm_put_ref);
2303 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2304 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2306 /* ldlm_resource.c */
2307 EXPORT_SYMBOL(ldlm_namespace_new);
2308 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2309 EXPORT_SYMBOL(ldlm_namespace_free);
2310 EXPORT_SYMBOL(ldlm_namespace_dump);
2311 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2312 EXPORT_SYMBOL(ldlm_resource_get);
2313 EXPORT_SYMBOL(ldlm_resource_putref);
2314 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2317 EXPORT_SYMBOL(client_import_add_conn);
2318 EXPORT_SYMBOL(client_import_del_conn);
2319 EXPORT_SYMBOL(client_obd_setup);
2320 EXPORT_SYMBOL(client_obd_cleanup);
2321 EXPORT_SYMBOL(client_connect_import);
2322 EXPORT_SYMBOL(client_disconnect_export);
2323 EXPORT_SYMBOL(target_start_recovery_thread);
2324 EXPORT_SYMBOL(target_stop_recovery_thread);
2325 EXPORT_SYMBOL(target_handle_connect);
2326 EXPORT_SYMBOL(target_cleanup_recovery);
2327 EXPORT_SYMBOL(target_destroy_export);
2328 EXPORT_SYMBOL(target_cancel_recovery_timer);
2329 EXPORT_SYMBOL(target_send_reply);
2330 EXPORT_SYMBOL(target_queue_recovery_request);
2331 EXPORT_SYMBOL(target_handle_ping);
2332 EXPORT_SYMBOL(target_pack_pool_reply);
2333 EXPORT_SYMBOL(target_handle_disconnect);
2336 EXPORT_SYMBOL(lock_res_and_lock);
2337 EXPORT_SYMBOL(unlock_res_and_lock);