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 static cfs_semaphore_t ldlm_ref_sem;
67 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 cfs_spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
90 static cfs_list_t waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
93 static struct expired_lock_thread {
94 cfs_waitq_t elt_waitq;
97 cfs_list_t elt_expired_locks;
98 } expired_lock_thread;
101 #define ELT_STOPPED 0
103 #define ELT_TERMINATE 2
105 struct ldlm_bl_pool {
106 cfs_spinlock_t blp_lock;
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 cfs_list_t blp_prio_list;
116 * blp_list is used for all other callbacks which are likely
117 * to take longer to process.
121 cfs_waitq_t blp_waitq;
122 cfs_completion_t blp_comp;
123 cfs_atomic_t blp_num_threads;
124 cfs_atomic_t blp_busy_threads;
129 struct ldlm_bl_work_item {
130 cfs_list_t blwi_entry;
131 struct ldlm_namespace *blwi_ns;
132 struct ldlm_lock_desc blwi_ld;
133 struct ldlm_lock *blwi_lock;
134 cfs_list_t blwi_head;
140 static inline int have_expired_locks(void)
145 cfs_spin_lock_bh(&waiting_locks_spinlock);
146 need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
147 cfs_spin_unlock_bh(&waiting_locks_spinlock);
152 static int expired_lock_main(void *arg)
154 cfs_list_t *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 cfs_spin_lock_bh(&waiting_locks_spinlock);
171 if (expired_lock_thread.elt_dump) {
172 cfs_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 cfs_spin_lock_bh(&waiting_locks_spinlock);
181 expired_lock_thread.elt_dump = 0;
186 while (!cfs_list_empty(expired)) {
187 struct obd_export *export;
188 struct ldlm_lock *lock;
190 lock = cfs_list_entry(expired->next, struct ldlm_lock,
192 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
193 (void *)lock >= LP_POISON) {
194 cfs_spin_unlock_bh(&waiting_locks_spinlock);
195 CERROR("free lock on elt list %p\n", lock);
198 cfs_list_del_init(&lock->l_pending_chain);
199 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
200 (void *)lock->l_export >= LP_POISON) {
201 CERROR("lock with free export on elt list %p\n",
203 lock->l_export = NULL;
204 LDLM_ERROR(lock, "free export");
205 /* release extra ref grabbed by
206 * ldlm_add_waiting_lock() or
207 * ldlm_failed_ast() */
208 LDLM_LOCK_RELEASE(lock);
211 export = class_export_lock_get(lock->l_export, lock);
212 cfs_spin_unlock_bh(&waiting_locks_spinlock);
215 class_fail_export(export);
216 class_export_lock_put(export, lock);
218 /* release extra ref grabbed by ldlm_add_waiting_lock()
219 * or ldlm_failed_ast() */
220 LDLM_LOCK_RELEASE(lock);
222 cfs_spin_lock_bh(&waiting_locks_spinlock);
224 cfs_spin_unlock_bh(&waiting_locks_spinlock);
226 if (do_dump && obd_dump_on_eviction) {
227 CERROR("dump the log upon eviction\n");
228 libcfs_debug_dumplog();
231 if (expired_lock_thread.elt_state == ELT_TERMINATE)
235 expired_lock_thread.elt_state = ELT_STOPPED;
236 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
240 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
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 cfs_spin_lock(&lock->l_export->exp_lock);
256 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
258 if (req->rq_ops->hpreq_lock_match) {
259 match = req->rq_ops->hpreq_lock_match(req, lock);
264 cfs_spin_unlock(&lock->l_export->exp_lock);
268 /* This is called from within a timer interrupt and cannot schedule */
269 static void waiting_locks_callback(unsigned long unused)
271 struct ldlm_lock *lock, *last = NULL;
274 cfs_spin_lock_bh(&waiting_locks_spinlock);
275 while (!cfs_list_empty(&waiting_locks_list)) {
276 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
278 if (cfs_time_after(lock->l_callback_timeout,
279 cfs_time_current()) ||
280 (lock->l_req_mode == LCK_GROUP))
283 if (ptlrpc_check_suspend()) {
284 /* there is a case when we talk to one mds, holding
285 * lock from another mds. this way we easily can get
286 * here, if second mds is being recovered. so, we
287 * suspend timeouts. bug 6019 */
289 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
290 lock->l_export->exp_client_uuid.uuid,
291 lock->l_export->exp_connection->c_remote_uuid.uuid,
292 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
294 cfs_list_del_init(&lock->l_pending_chain);
295 cfs_spin_unlock_bh(&waiting_locks_spinlock);
296 ldlm_add_waiting_lock(lock);
300 /* if timeout overlaps the activation time of suspended timeouts
301 * then extend it to give a chance for client to reconnect */
302 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
303 cfs_time_seconds(obd_timeout)/2),
304 ptlrpc_suspend_wakeup_time())) {
305 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
306 lock->l_export->exp_client_uuid.uuid,
307 lock->l_export->exp_connection->c_remote_uuid.uuid,
308 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
310 cfs_list_del_init(&lock->l_pending_chain);
311 cfs_spin_unlock_bh(&waiting_locks_spinlock);
312 ldlm_add_waiting_lock(lock);
316 /* Check if we need to prolong timeout */
317 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
318 ldlm_lock_busy(lock)) {
321 if (lock->l_pending_chain.next == &waiting_locks_list)
326 cfs_spin_unlock_bh(&waiting_locks_spinlock);
327 LDLM_DEBUG(lock, "prolong the busy lock");
328 ldlm_refresh_waiting_lock(lock,
329 ldlm_get_enq_timeout(lock));
330 cfs_spin_lock_bh(&waiting_locks_spinlock);
333 LDLM_LOCK_RELEASE(lock);
337 LDLM_LOCK_RELEASE(lock);
340 lock->l_resource->lr_namespace->ns_timeouts++;
341 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
342 "evicting client at %s ",
343 cfs_time_current_sec()- lock->l_last_activity,
345 lock->l_export->exp_connection->c_peer.nid));
349 /* no needs to take an extra ref on the lock since it was in
350 * the waiting_locks_list and ldlm_add_waiting_lock()
351 * already grabbed a ref */
352 cfs_list_del(&lock->l_pending_chain);
353 cfs_list_add(&lock->l_pending_chain,
354 &expired_lock_thread.elt_expired_locks);
357 if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
358 if (obd_dump_on_timeout)
359 expired_lock_thread.elt_dump = __LINE__;
361 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
365 * Make sure the timer will fire again if we have any locks
368 if (!cfs_list_empty(&waiting_locks_list)) {
369 cfs_time_t timeout_rounded;
370 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
372 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
373 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
375 cfs_spin_unlock_bh(&waiting_locks_spinlock);
379 * Indicate that we're waiting for a client to call us back cancelling a given
380 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
381 * timer to fire appropriately. (We round up to the next second, to avoid
382 * floods of timer firings during periods of high lock contention and traffic).
383 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
384 * if it has been added to the waiting list (1 is returned).
386 * Called with the namespace lock held.
388 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
391 cfs_time_t timeout_rounded;
393 if (!cfs_list_empty(&lock->l_pending_chain))
396 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
397 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
400 timeout = cfs_time_shift(seconds);
401 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
402 lock->l_callback_timeout = timeout;
404 timeout_rounded = round_timeout(lock->l_callback_timeout);
406 if (cfs_time_before(timeout_rounded,
407 cfs_timer_deadline(&waiting_locks_timer)) ||
408 !cfs_timer_is_armed(&waiting_locks_timer)) {
409 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
411 /* if the new lock has a shorter timeout than something earlier on
412 the list, we'll wait the longer amount of time; no big deal. */
414 cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
418 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
421 int timeout = ldlm_get_enq_timeout(lock);
423 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
425 cfs_spin_lock_bh(&waiting_locks_spinlock);
426 if (lock->l_destroyed) {
427 static cfs_time_t next;
428 cfs_spin_unlock_bh(&waiting_locks_spinlock);
429 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
430 if (cfs_time_after(cfs_time_current(), next)) {
431 next = cfs_time_shift(14400);
432 libcfs_debug_dumpstack(NULL);
437 ret = __ldlm_add_waiting_lock(lock, timeout);
439 /* grab ref on the lock if it has been added to the
442 cfs_spin_unlock_bh(&waiting_locks_spinlock);
444 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
445 ret == 0 ? "not re-" : "", timeout,
446 AT_OFF ? "off" : "on");
451 * Remove a lock from the pending list, likely because it had its cancellation
452 * callback arrive without incident. This adjusts the lock-timeout timer if
453 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
454 * As done by ldlm_del_waiting_lock(), the caller must release the lock
455 * reference when the lock is removed from any list (1 is returned).
457 * Called with namespace lock held.
459 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
461 cfs_list_t *list_next;
463 if (cfs_list_empty(&lock->l_pending_chain))
466 list_next = lock->l_pending_chain.next;
467 if (lock->l_pending_chain.prev == &waiting_locks_list) {
468 /* Removing the head of the list, adjust timer. */
469 if (list_next == &waiting_locks_list) {
470 /* No more, just cancel. */
471 cfs_timer_disarm(&waiting_locks_timer);
473 struct ldlm_lock *next;
474 next = cfs_list_entry(list_next, struct ldlm_lock,
476 cfs_timer_arm(&waiting_locks_timer,
477 round_timeout(next->l_callback_timeout));
480 cfs_list_del_init(&lock->l_pending_chain);
485 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
489 if (lock->l_export == NULL) {
490 /* We don't have a "waiting locks list" on clients. */
491 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
495 cfs_spin_lock_bh(&waiting_locks_spinlock);
496 ret = __ldlm_del_waiting_lock(lock);
497 cfs_spin_unlock_bh(&waiting_locks_spinlock);
499 /* release lock ref if it has indeed been removed
501 LDLM_LOCK_RELEASE(lock);
503 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
510 * Called with namespace lock held.
512 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
514 if (lock->l_export == NULL) {
515 /* We don't have a "waiting locks list" on clients. */
516 LDLM_DEBUG(lock, "client lock: no-op");
520 cfs_spin_lock_bh(&waiting_locks_spinlock);
522 if (cfs_list_empty(&lock->l_pending_chain)) {
523 cfs_spin_unlock_bh(&waiting_locks_spinlock);
524 LDLM_DEBUG(lock, "wasn't waiting");
528 /* we remove/add the lock to the waiting list, so no needs to
529 * release/take a lock reference */
530 __ldlm_del_waiting_lock(lock);
531 __ldlm_add_waiting_lock(lock, timeout);
532 cfs_spin_unlock_bh(&waiting_locks_spinlock);
534 LDLM_DEBUG(lock, "refreshed");
537 #else /* !__KERNEL__ */
539 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
541 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
545 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
550 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
554 #endif /* __KERNEL__ */
556 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
557 const char *ast_type)
559 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
560 "to a lock %s callback time out: rc %d\n",
561 lock->l_export->exp_obd->obd_name,
562 obd_export_nid2str(lock->l_export), ast_type, rc);
564 if (obd_dump_on_timeout)
565 libcfs_debug_dumplog();
567 cfs_spin_lock_bh(&waiting_locks_spinlock);
568 if (__ldlm_del_waiting_lock(lock) == 0)
569 /* the lock was not in any list, grab an extra ref before adding
570 * the lock to the expired list */
572 cfs_list_add(&lock->l_pending_chain,
573 &expired_lock_thread.elt_expired_locks);
574 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
575 cfs_spin_unlock_bh(&waiting_locks_spinlock);
577 class_fail_export(lock->l_export);
581 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
582 struct ptlrpc_request *req, int rc,
583 const char *ast_type)
585 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
587 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
588 LASSERT(lock->l_export);
589 if (lock->l_export->exp_libclient) {
590 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
591 " timeout, just cancelling lock", ast_type,
592 libcfs_nid2str(peer.nid));
593 ldlm_lock_cancel(lock);
595 } else if (lock->l_flags & LDLM_FL_CANCEL) {
596 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
597 "cancel was received (AST reply lost?)",
598 ast_type, libcfs_nid2str(peer.nid));
599 ldlm_lock_cancel(lock);
602 ldlm_del_waiting_lock(lock);
603 ldlm_failed_ast(lock, rc, ast_type);
607 LDLM_DEBUG(lock, "client (nid %s) returned %d"
608 " from %s AST - normal race",
609 libcfs_nid2str(peer.nid),
611 lustre_msg_get_status(req->rq_repmsg) : -1,
614 LDLM_ERROR(lock, "client (nid %s) returned %d "
615 "from %s AST", libcfs_nid2str(peer.nid),
616 (req->rq_repmsg != NULL) ?
617 lustre_msg_get_status(req->rq_repmsg) : 0,
619 ldlm_lock_cancel(lock);
620 /* Server-side AST functions are called from ldlm_reprocess_all,
621 * which needs to be told to please restart its reprocessing. */
628 static int ldlm_cb_interpret(const struct lu_env *env,
629 struct ptlrpc_request *req, void *data, int rc)
631 struct ldlm_cb_set_arg *arg;
632 struct ldlm_lock *lock;
635 LASSERT(data != NULL);
637 arg = req->rq_async_args.pointer_arg[0];
638 lock = req->rq_async_args.pointer_arg[1];
639 LASSERT(lock != NULL);
641 /* If client canceled the lock but the cancel has not
642 * been received yet, we need to update lvbo to have the
643 * proper attributes cached. */
644 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
645 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
646 rc = ldlm_handle_ast_error(lock, req, rc,
647 arg->type == LDLM_BL_CALLBACK
648 ? "blocking" : "completion");
651 LDLM_LOCK_RELEASE(lock);
654 cfs_atomic_set(&arg->restart, 1);
659 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
660 struct ldlm_cb_set_arg *arg,
661 struct ldlm_lock *lock,
667 if (unlikely(instant_cancel)) {
668 rc = ptl_send_rpc(req, 1);
669 ptlrpc_req_finished(req);
671 /* If we cancelled the lock, we need to restart
672 * ldlm_reprocess_queue */
673 cfs_atomic_set(&arg->restart, 1);
676 ptlrpc_set_add_req(arg->set, req);
683 * Check if there are requests in the export request list which prevent
684 * the lock canceling and make these requests high priority ones.
686 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
688 struct ptlrpc_request *req;
691 if (lock->l_export == NULL) {
692 LDLM_DEBUG(lock, "client lock: no-op");
696 cfs_spin_lock(&lock->l_export->exp_lock);
697 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
699 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
700 req->rq_ops->hpreq_lock_match(req, lock))
701 ptlrpc_hpreq_reorder(req);
703 cfs_spin_unlock(&lock->l_export->exp_lock);
708 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
709 * enqueued server lock conflicts with given one.
711 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
712 * to wait for client response.
714 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
715 struct ldlm_lock_desc *desc,
716 void *data, int flag)
718 struct ldlm_cb_set_arg *arg = data;
719 struct ldlm_request *body;
720 struct ptlrpc_request *req;
721 int instant_cancel = 0;
725 if (flag == LDLM_CB_CANCELING)
726 /* Don't need to do anything here. */
730 LASSERT(data != NULL);
731 if (lock->l_export->exp_obd->obd_recovering != 0) {
732 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
733 ldlm_lock_dump(D_ERROR, lock, 0);
736 ldlm_lock_reorder_req(lock);
738 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
739 &RQF_LDLM_BL_CALLBACK,
740 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
744 req->rq_async_args.pointer_arg[0] = arg;
745 req->rq_async_args.pointer_arg[1] = lock;
746 req->rq_interpret_reply = ldlm_cb_interpret;
747 req->rq_no_resend = 1;
749 lock_res(lock->l_resource);
750 if (lock->l_granted_mode != lock->l_req_mode) {
751 /* this blocking AST will be communicated as part of the
752 * completion AST instead */
753 unlock_res(lock->l_resource);
754 ptlrpc_req_finished(req);
755 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
759 if (lock->l_destroyed) {
760 /* What's the point? */
761 unlock_res(lock->l_resource);
762 ptlrpc_req_finished(req);
766 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
769 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
770 body->lock_handle[0] = lock->l_remote_handle;
771 body->lock_desc = *desc;
772 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
774 LDLM_DEBUG(lock, "server preparing blocking AST");
776 ptlrpc_request_set_replen(req);
777 if (instant_cancel) {
778 unlock_res(lock->l_resource);
779 ldlm_lock_cancel(lock);
781 LASSERT(lock->l_granted_mode == lock->l_req_mode);
782 ldlm_add_waiting_lock(lock);
783 unlock_res(lock->l_resource);
786 req->rq_send_state = LUSTRE_IMP_FULL;
787 /* ptlrpc_request_alloc_pack already set timeout */
789 req->rq_timeout = ldlm_get_rq_timeout();
791 if (lock->l_export && lock->l_export->exp_nid_stats &&
792 lock->l_export->exp_nid_stats->nid_ldlm_stats)
793 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
794 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
796 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
801 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
803 struct ldlm_cb_set_arg *arg = data;
804 struct ldlm_request *body;
805 struct ptlrpc_request *req;
806 long total_enqueue_wait;
807 int instant_cancel = 0;
811 LASSERT(lock != NULL);
812 LASSERT(data != NULL);
814 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
815 lock->l_last_activity);
817 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
818 &RQF_LDLM_CP_CALLBACK);
822 lock_res_and_lock(lock);
823 if (lock->l_resource->lr_lvb_len)
824 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
825 lock->l_resource->lr_lvb_len);
826 unlock_res_and_lock(lock);
828 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
830 ptlrpc_request_free(req);
834 req->rq_async_args.pointer_arg[0] = arg;
835 req->rq_async_args.pointer_arg[1] = lock;
836 req->rq_interpret_reply = ldlm_cb_interpret;
837 req->rq_no_resend = 1;
838 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
840 body->lock_handle[0] = lock->l_remote_handle;
841 body->lock_flags = flags;
842 ldlm_lock2desc(lock, &body->lock_desc);
843 if (lock->l_resource->lr_lvb_len) {
844 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
846 lock_res_and_lock(lock);
847 memcpy(lvb, lock->l_resource->lr_lvb_data,
848 lock->l_resource->lr_lvb_len);
849 unlock_res_and_lock(lock);
852 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
855 /* Server-side enqueue wait time estimate, used in
856 __ldlm_add_waiting_lock to set future enqueue timers */
857 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
858 at_measured(&lock->l_resource->lr_namespace->ns_at_estimate,
861 /* bz18618. Don't add lock enqueue time we spend waiting for a
862 previous callback to fail. Locks waiting legitimately will
863 get extended by ldlm_refresh_waiting_lock regardless of the
864 estimate, so it's okay to underestimate here. */
865 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
866 "It is likely that a previous callback timed out.",
868 at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
870 ptlrpc_request_set_replen(req);
872 req->rq_send_state = LUSTRE_IMP_FULL;
873 /* ptlrpc_request_pack already set timeout */
875 req->rq_timeout = ldlm_get_rq_timeout();
877 /* We only send real blocking ASTs after the lock is granted */
878 lock_res_and_lock(lock);
879 if (lock->l_flags & LDLM_FL_AST_SENT) {
880 body->lock_flags |= LDLM_FL_AST_SENT;
881 /* copy ast flags like LDLM_FL_DISCARD_DATA */
882 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
884 /* We might get here prior to ldlm_handle_enqueue setting
885 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
886 * into waiting list, but this is safe and similar code in
887 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
888 * that would not only cancel the lock, but will also remove
889 * it from waiting list */
890 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
891 unlock_res_and_lock(lock);
892 ldlm_lock_cancel(lock);
894 lock_res_and_lock(lock);
896 /* start the lock-timeout clock */
897 ldlm_add_waiting_lock(lock);
900 unlock_res_and_lock(lock);
902 if (lock->l_export && lock->l_export->exp_nid_stats &&
903 lock->l_export->exp_nid_stats->nid_ldlm_stats)
904 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
905 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
907 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
912 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
914 struct ldlm_resource *res = lock->l_resource;
915 struct ldlm_request *body;
916 struct ptlrpc_request *req;
920 LASSERT(lock != NULL);
922 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
923 &RQF_LDLM_GL_CALLBACK,
924 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
929 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
930 body->lock_handle[0] = lock->l_remote_handle;
931 ldlm_lock2desc(lock, &body->lock_desc);
933 lock_res_and_lock(lock);
934 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
935 lock->l_resource->lr_lvb_len);
936 unlock_res_and_lock(lock);
937 res = lock->l_resource;
938 ptlrpc_request_set_replen(req);
941 req->rq_send_state = LUSTRE_IMP_FULL;
942 /* ptlrpc_request_alloc_pack already set timeout */
944 req->rq_timeout = ldlm_get_rq_timeout();
946 if (lock->l_export && lock->l_export->exp_nid_stats &&
947 lock->l_export->exp_nid_stats->nid_ldlm_stats)
948 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
949 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
951 rc = ptlrpc_queue_wait(req);
952 if (rc == -ELDLM_NO_LOCK_DATA)
953 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
955 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
957 rc = ldlm_res_lvbo_update(res, req, 1);
959 ptlrpc_req_finished(req);
961 ldlm_reprocess_all(res);
967 extern unsigned long long lu_time_stamp_get(void);
969 #define lu_time_stamp_get() time(NULL)
972 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
973 struct lprocfs_stats *srv_stats)
975 int lock_type = 0, op = 0;
977 lock_type = dlm_req->lock_desc.l_resource.lr_type;
981 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
984 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
985 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
987 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
990 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
993 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1001 lprocfs_counter_incr(srv_stats, op);
1007 * Main server-side entry point into LDLM. This is called by ptlrpc service
1008 * threads to carry out client lock enqueueing requests.
1010 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1011 struct ptlrpc_request *req,
1012 const struct ldlm_request *dlm_req,
1013 const struct ldlm_callback_suite *cbs)
1015 struct ldlm_reply *dlm_rep;
1017 ldlm_error_t err = ELDLM_OK;
1018 struct ldlm_lock *lock = NULL;
1019 void *cookie = NULL;
1023 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1025 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1026 flags = dlm_req->lock_flags;
1028 LASSERT(req->rq_export);
1030 if (req->rq_rqbd->rqbd_service->srv_stats)
1031 ldlm_svc_get_eopc(dlm_req,
1032 req->rq_rqbd->rqbd_service->srv_stats);
1034 if (req->rq_export && req->rq_export->exp_nid_stats &&
1035 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1036 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1037 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1039 if (unlikely(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 (unlikely(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 &
1049 (dlm_req->lock_desc.l_req_mode-1))) {
1050 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1051 dlm_req->lock_desc.l_req_mode);
1052 GOTO(out, rc = -EFAULT);
1055 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1056 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1058 DEBUG_REQ(D_ERROR, req,
1059 "PLAIN lock request from IBITS client?");
1060 GOTO(out, rc = -EPROTO);
1062 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1064 DEBUG_REQ(D_ERROR, req,
1065 "IBITS lock request from unaware client?");
1066 GOTO(out, rc = -EPROTO);
1070 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1071 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1072 ibits for mgc/mgs) */
1074 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1075 * inodebits lock if client does not support them. */
1076 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1077 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1078 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1079 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1080 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1081 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1082 dlm_req->lock_desc.l_req_mode = LCK_CR;
1086 if (unlikely(flags & LDLM_FL_REPLAY)) {
1087 /* Find an existing lock in the per-export lock hash */
1088 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1089 (void *)&dlm_req->lock_handle[0]);
1091 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1092 LPX64, lock->l_handle.h_cookie);
1093 GOTO(existing_lock, rc = 0);
1097 /* The lock's callback data might be set in the policy function */
1098 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1099 dlm_req->lock_desc.l_resource.lr_type,
1100 dlm_req->lock_desc.l_req_mode,
1104 GOTO(out, rc = -ENOMEM);
1106 lock->l_last_activity = cfs_time_current_sec();
1107 lock->l_remote_handle = dlm_req->lock_handle[0];
1108 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1110 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1111 /* Don't enqueue a lock onto the export if it has already
1112 * been evicted. Cancel it now instead. (bug 3822) */
1113 if (req->rq_export->exp_failed) {
1114 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1115 GOTO(out, rc = -ENOTCONN);
1118 lock->l_export = class_export_lock_get(req->rq_export, lock);
1119 if (lock->l_export->exp_lock_hash)
1120 cfs_hash_add(lock->l_export->exp_lock_hash,
1121 &lock->l_remote_handle,
1126 if (flags & LDLM_FL_HAS_INTENT) {
1127 /* In this case, the reply buffer is allocated deep in
1128 * local_lock_enqueue by the policy function. */
1131 lock_res_and_lock(lock);
1132 if (lock->l_resource->lr_lvb_len) {
1133 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1135 lock->l_resource->lr_lvb_len);
1137 unlock_res_and_lock(lock);
1139 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1140 GOTO(out, rc = -ENOMEM);
1142 rc = req_capsule_server_pack(&req->rq_pill);
1147 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1148 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1149 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1150 lock->l_req_extent = lock->l_policy_data.l_extent;
1152 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1156 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1157 dlm_rep->lock_flags = flags;
1159 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1160 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1162 /* We never send a blocking AST until the lock is granted, but
1163 * we can tell it right now */
1164 lock_res_and_lock(lock);
1166 /* Now take into account flags to be inherited from original lock
1167 request both in reply to client and in our own lock flags. */
1168 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1169 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1171 /* Don't move a pending lock onto the export if it has already
1172 * been evicted. Cancel it now instead. (bug 5683) */
1173 if (unlikely(req->rq_export->exp_failed ||
1174 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1175 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1177 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1178 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1179 if (lock->l_granted_mode == lock->l_req_mode) {
1181 * Only cancel lock if it was granted, because it would
1182 * be destroyed immediately and would never be granted
1183 * in the future, causing timeouts on client. Not
1184 * granted lock will be cancelled immediately after
1185 * sending completion AST.
1187 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1188 unlock_res_and_lock(lock);
1189 ldlm_lock_cancel(lock);
1190 lock_res_and_lock(lock);
1192 ldlm_add_waiting_lock(lock);
1195 /* Make sure we never ever grant usual metadata locks to liblustre
1197 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1198 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1199 req->rq_export->exp_libclient) {
1200 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1201 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1202 CERROR("Granting sync lock to libclient. "
1203 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1204 dlm_req->lock_flags, dlm_rep->lock_flags,
1206 LDLM_ERROR(lock, "sync lock");
1207 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1208 struct ldlm_intent *it;
1210 it = req_capsule_client_get(&req->rq_pill,
1213 CERROR("This is intent %s ("LPU64")\n",
1214 ldlm_it2str(it->opc), it->opc);
1220 unlock_res_and_lock(lock);
1224 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1225 if (!req->rq_packed_final) {
1226 err = lustre_pack_reply(req, 1, NULL, NULL);
1231 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1232 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1234 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1235 "(err=%d, rc=%d)", err, rc);
1237 lock_res_and_lock(lock);
1239 if (lock->l_resource->lr_lvb_len > 0) {
1242 lvb = req_capsule_server_get(&req->rq_pill,
1244 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1247 memcpy(lvb, lock->l_resource->lr_lvb_data,
1248 lock->l_resource->lr_lvb_len);
1251 ldlm_resource_unlink_lock(lock);
1252 ldlm_lock_destroy_nolock(lock);
1254 unlock_res_and_lock(lock);
1256 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1257 ldlm_reprocess_all(lock->l_resource);
1259 LDLM_LOCK_RELEASE(lock);
1262 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1268 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1269 ldlm_completion_callback completion_callback,
1270 ldlm_blocking_callback blocking_callback,
1271 ldlm_glimpse_callback glimpse_callback)
1273 struct ldlm_request *dlm_req;
1274 struct ldlm_callback_suite cbs = {
1275 .lcs_completion = completion_callback,
1276 .lcs_blocking = blocking_callback,
1277 .lcs_glimpse = glimpse_callback
1281 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1282 if (dlm_req != NULL) {
1283 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1284 req, dlm_req, &cbs);
1291 int ldlm_handle_convert0(struct ptlrpc_request *req,
1292 const struct ldlm_request *dlm_req)
1294 struct ldlm_reply *dlm_rep;
1295 struct ldlm_lock *lock;
1299 if (req->rq_export && req->rq_export->exp_nid_stats &&
1300 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1301 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1302 LDLM_CONVERT - LDLM_FIRST_OPC);
1304 rc = req_capsule_server_pack(&req->rq_pill);
1308 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1309 dlm_rep->lock_flags = dlm_req->lock_flags;
1311 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1313 req->rq_status = EINVAL;
1317 LDLM_DEBUG(lock, "server-side convert handler START");
1319 lock->l_last_activity = cfs_time_current_sec();
1320 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1321 &dlm_rep->lock_flags);
1323 if (ldlm_del_waiting_lock(lock))
1324 LDLM_DEBUG(lock, "converted waiting lock");
1327 req->rq_status = EDEADLOCK;
1332 if (!req->rq_status)
1333 ldlm_reprocess_all(lock->l_resource);
1334 LDLM_DEBUG(lock, "server-side convert handler END");
1335 LDLM_LOCK_PUT(lock);
1337 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1342 int ldlm_handle_convert(struct ptlrpc_request *req)
1345 struct ldlm_request *dlm_req;
1347 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1348 if (dlm_req != NULL) {
1349 rc = ldlm_handle_convert0(req, dlm_req);
1351 CERROR ("Can't unpack dlm_req\n");
1357 /* Cancel all the locks whos handles are packed into ldlm_request */
1358 int ldlm_request_cancel(struct ptlrpc_request *req,
1359 const struct ldlm_request *dlm_req, int first)
1361 struct ldlm_resource *res, *pres = NULL;
1362 struct ldlm_lock *lock;
1363 int i, count, done = 0;
1366 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1370 /* There is no lock on the server at the replay time,
1371 * skip lock cancelling to make replay tests to pass. */
1372 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1375 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1376 "starting at %d", count, first);
1378 for (i = first; i < count; i++) {
1379 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1381 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1382 "lock (cookie "LPU64")",
1383 dlm_req->lock_handle[i].cookie);
1387 res = lock->l_resource;
1392 ldlm_reprocess_all(pres);
1393 LDLM_RESOURCE_DELREF(pres);
1394 ldlm_resource_putref(pres);
1397 ldlm_resource_getref(res);
1398 LDLM_RESOURCE_ADDREF(res);
1399 ldlm_res_lvbo_update(res, NULL, 1);
1403 ldlm_lock_cancel(lock);
1404 LDLM_LOCK_PUT(lock);
1407 ldlm_reprocess_all(pres);
1408 LDLM_RESOURCE_DELREF(pres);
1409 ldlm_resource_putref(pres);
1411 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1415 int ldlm_handle_cancel(struct ptlrpc_request *req)
1417 struct ldlm_request *dlm_req;
1421 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1422 if (dlm_req == NULL) {
1423 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1427 if (req->rq_export && req->rq_export->exp_nid_stats &&
1428 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1429 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1430 LDLM_CANCEL - LDLM_FIRST_OPC);
1432 rc = req_capsule_server_pack(&req->rq_pill);
1436 if (!ldlm_request_cancel(req, dlm_req, 0))
1437 req->rq_status = ESTALE;
1439 if (ptlrpc_reply(req) != 0)
1445 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1446 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1451 LDLM_DEBUG(lock, "client blocking AST callback handler");
1453 lock_res_and_lock(lock);
1454 lock->l_flags |= LDLM_FL_CBPENDING;
1456 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1457 lock->l_flags |= LDLM_FL_CANCEL;
1459 do_ast = (!lock->l_readers && !lock->l_writers);
1460 unlock_res_and_lock(lock);
1463 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1464 lock, lock->l_blocking_ast);
1465 if (lock->l_blocking_ast != NULL)
1466 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1469 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1473 LDLM_DEBUG(lock, "client blocking callback handler END");
1474 LDLM_LOCK_RELEASE(lock);
1478 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1479 struct ldlm_namespace *ns,
1480 struct ldlm_request *dlm_req,
1481 struct ldlm_lock *lock)
1483 CFS_LIST_HEAD(ast_list);
1486 LDLM_DEBUG(lock, "client completion callback handler START");
1488 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1489 int to = cfs_time_seconds(1);
1491 cfs_schedule_timeout_and_set_state(
1492 CFS_TASK_INTERRUPTIBLE, to);
1493 if (lock->l_granted_mode == lock->l_req_mode ||
1499 lock_res_and_lock(lock);
1500 if (lock->l_destroyed ||
1501 lock->l_granted_mode == lock->l_req_mode) {
1502 /* bug 11300: the lock has already been granted */
1503 unlock_res_and_lock(lock);
1504 LDLM_DEBUG(lock, "Double grant race happened");
1505 LDLM_LOCK_RELEASE(lock);
1510 /* If we receive the completion AST before the actual enqueue returned,
1511 * then we might need to switch lock modes, resources, or extents. */
1512 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1513 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1514 LDLM_DEBUG(lock, "completion AST, new lock mode");
1517 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1518 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1519 LDLM_DEBUG(lock, "completion AST, new policy data");
1522 ldlm_resource_unlink_lock(lock);
1523 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1524 &lock->l_resource->lr_name,
1525 sizeof(lock->l_resource->lr_name)) != 0) {
1526 unlock_res_and_lock(lock);
1527 if (ldlm_lock_change_resource(ns, lock,
1528 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1529 LDLM_ERROR(lock, "Failed to allocate resource");
1530 LDLM_LOCK_RELEASE(lock);
1534 LDLM_DEBUG(lock, "completion AST, new resource");
1535 CERROR("change resource!\n");
1536 lock_res_and_lock(lock);
1539 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1540 /* BL_AST locks are not needed in lru.
1541 * let ldlm_cancel_lru() be fast. */
1542 ldlm_lock_remove_from_lru(lock);
1543 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1544 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1547 if (lock->l_lvb_len) {
1548 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1549 RCL_CLIENT) < lock->l_lvb_len) {
1550 LDLM_ERROR(lock, "completion AST did not contain "
1553 void *lvb = req_capsule_client_get(&req->rq_pill,
1555 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1559 ldlm_grant_lock(lock, &ast_list);
1560 unlock_res_and_lock(lock);
1562 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1564 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1566 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1568 LDLM_LOCK_RELEASE(lock);
1572 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1573 struct ldlm_namespace *ns,
1574 struct ldlm_request *dlm_req,
1575 struct ldlm_lock *lock)
1580 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1582 if (lock->l_glimpse_ast != NULL)
1583 rc = lock->l_glimpse_ast(lock, req);
1585 if (req->rq_repmsg != NULL) {
1588 req->rq_status = rc;
1592 lock_res_and_lock(lock);
1593 if (lock->l_granted_mode == LCK_PW &&
1594 !lock->l_readers && !lock->l_writers &&
1595 cfs_time_after(cfs_time_current(),
1596 cfs_time_add(lock->l_last_used,
1597 cfs_time_seconds(10)))) {
1598 unlock_res_and_lock(lock);
1599 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1600 ldlm_handle_bl_callback(ns, NULL, lock);
1605 unlock_res_and_lock(lock);
1606 LDLM_LOCK_RELEASE(lock);
1610 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1612 if (req->rq_no_reply)
1615 req->rq_status = rc;
1616 if (!req->rq_packed_final) {
1617 rc = lustre_pack_reply(req, 1, NULL, NULL);
1621 return ptlrpc_reply(req);
1625 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1626 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1627 cfs_list_t *cancels, int count)
1629 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1630 struct ldlm_bl_work_item *blwi;
1633 if (cancels && count == 0)
1636 OBD_ALLOC(blwi, sizeof(*blwi));
1642 blwi->blwi_ld = *ld;
1644 cfs_list_add(&blwi->blwi_head, cancels);
1645 cfs_list_del_init(cancels);
1646 blwi->blwi_count = count;
1648 blwi->blwi_lock = lock;
1650 cfs_spin_lock(&blp->blp_lock);
1651 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1652 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1653 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1655 /* other blocking callbacks are added to the regular list */
1656 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1658 cfs_waitq_signal(&blp->blp_waitq);
1659 cfs_spin_unlock(&blp->blp_lock);
1665 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1666 struct ldlm_lock *lock)
1669 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1675 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1676 cfs_list_t *cancels, int count)
1679 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1685 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1686 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1688 struct obd_device *obd = req->rq_export->exp_obd;
1695 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1697 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1699 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1701 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1704 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1706 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1708 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1711 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1714 /* We are responsible for swabbing contents of val */
1716 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1717 /* Pass it on to mdc (the "export" in this case) */
1718 rc = obd_set_info_async(req->rq_export,
1719 sizeof(KEY_HSM_COPYTOOL_SEND),
1720 KEY_HSM_COPYTOOL_SEND,
1723 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1728 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1729 const char *msg, int rc,
1730 struct lustre_handle *handle)
1732 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1733 "%s: [nid %s] [rc %d] [lock "LPX64"]",
1734 msg, libcfs_id2str(req->rq_peer), rc,
1735 handle ? handle->cookie : 0);
1736 if (req->rq_no_reply)
1737 CWARN("No reply was sent, maybe cause bug 21636.\n");
1739 CWARN("Send reply failed, maybe cause bug 21636.\n");
1742 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1743 static int ldlm_callback_handler(struct ptlrpc_request *req)
1745 struct ldlm_namespace *ns;
1746 struct ldlm_request *dlm_req;
1747 struct ldlm_lock *lock;
1751 /* Requests arrive in sender's byte order. The ptlrpc service
1752 * handler has already checked and, if necessary, byte-swapped the
1753 * incoming request message body, but I am responsible for the
1754 * message buffers. */
1756 /* do nothing for sec context finalize */
1757 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1760 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1762 if (req->rq_export == NULL) {
1763 rc = ldlm_callback_reply(req, -ENOTCONN);
1764 ldlm_callback_errmsg(req, "Operate on unconnected server",
1769 LASSERT(req->rq_export != NULL);
1770 LASSERT(req->rq_export->exp_obd != NULL);
1772 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1773 case LDLM_BL_CALLBACK:
1774 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1777 case LDLM_CP_CALLBACK:
1778 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1781 case LDLM_GL_CALLBACK:
1782 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1786 rc = ldlm_handle_setinfo(req);
1787 ldlm_callback_reply(req, rc);
1789 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1790 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1791 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1792 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1794 rc = llog_origin_handle_cancel(req);
1795 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1797 ldlm_callback_reply(req, rc);
1799 case OBD_QC_CALLBACK:
1800 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1801 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1803 rc = target_handle_qc_callback(req);
1804 ldlm_callback_reply(req, rc);
1808 /* reply in handler */
1809 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1810 rc = target_handle_dqacq_callback(req);
1812 case LLOG_ORIGIN_HANDLE_CREATE:
1813 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1814 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1816 rc = llog_origin_handle_create(req);
1817 ldlm_callback_reply(req, rc);
1819 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1820 req_capsule_set(&req->rq_pill,
1821 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1822 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1824 rc = llog_origin_handle_next_block(req);
1825 ldlm_callback_reply(req, rc);
1827 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1828 req_capsule_set(&req->rq_pill,
1829 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1830 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1832 rc = llog_origin_handle_read_header(req);
1833 ldlm_callback_reply(req, rc);
1835 case LLOG_ORIGIN_HANDLE_CLOSE:
1836 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1838 rc = llog_origin_handle_close(req);
1839 ldlm_callback_reply(req, rc);
1842 CERROR("unknown opcode %u\n",
1843 lustre_msg_get_opc(req->rq_reqmsg));
1844 ldlm_callback_reply(req, -EPROTO);
1848 ns = req->rq_export->exp_obd->obd_namespace;
1849 LASSERT(ns != NULL);
1851 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1853 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1854 if (dlm_req == NULL) {
1855 rc = ldlm_callback_reply(req, -EPROTO);
1856 ldlm_callback_errmsg(req, "Operate without parameter", rc,
1861 /* Force a known safe race, send a cancel to the server for a lock
1862 * which the server has already started a blocking callback on. */
1863 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1864 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1865 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1867 CERROR("ldlm_cli_cancel: %d\n", rc);
1870 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1872 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1873 "disappeared\n", dlm_req->lock_handle[0].cookie);
1874 rc = ldlm_callback_reply(req, -EINVAL);
1875 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
1876 &dlm_req->lock_handle[0]);
1880 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1881 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1882 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1884 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1885 lock_res_and_lock(lock);
1886 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1887 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1888 /* If somebody cancels lock and cache is already dropped,
1889 * or lock is failed before cp_ast received on client,
1890 * we can tell the server we have no lock. Otherwise, we
1891 * should send cancel after dropping the cache. */
1892 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1893 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1894 (lock->l_flags & LDLM_FL_FAILED)) {
1895 LDLM_DEBUG(lock, "callback on lock "
1896 LPX64" - lock disappeared\n",
1897 dlm_req->lock_handle[0].cookie);
1898 unlock_res_and_lock(lock);
1899 LDLM_LOCK_RELEASE(lock);
1900 rc = ldlm_callback_reply(req, -EINVAL);
1901 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
1902 &dlm_req->lock_handle[0]);
1905 /* BL_AST locks are not needed in lru.
1906 * let ldlm_cancel_lru() be fast. */
1907 ldlm_lock_remove_from_lru(lock);
1908 lock->l_flags |= LDLM_FL_BL_AST;
1910 unlock_res_and_lock(lock);
1912 /* We want the ost thread to get this reply so that it can respond
1913 * to ost requests (write cache writeback) that might be triggered
1916 * But we'd also like to be able to indicate in the reply that we're
1917 * cancelling right now, because it's unused, or have an intent result
1918 * in the reply, so we might have to push the responsibility for sending
1919 * the reply down into the AST handlers, alas. */
1921 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1922 case LDLM_BL_CALLBACK:
1923 CDEBUG(D_INODE, "blocking ast\n");
1924 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1925 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1926 rc = ldlm_callback_reply(req, 0);
1927 if (req->rq_no_reply || rc)
1928 ldlm_callback_errmsg(req, "Normal process", rc,
1929 &dlm_req->lock_handle[0]);
1931 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1932 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1934 case LDLM_CP_CALLBACK:
1935 CDEBUG(D_INODE, "completion ast\n");
1936 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1937 ldlm_callback_reply(req, 0);
1938 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1940 case LDLM_GL_CALLBACK:
1941 CDEBUG(D_INODE, "glimpse ast\n");
1942 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1943 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1946 LBUG(); /* checked above */
1952 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1957 /* Requests arrive in sender's byte order. The ptlrpc service
1958 * handler has already checked and, if necessary, byte-swapped the
1959 * incoming request message body, but I am responsible for the
1960 * message buffers. */
1962 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1964 if (req->rq_export == NULL) {
1965 struct ldlm_request *dlm_req;
1967 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1968 lustre_msg_get_opc(req->rq_reqmsg),
1969 libcfs_id2str(req->rq_peer),
1970 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1972 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
1973 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1974 dlm_req = req_capsule_client_get(&req->rq_pill,
1976 if (dlm_req != NULL)
1977 ldlm_lock_dump_handle(D_ERROR,
1978 &dlm_req->lock_handle[0]);
1980 ldlm_callback_reply(req, -ENOTCONN);
1984 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1986 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1988 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1989 CDEBUG(D_INODE, "cancel\n");
1990 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1992 rc = ldlm_handle_cancel(req);
1996 case OBD_LOG_CANCEL:
1997 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1998 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2000 rc = llog_origin_handle_cancel(req);
2001 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2003 ldlm_callback_reply(req, rc);
2006 CERROR("invalid opcode %d\n",
2007 lustre_msg_get_opc(req->rq_reqmsg));
2008 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2009 ldlm_callback_reply(req, -EINVAL);
2015 void ldlm_revoke_lock_cb(void *obj, void *data)
2017 cfs_list_t *rpc_list = data;
2018 struct ldlm_lock *lock = obj;
2020 lock_res_and_lock(lock);
2022 if (lock->l_req_mode != lock->l_granted_mode) {
2023 unlock_res_and_lock(lock);
2027 LASSERT(lock->l_resource);
2028 if (lock->l_resource->lr_type != LDLM_IBITS &&
2029 lock->l_resource->lr_type != LDLM_PLAIN) {
2030 unlock_res_and_lock(lock);
2034 if (lock->l_flags & LDLM_FL_AST_SENT) {
2035 unlock_res_and_lock(lock);
2039 LASSERT(lock->l_blocking_ast);
2040 LASSERT(!lock->l_blocking_lock);
2042 lock->l_flags |= LDLM_FL_AST_SENT;
2043 if (lock->l_export && lock->l_export->exp_lock_hash &&
2044 !cfs_hlist_unhashed(&lock->l_exp_hash))
2045 cfs_hash_del(lock->l_export->exp_lock_hash,
2046 &lock->l_remote_handle, &lock->l_exp_hash);
2047 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2048 LDLM_LOCK_GET(lock);
2050 unlock_res_and_lock(lock);
2053 void ldlm_revoke_export_locks(struct obd_export *exp)
2055 cfs_list_t rpc_list;
2058 CFS_INIT_LIST_HEAD(&rpc_list);
2059 cfs_hash_for_each_empty(exp->exp_lock_hash,
2060 ldlm_revoke_lock_cb, &rpc_list);
2061 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
2067 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2069 struct ldlm_bl_work_item *blwi = NULL;
2070 static unsigned int num_bl = 0;
2072 cfs_spin_lock(&blp->blp_lock);
2073 /* process a request from the blp_list at least every blp_num_threads */
2074 if (!cfs_list_empty(&blp->blp_list) &&
2075 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2076 blwi = cfs_list_entry(blp->blp_list.next,
2077 struct ldlm_bl_work_item, blwi_entry);
2079 if (!cfs_list_empty(&blp->blp_prio_list))
2080 blwi = cfs_list_entry(blp->blp_prio_list.next,
2081 struct ldlm_bl_work_item,
2085 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2087 cfs_list_del(&blwi->blwi_entry);
2089 cfs_spin_unlock(&blp->blp_lock);
2094 /* This only contains temporary data until the thread starts */
2095 struct ldlm_bl_thread_data {
2096 char bltd_name[CFS_CURPROC_COMM_MAX];
2097 struct ldlm_bl_pool *bltd_blp;
2098 cfs_completion_t bltd_comp;
2102 static int ldlm_bl_thread_main(void *arg);
2104 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2106 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2109 cfs_init_completion(&bltd.bltd_comp);
2110 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
2112 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2113 cfs_atomic_read(&blp->blp_num_threads), rc);
2116 cfs_wait_for_completion(&bltd.bltd_comp);
2121 static int ldlm_bl_thread_main(void *arg)
2123 struct ldlm_bl_pool *blp;
2127 struct ldlm_bl_thread_data *bltd = arg;
2129 blp = bltd->bltd_blp;
2132 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2133 cfs_atomic_inc(&blp->blp_busy_threads);
2135 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2136 "ldlm_bl_%02d", bltd->bltd_num);
2137 cfs_daemonize(bltd->bltd_name);
2139 cfs_complete(&bltd->bltd_comp);
2140 /* cannot use bltd after this, it is only on caller's stack */
2144 struct l_wait_info lwi = { 0 };
2145 struct ldlm_bl_work_item *blwi = NULL;
2147 blwi = ldlm_bl_get_work(blp);
2152 cfs_atomic_dec(&blp->blp_busy_threads);
2153 l_wait_event_exclusive(blp->blp_waitq,
2154 (blwi = ldlm_bl_get_work(blp)) != NULL,
2156 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2158 if (blwi->blwi_ns == NULL)
2159 /* added by ldlm_cleanup() */
2162 /* Not fatal if racy and have a few too many threads */
2163 if (unlikely(busy < blp->blp_max_threads &&
2164 busy >= cfs_atomic_read(&blp->blp_num_threads)))
2165 /* discard the return value, we tried */
2166 ldlm_bl_thread_start(blp);
2168 if (blwi->blwi_ns == NULL)
2169 /* added by ldlm_cleanup() */
2173 if (blwi->blwi_count) {
2174 /* The special case when we cancel locks in lru
2175 * asynchronously, we pass the list of locks here.
2176 * Thus lock is marked LDLM_FL_CANCELING, and already
2177 * canceled locally. */
2178 ldlm_cli_cancel_list(&blwi->blwi_head,
2179 blwi->blwi_count, NULL, 0);
2181 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2184 OBD_FREE(blwi, sizeof(*blwi));
2187 cfs_atomic_dec(&blp->blp_busy_threads);
2188 cfs_atomic_dec(&blp->blp_num_threads);
2189 cfs_complete(&blp->blp_comp);
2195 static int ldlm_setup(void);
2196 static int ldlm_cleanup(void);
2198 int ldlm_get_ref(void)
2202 cfs_mutex_down(&ldlm_ref_sem);
2203 if (++ldlm_refcount == 1) {
2208 cfs_mutex_up(&ldlm_ref_sem);
2213 void ldlm_put_ref(void)
2216 cfs_mutex_down(&ldlm_ref_sem);
2217 if (ldlm_refcount == 1) {
2218 int rc = ldlm_cleanup();
2220 CERROR("ldlm_cleanup failed: %d\n", rc);
2226 cfs_mutex_up(&ldlm_ref_sem);
2232 * Export handle<->lock hash operations.
2235 ldlm_export_lock_hash(cfs_hash_t *hs, void *key, unsigned mask)
2237 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2241 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2243 struct ldlm_lock *lock;
2246 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2247 RETURN(&lock->l_remote_handle);
2251 ldlm_export_lock_compare(void *key, cfs_hlist_node_t *hnode)
2254 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2258 ldlm_export_lock_get(cfs_hlist_node_t *hnode)
2260 struct ldlm_lock *lock;
2263 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2264 LDLM_LOCK_GET(lock);
2270 ldlm_export_lock_put(cfs_hlist_node_t *hnode)
2272 struct ldlm_lock *lock;
2275 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2276 LDLM_LOCK_RELEASE(lock);
2281 static cfs_hash_ops_t ldlm_export_lock_ops = {
2282 .hs_hash = ldlm_export_lock_hash,
2283 .hs_key = ldlm_export_lock_key,
2284 .hs_compare = ldlm_export_lock_compare,
2285 .hs_get = ldlm_export_lock_get,
2286 .hs_put = ldlm_export_lock_put
2289 int ldlm_init_export(struct obd_export *exp)
2293 exp->exp_lock_hash =
2294 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2295 HASH_EXP_LOCK_CUR_BITS, HASH_EXP_LOCK_MAX_BITS,
2296 &ldlm_export_lock_ops, CFS_HASH_REHASH);
2298 if (!exp->exp_lock_hash)
2303 EXPORT_SYMBOL(ldlm_init_export);
2305 void ldlm_destroy_export(struct obd_export *exp)
2308 cfs_hash_putref(exp->exp_lock_hash);
2309 exp->exp_lock_hash = NULL;
2312 EXPORT_SYMBOL(ldlm_destroy_export);
2314 static int ldlm_setup(void)
2316 struct ldlm_bl_pool *blp;
2318 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2319 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2325 if (ldlm_state != NULL)
2328 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2329 if (ldlm_state == NULL)
2333 rc = ldlm_proc_setup();
2339 if (ldlm_num_threads) {
2340 /* If ldlm_num_threads is set, it is the min and the max. */
2341 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2342 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2343 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2344 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2345 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2349 ldlm_state->ldlm_cb_service =
2350 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2351 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2352 LDLM_CB_REPLY_PORTAL, 2,
2353 ldlm_callback_handler, "ldlm_cbd",
2354 ldlm_svc_proc_dir, NULL,
2355 ldlm_min_threads, ldlm_max_threads,
2357 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2359 if (!ldlm_state->ldlm_cb_service) {
2360 CERROR("failed to start service\n");
2361 GOTO(out_proc, rc = -ENOMEM);
2364 ldlm_state->ldlm_cancel_service =
2365 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2366 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2367 LDLM_CANCEL_REPLY_PORTAL, 6,
2368 ldlm_cancel_handler, "ldlm_canceld",
2369 ldlm_svc_proc_dir, NULL,
2370 ldlm_min_threads, ldlm_max_threads,
2372 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2375 if (!ldlm_state->ldlm_cancel_service) {
2376 CERROR("failed to start service\n");
2377 GOTO(out_proc, rc = -ENOMEM);
2380 OBD_ALLOC(blp, sizeof(*blp));
2382 GOTO(out_proc, rc = -ENOMEM);
2383 ldlm_state->ldlm_bl_pool = blp;
2385 cfs_spin_lock_init(&blp->blp_lock);
2386 CFS_INIT_LIST_HEAD(&blp->blp_list);
2387 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2388 cfs_waitq_init(&blp->blp_waitq);
2389 cfs_atomic_set(&blp->blp_num_threads, 0);
2390 cfs_atomic_set(&blp->blp_busy_threads, 0);
2391 blp->blp_min_threads = ldlm_min_threads;
2392 blp->blp_max_threads = ldlm_max_threads;
2395 for (i = 0; i < blp->blp_min_threads; i++) {
2396 rc = ldlm_bl_thread_start(blp);
2398 GOTO(out_thread, rc);
2401 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2403 GOTO(out_thread, rc);
2405 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2407 GOTO(out_thread, rc);
2409 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2410 expired_lock_thread.elt_state = ELT_STOPPED;
2411 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2413 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2414 cfs_spin_lock_init(&waiting_locks_spinlock);
2415 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2417 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2419 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2420 GOTO(out_thread, rc);
2423 cfs_wait_event(expired_lock_thread.elt_waitq,
2424 expired_lock_thread.elt_state == ELT_READY);
2428 rc = ldlm_pools_init();
2430 GOTO(out_thread, rc);
2436 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2437 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2442 ldlm_proc_cleanup();
2445 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2450 static int ldlm_cleanup(void)
2453 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2457 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2458 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2459 CERROR("ldlm still has namespaces; clean these up first.\n");
2460 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2461 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2470 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2471 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2473 cfs_init_completion(&blp->blp_comp);
2475 cfs_spin_lock(&blp->blp_lock);
2476 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2477 cfs_waitq_signal(&blp->blp_waitq);
2478 cfs_spin_unlock(&blp->blp_lock);
2480 cfs_wait_for_completion(&blp->blp_comp);
2482 OBD_FREE(blp, sizeof(*blp));
2484 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2485 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2486 ldlm_proc_cleanup();
2488 expired_lock_thread.elt_state = ELT_TERMINATE;
2489 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2490 cfs_wait_event(expired_lock_thread.elt_waitq,
2491 expired_lock_thread.elt_state == ELT_STOPPED);
2493 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2494 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2497 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2503 int __init ldlm_init(void)
2505 cfs_init_mutex(&ldlm_ref_sem);
2506 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2507 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2508 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2509 sizeof(struct ldlm_resource), 0,
2510 CFS_SLAB_HWCACHE_ALIGN);
2511 if (ldlm_resource_slab == NULL)
2514 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2515 sizeof(struct ldlm_lock), 0,
2516 CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2517 if (ldlm_lock_slab == NULL) {
2518 cfs_mem_cache_destroy(ldlm_resource_slab);
2522 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2523 sizeof(struct ldlm_interval),
2524 0, CFS_SLAB_HWCACHE_ALIGN);
2525 if (ldlm_interval_slab == NULL) {
2526 cfs_mem_cache_destroy(ldlm_resource_slab);
2527 cfs_mem_cache_destroy(ldlm_lock_slab);
2530 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2531 class_export_dump_hook = ldlm_dump_export_locks;
2536 void __exit ldlm_exit(void)
2540 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2541 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2542 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2544 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2545 * synchronize_rcu() to wait a grace period elapsed, so that
2546 * ldlm_lock_free() get a chance to be called. */
2549 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2550 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2551 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2552 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2556 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2559 EXPORT_SYMBOL(ldlm_get_processing_policy);
2560 EXPORT_SYMBOL(ldlm_lock2desc);
2561 EXPORT_SYMBOL(ldlm_register_intent);
2562 EXPORT_SYMBOL(ldlm_lockname);
2563 EXPORT_SYMBOL(ldlm_typename);
2564 EXPORT_SYMBOL(ldlm_lock2handle);
2565 EXPORT_SYMBOL(__ldlm_handle2lock);
2566 EXPORT_SYMBOL(ldlm_lock_get);
2567 EXPORT_SYMBOL(ldlm_lock_put);
2568 EXPORT_SYMBOL(ldlm_lock_match);
2569 EXPORT_SYMBOL(ldlm_lock_cancel);
2570 EXPORT_SYMBOL(ldlm_lock_addref);
2571 EXPORT_SYMBOL(ldlm_lock_addref_try);
2572 EXPORT_SYMBOL(ldlm_lock_decref);
2573 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2574 EXPORT_SYMBOL(ldlm_lock_change_resource);
2575 EXPORT_SYMBOL(ldlm_it2str);
2576 EXPORT_SYMBOL(ldlm_lock_dump);
2577 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2578 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2579 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2580 EXPORT_SYMBOL(ldlm_lock_allow_match);
2581 EXPORT_SYMBOL(ldlm_lock_downgrade);
2582 EXPORT_SYMBOL(ldlm_lock_convert);
2584 /* ldlm_request.c */
2585 EXPORT_SYMBOL(ldlm_completion_ast_async);
2586 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2587 EXPORT_SYMBOL(ldlm_completion_ast);
2588 EXPORT_SYMBOL(ldlm_blocking_ast);
2589 EXPORT_SYMBOL(ldlm_glimpse_ast);
2590 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2591 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2592 EXPORT_SYMBOL(ldlm_prep_elc_req);
2593 EXPORT_SYMBOL(ldlm_cli_convert);
2594 EXPORT_SYMBOL(ldlm_cli_enqueue);
2595 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2596 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2597 EXPORT_SYMBOL(ldlm_cli_cancel);
2598 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2599 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2600 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2601 EXPORT_SYMBOL(ldlm_replay_locks);
2602 EXPORT_SYMBOL(ldlm_resource_foreach);
2603 EXPORT_SYMBOL(ldlm_namespace_foreach);
2604 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2605 EXPORT_SYMBOL(ldlm_resource_iterate);
2606 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2607 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2610 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2611 EXPORT_SYMBOL(ldlm_server_completion_ast);
2612 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2613 EXPORT_SYMBOL(ldlm_handle_enqueue);
2614 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2615 EXPORT_SYMBOL(ldlm_handle_cancel);
2616 EXPORT_SYMBOL(ldlm_request_cancel);
2617 EXPORT_SYMBOL(ldlm_handle_convert);
2618 EXPORT_SYMBOL(ldlm_handle_convert0);
2619 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2620 EXPORT_SYMBOL(ldlm_get_ref);
2621 EXPORT_SYMBOL(ldlm_put_ref);
2622 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2623 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2625 /* ldlm_resource.c */
2626 EXPORT_SYMBOL(ldlm_namespace_new);
2627 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2628 EXPORT_SYMBOL(ldlm_namespace_free);
2629 EXPORT_SYMBOL(ldlm_namespace_dump);
2630 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2631 EXPORT_SYMBOL(ldlm_resource_get);
2632 EXPORT_SYMBOL(ldlm_resource_putref);
2633 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2636 EXPORT_SYMBOL(client_import_add_conn);
2637 EXPORT_SYMBOL(client_import_del_conn);
2638 EXPORT_SYMBOL(client_obd_setup);
2639 EXPORT_SYMBOL(client_obd_cleanup);
2640 EXPORT_SYMBOL(client_connect_import);
2641 EXPORT_SYMBOL(client_disconnect_export);
2642 EXPORT_SYMBOL(server_disconnect_export);
2643 EXPORT_SYMBOL(target_stop_recovery_thread);
2644 EXPORT_SYMBOL(target_handle_connect);
2645 EXPORT_SYMBOL(target_cleanup_recovery);
2646 EXPORT_SYMBOL(target_destroy_export);
2647 EXPORT_SYMBOL(target_cancel_recovery_timer);
2648 EXPORT_SYMBOL(target_send_reply);
2649 EXPORT_SYMBOL(target_queue_recovery_request);
2650 EXPORT_SYMBOL(target_handle_ping);
2651 EXPORT_SYMBOL(target_pack_pool_reply);
2652 EXPORT_SYMBOL(target_handle_disconnect);
2655 EXPORT_SYMBOL(lock_res_and_lock);
2656 EXPORT_SYMBOL(unlock_res_and_lock);