1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lockd.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 # define EXPORT_SYMTAB
45 #define DEBUG_SUBSYSTEM S_LDLM
48 # include <libcfs/libcfs.h>
50 # include <liblustre.h>
53 #include <lustre_dlm.h>
54 #include <obd_class.h>
55 #include <libcfs/list.h>
56 #include "ldlm_internal.h"
59 static int ldlm_num_threads;
60 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
61 "number of DLM service threads to start");
64 extern cfs_mem_cache_t *ldlm_resource_slab;
65 extern cfs_mem_cache_t *ldlm_lock_slab;
66 extern struct lustre_lock ldlm_handle_lock;
68 static struct semaphore ldlm_ref_sem;
69 static int ldlm_refcount;
71 static struct ldlm_state *ldlm_state;
73 inline cfs_time_t round_timeout(cfs_time_t timeout)
75 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
78 /* timeout for initial callback (AST) reply (bz10399) */
79 static inline unsigned int ldlm_get_rq_timeout(void)
82 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
84 return timeout < 1 ? 1 : timeout;
88 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
89 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
90 static struct list_head waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
93 static struct expired_lock_thread {
94 cfs_waitq_t elt_waitq;
97 struct list_head elt_expired_locks;
98 } expired_lock_thread;
101 #define ELT_STOPPED 0
103 #define ELT_TERMINATE 2
105 struct ldlm_bl_pool {
109 * blp_prio_list is used for callbacks that should be handled
110 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
113 struct list_head blp_prio_list;
116 * blp_list is used for all other callbacks which are likely
117 * to take longer to process.
119 struct list_head blp_list;
121 cfs_waitq_t blp_waitq;
122 struct completion blp_comp;
123 atomic_t blp_num_threads;
124 atomic_t blp_busy_threads;
129 struct ldlm_bl_work_item {
130 struct list_head blwi_entry;
131 struct ldlm_namespace *blwi_ns;
132 struct ldlm_lock_desc blwi_ld;
133 struct ldlm_lock *blwi_lock;
134 struct list_head blwi_head;
136 struct completion blwi_comp;
138 int blwi_mem_pressure;
143 static inline int have_expired_locks(void)
148 spin_lock_bh(&waiting_locks_spinlock);
149 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
150 spin_unlock_bh(&waiting_locks_spinlock);
155 static int expired_lock_main(void *arg)
157 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
158 struct l_wait_info lwi = { 0 };
162 cfs_daemonize("ldlm_elt");
164 expired_lock_thread.elt_state = ELT_READY;
165 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
168 l_wait_event(expired_lock_thread.elt_waitq,
169 have_expired_locks() ||
170 expired_lock_thread.elt_state == ELT_TERMINATE,
173 spin_lock_bh(&waiting_locks_spinlock);
174 if (expired_lock_thread.elt_dump) {
175 spin_unlock_bh(&waiting_locks_spinlock);
177 /* from waiting_locks_callback, but not in timer */
178 libcfs_debug_dumplog();
179 libcfs_run_lbug_upcall(__FILE__,
180 "waiting_locks_callback",
181 expired_lock_thread.elt_dump);
183 spin_lock_bh(&waiting_locks_spinlock);
184 expired_lock_thread.elt_dump = 0;
189 while (!list_empty(expired)) {
190 struct obd_export *export;
191 struct ldlm_lock *lock;
193 lock = list_entry(expired->next, struct ldlm_lock,
195 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
196 (void *)lock >= LP_POISON) {
197 spin_unlock_bh(&waiting_locks_spinlock);
198 CERROR("free lock on elt list %p\n", lock);
201 list_del_init(&lock->l_pending_chain);
202 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
203 (void *)lock->l_export >= LP_POISON) {
204 CERROR("lock with free export on elt list %p\n",
206 lock->l_export = NULL;
207 LDLM_ERROR(lock, "free export");
208 /* release extra ref grabbed by
209 * ldlm_add_waiting_lock() or
210 * ldlm_failed_ast() */
214 export = class_export_get(lock->l_export);
215 spin_unlock_bh(&waiting_locks_spinlock);
217 /* release extra ref grabbed by ldlm_add_waiting_lock()
218 * or ldlm_failed_ast() */
222 class_fail_export(export);
223 class_export_put(export);
224 spin_lock_bh(&waiting_locks_spinlock);
226 spin_unlock_bh(&waiting_locks_spinlock);
228 if (do_dump && obd_dump_on_eviction) {
229 CERROR("dump the log upon eviction\n");
230 libcfs_debug_dumplog();
233 if (expired_lock_thread.elt_state == ELT_TERMINATE)
237 expired_lock_thread.elt_state = ELT_STOPPED;
238 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
243 * Check if there is a request in the export request list
244 * which prevents the lock canceling.
246 static int ldlm_lock_busy(struct ldlm_lock *lock)
248 struct ptlrpc_request *req;
252 if (lock->l_export == NULL)
255 spin_lock(&lock->l_export->exp_lock);
256 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
257 if (req->rq_ops->hpreq_lock_match) {
258 match = req->rq_ops->hpreq_lock_match(req, lock);
263 spin_unlock(&lock->l_export->exp_lock);
267 /* This is called from within a timer interrupt and cannot schedule */
268 static void waiting_locks_callback(unsigned long unused)
270 struct ldlm_lock *lock, *last = NULL;
272 spin_lock_bh(&waiting_locks_spinlock);
273 while (!list_empty(&waiting_locks_list)) {
274 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
276 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current())
277 || (lock->l_req_mode == LCK_GROUP))
280 /* Check if we need to prolong timeout */
281 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
282 ldlm_lock_busy(lock)) {
285 if (lock->l_pending_chain.next == &waiting_locks_list)
289 spin_unlock_bh(&waiting_locks_spinlock);
290 LDLM_DEBUG(lock, "prolong the busy lock");
291 ldlm_refresh_waiting_lock(lock,
292 ldlm_get_enq_timeout(lock));
293 spin_lock_bh(&waiting_locks_spinlock);
303 lock->l_resource->lr_namespace->ns_timeouts++;
304 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
305 "evicting client at %s ",
306 cfs_time_current_sec()- lock->l_last_activity,
308 lock->l_export->exp_connection->c_peer.nid));
310 LDLM_ERROR(lock, "waiting on lock multiple times");
311 CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
313 waiting_locks_list.next, waiting_locks_list.prev,
314 &lock->l_pending_chain,
315 lock->l_pending_chain.next,
316 lock->l_pending_chain.prev);
318 CFS_INIT_LIST_HEAD(&waiting_locks_list); /* HACK */
319 expired_lock_thread.elt_dump = __LINE__;
322 CEMERG("would be an LBUG, but isn't (bug 5653)\n");
323 libcfs_debug_dumpstack(NULL);
324 /*blocks* libcfs_debug_dumplog(); */
325 /*blocks* libcfs_run_lbug_upcall(file, func, line); */
330 /* no needs to take an extra ref on the lock since it was in
331 * the waiting_locks_list and ldlm_add_waiting_lock()
332 * already grabbed a ref */
333 list_del(&lock->l_pending_chain);
334 list_add(&lock->l_pending_chain,
335 &expired_lock_thread.elt_expired_locks);
338 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
339 if (obd_dump_on_timeout)
340 expired_lock_thread.elt_dump = __LINE__;
342 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
346 * Make sure the timer will fire again if we have any locks
349 if (!list_empty(&waiting_locks_list)) {
350 cfs_time_t timeout_rounded;
351 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
353 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
354 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
356 spin_unlock_bh(&waiting_locks_spinlock);
360 * Indicate that we're waiting for a client to call us back cancelling a given
361 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
362 * timer to fire appropriately. (We round up to the next second, to avoid
363 * floods of timer firings during periods of high lock contention and traffic).
364 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
365 * if it has been added to the waiting list (1 is returned).
367 * Called with the namespace lock held.
369 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
372 cfs_time_t timeout_rounded;
374 if (!list_empty(&lock->l_pending_chain))
377 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
378 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
381 timeout = cfs_time_shift(seconds);
382 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
383 lock->l_callback_timeout = timeout;
385 timeout_rounded = round_timeout(lock->l_callback_timeout);
387 if (cfs_time_before(timeout_rounded,
388 cfs_timer_deadline(&waiting_locks_timer)) ||
389 !cfs_timer_is_armed(&waiting_locks_timer)) {
390 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
392 /* if the new lock has a shorter timeout than something earlier on
393 the list, we'll wait the longer amount of time; no big deal. */
394 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
398 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
401 int timeout = ldlm_get_enq_timeout(lock);
403 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
405 spin_lock_bh(&waiting_locks_spinlock);
406 if (lock->l_destroyed) {
407 static cfs_time_t next;
408 spin_unlock_bh(&waiting_locks_spinlock);
409 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
410 if (cfs_time_after(cfs_time_current(), next)) {
411 next = cfs_time_shift(14400);
412 libcfs_debug_dumpstack(NULL);
417 ret = __ldlm_add_waiting_lock(lock, timeout);
419 /* grab ref on the lock if it has been added to the
422 spin_unlock_bh(&waiting_locks_spinlock);
424 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
425 ret == 0 ? "not re-" : "", timeout,
426 AT_OFF ? "off" : "on");
431 * Remove a lock from the pending list, likely because it had its cancellation
432 * callback arrive without incident. This adjusts the lock-timeout timer if
433 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
434 * As done by ldlm_del_waiting_lock(), the caller must release the lock
435 * reference when the lock is removed from any list (1 is returned).
437 * Called with namespace lock held.
439 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
441 struct list_head *list_next;
443 if (list_empty(&lock->l_pending_chain))
446 list_next = lock->l_pending_chain.next;
447 if (lock->l_pending_chain.prev == &waiting_locks_list) {
448 /* Removing the head of the list, adjust timer. */
449 if (list_next == &waiting_locks_list) {
450 /* No more, just cancel. */
451 cfs_timer_disarm(&waiting_locks_timer);
453 struct ldlm_lock *next;
454 next = list_entry(list_next, struct ldlm_lock,
456 cfs_timer_arm(&waiting_locks_timer,
457 round_timeout(next->l_callback_timeout));
460 list_del_init(&lock->l_pending_chain);
465 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
469 if (lock->l_export == NULL) {
470 /* We don't have a "waiting locks list" on clients. */
471 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
475 spin_lock_bh(&waiting_locks_spinlock);
476 ret = __ldlm_del_waiting_lock(lock);
477 spin_unlock_bh(&waiting_locks_spinlock);
479 /* release lock ref if it has indeed been removed
489 * Called with namespace lock held.
491 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
493 if (lock->l_export == NULL) {
494 /* We don't have a "waiting locks list" on clients. */
495 LDLM_DEBUG(lock, "client lock: no-op");
499 spin_lock_bh(&waiting_locks_spinlock);
501 if (list_empty(&lock->l_pending_chain)) {
502 spin_unlock_bh(&waiting_locks_spinlock);
503 LDLM_DEBUG(lock, "wasn't waiting");
507 /* we remove/add the lock to the waiting list, so no needs to
508 * release/take a lock reference */
509 __ldlm_del_waiting_lock(lock);
510 __ldlm_add_waiting_lock(lock, timeout);
511 spin_unlock_bh(&waiting_locks_spinlock);
513 LDLM_DEBUG(lock, "refreshed");
516 #else /* !__KERNEL__ */
518 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
520 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
524 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
529 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
533 #endif /* __KERNEL__ */
535 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
536 const char *ast_type)
538 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
539 char *str = libcfs_nid2str(conn->c_peer.nid);
541 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
542 "to a lock %s callback to %s timed out: rc %d\n",
543 lock->l_export->exp_obd->obd_name, str,
544 ast_type, obd_export_nid2str(lock->l_export), rc);
546 if (obd_dump_on_timeout)
547 libcfs_debug_dumplog();
549 spin_lock_bh(&waiting_locks_spinlock);
550 if (__ldlm_del_waiting_lock(lock) == 0)
551 /* the lock was not in any list, grab an extra ref before adding
552 * the lock to the expired list */
554 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
555 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
556 spin_unlock_bh(&waiting_locks_spinlock);
558 class_fail_export(lock->l_export);
562 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
563 struct ptlrpc_request *req, int rc,
564 const char *ast_type)
566 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
568 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
569 LASSERT(lock->l_export);
570 if (lock->l_export->exp_libclient) {
571 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
572 " timeout, just cancelling lock", ast_type,
573 libcfs_nid2str(peer.nid));
574 ldlm_lock_cancel(lock);
576 } else if (lock->l_flags & LDLM_FL_CANCEL) {
577 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
578 "cancel was received (AST reply lost?)",
579 ast_type, libcfs_nid2str(peer.nid));
580 ldlm_lock_cancel(lock);
583 ldlm_del_waiting_lock(lock);
584 ldlm_failed_ast(lock, rc, ast_type);
588 struct ldlm_resource *res = lock->l_resource;
590 LDLM_DEBUG(lock, "client (nid %s) returned %d"
591 " from %s AST - normal race",
592 libcfs_nid2str(peer.nid),
593 lustre_msg_get_status(req->rq_repmsg),
596 /* update lvbo to return proper attributes.
598 ldlm_resource_getref(res);
599 ldlm_res_lvbo_update(res, NULL, 0, 1);
600 ldlm_resource_putref(res);
603 LDLM_ERROR(lock, "client (nid %s) returned %d "
604 "from %s AST", libcfs_nid2str(peer.nid),
605 (req->rq_repmsg != NULL) ?
606 lustre_msg_get_status(req->rq_repmsg) : 0,
609 ldlm_lock_cancel(lock);
610 /* Server-side AST functions are called from ldlm_reprocess_all,
611 * which needs to be told to please restart its reprocessing. */
618 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
620 struct ldlm_cb_set_arg *arg;
621 struct ldlm_lock *lock;
624 LASSERT(data != NULL);
626 arg = req->rq_async_args.pointer_arg[0];
627 lock = req->rq_async_args.pointer_arg[1];
628 LASSERT(lock != NULL);
630 rc = ldlm_handle_ast_error(lock, req, rc,
631 arg->type == LDLM_BL_CALLBACK
632 ? "blocking" : "completion");
638 atomic_set(&arg->restart, 1);
643 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
644 struct ldlm_cb_set_arg *arg,
645 struct ldlm_lock *lock,
651 if (unlikely(instant_cancel)) {
652 rc = ptl_send_rpc(req, 1);
653 ptlrpc_req_finished(req);
655 /* If we cancelled the lock, we need to restart
656 * ldlm_reprocess_queue */
657 atomic_set(&arg->restart, 1);
660 ptlrpc_set_add_req(arg->set, req);
667 * Check if there are requests in the export request list which prevent
668 * the lock canceling and make these requests high priority ones.
670 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
672 struct ptlrpc_request *req;
675 if (lock->l_export == NULL) {
676 LDLM_DEBUG(lock, "client lock: no-op");
680 spin_lock(&lock->l_export->exp_lock);
681 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
682 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
683 req->rq_ops->hpreq_lock_match(req, lock))
684 ptlrpc_hpreq_reorder(req);
686 spin_unlock(&lock->l_export->exp_lock);
691 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
692 * enqueued server lock conflicts with given one.
694 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
695 * to wait for client response.
697 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
698 struct ldlm_lock_desc *desc,
699 void *data, int flag)
701 struct ldlm_cb_set_arg *arg = data;
702 struct ldlm_request *body;
703 struct ptlrpc_request *req;
704 __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
705 [DLM_LOCKREQ_OFF] = sizeof(*body) };
706 int instant_cancel = 0, rc;
709 if (flag == LDLM_CB_CANCELING) {
710 /* Don't need to do anything here. */
715 LASSERT(data != NULL);
717 ldlm_lock_reorder_req(lock);
719 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
720 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
725 req->rq_async_args.pointer_arg[0] = arg;
726 req->rq_async_args.pointer_arg[1] = lock;
727 req->rq_interpret_reply = ldlm_cb_interpret;
728 req->rq_no_resend = 1;
730 lock_res(lock->l_resource);
731 if (lock->l_granted_mode != lock->l_req_mode) {
732 /* this blocking AST will be communicated as part of the
733 * completion AST instead */
734 unlock_res(lock->l_resource);
735 ptlrpc_req_finished(req);
736 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
740 if (lock->l_destroyed) {
741 /* What's the point? */
742 unlock_res(lock->l_resource);
743 ptlrpc_req_finished(req);
748 if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
749 unlock_res(lock->l_resource);
750 ptlrpc_req_finished(req);
751 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
756 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
759 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
760 body->lock_handle[0] = lock->l_remote_handle;
761 body->lock_desc = *desc;
762 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
764 LDLM_DEBUG(lock, "server preparing blocking AST");
766 lock->l_last_activity = cfs_time_current_sec();
768 ptlrpc_req_set_repsize(req, 1, NULL);
769 if (instant_cancel) {
770 unlock_res(lock->l_resource);
771 ldlm_lock_cancel(lock);
773 LASSERT(lock->l_granted_mode == lock->l_req_mode);
774 ldlm_add_waiting_lock(lock);
775 unlock_res(lock->l_resource);
778 req->rq_send_state = LUSTRE_IMP_FULL;
779 /* ptlrpc_prep_req already set timeout */
781 req->rq_timeout = ldlm_get_rq_timeout();
783 if (lock->l_export && lock->l_export->exp_nid_stats &&
784 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
785 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
786 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
789 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
794 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
796 struct ldlm_cb_set_arg *arg = data;
797 struct ldlm_request *body;
798 struct ptlrpc_request *req;
799 long total_enqueue_wait;
800 __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
801 [DLM_LOCKREQ_OFF] = sizeof(*body) };
802 int rc, buffers = 2, instant_cancel = 0;
805 LASSERT(lock != NULL);
806 LASSERT(data != NULL);
808 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
809 lock->l_last_activity);
811 lock_res_and_lock(lock);
812 if (lock->l_resource->lr_lvb_len) {
813 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
816 unlock_res_and_lock(lock);
818 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
819 LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
824 req->rq_async_args.pointer_arg[0] = arg;
825 req->rq_async_args.pointer_arg[1] = lock;
826 req->rq_interpret_reply = ldlm_cb_interpret;
827 req->rq_no_resend = 1;
829 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
830 body->lock_handle[0] = lock->l_remote_handle;
831 body->lock_flags = flags;
832 ldlm_lock2desc(lock, &body->lock_desc);
837 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
838 lock->l_resource->lr_lvb_len);
839 lock_res_and_lock(lock);
840 memcpy(lvb, lock->l_resource->lr_lvb_data,
841 lock->l_resource->lr_lvb_len);
842 unlock_res_and_lock(lock);
845 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
848 /* Server-side enqueue wait time estimate, used in
849 __ldlm_add_waiting_lock to set future enqueue timers */
850 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
851 at_measured(&lock->l_resource->lr_namespace->ns_at_estimate,
854 /* bz18618. Don't add lock enqueue time we spend waiting for a
855 previous callback to fail. Locks waiting legitimately will
856 get extended by ldlm_refresh_waiting_lock regardless of the
857 estimate, so it's okay to underestimate here. */
858 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
859 "It is likely that a previous callback timed out.",
861 at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
863 ptlrpc_req_set_repsize(req, 1, NULL);
865 req->rq_send_state = LUSTRE_IMP_FULL;
866 /* ptlrpc_prep_req already set timeout */
868 req->rq_timeout = ldlm_get_rq_timeout();
870 /* We only send real blocking ASTs after the lock is granted */
871 lock_res_and_lock(lock);
872 if (lock->l_flags & LDLM_FL_AST_SENT) {
873 body->lock_flags |= LDLM_FL_AST_SENT;
874 /* copy ast flags like LDLM_FL_DISCARD_DATA */
875 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
877 /* We might get here prior to ldlm_handle_enqueue setting
878 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
879 * into waiting list, but this is safe and similar code in
880 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
881 * that would not only cancel the lock, but will also remove
882 * it from waiting list */
883 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
884 unlock_res_and_lock(lock);
885 ldlm_lock_cancel(lock);
887 lock_res_and_lock(lock);
889 ldlm_add_waiting_lock(lock); /* start the lock-timeout
893 unlock_res_and_lock(lock);
895 if (lock->l_export && lock->l_export->exp_nid_stats &&
896 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
897 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
898 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
901 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
906 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
908 struct ldlm_resource *res = lock->l_resource;
909 struct ldlm_request *body;
910 struct ptlrpc_request *req;
911 __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
912 [DLM_LOCKREQ_OFF] = sizeof(*body) };
916 LASSERT(lock != NULL && lock->l_export != NULL);
918 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
919 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
924 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
925 body->lock_handle[0] = lock->l_remote_handle;
926 ldlm_lock2desc(lock, &body->lock_desc);
928 lock_res_and_lock(lock);
929 size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
930 unlock_res_and_lock(lock);
931 res = lock->l_resource;
932 ptlrpc_req_set_repsize(req, 2, size);
934 req->rq_send_state = LUSTRE_IMP_FULL;
935 /* ptlrpc_prep_req already set timeout */
937 req->rq_timeout = ldlm_get_rq_timeout();
939 if (lock->l_export && lock->l_export->exp_nid_stats &&
940 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
941 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
942 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
945 rc = ptlrpc_queue_wait(req);
946 if (rc == -ELDLM_NO_LOCK_DATA)
947 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
949 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
951 rc = ldlm_res_lvbo_update(res, req,
953 ptlrpc_req_finished(req);
955 ldlm_reprocess_all(res);
960 static void ldlm_svc_get_eopc(struct ldlm_request *dlm_req,
961 struct lprocfs_stats *srv_stats)
963 int lock_type = 0, op = 0;
965 lock_type = dlm_req->lock_desc.l_resource.lr_type;
969 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
972 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
973 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
975 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
978 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
981 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
989 lprocfs_counter_incr(srv_stats, op);
995 * Main server-side entry point into LDLM. This is called by ptlrpc service
996 * threads to carry out client lock enqueueing requests.
998 int ldlm_handle_enqueue(struct ptlrpc_request *req,
999 ldlm_completion_callback completion_callback,
1000 ldlm_blocking_callback blocking_callback,
1001 ldlm_glimpse_callback glimpse_callback)
1003 struct obd_device *obddev = req->rq_export->exp_obd;
1004 struct ldlm_reply *dlm_rep;
1005 struct ldlm_request *dlm_req;
1006 __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1007 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
1010 ldlm_error_t err = ELDLM_OK;
1011 struct ldlm_lock *lock = NULL;
1012 void *cookie = NULL;
1015 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1017 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1018 lustre_swab_ldlm_request);
1019 if (dlm_req == NULL) {
1020 CERROR ("Can't unpack dlm_req\n");
1021 GOTO(out, rc = -EFAULT);
1024 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1025 flags = dlm_req->lock_flags;
1027 LASSERT(req->rq_export);
1029 if (req->rq_rqbd->rqbd_service->srv_stats)
1030 ldlm_svc_get_eopc(dlm_req,
1031 req->rq_rqbd->rqbd_service->srv_stats);
1033 if (req->rq_export && req->rq_export->exp_nid_stats &&
1034 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1035 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1036 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1039 if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1040 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
1041 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1042 dlm_req->lock_desc.l_resource.lr_type);
1043 GOTO(out, rc = -EFAULT);
1046 if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1047 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1048 dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
1049 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1050 dlm_req->lock_desc.l_req_mode);
1051 GOTO(out, rc = -EFAULT);
1054 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1055 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
1056 DEBUG_REQ(D_ERROR, req,
1057 "PLAIN lock request from IBITS client?");
1058 GOTO(out, rc = -EPROTO);
1060 } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
1061 DEBUG_REQ(D_ERROR, req,
1062 "IBITS lock request from unaware client?");
1063 GOTO(out, rc = -EPROTO);
1067 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1068 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1069 ibits for mgc/mgs) */
1071 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1072 * inodebits lock if client does not support them. */
1073 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1074 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1075 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1076 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1077 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1078 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1079 dlm_req->lock_desc.l_req_mode = LCK_CR;
1083 if (flags & LDLM_FL_REPLAY) {
1084 /* Find an existing lock in the per-export lock hash */
1085 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
1086 (void *)&dlm_req->lock_handle[0]);
1088 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1089 LPX64, lock->l_handle.h_cookie);
1090 GOTO(existing_lock, rc = 0);
1094 /* The lock's callback data might be set in the policy function */
1095 lock = ldlm_lock_create(obddev->obd_namespace,
1096 dlm_req->lock_desc.l_resource.lr_name,
1097 dlm_req->lock_desc.l_resource.lr_type,
1098 dlm_req->lock_desc.l_req_mode,
1099 blocking_callback, completion_callback,
1100 glimpse_callback, NULL, 0);
1102 GOTO(out, rc = -ENOMEM);
1104 lock->l_last_activity = cfs_time_current_sec();
1105 lock->l_remote_handle = dlm_req->lock_handle[0];
1106 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1108 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1109 /* Don't enqueue a lock onto the export if it has already
1110 * been evicted. Cancel it now instead. (bug 3822) */
1111 if (req->rq_export->exp_failed) {
1112 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1113 GOTO(out, rc = -ENOTCONN);
1115 lock->l_export = class_export_get(req->rq_export);
1117 if (lock->l_export->exp_lock_hash)
1118 lustre_hash_add(lock->l_export->exp_lock_hash,
1119 &lock->l_remote_handle, &lock->l_exp_hash);
1123 if (flags & LDLM_FL_HAS_INTENT) {
1124 /* In this case, the reply buffer is allocated deep in
1125 * local_lock_enqueue by the policy function. */
1130 lock_res_and_lock(lock);
1131 if (lock->l_resource->lr_lvb_len) {
1132 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1135 unlock_res_and_lock(lock);
1137 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1138 GOTO(out, rc = -ENOMEM);
1140 rc = lustre_pack_reply(req, buffers, size, NULL);
1145 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1146 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1147 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1148 lock->l_req_extent = lock->l_policy_data.l_extent;
1150 err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, (int *)&flags);
1154 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1156 dlm_rep->lock_flags = flags;
1158 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1159 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1161 /* We never send a blocking AST until the lock is granted, but
1162 * we can tell it right now */
1163 lock_res_and_lock(lock);
1165 /* Now take into account flags to be inherited from original lock
1166 request both in reply to client and in our own lock flags. */
1167 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1168 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1170 /* Don't move a pending lock onto the export if it has already
1171 * been evicted. Cancel it now instead. (bug 5683) */
1172 if (req->rq_export->exp_failed ||
1173 OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
1174 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1176 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1177 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1178 if (lock->l_granted_mode == lock->l_req_mode) {
1179 /* Only cancel lock if it was granted, because it
1180 * would be destroyed immediatelly and would never
1181 * be granted in the future, causing timeouts on client.
1182 * Not granted lock will be cancelled immediatelly after
1183 * sending completion AST.
1185 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1186 unlock_res_and_lock(lock);
1187 ldlm_lock_cancel(lock);
1188 lock_res_and_lock(lock);
1190 ldlm_add_waiting_lock(lock);
1193 /* Make sure we never ever grant usual metadata locks to liblustre
1195 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1196 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1197 req->rq_export->exp_libclient) {
1198 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1199 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1200 CERROR("Granting sync lock to libclient. "
1201 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1202 dlm_req->lock_flags, dlm_rep->lock_flags,
1204 LDLM_ERROR(lock, "sync lock");
1205 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1206 struct ldlm_intent *it;
1207 it = lustre_msg_buf(req->rq_reqmsg,
1211 CERROR("This is intent %s ("LPU64")\n",
1212 ldlm_it2str(it->opc), it->opc);
1218 unlock_res_and_lock(lock);
1222 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1223 if (!req->rq_packed_final) {
1224 err = lustre_pack_reply(req, 1, NULL, NULL);
1229 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1230 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1232 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1233 "(err=%d, rc=%d)", err, rc);
1235 if (rc == 0 && obddev->obd_fail)
1239 lock_res_and_lock(lock);
1240 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1241 if (size[DLM_REPLY_REC_OFF] > 0) {
1242 void *lvb = lustre_msg_buf(req->rq_repmsg,
1244 size[DLM_REPLY_REC_OFF]);
1245 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1248 memcpy(lvb, lock->l_resource->lr_lvb_data,
1249 size[DLM_REPLY_REC_OFF]);
1251 unlock_res_and_lock(lock);
1253 lock_res_and_lock(lock);
1254 ldlm_resource_unlink_lock(lock);
1255 ldlm_lock_destroy_nolock(lock);
1256 unlock_res_and_lock(lock);
1259 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1260 ldlm_reprocess_all(lock->l_resource);
1262 LDLM_LOCK_PUT(lock);
1265 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1271 int ldlm_handle_convert(struct ptlrpc_request *req)
1273 struct ldlm_request *dlm_req;
1274 struct ldlm_reply *dlm_rep;
1275 struct ldlm_lock *lock;
1277 __u32 size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1278 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
1281 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1282 lustre_swab_ldlm_request);
1283 if (dlm_req == NULL) {
1284 CERROR ("Can't unpack dlm_req\n");
1288 if (req->rq_export && req->rq_export->exp_nid_stats &&
1289 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1290 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1291 LDLM_CONVERT - LDLM_FIRST_OPC);
1294 rc = lustre_pack_reply(req, 2, size, NULL);
1298 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1300 dlm_rep->lock_flags = dlm_req->lock_flags;
1302 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1304 req->rq_status = EINVAL;
1308 LDLM_DEBUG(lock, "server-side convert handler START");
1310 lock->l_last_activity = cfs_time_current_sec();
1311 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1312 &dlm_rep->lock_flags);
1314 if (ldlm_del_waiting_lock(lock))
1315 LDLM_DEBUG(lock, "converted waiting lock");
1318 req->rq_status = EDEADLOCK;
1323 if (!req->rq_status)
1324 ldlm_reprocess_all(lock->l_resource);
1325 LDLM_DEBUG(lock, "server-side convert handler END");
1326 LDLM_LOCK_PUT(lock);
1328 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1333 /* Cancel all the locks whos handles are packed into ldlm_request */
1334 int ldlm_request_cancel(struct ptlrpc_request *req,
1335 struct ldlm_request *dlm_req, int first)
1337 struct ldlm_resource *res, *pres = NULL;
1338 struct ldlm_lock *lock;
1339 int i, count, done = 0;
1342 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1346 /* There is no lock on the server at the replay time,
1347 * skip lock cancelling to make replay tests to pass. */
1348 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1351 for (i = first; i < count; i++) {
1352 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1354 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1355 "lock (cookie "LPU64")",
1356 dlm_req->lock_handle[i].cookie);
1361 res = lock->l_resource;
1364 ldlm_reprocess_all(pres);
1365 ldlm_resource_putref(pres);
1368 ldlm_resource_getref(res);
1369 ldlm_res_lvbo_update(res, NULL, 0, 1);
1373 ldlm_lock_cancel(lock);
1374 LDLM_LOCK_PUT(lock);
1377 ldlm_reprocess_all(pres);
1378 ldlm_resource_putref(pres);
1383 int ldlm_handle_cancel(struct ptlrpc_request *req)
1385 struct ldlm_request *dlm_req;
1389 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1390 lustre_swab_ldlm_request);
1391 if (dlm_req == NULL) {
1392 CERROR("bad request buffer for cancel\n");
1396 if (req->rq_export && req->rq_export->exp_nid_stats &&
1397 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1398 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1399 LDLM_CANCEL - LDLM_FIRST_OPC);
1402 rc = lustre_pack_reply(req, 1, NULL, NULL);
1406 if (!ldlm_request_cancel(req, dlm_req, 0))
1407 req->rq_status = ESTALE;
1409 if (ptlrpc_reply(req) != 0)
1415 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1416 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1421 LDLM_DEBUG(lock, "client blocking AST callback handler");
1423 lock_res_and_lock(lock);
1424 lock->l_flags |= LDLM_FL_CBPENDING;
1426 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1427 lock->l_flags |= LDLM_FL_CANCEL;
1429 do_ast = (!lock->l_readers && !lock->l_writers);
1430 unlock_res_and_lock(lock);
1433 CDEBUG(D_DLMTRACE, "Lock %p is already unused, calling callback (%p)\n",
1434 lock, lock->l_blocking_ast);
1435 if (lock->l_blocking_ast != NULL)
1436 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1439 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1443 LDLM_LOCK_PUT(lock);
1447 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1448 struct ldlm_namespace *ns,
1449 struct ldlm_request *dlm_req,
1450 struct ldlm_lock *lock)
1452 CFS_LIST_HEAD(ast_list);
1455 LDLM_DEBUG(lock, "client completion callback handler START");
1457 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1458 int to = cfs_time_seconds(1);
1460 to = schedule_timeout(to);
1461 if (lock->l_granted_mode == lock->l_req_mode ||
1467 lock_res_and_lock(lock);
1468 if (lock->l_destroyed ||
1469 lock->l_granted_mode == lock->l_req_mode) {
1470 /* bug 11300: the lock has already been granted */
1471 unlock_res_and_lock(lock);
1472 LDLM_DEBUG(lock, "Double grant race happened");
1473 LDLM_LOCK_PUT(lock);
1478 /* If we receive the completion AST before the actual enqueue returned,
1479 * then we might need to switch lock modes, resources, or extents. */
1480 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1481 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1482 LDLM_DEBUG(lock, "completion AST, new lock mode");
1485 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1486 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1487 LDLM_DEBUG(lock, "completion AST, new policy data");
1490 ldlm_resource_unlink_lock(lock);
1491 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1492 &lock->l_resource->lr_name,
1493 sizeof(lock->l_resource->lr_name)) != 0) {
1494 unlock_res_and_lock(lock);
1495 if (ldlm_lock_change_resource(ns, lock,
1496 dlm_req->lock_desc.l_resource.lr_name)) {
1497 LDLM_ERROR(lock, "Failed to allocate resource");
1498 LDLM_LOCK_PUT(lock);
1502 LDLM_DEBUG(lock, "completion AST, new resource");
1503 CERROR("change resource!\n");
1504 lock_res_and_lock(lock);
1507 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1508 /* BL_AST locks are not needed in lru.
1509 * let ldlm_cancel_lru() be fast. */
1510 ldlm_lock_remove_from_lru(lock);
1511 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1512 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1515 if (lock->l_lvb_len) {
1517 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1518 lock->l_lvb_swabber);
1520 LDLM_ERROR(lock, "completion AST did not contain "
1523 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1527 ldlm_grant_lock(lock, &ast_list);
1528 unlock_res_and_lock(lock);
1530 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1532 ldlm_run_cp_ast_work(&ast_list);
1534 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1536 LDLM_LOCK_PUT(lock);
1540 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1541 struct ldlm_namespace *ns,
1542 struct ldlm_request *dlm_req,
1543 struct ldlm_lock *lock)
1548 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1550 if (lock->l_glimpse_ast != NULL)
1551 rc = lock->l_glimpse_ast(lock, req);
1553 if (req->rq_repmsg != NULL) {
1556 req->rq_status = rc;
1560 lock_res_and_lock(lock);
1561 if (lock->l_granted_mode == LCK_PW &&
1562 !lock->l_readers && !lock->l_writers &&
1563 cfs_time_after(cfs_time_current(),
1564 cfs_time_add(lock->l_last_used,
1565 cfs_time_seconds(10)))) {
1566 unlock_res_and_lock(lock);
1567 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1568 ldlm_handle_bl_callback(ns, NULL, lock);
1573 unlock_res_and_lock(lock);
1574 LDLM_LOCK_PUT(lock);
1578 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1580 req->rq_status = rc;
1581 if (!req->rq_packed_final) {
1582 rc = lustre_pack_reply(req, 1, NULL, NULL);
1586 return ptlrpc_reply(req);
1590 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1592 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1595 spin_lock(&blp->blp_lock);
1596 if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1597 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1598 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1600 /* other blocking callbacks are added to the regular list */
1601 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1603 spin_unlock(&blp->blp_lock);
1605 cfs_waitq_signal(&blp->blp_waitq);
1607 /* can not use blwi->blwi_mode as blwi could be already freed in
1609 if (mode == LDLM_SYNC)
1610 wait_for_completion(&blwi->blwi_comp);
1615 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1616 struct ldlm_namespace *ns,
1617 struct ldlm_lock_desc *ld,
1618 struct list_head *cancels, int count,
1619 struct ldlm_lock *lock,
1622 init_completion(&blwi->blwi_comp);
1623 INIT_LIST_HEAD(&blwi->blwi_head);
1625 if (libcfs_memory_pressure_get())
1626 blwi->blwi_mem_pressure = 1;
1629 blwi->blwi_mode = mode;
1631 blwi->blwi_ld = *ld;
1633 list_add(&blwi->blwi_head, cancels);
1634 list_del_init(cancels);
1635 blwi->blwi_count = count;
1637 blwi->blwi_lock = lock;
1641 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1642 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1643 struct list_head *cancels, int count, int mode)
1647 if (cancels && count == 0)
1650 if (mode == LDLM_SYNC) {
1651 /* if it is synchronous call do minimum mem alloc, as it could
1652 * be triggered from kernel shrinker
1654 struct ldlm_bl_work_item blwi;
1655 memset(&blwi, 0, sizeof(blwi));
1656 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1657 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1659 struct ldlm_bl_work_item *blwi;
1660 OBD_ALLOC(blwi, sizeof(*blwi));
1663 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1665 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1670 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1671 struct ldlm_lock *lock)
1674 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1680 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1681 struct list_head *cancels, int count, int mode)
1684 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1690 static int ldlm_callback_handler(struct ptlrpc_request *req)
1692 struct ldlm_namespace *ns;
1693 struct ldlm_request *dlm_req;
1694 struct ldlm_lock *lock;
1698 /* Requests arrive in sender's byte order. The ptlrpc service
1699 * handler has already checked and, if necessary, byte-swapped the
1700 * incoming request message body, but I am responsible for the
1701 * message buffers. */
1703 if (req->rq_export == NULL) {
1704 ldlm_callback_reply(req, -ENOTCONN);
1708 LASSERT(req->rq_export != NULL);
1709 LASSERT(req->rq_export->exp_obd != NULL);
1711 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1712 case LDLM_BL_CALLBACK:
1713 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1715 case LDLM_CP_CALLBACK:
1716 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1718 case LDLM_GL_CALLBACK:
1719 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1721 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1722 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1723 rc = llog_origin_handle_cancel(req);
1724 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1725 ldlm_callback_reply(req, rc);
1727 case OBD_QC_CALLBACK:
1728 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1729 rc = target_handle_qc_callback(req);
1730 ldlm_callback_reply(req, rc);
1734 /* reply in handler */
1735 rc = target_handle_dqacq_callback(req);
1737 case LLOG_ORIGIN_HANDLE_CREATE:
1738 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1739 rc = llog_origin_handle_create(req);
1740 ldlm_callback_reply(req, rc);
1742 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1743 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1744 rc = llog_origin_handle_next_block(req);
1745 ldlm_callback_reply(req, rc);
1747 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1748 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1749 rc = llog_origin_handle_read_header(req);
1750 ldlm_callback_reply(req, rc);
1752 case LLOG_ORIGIN_HANDLE_CLOSE:
1753 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1754 rc = llog_origin_handle_close(req);
1755 ldlm_callback_reply(req, rc);
1758 CERROR("unknown opcode %u\n",
1759 lustre_msg_get_opc(req->rq_reqmsg));
1760 ldlm_callback_reply(req, -EPROTO);
1764 ns = req->rq_export->exp_obd->obd_namespace;
1765 LASSERT(ns != NULL);
1767 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1768 lustre_swab_ldlm_request);
1769 if (dlm_req == NULL) {
1770 CERROR ("can't unpack dlm_req\n");
1771 ldlm_callback_reply(req, -EPROTO);
1775 /* Force a known safe race, send a cancel to the server for a lock
1776 * which the server has already started a blocking callback on. */
1777 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1778 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1779 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1781 CERROR("ldlm_cli_cancel: %d\n", rc);
1784 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1786 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1787 "disappeared\n", dlm_req->lock_handle[0].cookie);
1788 ldlm_callback_reply(req, -EINVAL);
1792 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1793 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1794 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1796 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1797 lock_res_and_lock(lock);
1798 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1799 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1800 /* If somebody cancels lock and cache is already droped,
1801 * or lock is failed before cp_ast received on client,
1802 * we can tell the server we have no lock. Otherwise, we
1803 * should send cancel after dropping the cache. */
1804 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1805 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1806 (lock->l_flags & LDLM_FL_FAILED)) {
1807 LDLM_DEBUG(lock, "callback on lock "
1808 LPX64" - lock disappeared\n",
1809 dlm_req->lock_handle[0].cookie);
1810 unlock_res_and_lock(lock);
1811 LDLM_LOCK_PUT(lock);
1812 ldlm_callback_reply(req, -EINVAL);
1815 /* BL_AST locks are not needed in lru.
1816 * let ldlm_cancel_lru() be fast. */
1817 ldlm_lock_remove_from_lru(lock);
1818 lock->l_flags |= LDLM_FL_BL_AST;
1820 unlock_res_and_lock(lock);
1822 /* We want the ost thread to get this reply so that it can respond
1823 * to ost requests (write cache writeback) that might be triggered
1826 * But we'd also like to be able to indicate in the reply that we're
1827 * cancelling right now, because it's unused, or have an intent result
1828 * in the reply, so we might have to push the responsibility for sending
1829 * the reply down into the AST handlers, alas. */
1831 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1832 case LDLM_BL_CALLBACK:
1833 CDEBUG(D_INODE, "blocking ast\n");
1834 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1835 ldlm_callback_reply(req, 0);
1836 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1837 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1839 case LDLM_CP_CALLBACK:
1840 CDEBUG(D_INODE, "completion ast\n");
1841 ldlm_callback_reply(req, 0);
1842 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1844 case LDLM_GL_CALLBACK:
1845 CDEBUG(D_INODE, "glimpse ast\n");
1846 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1849 LBUG(); /* checked above */
1855 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1860 /* Requests arrive in sender's byte order. The ptlrpc service
1861 * handler has already checked and, if necessary, byte-swapped the
1862 * incoming request message body, but I am responsible for the
1863 * message buffers. */
1865 if (req->rq_export == NULL) {
1866 struct ldlm_request *dlm_req;
1868 CERROR("%s from %s arrived at %lu with bad export cookie "
1870 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
1871 libcfs_nid2str(req->rq_peer.nid),
1872 req->rq_arrival_time.tv_sec,
1873 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1875 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
1876 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1878 lustre_swab_ldlm_request);
1879 if (dlm_req != NULL)
1880 ldlm_lock_dump_handle(D_ERROR,
1881 &dlm_req->lock_handle[0]);
1884 ldlm_callback_reply(req, -ENOTCONN);
1888 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1890 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1892 CDEBUG(D_INODE, "cancel\n");
1893 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1894 rc = ldlm_handle_cancel(req);
1898 case OBD_LOG_CANCEL:
1899 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1900 rc = llog_origin_handle_cancel(req);
1901 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1902 ldlm_callback_reply(req, rc);
1905 CERROR("invalid opcode %d\n",
1906 lustre_msg_get_opc(req->rq_reqmsg));
1907 ldlm_callback_reply(req, -EINVAL);
1914 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1916 struct ldlm_bl_work_item *blwi = NULL;
1917 static unsigned int num_bl = 0;
1919 spin_lock(&blp->blp_lock);
1920 /* process a request from the blp_list at least every blp_num_threads */
1921 if (!list_empty(&blp->blp_list) &&
1922 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1923 blwi = list_entry(blp->blp_list.next,
1924 struct ldlm_bl_work_item, blwi_entry);
1926 if (!list_empty(&blp->blp_prio_list))
1927 blwi = list_entry(blp->blp_prio_list.next,
1928 struct ldlm_bl_work_item, blwi_entry);
1931 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1933 list_del(&blwi->blwi_entry);
1935 spin_unlock(&blp->blp_lock);
1940 /* This only contains temporary data until the thread starts */
1941 struct ldlm_bl_thread_data {
1942 char bltd_name[CFS_CURPROC_COMM_MAX];
1943 struct ldlm_bl_pool *bltd_blp;
1944 struct completion bltd_comp;
1948 static int ldlm_bl_thread_main(void *arg);
1950 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1952 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1955 init_completion(&bltd.bltd_comp);
1956 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1958 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1959 atomic_read(&blp->blp_num_threads), rc);
1962 wait_for_completion(&bltd.bltd_comp);
1967 static int ldlm_bl_thread_main(void *arg)
1969 struct ldlm_bl_pool *blp;
1973 struct ldlm_bl_thread_data *bltd = arg;
1975 blp = bltd->bltd_blp;
1977 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1978 atomic_inc(&blp->blp_busy_threads);
1980 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1981 "ldlm_bl_%02d", bltd->bltd_num);
1982 cfs_daemonize(bltd->bltd_name);
1984 complete(&bltd->bltd_comp);
1985 /* cannot use bltd after this, it is only on caller's stack */
1989 struct l_wait_info lwi = { 0 };
1990 struct ldlm_bl_work_item *blwi = NULL;
1992 blwi = ldlm_bl_get_work(blp);
1997 atomic_dec(&blp->blp_busy_threads);
1998 l_wait_event_exclusive(blp->blp_waitq,
1999 (blwi = ldlm_bl_get_work(blp)) != NULL,
2001 busy = atomic_inc_return(&blp->blp_busy_threads);
2003 if (blwi->blwi_ns == NULL)
2004 /* added by ldlm_cleanup() */
2007 /* Not fatal if racy and have a few too many threads */
2008 if (unlikely(busy < blp->blp_max_threads &&
2009 busy >= atomic_read(&blp->blp_num_threads)))
2010 /* discard the return value, we tried */
2011 ldlm_bl_thread_start(blp);
2013 if (blwi->blwi_ns == NULL)
2014 /* added by ldlm_cleanup() */
2017 if (blwi->blwi_mem_pressure)
2018 libcfs_memory_pressure_set();
2020 if (blwi->blwi_count) {
2021 /* The special case when we cancel locks in lru
2022 * asynchronously, we pass the list of locks here.
2023 * Thus lock is marked LDLM_FL_CANCELING, and already
2024 * canceled locally. */
2025 ldlm_cli_cancel_list(&blwi->blwi_head,
2026 blwi->blwi_count, NULL, 0);
2028 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2031 if (blwi->blwi_mem_pressure)
2032 libcfs_memory_pressure_clr();
2034 if (blwi->blwi_mode == LDLM_ASYNC)
2035 OBD_FREE(blwi, sizeof(*blwi));
2037 complete(&blwi->blwi_comp);
2040 atomic_dec(&blp->blp_busy_threads);
2041 atomic_dec(&blp->blp_num_threads);
2042 complete(&blp->blp_comp);
2049 * Export handle<->lock hash operations.
2052 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2054 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2058 ldlm_export_lock_key(struct hlist_node *hnode)
2060 struct ldlm_lock *lock;
2063 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2064 RETURN(&lock->l_remote_handle);
2068 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2071 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2075 ldlm_export_lock_get(struct hlist_node *hnode)
2077 struct ldlm_lock *lock;
2080 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2081 LDLM_LOCK_GET(lock);
2087 ldlm_export_lock_put(struct hlist_node *hnode)
2089 struct ldlm_lock *lock;
2092 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2093 LDLM_LOCK_PUT(lock);
2098 static lustre_hash_ops_t ldlm_export_lock_ops = {
2099 .lh_hash = ldlm_export_lock_hash,
2100 .lh_key = ldlm_export_lock_key,
2101 .lh_compare = ldlm_export_lock_compare,
2102 .lh_get = ldlm_export_lock_get,
2103 .lh_put = ldlm_export_lock_put
2106 int ldlm_init_export(struct obd_export *exp)
2110 exp->exp_lock_hash =
2111 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2112 7, 16, &ldlm_export_lock_ops, LH_REHASH);
2114 if (!exp->exp_lock_hash)
2119 EXPORT_SYMBOL(ldlm_init_export);
2121 void ldlm_destroy_export(struct obd_export *exp)
2124 lustre_hash_exit(exp->exp_lock_hash);
2125 exp->exp_lock_hash = NULL;
2128 EXPORT_SYMBOL(ldlm_destroy_export);
2130 static int ldlm_setup(void);
2131 static int ldlm_cleanup(void);
2133 int ldlm_get_ref(void)
2137 mutex_down(&ldlm_ref_sem);
2138 if (++ldlm_refcount == 1) {
2143 mutex_up(&ldlm_ref_sem);
2148 void ldlm_put_ref(void)
2151 mutex_down(&ldlm_ref_sem);
2152 if (ldlm_refcount == 1) {
2153 int rc = ldlm_cleanup();
2155 CERROR("ldlm_cleanup failed: %d\n", rc);
2161 mutex_up(&ldlm_ref_sem);
2166 static int ldlm_setup(void)
2168 struct ldlm_bl_pool *blp;
2170 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2171 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2177 if (ldlm_state != NULL)
2180 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2181 if (ldlm_state == NULL)
2185 rc = ldlm_proc_setup();
2191 if (ldlm_num_threads) {
2192 /* If ldlm_num_threads is set, it is the min and the max. */
2193 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2194 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2195 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2196 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2197 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2201 ldlm_state->ldlm_cb_service =
2202 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2203 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2204 LDLM_CB_REPLY_PORTAL, 2,
2205 ldlm_callback_handler, "ldlm_cbd",
2206 ldlm_svc_proc_dir, NULL,
2207 ldlm_min_threads, ldlm_max_threads,
2210 if (!ldlm_state->ldlm_cb_service) {
2211 CERROR("failed to start service\n");
2212 GOTO(out_proc, rc = -ENOMEM);
2215 ldlm_state->ldlm_cancel_service =
2216 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2217 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2218 LDLM_CANCEL_REPLY_PORTAL, 6,
2219 ldlm_cancel_handler, "ldlm_canceld",
2220 ldlm_svc_proc_dir, NULL,
2221 ldlm_min_threads, ldlm_max_threads,
2224 if (!ldlm_state->ldlm_cancel_service) {
2225 CERROR("failed to start service\n");
2226 GOTO(out_proc, rc = -ENOMEM);
2229 OBD_ALLOC(blp, sizeof(*blp));
2231 GOTO(out_proc, rc = -ENOMEM);
2232 ldlm_state->ldlm_bl_pool = blp;
2234 spin_lock_init(&blp->blp_lock);
2235 CFS_INIT_LIST_HEAD(&blp->blp_list);
2236 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2237 cfs_waitq_init(&blp->blp_waitq);
2238 atomic_set(&blp->blp_num_threads, 0);
2239 atomic_set(&blp->blp_busy_threads, 0);
2240 blp->blp_min_threads = ldlm_min_threads;
2241 blp->blp_max_threads = ldlm_max_threads;
2244 for (i = 0; i < blp->blp_min_threads; i++) {
2245 rc = ldlm_bl_thread_start(blp);
2247 GOTO(out_thread, rc);
2250 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2252 GOTO(out_thread, rc);
2254 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2256 GOTO(out_thread, rc);
2258 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2259 expired_lock_thread.elt_state = ELT_STOPPED;
2260 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2262 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2263 spin_lock_init(&waiting_locks_spinlock);
2264 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2266 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2268 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2269 GOTO(out_thread, rc);
2272 wait_event(expired_lock_thread.elt_waitq,
2273 expired_lock_thread.elt_state == ELT_READY);
2277 rc = ldlm_pools_init();
2279 GOTO(out_thread, rc);
2286 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2287 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2292 ldlm_proc_cleanup();
2295 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2300 static int ldlm_cleanup(void)
2303 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2307 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2308 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2309 CERROR("ldlm still has namespaces; clean these up first.\n");
2310 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2311 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2320 while (atomic_read(&blp->blp_num_threads) > 0) {
2321 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2323 init_completion(&blp->blp_comp);
2325 spin_lock(&blp->blp_lock);
2326 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2327 cfs_waitq_signal(&blp->blp_waitq);
2328 spin_unlock(&blp->blp_lock);
2330 wait_for_completion(&blp->blp_comp);
2332 OBD_FREE(blp, sizeof(*blp));
2334 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2335 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2336 ldlm_proc_cleanup();
2338 expired_lock_thread.elt_state = ELT_TERMINATE;
2339 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2340 wait_event(expired_lock_thread.elt_waitq,
2341 expired_lock_thread.elt_state == ELT_STOPPED);
2343 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2344 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2347 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2353 int __init ldlm_init(void)
2355 init_mutex(&ldlm_ref_sem);
2356 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2357 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2358 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2359 sizeof(struct ldlm_resource), 0,
2360 SLAB_HWCACHE_ALIGN);
2361 if (ldlm_resource_slab == NULL)
2364 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2365 sizeof(struct ldlm_lock), 0,
2366 SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
2367 if (ldlm_lock_slab == NULL) {
2368 cfs_mem_cache_destroy(ldlm_resource_slab);
2372 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2373 sizeof(struct ldlm_interval),
2374 0, SLAB_HWCACHE_ALIGN);
2375 if (ldlm_interval_slab == NULL) {
2376 cfs_mem_cache_destroy(ldlm_resource_slab);
2377 cfs_mem_cache_destroy(ldlm_lock_slab);
2384 void __exit ldlm_exit(void)
2388 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2389 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2390 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2392 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2393 * synchronize_rcu() to wait a grace period elapsed, so that
2394 * ldlm_lock_free() get a chance to be called. */
2397 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2398 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2399 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2400 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2404 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2407 EXPORT_SYMBOL(ldlm_get_processing_policy);
2408 EXPORT_SYMBOL(ldlm_lock2desc);
2409 EXPORT_SYMBOL(ldlm_register_intent);
2410 EXPORT_SYMBOL(ldlm_lockname);
2411 EXPORT_SYMBOL(ldlm_typename);
2412 EXPORT_SYMBOL(ldlm_lock2handle);
2413 EXPORT_SYMBOL(__ldlm_handle2lock);
2414 EXPORT_SYMBOL(ldlm_lock_get);
2415 EXPORT_SYMBOL(ldlm_lock_put);
2416 EXPORT_SYMBOL(ldlm_lock_fast_match);
2417 EXPORT_SYMBOL(ldlm_lock_match);
2418 EXPORT_SYMBOL(ldlm_lock_cancel);
2419 EXPORT_SYMBOL(ldlm_lock_addref);
2420 EXPORT_SYMBOL(ldlm_lock_decref);
2421 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2422 EXPORT_SYMBOL(ldlm_lock_change_resource);
2423 EXPORT_SYMBOL(ldlm_lock_set_data);
2424 EXPORT_SYMBOL(ldlm_it2str);
2425 EXPORT_SYMBOL(ldlm_lock_dump);
2426 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2427 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2428 EXPORT_SYMBOL(ldlm_lock_allow_match);
2430 /* ldlm_request.c */
2431 EXPORT_SYMBOL(ldlm_completion_ast);
2432 EXPORT_SYMBOL(ldlm_blocking_ast);
2433 EXPORT_SYMBOL(ldlm_glimpse_ast);
2434 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2435 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2436 EXPORT_SYMBOL(ldlm_prep_elc_req);
2437 EXPORT_SYMBOL(ldlm_cli_convert);
2438 EXPORT_SYMBOL(ldlm_cli_enqueue);
2439 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2440 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2441 EXPORT_SYMBOL(ldlm_cli_cancel);
2442 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2443 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2444 EXPORT_SYMBOL(ldlm_cli_join_lru);
2445 EXPORT_SYMBOL(ldlm_replay_locks);
2446 EXPORT_SYMBOL(ldlm_resource_foreach);
2447 EXPORT_SYMBOL(ldlm_namespace_foreach);
2448 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2449 EXPORT_SYMBOL(ldlm_resource_iterate);
2450 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2451 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2454 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2455 EXPORT_SYMBOL(ldlm_server_completion_ast);
2456 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2457 EXPORT_SYMBOL(ldlm_handle_enqueue);
2458 EXPORT_SYMBOL(ldlm_handle_cancel);
2459 EXPORT_SYMBOL(ldlm_request_cancel);
2460 EXPORT_SYMBOL(ldlm_handle_convert);
2461 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2462 EXPORT_SYMBOL(ldlm_get_ref);
2463 EXPORT_SYMBOL(ldlm_put_ref);
2464 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2466 /* ldlm_resource.c */
2467 EXPORT_SYMBOL(ldlm_namespace_new);
2468 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2469 EXPORT_SYMBOL(ldlm_namespace_free);
2470 EXPORT_SYMBOL(ldlm_namespace_dump);
2471 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2472 EXPORT_SYMBOL(ldlm_resource_get);
2473 EXPORT_SYMBOL(ldlm_resource_putref);
2474 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2477 EXPORT_SYMBOL(client_import_add_conn);
2478 EXPORT_SYMBOL(client_import_del_conn);
2479 EXPORT_SYMBOL(client_obd_setup);
2480 EXPORT_SYMBOL(client_obd_cleanup);
2481 EXPORT_SYMBOL(client_connect_import);
2482 EXPORT_SYMBOL(client_disconnect_export);
2483 EXPORT_SYMBOL(server_disconnect_export);
2484 EXPORT_SYMBOL(target_abort_recovery);
2485 EXPORT_SYMBOL(target_cleanup_recovery);
2486 EXPORT_SYMBOL(target_handle_connect);
2487 EXPORT_SYMBOL(target_destroy_export);
2488 EXPORT_SYMBOL(target_cancel_recovery_timer);
2489 EXPORT_SYMBOL(target_send_reply);
2490 EXPORT_SYMBOL(target_queue_recovery_request);
2491 EXPORT_SYMBOL(target_handle_ping);
2492 EXPORT_SYMBOL(target_pack_pool_reply);
2493 EXPORT_SYMBOL(target_handle_disconnect);
2494 EXPORT_SYMBOL(target_handle_reply);
2497 EXPORT_SYMBOL(lock_res_and_lock);
2498 EXPORT_SYMBOL(unlock_res_and_lock);