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 2008 Sun Microsystems, Inc. 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;
140 static inline int have_expired_locks(void)
145 spin_lock_bh(&waiting_locks_spinlock);
146 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
147 spin_unlock_bh(&waiting_locks_spinlock);
152 static int expired_lock_main(void *arg)
154 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
155 struct l_wait_info lwi = { 0 };
159 cfs_daemonize("ldlm_elt");
161 expired_lock_thread.elt_state = ELT_READY;
162 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
165 l_wait_event(expired_lock_thread.elt_waitq,
166 have_expired_locks() ||
167 expired_lock_thread.elt_state == ELT_TERMINATE,
170 spin_lock_bh(&waiting_locks_spinlock);
171 if (expired_lock_thread.elt_dump) {
172 spin_unlock_bh(&waiting_locks_spinlock);
174 /* from waiting_locks_callback, but not in timer */
175 libcfs_debug_dumplog();
176 libcfs_run_lbug_upcall(__FILE__,
177 "waiting_locks_callback",
178 expired_lock_thread.elt_dump);
180 spin_lock_bh(&waiting_locks_spinlock);
181 expired_lock_thread.elt_dump = 0;
186 while (!list_empty(expired)) {
187 struct obd_export *export;
188 struct ldlm_lock *lock;
190 lock = list_entry(expired->next, struct ldlm_lock,
192 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
193 (void *)lock >= LP_POISON) {
194 spin_unlock_bh(&waiting_locks_spinlock);
195 CERROR("free lock on elt list %p\n", lock);
198 list_del_init(&lock->l_pending_chain);
199 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
200 (void *)lock->l_export >= LP_POISON) {
201 CERROR("lock with free export on elt list %p\n",
203 lock->l_export = NULL;
204 LDLM_ERROR(lock, "free export");
205 /* release extra ref grabbed by
206 * ldlm_add_waiting_lock() or
207 * ldlm_failed_ast() */
211 export = class_export_get(lock->l_export);
212 spin_unlock_bh(&waiting_locks_spinlock);
214 /* release extra ref grabbed by ldlm_add_waiting_lock()
215 * or ldlm_failed_ast() */
219 class_fail_export(export);
220 class_export_put(export);
221 spin_lock_bh(&waiting_locks_spinlock);
223 spin_unlock_bh(&waiting_locks_spinlock);
225 if (do_dump && obd_dump_on_eviction) {
226 CERROR("dump the log upon eviction\n");
227 libcfs_debug_dumplog();
230 if (expired_lock_thread.elt_state == ELT_TERMINATE)
234 expired_lock_thread.elt_state = ELT_STOPPED;
235 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
240 * Check if there is a request in the export request list
241 * which prevents the lock canceling.
243 static int ldlm_lock_busy(struct ldlm_lock *lock)
245 struct ptlrpc_request *req;
249 if (lock->l_export == NULL)
252 spin_lock(&lock->l_export->exp_lock);
253 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
254 if (req->rq_ops->hpreq_lock_match) {
255 match = req->rq_ops->hpreq_lock_match(req, lock);
260 spin_unlock(&lock->l_export->exp_lock);
264 /* This is called from within a timer interrupt and cannot schedule */
265 static void waiting_locks_callback(unsigned long unused)
267 struct ldlm_lock *lock, *last = NULL;
269 spin_lock_bh(&waiting_locks_spinlock);
270 while (!list_empty(&waiting_locks_list)) {
271 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
273 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current())
274 || (lock->l_req_mode == LCK_GROUP))
277 /* Check if we need to prolong timeout */
278 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
279 ldlm_lock_busy(lock)) {
282 if (lock->l_pending_chain.next == &waiting_locks_list)
286 spin_unlock_bh(&waiting_locks_spinlock);
287 LDLM_DEBUG(lock, "prolong the busy lock");
288 ldlm_refresh_waiting_lock(lock,
289 ldlm_get_enq_timeout(lock));
290 spin_lock_bh(&waiting_locks_spinlock);
300 lock->l_resource->lr_namespace->ns_timeouts++;
301 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
302 "evicting client at %s ",
303 cfs_time_current_sec()- lock->l_last_activity,
305 lock->l_export->exp_connection->c_peer.nid));
307 LDLM_ERROR(lock, "waiting on lock multiple times");
308 CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
310 waiting_locks_list.next, waiting_locks_list.prev,
311 &lock->l_pending_chain,
312 lock->l_pending_chain.next,
313 lock->l_pending_chain.prev);
315 CFS_INIT_LIST_HEAD(&waiting_locks_list); /* HACK */
316 expired_lock_thread.elt_dump = __LINE__;
319 CEMERG("would be an LBUG, but isn't (bug 5653)\n");
320 libcfs_debug_dumpstack(NULL);
321 /*blocks* libcfs_debug_dumplog(); */
322 /*blocks* libcfs_run_lbug_upcall(file, func, line); */
327 /* no needs to take an extra ref on the lock since it was in
328 * the waiting_locks_list and ldlm_add_waiting_lock()
329 * already grabbed a ref */
330 list_del(&lock->l_pending_chain);
331 list_add(&lock->l_pending_chain,
332 &expired_lock_thread.elt_expired_locks);
335 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
336 if (obd_dump_on_timeout)
337 expired_lock_thread.elt_dump = __LINE__;
339 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
343 * Make sure the timer will fire again if we have any locks
346 if (!list_empty(&waiting_locks_list)) {
347 cfs_time_t timeout_rounded;
348 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
350 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
351 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
353 spin_unlock_bh(&waiting_locks_spinlock);
357 * Indicate that we're waiting for a client to call us back cancelling a given
358 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
359 * timer to fire appropriately. (We round up to the next second, to avoid
360 * floods of timer firings during periods of high lock contention and traffic).
361 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
362 * if it has been added to the waiting list (1 is returned).
364 * Called with the namespace lock held.
366 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
369 cfs_time_t timeout_rounded;
371 if (!list_empty(&lock->l_pending_chain))
374 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
375 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
378 timeout = cfs_time_shift(seconds);
379 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
380 lock->l_callback_timeout = timeout;
382 timeout_rounded = round_timeout(lock->l_callback_timeout);
384 if (cfs_time_before(timeout_rounded,
385 cfs_timer_deadline(&waiting_locks_timer)) ||
386 !cfs_timer_is_armed(&waiting_locks_timer)) {
387 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
389 /* if the new lock has a shorter timeout than something earlier on
390 the list, we'll wait the longer amount of time; no big deal. */
391 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
395 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
399 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
401 spin_lock_bh(&waiting_locks_spinlock);
402 if (lock->l_destroyed) {
403 static cfs_time_t next;
404 spin_unlock_bh(&waiting_locks_spinlock);
405 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
406 if (cfs_time_after(cfs_time_current(), next)) {
407 next = cfs_time_shift(14400);
408 libcfs_debug_dumpstack(NULL);
413 ret = __ldlm_add_waiting_lock(lock, ldlm_get_enq_timeout(lock));
415 /* grab ref on the lock if it has been added to the
418 spin_unlock_bh(&waiting_locks_spinlock);
420 LDLM_DEBUG(lock, "%sadding to wait list",
421 ret == 0 ? "not re-" : "");
426 * Remove a lock from the pending list, likely because it had its cancellation
427 * callback arrive without incident. This adjusts the lock-timeout timer if
428 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
429 * As done by ldlm_del_waiting_lock(), the caller must release the lock
430 * reference when the lock is removed from any list (1 is returned).
432 * Called with namespace lock held.
434 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
436 struct list_head *list_next;
438 if (list_empty(&lock->l_pending_chain))
441 list_next = lock->l_pending_chain.next;
442 if (lock->l_pending_chain.prev == &waiting_locks_list) {
443 /* Removing the head of the list, adjust timer. */
444 if (list_next == &waiting_locks_list) {
445 /* No more, just cancel. */
446 cfs_timer_disarm(&waiting_locks_timer);
448 struct ldlm_lock *next;
449 next = list_entry(list_next, struct ldlm_lock,
451 cfs_timer_arm(&waiting_locks_timer,
452 round_timeout(next->l_callback_timeout));
455 list_del_init(&lock->l_pending_chain);
460 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
464 if (lock->l_export == NULL) {
465 /* We don't have a "waiting locks list" on clients. */
466 LDLM_DEBUG(lock, "client lock: no-op");
470 spin_lock_bh(&waiting_locks_spinlock);
471 ret = __ldlm_del_waiting_lock(lock);
472 spin_unlock_bh(&waiting_locks_spinlock);
474 /* release lock ref if it has indeed been removed
478 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
485 * Called with namespace lock held.
487 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
489 if (lock->l_export == NULL) {
490 /* We don't have a "waiting locks list" on clients. */
491 LDLM_DEBUG(lock, "client lock: no-op");
495 spin_lock_bh(&waiting_locks_spinlock);
497 if (list_empty(&lock->l_pending_chain)) {
498 spin_unlock_bh(&waiting_locks_spinlock);
499 LDLM_DEBUG(lock, "wasn't waiting");
503 /* we remove/add the lock to the waiting list, so no needs to
504 * release/take a lock reference */
505 __ldlm_del_waiting_lock(lock);
506 __ldlm_add_waiting_lock(lock, timeout);
507 spin_unlock_bh(&waiting_locks_spinlock);
509 LDLM_DEBUG(lock, "refreshed");
512 #else /* !__KERNEL__ */
514 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
516 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
520 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
525 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
529 #endif /* __KERNEL__ */
531 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
532 const char *ast_type)
534 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
535 char *str = libcfs_nid2str(conn->c_peer.nid);
537 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
538 "to a lock %s callback to %s timed out: rc %d\n",
539 lock->l_export->exp_obd->obd_name, str,
540 ast_type, obd_export_nid2str(lock->l_export), rc);
542 if (obd_dump_on_timeout)
543 libcfs_debug_dumplog();
545 spin_lock_bh(&waiting_locks_spinlock);
546 if (__ldlm_del_waiting_lock(lock) == 0)
547 /* the lock was not in any list, grab an extra ref before adding
548 * the lock to the expired list */
550 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
551 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
552 spin_unlock_bh(&waiting_locks_spinlock);
554 class_fail_export(lock->l_export);
558 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
559 struct ptlrpc_request *req, int rc,
560 const char *ast_type)
562 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
564 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
565 LASSERT(lock->l_export);
566 if (lock->l_export->exp_libclient) {
567 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
568 " timeout, just cancelling lock", ast_type,
569 libcfs_nid2str(peer.nid));
570 ldlm_lock_cancel(lock);
572 } else if (lock->l_flags & LDLM_FL_CANCEL) {
573 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
574 "cancel was received (AST reply lost?)",
575 ast_type, libcfs_nid2str(peer.nid));
576 ldlm_lock_cancel(lock);
579 ldlm_del_waiting_lock(lock);
580 ldlm_failed_ast(lock, rc, ast_type);
584 LDLM_DEBUG(lock, "client (nid %s) returned %d"
585 " from %s AST - normal race",
586 libcfs_nid2str(peer.nid),
587 lustre_msg_get_status(req->rq_repmsg),
590 LDLM_ERROR(lock, "client (nid %s) returned %d "
591 "from %s AST", libcfs_nid2str(peer.nid),
592 (req->rq_repmsg != NULL) ?
593 lustre_msg_get_status(req->rq_repmsg) : 0,
595 ldlm_lock_cancel(lock);
596 /* Server-side AST functions are called from ldlm_reprocess_all,
597 * which needs to be told to please restart its reprocessing. */
604 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
606 struct ldlm_cb_set_arg *arg;
607 struct ldlm_lock *lock;
610 LASSERT(data != NULL);
612 arg = req->rq_async_args.pointer_arg[0];
613 lock = req->rq_async_args.pointer_arg[1];
614 LASSERT(lock != NULL);
616 /* If client canceled the lock but the cancel has not
617 * been recieved yet, we need to update lvbo to have the
618 * proper attributes cached. */
619 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
620 ldlm_res_lvbo_update(lock->l_resource, NULL,
622 rc = ldlm_handle_ast_error(lock, req, rc,
623 arg->type == LDLM_BL_CALLBACK
624 ? "blocking" : "completion");
630 atomic_set(&arg->restart, 1);
635 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
636 struct ldlm_cb_set_arg *arg,
637 struct ldlm_lock *lock,
643 if (unlikely(instant_cancel)) {
644 rc = ptl_send_rpc(req, 1);
645 ptlrpc_req_finished(req);
647 /* If we cancelled the lock, we need to restart
648 * ldlm_reprocess_queue */
649 atomic_set(&arg->restart, 1);
652 ptlrpc_set_add_req(arg->set, req);
659 * Check if there are requests in the export request list which prevent
660 * the lock canceling and make these requests high priority ones.
662 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
664 struct ptlrpc_request *req;
667 if (lock->l_export == NULL) {
668 LDLM_DEBUG(lock, "client lock: no-op");
672 spin_lock(&lock->l_export->exp_lock);
673 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
674 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
675 req->rq_ops->hpreq_lock_match(req, lock))
676 ptlrpc_hpreq_reorder(req);
678 spin_unlock(&lock->l_export->exp_lock);
683 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
684 * enqueued server lock conflicts with given one.
686 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
687 * to wait for client response.
689 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
690 struct ldlm_lock_desc *desc,
691 void *data, int flag)
693 struct ldlm_cb_set_arg *arg = data;
694 struct ldlm_request *body;
695 struct ptlrpc_request *req;
696 __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
697 [DLM_LOCKREQ_OFF] = sizeof(*body) };
698 int instant_cancel = 0, rc;
701 if (flag == LDLM_CB_CANCELING) {
702 /* Don't need to do anything here. */
707 LASSERT(data != NULL);
709 ldlm_lock_reorder_req(lock);
711 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
712 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
717 req->rq_async_args.pointer_arg[0] = arg;
718 req->rq_async_args.pointer_arg[1] = lock;
719 req->rq_interpret_reply = ldlm_cb_interpret;
720 req->rq_no_resend = 1;
722 lock_res(lock->l_resource);
723 if (lock->l_granted_mode != lock->l_req_mode) {
724 /* this blocking AST will be communicated as part of the
725 * completion AST instead */
726 unlock_res(lock->l_resource);
727 ptlrpc_req_finished(req);
728 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
732 if (lock->l_destroyed) {
733 /* What's the point? */
734 unlock_res(lock->l_resource);
735 ptlrpc_req_finished(req);
740 if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
741 unlock_res(lock->l_resource);
742 ptlrpc_req_finished(req);
743 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
748 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
751 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
752 body->lock_handle[0] = lock->l_remote_handle;
753 body->lock_desc = *desc;
754 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
756 LDLM_DEBUG(lock, "server preparing blocking AST");
758 lock->l_last_activity = cfs_time_current_sec();
760 ptlrpc_req_set_repsize(req, 1, NULL);
761 if (instant_cancel) {
762 unlock_res(lock->l_resource);
763 ldlm_lock_cancel(lock);
765 LASSERT(lock->l_granted_mode == lock->l_req_mode);
766 ldlm_add_waiting_lock(lock);
767 unlock_res(lock->l_resource);
770 req->rq_send_state = LUSTRE_IMP_FULL;
771 /* ptlrpc_prep_req already set timeout */
773 req->rq_timeout = ldlm_get_rq_timeout();
775 if (lock->l_export && lock->l_export->exp_nid_stats &&
776 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
777 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
778 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
781 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
786 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
788 struct ldlm_cb_set_arg *arg = data;
789 struct ldlm_request *body;
790 struct ptlrpc_request *req;
791 long total_enqueue_wait;
792 __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
793 [DLM_LOCKREQ_OFF] = sizeof(*body) };
794 int rc, buffers = 2, instant_cancel = 0;
797 LASSERT(lock != NULL);
798 LASSERT(data != NULL);
800 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
801 lock->l_last_activity);
803 if (total_enqueue_wait > obd_timeout)
804 /* non-fatal with AT - change to LDLM_DEBUG? */
805 LDLM_WARN(lock, "enqueue wait took %lus from %lu",
806 total_enqueue_wait, lock->l_last_activity);
808 lock_res_and_lock(lock);
809 if (lock->l_resource->lr_lvb_len) {
810 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
813 unlock_res_and_lock(lock);
815 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
816 LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
821 req->rq_async_args.pointer_arg[0] = arg;
822 req->rq_async_args.pointer_arg[1] = lock;
823 req->rq_interpret_reply = ldlm_cb_interpret;
824 req->rq_no_resend = 1;
826 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
827 body->lock_handle[0] = lock->l_remote_handle;
828 body->lock_flags = flags;
829 ldlm_lock2desc(lock, &body->lock_desc);
834 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
835 lock->l_resource->lr_lvb_len);
836 lock_res_and_lock(lock);
837 memcpy(lvb, lock->l_resource->lr_lvb_data,
838 lock->l_resource->lr_lvb_len);
839 unlock_res_and_lock(lock);
842 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
845 /* Server-side enqueue wait time estimate, used in
846 __ldlm_add_waiting_lock to set future enqueue timers */
847 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
850 ptlrpc_req_set_repsize(req, 1, NULL);
852 req->rq_send_state = LUSTRE_IMP_FULL;
853 /* ptlrpc_prep_req already set timeout */
855 req->rq_timeout = ldlm_get_rq_timeout();
857 /* We only send real blocking ASTs after the lock is granted */
858 lock_res_and_lock(lock);
859 if (lock->l_flags & LDLM_FL_AST_SENT) {
860 body->lock_flags |= LDLM_FL_AST_SENT;
862 /* We might get here prior to ldlm_handle_enqueue setting
863 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
864 * into waiting list, but this is safe and similar code in
865 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
866 * that would not only cancel the lock, but will also remove
867 * it from waiting list */
868 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
869 unlock_res_and_lock(lock);
870 ldlm_lock_cancel(lock);
872 lock_res_and_lock(lock);
874 ldlm_add_waiting_lock(lock); /* start the lock-timeout
878 unlock_res_and_lock(lock);
880 if (lock->l_export && lock->l_export->exp_nid_stats &&
881 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
882 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
883 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
886 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
891 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
893 struct ldlm_resource *res = lock->l_resource;
894 struct ldlm_request *body;
895 struct ptlrpc_request *req;
896 __u32 size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
897 [DLM_LOCKREQ_OFF] = sizeof(*body) };
901 LASSERT(lock != NULL);
903 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
904 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
909 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
910 body->lock_handle[0] = lock->l_remote_handle;
911 ldlm_lock2desc(lock, &body->lock_desc);
913 lock_res_and_lock(lock);
914 size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
915 unlock_res_and_lock(lock);
916 res = lock->l_resource;
917 ptlrpc_req_set_repsize(req, 2, size);
919 req->rq_send_state = LUSTRE_IMP_FULL;
920 /* ptlrpc_prep_req already set timeout */
922 req->rq_timeout = ldlm_get_rq_timeout();
924 if (lock->l_export && lock->l_export->exp_nid_stats &&
925 lock->l_export->exp_nid_stats->nid_ldlm_stats) {
926 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
927 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
930 rc = ptlrpc_queue_wait(req);
931 if (rc == -ELDLM_NO_LOCK_DATA)
932 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
934 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
936 rc = ldlm_res_lvbo_update(res, req,
938 ptlrpc_req_finished(req);
940 ldlm_reprocess_all(res);
945 static void ldlm_svc_get_eopc(struct ldlm_request *dlm_req,
946 struct lprocfs_stats *srv_stats)
948 int lock_type = 0, op = 0;
950 lock_type = dlm_req->lock_desc.l_resource.lr_type;
954 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
957 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
958 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
960 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
963 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
966 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
974 lprocfs_counter_incr(srv_stats, op);
980 * Main server-side entry point into LDLM. This is called by ptlrpc service
981 * threads to carry out client lock enqueueing requests.
983 int ldlm_handle_enqueue(struct ptlrpc_request *req,
984 ldlm_completion_callback completion_callback,
985 ldlm_blocking_callback blocking_callback,
986 ldlm_glimpse_callback glimpse_callback)
988 struct obd_device *obddev = req->rq_export->exp_obd;
989 struct ldlm_reply *dlm_rep;
990 struct ldlm_request *dlm_req;
991 __u32 size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
992 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
995 ldlm_error_t err = ELDLM_OK;
996 struct ldlm_lock *lock = NULL;
1000 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1002 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1003 lustre_swab_ldlm_request);
1004 if (dlm_req == NULL) {
1005 CERROR ("Can't unpack dlm_req\n");
1006 GOTO(out, rc = -EFAULT);
1009 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1010 flags = dlm_req->lock_flags;
1012 LASSERT(req->rq_export);
1014 if (req->rq_rqbd->rqbd_service->srv_stats)
1015 ldlm_svc_get_eopc(dlm_req,
1016 req->rq_rqbd->rqbd_service->srv_stats);
1018 if (req->rq_export && req->rq_export->exp_nid_stats &&
1019 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1020 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1021 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1024 if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1025 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
1026 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1027 dlm_req->lock_desc.l_resource.lr_type);
1028 GOTO(out, rc = -EFAULT);
1031 if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1032 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1033 dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
1034 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1035 dlm_req->lock_desc.l_req_mode);
1036 GOTO(out, rc = -EFAULT);
1039 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1040 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
1041 DEBUG_REQ(D_ERROR, req,
1042 "PLAIN lock request from IBITS client?");
1043 GOTO(out, rc = -EPROTO);
1045 } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
1046 DEBUG_REQ(D_ERROR, req,
1047 "IBITS lock request from unaware client?");
1048 GOTO(out, rc = -EPROTO);
1052 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1053 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1054 ibits for mgc/mgs) */
1056 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1057 * inodebits lock if client does not support them. */
1058 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1059 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1060 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1061 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1062 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1063 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1064 dlm_req->lock_desc.l_req_mode = LCK_CR;
1068 if (flags & LDLM_FL_REPLAY) {
1069 /* Find an existing lock in the per-export lock hash */
1070 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
1071 (void *)&dlm_req->lock_handle[0]);
1073 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1074 LPX64, lock->l_handle.h_cookie);
1075 GOTO(existing_lock, rc = 0);
1079 /* The lock's callback data might be set in the policy function */
1080 lock = ldlm_lock_create(obddev->obd_namespace,
1081 dlm_req->lock_desc.l_resource.lr_name,
1082 dlm_req->lock_desc.l_resource.lr_type,
1083 dlm_req->lock_desc.l_req_mode,
1084 blocking_callback, completion_callback,
1085 glimpse_callback, NULL, 0);
1087 GOTO(out, rc = -ENOMEM);
1089 lock->l_last_activity = cfs_time_current_sec();
1090 lock->l_remote_handle = dlm_req->lock_handle[0];
1091 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1093 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1094 /* Don't enqueue a lock onto the export if it has already
1095 * been evicted. Cancel it now instead. (bug 3822) */
1096 if (req->rq_export->exp_failed) {
1097 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1098 GOTO(out, rc = -ENOTCONN);
1100 lock->l_export = class_export_get(req->rq_export);
1102 if (lock->l_export->exp_lock_hash)
1103 lustre_hash_add(lock->l_export->exp_lock_hash,
1104 &lock->l_remote_handle, &lock->l_exp_hash);
1108 if (flags & LDLM_FL_HAS_INTENT) {
1109 /* In this case, the reply buffer is allocated deep in
1110 * local_lock_enqueue by the policy function. */
1115 lock_res_and_lock(lock);
1116 if (lock->l_resource->lr_lvb_len) {
1117 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1120 unlock_res_and_lock(lock);
1122 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1123 GOTO(out, rc = -ENOMEM);
1125 rc = lustre_pack_reply(req, buffers, size, NULL);
1130 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1131 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1132 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1133 lock->l_req_extent = lock->l_policy_data.l_extent;
1135 err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, (int *)&flags);
1139 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1141 dlm_rep->lock_flags = flags;
1143 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1144 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1146 /* We never send a blocking AST until the lock is granted, but
1147 * we can tell it right now */
1148 lock_res_and_lock(lock);
1150 /* Now take into account flags to be inherited from original lock
1151 request both in reply to client and in our own lock flags. */
1152 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1153 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1155 /* Don't move a pending lock onto the export if it has already
1156 * been evicted. Cancel it now instead. (bug 5683) */
1157 if (req->rq_export->exp_failed ||
1158 OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
1159 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1161 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1162 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1163 if (lock->l_granted_mode == lock->l_req_mode) {
1164 /* Only cancel lock if it was granted, because it
1165 * would be destroyed immediatelly and would never
1166 * be granted in the future, causing timeouts on client.
1167 * Not granted lock will be cancelled immediatelly after
1168 * sending completion AST.
1170 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1171 unlock_res_and_lock(lock);
1172 ldlm_lock_cancel(lock);
1173 lock_res_and_lock(lock);
1175 ldlm_add_waiting_lock(lock);
1178 /* Make sure we never ever grant usual metadata locks to liblustre
1180 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1181 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1182 req->rq_export->exp_libclient) {
1183 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1184 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1185 CERROR("Granting sync lock to libclient. "
1186 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1187 dlm_req->lock_flags, dlm_rep->lock_flags,
1189 LDLM_ERROR(lock, "sync lock");
1190 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1191 struct ldlm_intent *it;
1192 it = lustre_msg_buf(req->rq_reqmsg,
1196 CERROR("This is intent %s ("LPU64")\n",
1197 ldlm_it2str(it->opc), it->opc);
1203 unlock_res_and_lock(lock);
1207 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1208 if (!req->rq_packed_final) {
1209 err = lustre_pack_reply(req, 1, NULL, NULL);
1214 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1215 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1217 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1218 "(err=%d, rc=%d)", err, rc);
1221 lock_res_and_lock(lock);
1222 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1223 if (size[DLM_REPLY_REC_OFF] > 0) {
1224 void *lvb = lustre_msg_buf(req->rq_repmsg,
1226 size[DLM_REPLY_REC_OFF]);
1227 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1230 memcpy(lvb, lock->l_resource->lr_lvb_data,
1231 size[DLM_REPLY_REC_OFF]);
1233 unlock_res_and_lock(lock);
1235 lock_res_and_lock(lock);
1236 ldlm_resource_unlink_lock(lock);
1237 ldlm_lock_destroy_nolock(lock);
1238 unlock_res_and_lock(lock);
1241 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1242 ldlm_reprocess_all(lock->l_resource);
1244 LDLM_LOCK_PUT(lock);
1247 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1253 int ldlm_handle_convert(struct ptlrpc_request *req)
1255 struct ldlm_request *dlm_req;
1256 struct ldlm_reply *dlm_rep;
1257 struct ldlm_lock *lock;
1259 __u32 size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1260 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
1263 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1264 lustre_swab_ldlm_request);
1265 if (dlm_req == NULL) {
1266 CERROR ("Can't unpack dlm_req\n");
1270 if (req->rq_export && req->rq_export->exp_nid_stats &&
1271 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1272 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1273 LDLM_CONVERT - LDLM_FIRST_OPC);
1276 rc = lustre_pack_reply(req, 2, size, NULL);
1280 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1282 dlm_rep->lock_flags = dlm_req->lock_flags;
1284 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1286 req->rq_status = EINVAL;
1290 LDLM_DEBUG(lock, "server-side convert handler START");
1292 lock->l_last_activity = cfs_time_current_sec();
1293 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1294 &dlm_rep->lock_flags);
1296 if (ldlm_del_waiting_lock(lock))
1297 LDLM_DEBUG(lock, "converted waiting lock");
1300 req->rq_status = EDEADLOCK;
1305 if (!req->rq_status)
1306 ldlm_reprocess_all(lock->l_resource);
1307 LDLM_DEBUG(lock, "server-side convert handler END");
1308 LDLM_LOCK_PUT(lock);
1310 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1315 /* Cancel all the locks whos handles are packed into ldlm_request */
1316 int ldlm_request_cancel(struct ptlrpc_request *req,
1317 struct ldlm_request *dlm_req, int first)
1319 struct ldlm_resource *res, *pres = NULL;
1320 struct ldlm_lock *lock;
1321 int i, count, done = 0;
1324 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1328 /* There is no lock on the server at the replay time,
1329 * skip lock cancelling to make replay tests to pass. */
1330 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1333 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks",
1335 for (i = first; i < count; i++) {
1336 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1338 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1339 "lock (cookie "LPU64")",
1340 dlm_req->lock_handle[i].cookie);
1345 res = lock->l_resource;
1348 ldlm_reprocess_all(pres);
1349 ldlm_resource_putref(pres);
1352 ldlm_resource_getref(res);
1353 ldlm_res_lvbo_update(res, NULL, 0, 1);
1357 ldlm_lock_cancel(lock);
1358 LDLM_LOCK_PUT(lock);
1361 ldlm_reprocess_all(pres);
1362 ldlm_resource_putref(pres);
1364 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1368 int ldlm_handle_cancel(struct ptlrpc_request *req)
1370 struct ldlm_request *dlm_req;
1374 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1375 lustre_swab_ldlm_request);
1376 if (dlm_req == NULL) {
1377 CERROR("bad request buffer for cancel\n");
1381 if (req->rq_export && req->rq_export->exp_nid_stats &&
1382 req->rq_export->exp_nid_stats->nid_ldlm_stats) {
1383 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1384 LDLM_CANCEL - LDLM_FIRST_OPC);
1387 rc = lustre_pack_reply(req, 1, NULL, NULL);
1391 if (!ldlm_request_cancel(req, dlm_req, 0))
1392 req->rq_status = ESTALE;
1394 if (ptlrpc_reply(req) != 0)
1400 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1401 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1406 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1408 lock_res_and_lock(lock);
1409 lock->l_flags |= LDLM_FL_CBPENDING;
1411 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1412 lock->l_flags |= LDLM_FL_CANCEL;
1414 do_ast = (!lock->l_readers && !lock->l_writers);
1415 unlock_res_and_lock(lock);
1418 LDLM_DEBUG(lock, "already unused, calling "
1419 "callback (%p)", lock->l_blocking_ast);
1420 if (lock->l_blocking_ast != NULL)
1421 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1424 LDLM_DEBUG(lock, "Lock still has references, will be"
1425 " cancelled later");
1428 LDLM_DEBUG(lock, "client blocking callback handler END");
1429 LDLM_LOCK_PUT(lock);
1433 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1434 struct ldlm_namespace *ns,
1435 struct ldlm_request *dlm_req,
1436 struct ldlm_lock *lock)
1438 CFS_LIST_HEAD(ast_list);
1441 LDLM_DEBUG(lock, "client completion callback handler START");
1443 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1444 int to = cfs_time_seconds(1);
1446 to = schedule_timeout(to);
1447 if (lock->l_granted_mode == lock->l_req_mode ||
1453 lock_res_and_lock(lock);
1454 if (lock->l_destroyed ||
1455 lock->l_granted_mode == lock->l_req_mode) {
1456 /* bug 11300: the lock has already been granted */
1457 unlock_res_and_lock(lock);
1458 LDLM_DEBUG(lock, "Double grant race happened");
1459 LDLM_LOCK_PUT(lock);
1464 /* If we receive the completion AST before the actual enqueue returned,
1465 * then we might need to switch lock modes, resources, or extents. */
1466 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1467 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1468 LDLM_DEBUG(lock, "completion AST, new lock mode");
1471 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1472 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1473 LDLM_DEBUG(lock, "completion AST, new policy data");
1476 ldlm_resource_unlink_lock(lock);
1477 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1478 &lock->l_resource->lr_name,
1479 sizeof(lock->l_resource->lr_name)) != 0) {
1480 unlock_res_and_lock(lock);
1481 if (ldlm_lock_change_resource(ns, lock,
1482 dlm_req->lock_desc.l_resource.lr_name)) {
1483 LDLM_ERROR(lock, "Failed to allocate resource");
1484 LDLM_LOCK_PUT(lock);
1488 LDLM_DEBUG(lock, "completion AST, new resource");
1489 CERROR("change resource!\n");
1490 lock_res_and_lock(lock);
1493 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1494 /* BL_AST locks are not needed in lru.
1495 * let ldlm_cancel_lru() be fast. */
1496 ldlm_lock_remove_from_lru(lock);
1497 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1498 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1501 if (lock->l_lvb_len) {
1503 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1504 lock->l_lvb_swabber);
1506 LDLM_ERROR(lock, "completion AST did not contain "
1509 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1513 ldlm_grant_lock(lock, &ast_list);
1514 unlock_res_and_lock(lock);
1516 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1518 ldlm_run_cp_ast_work(&ast_list);
1520 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1522 LDLM_LOCK_PUT(lock);
1526 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1527 struct ldlm_namespace *ns,
1528 struct ldlm_request *dlm_req,
1529 struct ldlm_lock *lock)
1534 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1536 if (lock->l_glimpse_ast != NULL)
1537 rc = lock->l_glimpse_ast(lock, req);
1539 if (req->rq_repmsg != NULL) {
1542 req->rq_status = rc;
1546 lock_res_and_lock(lock);
1547 if (lock->l_granted_mode == LCK_PW &&
1548 !lock->l_readers && !lock->l_writers &&
1549 cfs_time_after(cfs_time_current(),
1550 cfs_time_add(lock->l_last_used,
1551 cfs_time_seconds(10)))) {
1552 unlock_res_and_lock(lock);
1553 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1554 ldlm_handle_bl_callback(ns, NULL, lock);
1559 unlock_res_and_lock(lock);
1560 LDLM_LOCK_PUT(lock);
1564 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1566 req->rq_status = rc;
1567 if (!req->rq_packed_final) {
1568 rc = lustre_pack_reply(req, 1, NULL, NULL);
1572 return ptlrpc_reply(req);
1576 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1577 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1578 struct list_head *cancels, int count)
1580 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1581 struct ldlm_bl_work_item *blwi;
1584 if (cancels && count == 0)
1587 OBD_ALLOC(blwi, sizeof(*blwi));
1593 blwi->blwi_ld = *ld;
1595 list_add(&blwi->blwi_head, cancels);
1596 list_del_init(cancels);
1597 blwi->blwi_count = count;
1599 blwi->blwi_lock = lock;
1601 spin_lock(&blp->blp_lock);
1602 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1603 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1604 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1606 /* other blocking callbacks are added to the regular list */
1607 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1609 cfs_waitq_signal(&blp->blp_waitq);
1610 spin_unlock(&blp->blp_lock);
1616 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1617 struct ldlm_lock *lock)
1620 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1626 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1627 struct list_head *cancels, int count)
1630 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1636 static int ldlm_callback_handler(struct ptlrpc_request *req)
1638 struct ldlm_namespace *ns;
1639 struct ldlm_request *dlm_req;
1640 struct ldlm_lock *lock;
1644 /* Requests arrive in sender's byte order. The ptlrpc service
1645 * handler has already checked and, if necessary, byte-swapped the
1646 * incoming request message body, but I am responsible for the
1647 * message buffers. */
1649 if (req->rq_export == NULL) {
1650 struct ldlm_request *dlm_req;
1652 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1653 "export cookie "LPX64"; this is "
1654 "normal if this node rebooted with a lock held\n",
1655 lustre_msg_get_opc(req->rq_reqmsg),
1656 libcfs_id2str(req->rq_peer),
1657 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1659 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1661 lustre_swab_ldlm_request);
1662 if (dlm_req != NULL)
1663 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1664 dlm_req->lock_handle[0].cookie);
1666 ldlm_callback_reply(req, -ENOTCONN);
1670 LASSERT(req->rq_export != NULL);
1671 LASSERT(req->rq_export->exp_obd != NULL);
1673 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1674 case LDLM_BL_CALLBACK:
1675 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1677 case LDLM_CP_CALLBACK:
1678 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1680 case LDLM_GL_CALLBACK:
1681 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1683 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1684 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1685 rc = llog_origin_handle_cancel(req);
1686 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1687 ldlm_callback_reply(req, rc);
1689 case OBD_QC_CALLBACK:
1690 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1691 rc = target_handle_qc_callback(req);
1692 ldlm_callback_reply(req, rc);
1696 /* reply in handler */
1697 rc = target_handle_dqacq_callback(req);
1699 case LLOG_ORIGIN_HANDLE_CREATE:
1700 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1701 rc = llog_origin_handle_create(req);
1702 ldlm_callback_reply(req, rc);
1704 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1705 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1706 rc = llog_origin_handle_next_block(req);
1707 ldlm_callback_reply(req, rc);
1709 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1710 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1711 rc = llog_origin_handle_read_header(req);
1712 ldlm_callback_reply(req, rc);
1714 case LLOG_ORIGIN_HANDLE_CLOSE:
1715 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1716 rc = llog_origin_handle_close(req);
1717 ldlm_callback_reply(req, rc);
1720 CERROR("unknown opcode %u\n",
1721 lustre_msg_get_opc(req->rq_reqmsg));
1722 ldlm_callback_reply(req, -EPROTO);
1726 ns = req->rq_export->exp_obd->obd_namespace;
1727 LASSERT(ns != NULL);
1729 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1730 lustre_swab_ldlm_request);
1731 if (dlm_req == NULL) {
1732 CERROR ("can't unpack dlm_req\n");
1733 ldlm_callback_reply(req, -EPROTO);
1737 /* Force a known safe race, send a cancel to the server for a lock
1738 * which the server has already started a blocking callback on. */
1739 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1740 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1741 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1743 CERROR("ldlm_cli_cancel: %d\n", rc);
1746 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1748 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1749 "disappeared\n", dlm_req->lock_handle[0].cookie);
1750 ldlm_callback_reply(req, -EINVAL);
1754 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1755 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1756 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1758 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1759 lock_res_and_lock(lock);
1760 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1761 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1762 /* If somebody cancels lock and cache is already droped,
1763 * or lock is failed before cp_ast received on client,
1764 * we can tell the server we have no lock. Otherwise, we
1765 * should send cancel after dropping the cache. */
1766 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1767 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1768 (lock->l_flags & LDLM_FL_FAILED)) {
1769 LDLM_DEBUG(lock, "callback on lock "
1770 LPX64" - lock disappeared\n",
1771 dlm_req->lock_handle[0].cookie);
1772 unlock_res_and_lock(lock);
1773 LDLM_LOCK_PUT(lock);
1774 ldlm_callback_reply(req, -EINVAL);
1777 /* BL_AST locks are not needed in lru.
1778 * let ldlm_cancel_lru() be fast. */
1779 ldlm_lock_remove_from_lru(lock);
1780 lock->l_flags |= LDLM_FL_BL_AST;
1782 unlock_res_and_lock(lock);
1784 /* We want the ost thread to get this reply so that it can respond
1785 * to ost requests (write cache writeback) that might be triggered
1788 * But we'd also like to be able to indicate in the reply that we're
1789 * cancelling right now, because it's unused, or have an intent result
1790 * in the reply, so we might have to push the responsibility for sending
1791 * the reply down into the AST handlers, alas. */
1793 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1794 case LDLM_BL_CALLBACK:
1795 CDEBUG(D_INODE, "blocking ast\n");
1796 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1797 ldlm_callback_reply(req, 0);
1798 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1799 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1801 case LDLM_CP_CALLBACK:
1802 CDEBUG(D_INODE, "completion ast\n");
1803 ldlm_callback_reply(req, 0);
1804 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1806 case LDLM_GL_CALLBACK:
1807 CDEBUG(D_INODE, "glimpse ast\n");
1808 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1811 LBUG(); /* checked above */
1817 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1822 /* Requests arrive in sender's byte order. The ptlrpc service
1823 * handler has already checked and, if necessary, byte-swapped the
1824 * incoming request message body, but I am responsible for the
1825 * message buffers. */
1827 if (req->rq_export == NULL) {
1828 struct ldlm_request *dlm_req;
1830 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1831 lustre_msg_get_opc(req->rq_reqmsg),
1832 libcfs_id2str(req->rq_peer),
1833 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1835 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1837 lustre_swab_ldlm_request);
1838 if (dlm_req != NULL)
1839 ldlm_lock_dump_handle(D_ERROR,
1840 &dlm_req->lock_handle[0]);
1842 ldlm_callback_reply(req, -ENOTCONN);
1846 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1848 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1850 CDEBUG(D_INODE, "cancel\n");
1851 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1852 rc = ldlm_handle_cancel(req);
1856 case OBD_LOG_CANCEL:
1857 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1858 rc = llog_origin_handle_cancel(req);
1859 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_REP, 0);
1860 ldlm_callback_reply(req, rc);
1863 CERROR("invalid opcode %d\n",
1864 lustre_msg_get_opc(req->rq_reqmsg));
1865 ldlm_callback_reply(req, -EINVAL);
1872 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1874 struct ldlm_bl_work_item *blwi = NULL;
1875 static unsigned int num_bl = 0;
1877 spin_lock(&blp->blp_lock);
1878 /* process a request from the blp_list at least every blp_num_threads */
1879 if (!list_empty(&blp->blp_list) &&
1880 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1881 blwi = list_entry(blp->blp_list.next,
1882 struct ldlm_bl_work_item, blwi_entry);
1884 if (!list_empty(&blp->blp_prio_list))
1885 blwi = list_entry(blp->blp_prio_list.next,
1886 struct ldlm_bl_work_item, blwi_entry);
1889 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1891 list_del(&blwi->blwi_entry);
1893 spin_unlock(&blp->blp_lock);
1898 /* This only contains temporary data until the thread starts */
1899 struct ldlm_bl_thread_data {
1900 char bltd_name[CFS_CURPROC_COMM_MAX];
1901 struct ldlm_bl_pool *bltd_blp;
1902 struct completion bltd_comp;
1906 static int ldlm_bl_thread_main(void *arg);
1908 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1910 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1913 init_completion(&bltd.bltd_comp);
1914 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1916 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1917 atomic_read(&blp->blp_num_threads), rc);
1920 wait_for_completion(&bltd.bltd_comp);
1925 static int ldlm_bl_thread_main(void *arg)
1927 struct ldlm_bl_pool *blp;
1931 struct ldlm_bl_thread_data *bltd = arg;
1933 blp = bltd->bltd_blp;
1935 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1936 atomic_inc(&blp->blp_busy_threads);
1938 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1939 "ldlm_bl_%02d", bltd->bltd_num);
1940 cfs_daemonize(bltd->bltd_name);
1942 complete(&bltd->bltd_comp);
1943 /* cannot use bltd after this, it is only on caller's stack */
1947 struct l_wait_info lwi = { 0 };
1948 struct ldlm_bl_work_item *blwi = NULL;
1950 blwi = ldlm_bl_get_work(blp);
1955 atomic_dec(&blp->blp_busy_threads);
1956 l_wait_event_exclusive(blp->blp_waitq,
1957 (blwi = ldlm_bl_get_work(blp)) != NULL,
1959 busy = atomic_inc_return(&blp->blp_busy_threads);
1961 if (blwi->blwi_ns == NULL)
1962 /* added by ldlm_cleanup() */
1965 /* Not fatal if racy and have a few too many threads */
1966 if (unlikely(busy < blp->blp_max_threads &&
1967 busy >= atomic_read(&blp->blp_num_threads)))
1968 /* discard the return value, we tried */
1969 ldlm_bl_thread_start(blp);
1971 if (blwi->blwi_ns == NULL)
1972 /* added by ldlm_cleanup() */
1976 if (blwi->blwi_count) {
1977 /* The special case when we cancel locks in lru
1978 * asynchronously, we pass the list of locks here.
1979 * Thus lock is marked LDLM_FL_CANCELING, and already
1980 * canceled locally. */
1981 ldlm_cli_cancel_list(&blwi->blwi_head,
1982 blwi->blwi_count, NULL, 0);
1984 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1987 OBD_FREE(blwi, sizeof(*blwi));
1990 atomic_dec(&blp->blp_busy_threads);
1991 atomic_dec(&blp->blp_num_threads);
1992 complete(&blp->blp_comp);
1999 * Export handle<->lock hash operations.
2002 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2004 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2008 ldlm_export_lock_key(struct hlist_node *hnode)
2010 struct ldlm_lock *lock;
2013 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2014 RETURN(&lock->l_remote_handle);
2018 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2021 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2025 ldlm_export_lock_get(struct hlist_node *hnode)
2027 struct ldlm_lock *lock;
2030 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2031 LDLM_LOCK_GET(lock);
2037 ldlm_export_lock_put(struct hlist_node *hnode)
2039 struct ldlm_lock *lock;
2042 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2043 LDLM_LOCK_PUT(lock);
2048 static lustre_hash_ops_t ldlm_export_lock_ops = {
2049 .lh_hash = ldlm_export_lock_hash,
2050 .lh_key = ldlm_export_lock_key,
2051 .lh_compare = ldlm_export_lock_compare,
2052 .lh_get = ldlm_export_lock_get,
2053 .lh_put = ldlm_export_lock_put
2056 int ldlm_init_export(struct obd_export *exp)
2060 exp->exp_lock_hash =
2061 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2062 7, 16, &ldlm_export_lock_ops, LH_REHASH);
2064 if (!exp->exp_lock_hash)
2069 EXPORT_SYMBOL(ldlm_init_export);
2071 void ldlm_destroy_export(struct obd_export *exp)
2074 lustre_hash_exit(exp->exp_lock_hash);
2075 exp->exp_lock_hash = NULL;
2078 EXPORT_SYMBOL(ldlm_destroy_export);
2080 static int ldlm_setup(void);
2081 static int ldlm_cleanup(void);
2083 int ldlm_get_ref(void)
2087 mutex_down(&ldlm_ref_sem);
2088 if (++ldlm_refcount == 1) {
2093 mutex_up(&ldlm_ref_sem);
2098 void ldlm_put_ref(void)
2101 mutex_down(&ldlm_ref_sem);
2102 if (ldlm_refcount == 1) {
2103 int rc = ldlm_cleanup();
2105 CERROR("ldlm_cleanup failed: %d\n", rc);
2111 mutex_up(&ldlm_ref_sem);
2116 static int ldlm_setup(void)
2118 struct ldlm_bl_pool *blp;
2120 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2121 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2127 if (ldlm_state != NULL)
2130 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2131 if (ldlm_state == NULL)
2135 rc = ldlm_proc_setup();
2141 if (ldlm_num_threads) {
2142 /* If ldlm_num_threads is set, it is the min and the max. */
2143 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2144 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2145 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2146 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2147 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2151 ldlm_state->ldlm_cb_service =
2152 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2153 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2154 LDLM_CB_REPLY_PORTAL, 1800,
2155 ldlm_callback_handler, "ldlm_cbd",
2156 ldlm_svc_proc_dir, NULL,
2157 ldlm_min_threads, ldlm_max_threads,
2160 if (!ldlm_state->ldlm_cb_service) {
2161 CERROR("failed to start service\n");
2162 GOTO(out_proc, rc = -ENOMEM);
2165 ldlm_state->ldlm_cancel_service =
2166 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2167 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2168 LDLM_CANCEL_REPLY_PORTAL, 6000,
2169 ldlm_cancel_handler, "ldlm_canceld",
2170 ldlm_svc_proc_dir, NULL,
2171 ldlm_min_threads, ldlm_max_threads,
2174 if (!ldlm_state->ldlm_cancel_service) {
2175 CERROR("failed to start service\n");
2176 GOTO(out_proc, rc = -ENOMEM);
2179 OBD_ALLOC(blp, sizeof(*blp));
2181 GOTO(out_proc, rc = -ENOMEM);
2182 ldlm_state->ldlm_bl_pool = blp;
2184 spin_lock_init(&blp->blp_lock);
2185 CFS_INIT_LIST_HEAD(&blp->blp_list);
2186 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2187 cfs_waitq_init(&blp->blp_waitq);
2188 atomic_set(&blp->blp_num_threads, 0);
2189 atomic_set(&blp->blp_busy_threads, 0);
2190 blp->blp_min_threads = ldlm_min_threads;
2191 blp->blp_max_threads = ldlm_max_threads;
2194 for (i = 0; i < blp->blp_min_threads; i++) {
2195 rc = ldlm_bl_thread_start(blp);
2197 GOTO(out_thread, rc);
2200 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2202 GOTO(out_thread, rc);
2204 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2206 GOTO(out_thread, rc);
2208 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2209 expired_lock_thread.elt_state = ELT_STOPPED;
2210 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2212 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2213 spin_lock_init(&waiting_locks_spinlock);
2214 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2216 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2218 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2219 GOTO(out_thread, rc);
2222 wait_event(expired_lock_thread.elt_waitq,
2223 expired_lock_thread.elt_state == ELT_READY);
2227 rc = ldlm_pools_init();
2229 GOTO(out_thread, rc);
2236 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2237 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2242 ldlm_proc_cleanup();
2245 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2250 static int ldlm_cleanup(void)
2253 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2257 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2258 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2259 CERROR("ldlm still has namespaces; clean these up first.\n");
2260 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2261 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2270 while (atomic_read(&blp->blp_num_threads) > 0) {
2271 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2273 init_completion(&blp->blp_comp);
2275 spin_lock(&blp->blp_lock);
2276 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2277 cfs_waitq_signal(&blp->blp_waitq);
2278 spin_unlock(&blp->blp_lock);
2280 wait_for_completion(&blp->blp_comp);
2282 OBD_FREE(blp, sizeof(*blp));
2284 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2285 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2286 ldlm_proc_cleanup();
2288 expired_lock_thread.elt_state = ELT_TERMINATE;
2289 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2290 wait_event(expired_lock_thread.elt_waitq,
2291 expired_lock_thread.elt_state == ELT_STOPPED);
2293 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2294 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2297 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2303 int __init ldlm_init(void)
2305 init_mutex(&ldlm_ref_sem);
2306 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2307 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2308 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2309 sizeof(struct ldlm_resource), 0,
2310 SLAB_HWCACHE_ALIGN);
2311 if (ldlm_resource_slab == NULL)
2314 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2315 sizeof(struct ldlm_lock), 0,
2316 SLAB_HWCACHE_ALIGN);
2317 if (ldlm_lock_slab == NULL) {
2318 cfs_mem_cache_destroy(ldlm_resource_slab);
2322 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2323 sizeof(struct ldlm_interval),
2324 0, SLAB_HWCACHE_ALIGN);
2325 if (ldlm_interval_slab == NULL) {
2326 cfs_mem_cache_destroy(ldlm_resource_slab);
2327 cfs_mem_cache_destroy(ldlm_lock_slab);
2334 void __exit ldlm_exit(void)
2338 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2339 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2340 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2341 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2342 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2343 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2344 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2348 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2351 EXPORT_SYMBOL(ldlm_get_processing_policy);
2352 EXPORT_SYMBOL(ldlm_lock2desc);
2353 EXPORT_SYMBOL(ldlm_register_intent);
2354 EXPORT_SYMBOL(ldlm_lockname);
2355 EXPORT_SYMBOL(ldlm_typename);
2356 EXPORT_SYMBOL(ldlm_lock2handle);
2357 EXPORT_SYMBOL(__ldlm_handle2lock);
2358 EXPORT_SYMBOL(ldlm_lock_get);
2359 EXPORT_SYMBOL(ldlm_lock_put);
2360 EXPORT_SYMBOL(ldlm_lock_fast_match);
2361 EXPORT_SYMBOL(ldlm_lock_fast_release);
2362 EXPORT_SYMBOL(ldlm_lock_match);
2363 EXPORT_SYMBOL(ldlm_lock_cancel);
2364 EXPORT_SYMBOL(ldlm_lock_addref);
2365 EXPORT_SYMBOL(ldlm_lock_decref);
2366 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2367 EXPORT_SYMBOL(ldlm_lock_change_resource);
2368 EXPORT_SYMBOL(ldlm_lock_set_data);
2369 EXPORT_SYMBOL(ldlm_it2str);
2370 EXPORT_SYMBOL(ldlm_lock_dump);
2371 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2372 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2373 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2374 EXPORT_SYMBOL(ldlm_lock_allow_match);
2376 /* ldlm_request.c */
2377 EXPORT_SYMBOL(ldlm_completion_ast);
2378 EXPORT_SYMBOL(ldlm_blocking_ast);
2379 EXPORT_SYMBOL(ldlm_glimpse_ast);
2380 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2381 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2382 EXPORT_SYMBOL(ldlm_prep_elc_req);
2383 EXPORT_SYMBOL(ldlm_cli_convert);
2384 EXPORT_SYMBOL(ldlm_cli_enqueue);
2385 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2386 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2387 EXPORT_SYMBOL(ldlm_cli_cancel);
2388 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2389 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2390 EXPORT_SYMBOL(ldlm_cli_join_lru);
2391 EXPORT_SYMBOL(ldlm_replay_locks);
2392 EXPORT_SYMBOL(ldlm_resource_foreach);
2393 EXPORT_SYMBOL(ldlm_namespace_foreach);
2394 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2395 EXPORT_SYMBOL(ldlm_resource_iterate);
2396 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2397 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2400 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2401 EXPORT_SYMBOL(ldlm_server_completion_ast);
2402 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2403 EXPORT_SYMBOL(ldlm_handle_enqueue);
2404 EXPORT_SYMBOL(ldlm_handle_cancel);
2405 EXPORT_SYMBOL(ldlm_request_cancel);
2406 EXPORT_SYMBOL(ldlm_handle_convert);
2407 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2408 EXPORT_SYMBOL(ldlm_get_ref);
2409 EXPORT_SYMBOL(ldlm_put_ref);
2410 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2412 /* ldlm_resource.c */
2413 EXPORT_SYMBOL(ldlm_namespace_new);
2414 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2415 EXPORT_SYMBOL(ldlm_namespace_free);
2416 EXPORT_SYMBOL(ldlm_namespace_dump);
2417 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2418 EXPORT_SYMBOL(ldlm_resource_get);
2419 EXPORT_SYMBOL(ldlm_resource_putref);
2420 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2423 EXPORT_SYMBOL(client_import_add_conn);
2424 EXPORT_SYMBOL(client_import_del_conn);
2425 EXPORT_SYMBOL(client_obd_setup);
2426 EXPORT_SYMBOL(client_obd_cleanup);
2427 EXPORT_SYMBOL(client_connect_import);
2428 EXPORT_SYMBOL(client_disconnect_export);
2429 EXPORT_SYMBOL(target_abort_recovery);
2430 EXPORT_SYMBOL(target_cleanup_recovery);
2431 EXPORT_SYMBOL(target_handle_connect);
2432 EXPORT_SYMBOL(target_destroy_export);
2433 EXPORT_SYMBOL(target_cancel_recovery_timer);
2434 EXPORT_SYMBOL(target_send_reply);
2435 EXPORT_SYMBOL(target_queue_recovery_request);
2436 EXPORT_SYMBOL(target_handle_ping);
2437 EXPORT_SYMBOL(target_pack_pool_reply);
2438 EXPORT_SYMBOL(target_handle_disconnect);
2439 EXPORT_SYMBOL(target_handle_reply);
2442 EXPORT_SYMBOL(lock_res_and_lock);
2443 EXPORT_SYMBOL(unlock_res_and_lock);