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 struct semaphore 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 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() */
208 LDLM_LOCK_RELEASE(lock);
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() */
216 LDLM_LOCK_RELEASE(lock);
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);
239 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
242 * Check if there is a request in the export request list
243 * which prevents the lock canceling.
245 static int ldlm_lock_busy(struct ldlm_lock *lock)
247 struct ptlrpc_request *req;
251 if (lock->l_export == NULL)
254 spin_lock(&lock->l_export->exp_lock);
255 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
256 if (req->rq_ops->hpreq_lock_match) {
257 match = req->rq_ops->hpreq_lock_match(req, lock);
262 spin_unlock(&lock->l_export->exp_lock);
266 /* This is called from within a timer interrupt and cannot schedule */
267 static void waiting_locks_callback(unsigned long unused)
269 struct ldlm_lock *lock, *last = NULL;
272 spin_lock_bh(&waiting_locks_spinlock);
273 while (!list_empty(&waiting_locks_list)) {
274 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
276 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
277 (lock->l_req_mode == LCK_GROUP))
280 if (ptlrpc_check_suspend()) {
281 /* there is a case when we talk to one mds, holding
282 * lock from another mds. this way we easily can get
283 * here, if second mds is being recovered. so, we
284 * suspend timeouts. bug 6019 */
286 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
287 lock->l_export->exp_client_uuid.uuid,
288 lock->l_export->exp_connection->c_remote_uuid.uuid,
289 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
291 list_del_init(&lock->l_pending_chain);
292 spin_unlock_bh(&waiting_locks_spinlock);
293 ldlm_add_waiting_lock(lock);
297 /* if timeout overlaps the activation time of suspended timeouts
298 * then extend it to give a chance for client to reconnect */
299 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
300 cfs_time_seconds(obd_timeout)/2),
301 ptlrpc_suspend_wakeup_time())) {
302 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
303 lock->l_export->exp_client_uuid.uuid,
304 lock->l_export->exp_connection->c_remote_uuid.uuid,
305 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
307 list_del_init(&lock->l_pending_chain);
308 spin_unlock_bh(&waiting_locks_spinlock);
309 ldlm_add_waiting_lock(lock);
313 /* Check if we need to prolong timeout */
314 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
315 ldlm_lock_busy(lock)) {
318 if (lock->l_pending_chain.next == &waiting_locks_list)
322 spin_unlock_bh(&waiting_locks_spinlock);
323 LDLM_DEBUG(lock, "prolong the busy lock");
324 ldlm_refresh_waiting_lock(lock,
325 ldlm_get_enq_timeout(lock));
326 spin_lock_bh(&waiting_locks_spinlock);
336 lock->l_resource->lr_namespace->ns_timeouts++;
337 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
338 "evicting client at %s ",
339 cfs_time_current_sec()- lock->l_enqueued_time.tv_sec,
341 lock->l_export->exp_connection->c_peer.nid));
345 /* no needs to take an extra ref on the lock since it was in
346 * the waiting_locks_list and ldlm_add_waiting_lock()
347 * already grabbed a ref */
348 list_del(&lock->l_pending_chain);
349 list_add(&lock->l_pending_chain,
350 &expired_lock_thread.elt_expired_locks);
353 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
354 if (obd_dump_on_timeout)
355 expired_lock_thread.elt_dump = __LINE__;
357 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
361 * Make sure the timer will fire again if we have any locks
364 if (!list_empty(&waiting_locks_list)) {
365 cfs_time_t timeout_rounded;
366 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
368 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
369 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
371 spin_unlock_bh(&waiting_locks_spinlock);
375 * Indicate that we're waiting for a client to call us back cancelling a given
376 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
377 * timer to fire appropriately. (We round up to the next second, to avoid
378 * floods of timer firings during periods of high lock contention and traffic).
379 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
380 * if it has been added to the waiting list (1 is returned).
382 * Called with the namespace lock held.
384 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
387 cfs_time_t timeout_rounded;
389 if (!list_empty(&lock->l_pending_chain))
392 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
393 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
396 timeout = cfs_time_shift(seconds);
397 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
398 lock->l_callback_timeout = timeout;
400 timeout_rounded = round_timeout(lock->l_callback_timeout);
402 if (cfs_time_before(timeout_rounded,
403 cfs_timer_deadline(&waiting_locks_timer)) ||
404 !cfs_timer_is_armed(&waiting_locks_timer)) {
405 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
407 /* if the new lock has a shorter timeout than something earlier on
408 the list, we'll wait the longer amount of time; no big deal. */
409 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
413 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
417 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
419 spin_lock_bh(&waiting_locks_spinlock);
420 if (lock->l_destroyed) {
421 static cfs_time_t next;
422 spin_unlock_bh(&waiting_locks_spinlock);
423 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
424 if (cfs_time_after(cfs_time_current(), next)) {
425 next = cfs_time_shift(14400);
426 libcfs_debug_dumpstack(NULL);
431 ret = __ldlm_add_waiting_lock(lock, ldlm_get_enq_timeout(lock));
433 /* grab ref on the lock if it has been added to the
436 spin_unlock_bh(&waiting_locks_spinlock);
438 LDLM_DEBUG(lock, "%sadding to wait list",
439 ret == 0 ? "not re-" : "");
444 * Remove a lock from the pending list, likely because it had its cancellation
445 * callback arrive without incident. This adjusts the lock-timeout timer if
446 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
447 * As done by ldlm_del_waiting_lock(), the caller must release the lock
448 * reference when the lock is removed from any list (1 is returned).
450 * Called with namespace lock held.
452 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
454 struct list_head *list_next;
456 if (list_empty(&lock->l_pending_chain))
459 list_next = lock->l_pending_chain.next;
460 if (lock->l_pending_chain.prev == &waiting_locks_list) {
461 /* Removing the head of the list, adjust timer. */
462 if (list_next == &waiting_locks_list) {
463 /* No more, just cancel. */
464 cfs_timer_disarm(&waiting_locks_timer);
466 struct ldlm_lock *next;
467 next = list_entry(list_next, struct ldlm_lock,
469 cfs_timer_arm(&waiting_locks_timer,
470 round_timeout(next->l_callback_timeout));
473 list_del_init(&lock->l_pending_chain);
478 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
482 if (lock->l_export == NULL) {
483 /* We don't have a "waiting locks list" on clients. */
484 LDLM_DEBUG(lock, "client lock: no-op");
488 spin_lock_bh(&waiting_locks_spinlock);
489 ret = __ldlm_del_waiting_lock(lock);
490 spin_unlock_bh(&waiting_locks_spinlock);
492 /* release lock ref if it has indeed been removed
494 LDLM_LOCK_RELEASE(lock);
496 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
503 * Called with namespace lock held.
505 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
507 if (lock->l_export == NULL) {
508 /* We don't have a "waiting locks list" on clients. */
509 LDLM_DEBUG(lock, "client lock: no-op");
513 spin_lock_bh(&waiting_locks_spinlock);
515 if (list_empty(&lock->l_pending_chain)) {
516 spin_unlock_bh(&waiting_locks_spinlock);
517 LDLM_DEBUG(lock, "wasn't waiting");
521 /* we remove/add the lock to the waiting list, so no needs to
522 * release/take a lock reference */
523 __ldlm_del_waiting_lock(lock);
524 __ldlm_add_waiting_lock(lock, timeout);
525 spin_unlock_bh(&waiting_locks_spinlock);
527 LDLM_DEBUG(lock, "refreshed");
530 #else /* !__KERNEL__ */
532 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
534 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
538 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
543 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
547 #endif /* __KERNEL__ */
549 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
550 const char *ast_type)
552 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
553 "to a lock %s callback time out: rc %d\n",
554 lock->l_export->exp_obd->obd_name,
555 obd_export_nid2str(lock->l_export), ast_type, rc);
557 if (obd_dump_on_timeout)
558 libcfs_debug_dumplog();
560 spin_lock_bh(&waiting_locks_spinlock);
561 if (__ldlm_del_waiting_lock(lock) == 0)
562 /* the lock was not in any list, grab an extra ref before adding
563 * the lock to the expired list */
565 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
566 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
567 spin_unlock_bh(&waiting_locks_spinlock);
569 class_fail_export(lock->l_export);
573 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
574 struct ptlrpc_request *req, int rc,
575 const char *ast_type)
577 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
579 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
580 LASSERT(lock->l_export);
581 if (lock->l_export->exp_libclient) {
582 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
583 " timeout, just cancelling lock", ast_type,
584 libcfs_nid2str(peer.nid));
585 ldlm_lock_cancel(lock);
587 } else if (lock->l_flags & LDLM_FL_CANCEL) {
588 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
589 "cancel was received (AST reply lost?)",
590 ast_type, libcfs_nid2str(peer.nid));
591 ldlm_lock_cancel(lock);
594 ldlm_del_waiting_lock(lock);
595 ldlm_failed_ast(lock, rc, ast_type);
599 LDLM_DEBUG(lock, "client (nid %s) returned %d"
600 " from %s AST - normal race",
601 libcfs_nid2str(peer.nid),
603 lustre_msg_get_status(req->rq_repmsg) : -1,
606 LDLM_ERROR(lock, "client (nid %s) returned %d "
607 "from %s AST", libcfs_nid2str(peer.nid),
608 (req->rq_repmsg != NULL) ?
609 lustre_msg_get_status(req->rq_repmsg) : 0,
611 ldlm_lock_cancel(lock);
612 /* Server-side AST functions are called from ldlm_reprocess_all,
613 * which needs to be told to please restart its reprocessing. */
620 static int ldlm_cb_interpret(const struct lu_env *env,
621 struct ptlrpc_request *req, void *data, int rc)
623 struct ldlm_cb_set_arg *arg;
624 struct ldlm_lock *lock;
627 LASSERT(data != NULL);
629 arg = req->rq_async_args.pointer_arg[0];
630 lock = req->rq_async_args.pointer_arg[1];
631 LASSERT(lock != NULL);
633 /* If client canceled the lock but the cancel has not
634 * been recieved yet, we need to update lvbo to have the
635 * proper attributes cached. */
636 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
637 ldlm_res_lvbo_update(lock->l_resource, NULL,
639 rc = ldlm_handle_ast_error(lock, req, rc,
640 arg->type == LDLM_BL_CALLBACK
641 ? "blocking" : "completion");
644 LDLM_LOCK_RELEASE(lock);
647 atomic_set(&arg->restart, 1);
652 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
653 struct ldlm_cb_set_arg *arg,
654 struct ldlm_lock *lock,
660 if (unlikely(instant_cancel)) {
661 rc = ptl_send_rpc(req, 1);
662 ptlrpc_req_finished(req);
664 /* If we cancelled the lock, we need to restart
665 * ldlm_reprocess_queue */
666 atomic_set(&arg->restart, 1);
669 ptlrpc_set_add_req(arg->set, req);
676 * Check if there are requests in the export request list which prevent
677 * the lock canceling and make these requests high priority ones.
679 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
681 struct ptlrpc_request *req;
684 if (lock->l_export == NULL) {
685 LDLM_DEBUG(lock, "client lock: no-op");
689 spin_lock(&lock->l_export->exp_lock);
690 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
691 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
692 req->rq_ops->hpreq_lock_match(req, lock))
693 ptlrpc_hpreq_reorder(req);
695 spin_unlock(&lock->l_export->exp_lock);
700 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
701 * enqueued server lock conflicts with given one.
703 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
704 * to wait for client response.
706 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
707 struct ldlm_lock_desc *desc,
708 void *data, int flag)
710 struct ldlm_cb_set_arg *arg = data;
711 struct ldlm_request *body;
712 struct ptlrpc_request *req;
713 int instant_cancel = 0;
717 if (flag == LDLM_CB_CANCELING)
718 /* Don't need to do anything here. */
722 LASSERT(data != NULL);
723 if (lock->l_export->exp_obd->obd_recovering != 0) {
724 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
725 ldlm_lock_dump(D_ERROR, lock, 0);
728 ldlm_lock_reorder_req(lock);
730 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
731 &RQF_LDLM_BL_CALLBACK,
732 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
736 req->rq_async_args.pointer_arg[0] = arg;
737 req->rq_async_args.pointer_arg[1] = lock;
738 req->rq_interpret_reply = ldlm_cb_interpret;
739 req->rq_no_resend = 1;
741 lock_res(lock->l_resource);
742 if (lock->l_granted_mode != lock->l_req_mode) {
743 /* this blocking AST will be communicated as part of the
744 * completion AST instead */
745 unlock_res(lock->l_resource);
746 ptlrpc_req_finished(req);
747 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
751 if (lock->l_destroyed) {
752 /* What's the point? */
753 unlock_res(lock->l_resource);
754 ptlrpc_req_finished(req);
758 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
761 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
762 body->lock_handle[0] = lock->l_remote_handle;
763 body->lock_desc = *desc;
764 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
766 LDLM_DEBUG(lock, "server preparing blocking AST");
768 ptlrpc_request_set_replen(req);
769 if (instant_cancel) {
770 unlock_res(lock->l_resource);
771 ldlm_lock_cancel(lock);
773 LASSERT(lock->l_granted_mode == lock->l_req_mode);
774 ldlm_add_waiting_lock(lock);
775 unlock_res(lock->l_resource);
778 req->rq_send_state = LUSTRE_IMP_FULL;
779 /* ptlrpc_prep_req already set timeout */
781 req->rq_timeout = ldlm_get_rq_timeout();
783 if (lock->l_export && lock->l_export->exp_nid_stats &&
784 lock->l_export->exp_nid_stats->nid_ldlm_stats)
785 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
786 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
788 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
793 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
795 struct ldlm_cb_set_arg *arg = data;
796 struct ldlm_request *body;
797 struct ptlrpc_request *req;
798 struct timeval granted_time;
799 long total_enqueue_wait;
800 int instant_cancel = 0;
804 LASSERT(lock != NULL);
805 LASSERT(data != NULL);
807 do_gettimeofday(&granted_time);
808 total_enqueue_wait = cfs_timeval_sub(&granted_time,
809 &lock->l_enqueued_time, NULL);
811 if (total_enqueue_wait / ONE_MILLION > obd_timeout)
812 /* non-fatal with AT - change to LDLM_DEBUG? */
813 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
814 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
816 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
817 &RQF_LDLM_CP_CALLBACK);
821 lock_res_and_lock(lock);
822 if (lock->l_resource->lr_lvb_len)
823 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
824 lock->l_resource->lr_lvb_len);
825 unlock_res_and_lock(lock);
827 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
829 ptlrpc_request_free(req);
833 req->rq_async_args.pointer_arg[0] = arg;
834 req->rq_async_args.pointer_arg[1] = lock;
835 req->rq_interpret_reply = ldlm_cb_interpret;
836 req->rq_no_resend = 1;
837 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
839 body->lock_handle[0] = lock->l_remote_handle;
840 body->lock_flags = flags;
841 ldlm_lock2desc(lock, &body->lock_desc);
842 if (lock->l_resource->lr_lvb_len) {
843 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
845 lock_res_and_lock(lock);
846 memcpy(lvb, lock->l_resource->lr_lvb_data,
847 lock->l_resource->lr_lvb_len);
848 unlock_res_and_lock(lock);
851 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
854 /* Server-side enqueue wait time estimate, used in
855 __ldlm_add_waiting_lock to set future enqueue timers */
856 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
857 total_enqueue_wait / ONE_MILLION);
859 ptlrpc_request_set_replen(req);
861 req->rq_send_state = LUSTRE_IMP_FULL;
862 /* ptlrpc_prep_req already set timeout */
864 req->rq_timeout = ldlm_get_rq_timeout();
866 /* We only send real blocking ASTs after the lock is granted */
867 lock_res_and_lock(lock);
868 if (lock->l_flags & LDLM_FL_AST_SENT) {
869 body->lock_flags |= LDLM_FL_AST_SENT;
870 /* copy ast flags like LDLM_FL_DISCARD_DATA */
871 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
873 /* We might get here prior to ldlm_handle_enqueue setting
874 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
875 * into waiting list, but this is safe and similar code in
876 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
877 * that would not only cancel the lock, but will also remove
878 * it from waiting list */
879 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
880 unlock_res_and_lock(lock);
881 ldlm_lock_cancel(lock);
883 lock_res_and_lock(lock);
885 /* start the lock-timeout clock */
886 ldlm_add_waiting_lock(lock);
889 unlock_res_and_lock(lock);
891 if (lock->l_export && lock->l_export->exp_nid_stats &&
892 lock->l_export->exp_nid_stats->nid_ldlm_stats)
893 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
894 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
896 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
901 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
903 struct ldlm_resource *res = lock->l_resource;
904 struct ldlm_request *body;
905 struct ptlrpc_request *req;
909 LASSERT(lock != NULL);
911 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
912 &RQF_LDLM_GL_CALLBACK,
913 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
918 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
919 body->lock_handle[0] = lock->l_remote_handle;
920 ldlm_lock2desc(lock, &body->lock_desc);
922 lock_res_and_lock(lock);
923 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
924 lock->l_resource->lr_lvb_len);
925 unlock_res_and_lock(lock);
926 res = lock->l_resource;
927 ptlrpc_request_set_replen(req);
930 req->rq_send_state = LUSTRE_IMP_FULL;
931 /* ptlrpc_prep_req already set timeout */
933 req->rq_timeout = ldlm_get_rq_timeout();
935 if (lock->l_export && lock->l_export->exp_nid_stats &&
936 lock->l_export->exp_nid_stats->nid_ldlm_stats)
937 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
938 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
940 rc = ptlrpc_queue_wait(req);
941 if (rc == -ELDLM_NO_LOCK_DATA)
942 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
944 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
946 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
948 ptlrpc_req_finished(req);
950 ldlm_reprocess_all(res);
956 extern unsigned long long lu_time_stamp_get(void);
958 #define lu_time_stamp_get() time(NULL)
961 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
962 struct lprocfs_stats *srv_stats)
964 int lock_type = 0, op = 0;
966 lock_type = dlm_req->lock_desc.l_resource.lr_type;
970 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
973 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
974 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
976 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
979 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
982 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
990 lprocfs_counter_incr(srv_stats, op);
996 * Main server-side entry point into LDLM. This is called by ptlrpc service
997 * threads to carry out client lock enqueueing requests.
999 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1000 struct ptlrpc_request *req,
1001 const struct ldlm_request *dlm_req,
1002 const struct ldlm_callback_suite *cbs)
1004 struct ldlm_reply *dlm_rep;
1006 ldlm_error_t err = ELDLM_OK;
1007 struct ldlm_lock *lock = NULL;
1008 void *cookie = NULL;
1012 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1014 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1015 flags = dlm_req->lock_flags;
1017 LASSERT(req->rq_export);
1019 if (req->rq_rqbd->rqbd_service->srv_stats)
1020 ldlm_svc_get_eopc(dlm_req,
1021 req->rq_rqbd->rqbd_service->srv_stats);
1023 if (req->rq_export && req->rq_export->exp_nid_stats &&
1024 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1025 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1026 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1028 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1029 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1030 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1031 dlm_req->lock_desc.l_resource.lr_type);
1032 GOTO(out, rc = -EFAULT);
1035 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1036 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1037 dlm_req->lock_desc.l_req_mode &
1038 (dlm_req->lock_desc.l_req_mode-1))) {
1039 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1040 dlm_req->lock_desc.l_req_mode);
1041 GOTO(out, rc = -EFAULT);
1044 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1045 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1047 DEBUG_REQ(D_ERROR, req,
1048 "PLAIN lock request from IBITS client?");
1049 GOTO(out, rc = -EPROTO);
1051 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1053 DEBUG_REQ(D_ERROR, req,
1054 "IBITS lock request from unaware client?");
1055 GOTO(out, rc = -EPROTO);
1059 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1060 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1061 ibits for mgc/mgs) */
1063 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1064 * inodebits lock if client does not support them. */
1065 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1066 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1067 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1068 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1069 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1070 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1071 dlm_req->lock_desc.l_req_mode = LCK_CR;
1075 if (unlikely(flags & LDLM_FL_REPLAY)) {
1076 /* Find an existing lock in the per-export lock hash */
1077 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
1078 (void *)&dlm_req->lock_handle[0]);
1080 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1081 LPX64, lock->l_handle.h_cookie);
1082 GOTO(existing_lock, rc = 0);
1086 /* The lock's callback data might be set in the policy function */
1087 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1088 dlm_req->lock_desc.l_resource.lr_type,
1089 dlm_req->lock_desc.l_req_mode,
1093 GOTO(out, rc = -ENOMEM);
1095 do_gettimeofday(&lock->l_enqueued_time);
1096 lock->l_remote_handle = dlm_req->lock_handle[0];
1097 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1099 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1100 /* Don't enqueue a lock onto the export if it has already
1101 * been evicted. Cancel it now instead. (bug 3822) */
1102 if (req->rq_export->exp_failed) {
1103 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1104 GOTO(out, rc = -ENOTCONN);
1106 lock->l_export = class_export_get(req->rq_export);
1108 if (lock->l_export->exp_lock_hash)
1109 lustre_hash_add(lock->l_export->exp_lock_hash,
1110 &lock->l_remote_handle,
1115 if (flags & LDLM_FL_HAS_INTENT) {
1116 /* In this case, the reply buffer is allocated deep in
1117 * local_lock_enqueue by the policy function. */
1120 lock_res_and_lock(lock);
1121 if (lock->l_resource->lr_lvb_len) {
1122 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1124 lock->l_resource->lr_lvb_len);
1126 unlock_res_and_lock(lock);
1128 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1129 GOTO(out, rc = -ENOMEM);
1131 rc = req_capsule_server_pack(&req->rq_pill);
1136 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1137 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1138 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1139 lock->l_req_extent = lock->l_policy_data.l_extent;
1141 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1145 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1146 dlm_rep->lock_flags = flags;
1148 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1149 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1151 /* We never send a blocking AST until the lock is granted, but
1152 * we can tell it right now */
1153 lock_res_and_lock(lock);
1155 /* Now take into account flags to be inherited from original lock
1156 request both in reply to client and in our own lock flags. */
1157 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1158 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1160 /* Don't move a pending lock onto the export if it has already
1161 * been evicted. Cancel it now instead. (bug 5683) */
1162 if (unlikely(req->rq_export->exp_failed ||
1163 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1164 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1166 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1167 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1168 if (lock->l_granted_mode == lock->l_req_mode) {
1170 * Only cancel lock if it was granted, because it would
1171 * be destroyed immediatelly and would never be granted
1172 * in the future, causing timeouts on client. Not
1173 * granted lock will be cancelled immediatelly after
1174 * sending completion AST.
1176 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1177 unlock_res_and_lock(lock);
1178 ldlm_lock_cancel(lock);
1179 lock_res_and_lock(lock);
1181 ldlm_add_waiting_lock(lock);
1184 /* Make sure we never ever grant usual metadata locks to liblustre
1186 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1187 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1188 req->rq_export->exp_libclient) {
1189 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1190 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1191 CERROR("Granting sync lock to libclient. "
1192 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1193 dlm_req->lock_flags, dlm_rep->lock_flags,
1195 LDLM_ERROR(lock, "sync lock");
1196 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1197 struct ldlm_intent *it;
1199 it = req_capsule_client_get(&req->rq_pill,
1202 CERROR("This is intent %s ("LPU64")\n",
1203 ldlm_it2str(it->opc), it->opc);
1209 unlock_res_and_lock(lock);
1213 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1214 if (!req->rq_packed_final) {
1215 err = lustre_pack_reply(req, 1, NULL, NULL);
1220 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1221 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1223 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1224 "(err=%d, rc=%d)", err, rc);
1226 lock_res_and_lock(lock);
1228 if (lock->l_resource->lr_lvb_len > 0) {
1231 lvb = req_capsule_server_get(&req->rq_pill,
1233 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1236 memcpy(lvb, lock->l_resource->lr_lvb_data,
1237 lock->l_resource->lr_lvb_len);
1240 ldlm_resource_unlink_lock(lock);
1241 ldlm_lock_destroy_nolock(lock);
1243 unlock_res_and_lock(lock);
1245 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1246 ldlm_reprocess_all(lock->l_resource);
1248 LDLM_LOCK_RELEASE(lock);
1251 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1257 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1258 ldlm_completion_callback completion_callback,
1259 ldlm_blocking_callback blocking_callback,
1260 ldlm_glimpse_callback glimpse_callback)
1262 struct ldlm_request *dlm_req;
1263 struct ldlm_callback_suite cbs = {
1264 .lcs_completion = completion_callback,
1265 .lcs_blocking = blocking_callback,
1266 .lcs_glimpse = glimpse_callback
1270 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1271 if (dlm_req != NULL) {
1272 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1273 req, dlm_req, &cbs);
1280 int ldlm_handle_convert0(struct ptlrpc_request *req,
1281 const struct ldlm_request *dlm_req)
1283 struct ldlm_reply *dlm_rep;
1284 struct ldlm_lock *lock;
1288 if (req->rq_export && req->rq_export->exp_nid_stats &&
1289 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1290 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1291 LDLM_CONVERT - LDLM_FIRST_OPC);
1293 rc = req_capsule_server_pack(&req->rq_pill);
1297 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1298 dlm_rep->lock_flags = dlm_req->lock_flags;
1300 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1302 req->rq_status = EINVAL;
1306 LDLM_DEBUG(lock, "server-side convert handler START");
1308 do_gettimeofday(&lock->l_enqueued_time);
1309 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1310 &dlm_rep->lock_flags);
1312 if (ldlm_del_waiting_lock(lock))
1313 LDLM_DEBUG(lock, "converted waiting lock");
1316 req->rq_status = EDEADLOCK;
1321 if (!req->rq_status)
1322 ldlm_reprocess_all(lock->l_resource);
1323 LDLM_DEBUG(lock, "server-side convert handler END");
1324 LDLM_LOCK_PUT(lock);
1326 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1331 int ldlm_handle_convert(struct ptlrpc_request *req)
1334 struct ldlm_request *dlm_req;
1336 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1337 if (dlm_req != NULL) {
1338 rc = ldlm_handle_convert0(req, dlm_req);
1340 CERROR ("Can't unpack dlm_req\n");
1346 /* Cancel all the locks whos handles are packed into ldlm_request */
1347 int ldlm_request_cancel(struct ptlrpc_request *req,
1348 const struct ldlm_request *dlm_req, int first)
1350 struct ldlm_resource *res, *pres = NULL;
1351 struct ldlm_lock *lock;
1352 int i, count, done = 0;
1355 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1359 /* There is no lock on the server at the replay time,
1360 * skip lock cancelling to make replay tests to pass. */
1361 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1364 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1365 "starting at %d", count, first);
1367 for (i = first; i < count; i++) {
1368 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1370 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1371 "lock (cookie "LPU64")",
1372 dlm_req->lock_handle[i].cookie);
1376 res = lock->l_resource;
1381 ldlm_reprocess_all(pres);
1382 LDLM_RESOURCE_DELREF(pres);
1383 ldlm_resource_putref(pres);
1386 ldlm_resource_getref(res);
1387 LDLM_RESOURCE_ADDREF(res);
1388 ldlm_res_lvbo_update(res, NULL, 0, 1);
1392 ldlm_lock_cancel(lock);
1393 LDLM_LOCK_PUT(lock);
1396 ldlm_reprocess_all(pres);
1397 LDLM_RESOURCE_DELREF(pres);
1398 ldlm_resource_putref(pres);
1400 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1404 int ldlm_handle_cancel(struct ptlrpc_request *req)
1406 struct ldlm_request *dlm_req;
1410 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1411 if (dlm_req == NULL) {
1412 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1416 if (req->rq_export && req->rq_export->exp_nid_stats &&
1417 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1418 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1419 LDLM_CANCEL - LDLM_FIRST_OPC);
1421 rc = req_capsule_server_pack(&req->rq_pill);
1425 if (!ldlm_request_cancel(req, dlm_req, 0))
1426 req->rq_status = ESTALE;
1428 if (ptlrpc_reply(req) != 0)
1434 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1435 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1440 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1442 lock_res_and_lock(lock);
1443 lock->l_flags |= LDLM_FL_CBPENDING;
1445 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1446 lock->l_flags |= LDLM_FL_CANCEL;
1448 do_ast = (!lock->l_readers && !lock->l_writers);
1449 unlock_res_and_lock(lock);
1452 LDLM_DEBUG(lock, "already unused, calling "
1453 "callback (%p)", lock->l_blocking_ast);
1454 if (lock->l_blocking_ast != NULL)
1455 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1458 LDLM_DEBUG(lock, "Lock still has references, will be"
1459 " cancelled later");
1462 LDLM_DEBUG(lock, "client blocking callback handler END");
1463 LDLM_LOCK_RELEASE(lock);
1467 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1468 struct ldlm_namespace *ns,
1469 struct ldlm_request *dlm_req,
1470 struct ldlm_lock *lock)
1472 CFS_LIST_HEAD(ast_list);
1475 LDLM_DEBUG(lock, "client completion callback handler START");
1477 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1478 int to = cfs_time_seconds(1);
1480 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1481 if (lock->l_granted_mode == lock->l_req_mode ||
1487 lock_res_and_lock(lock);
1488 if (lock->l_destroyed ||
1489 lock->l_granted_mode == lock->l_req_mode) {
1490 /* bug 11300: the lock has already been granted */
1491 unlock_res_and_lock(lock);
1492 LDLM_DEBUG(lock, "Double grant race happened");
1493 LDLM_LOCK_RELEASE(lock);
1498 /* If we receive the completion AST before the actual enqueue returned,
1499 * then we might need to switch lock modes, resources, or extents. */
1500 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1501 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1502 LDLM_DEBUG(lock, "completion AST, new lock mode");
1505 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1506 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1507 LDLM_DEBUG(lock, "completion AST, new policy data");
1510 ldlm_resource_unlink_lock(lock);
1511 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1512 &lock->l_resource->lr_name,
1513 sizeof(lock->l_resource->lr_name)) != 0) {
1514 unlock_res_and_lock(lock);
1515 if (ldlm_lock_change_resource(ns, lock,
1516 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1517 LDLM_ERROR(lock, "Failed to allocate resource");
1518 LDLM_LOCK_RELEASE(lock);
1522 LDLM_DEBUG(lock, "completion AST, new resource");
1523 CERROR("change resource!\n");
1524 lock_res_and_lock(lock);
1527 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1528 /* BL_AST locks are not needed in lru.
1529 * let ldlm_cancel_lru() be fast. */
1530 ldlm_lock_remove_from_lru(lock);
1531 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1532 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1535 if (lock->l_lvb_len) {
1536 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1537 RCL_CLIENT) < lock->l_lvb_len) {
1538 LDLM_ERROR(lock, "completion AST did not contain "
1541 void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1543 (void *)lock->l_lvb_swabber);
1544 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1548 ldlm_grant_lock(lock, &ast_list);
1549 unlock_res_and_lock(lock);
1551 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1553 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1555 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1557 LDLM_LOCK_RELEASE(lock);
1561 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1562 struct ldlm_namespace *ns,
1563 struct ldlm_request *dlm_req,
1564 struct ldlm_lock *lock)
1569 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1571 if (lock->l_glimpse_ast != NULL)
1572 rc = lock->l_glimpse_ast(lock, req);
1574 if (req->rq_repmsg != NULL) {
1577 req->rq_status = rc;
1581 lock_res_and_lock(lock);
1582 if (lock->l_granted_mode == LCK_PW &&
1583 !lock->l_readers && !lock->l_writers &&
1584 cfs_time_after(cfs_time_current(),
1585 cfs_time_add(lock->l_last_used,
1586 cfs_time_seconds(10)))) {
1587 unlock_res_and_lock(lock);
1588 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1589 ldlm_handle_bl_callback(ns, NULL, lock);
1594 unlock_res_and_lock(lock);
1595 LDLM_LOCK_RELEASE(lock);
1599 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1601 if (req->rq_no_reply)
1604 req->rq_status = rc;
1605 if (!req->rq_packed_final) {
1606 rc = lustre_pack_reply(req, 1, NULL, NULL);
1610 return ptlrpc_reply(req);
1614 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1615 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1616 struct list_head *cancels, int count)
1618 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1619 struct ldlm_bl_work_item *blwi;
1622 if (cancels && count == 0)
1625 OBD_ALLOC(blwi, sizeof(*blwi));
1631 blwi->blwi_ld = *ld;
1633 list_add(&blwi->blwi_head, cancels);
1634 list_del_init(cancels);
1635 blwi->blwi_count = count;
1637 blwi->blwi_lock = lock;
1639 spin_lock(&blp->blp_lock);
1640 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1641 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1642 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1644 /* other blocking callbacks are added to the regular list */
1645 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1647 cfs_waitq_signal(&blp->blp_waitq);
1648 spin_unlock(&blp->blp_lock);
1654 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1655 struct ldlm_lock *lock)
1658 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1664 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1665 struct list_head *cancels, int count)
1668 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1674 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1675 static int ldlm_callback_handler(struct ptlrpc_request *req)
1677 struct ldlm_namespace *ns;
1678 struct ldlm_request *dlm_req;
1679 struct ldlm_lock *lock;
1683 /* Requests arrive in sender's byte order. The ptlrpc service
1684 * handler has already checked and, if necessary, byte-swapped the
1685 * incoming request message body, but I am responsible for the
1686 * message buffers. */
1688 /* do nothing for sec context finalize */
1689 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1692 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1694 if (req->rq_export == NULL) {
1695 struct ldlm_request *dlm_req;
1697 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1698 "export cookie "LPX64"; this is "
1699 "normal if this node rebooted with a lock held\n",
1700 lustre_msg_get_opc(req->rq_reqmsg),
1701 libcfs_id2str(req->rq_peer),
1702 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1704 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1705 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1706 if (dlm_req != NULL)
1707 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1708 dlm_req->lock_handle[0].cookie);
1710 ldlm_callback_reply(req, -ENOTCONN);
1714 LASSERT(req->rq_export != NULL);
1715 LASSERT(req->rq_export->exp_obd != NULL);
1717 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1718 case LDLM_BL_CALLBACK:
1719 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1722 case LDLM_CP_CALLBACK:
1723 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1726 case LDLM_GL_CALLBACK:
1727 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1730 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1731 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1732 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1733 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1735 rc = llog_origin_handle_cancel(req);
1736 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1738 ldlm_callback_reply(req, rc);
1740 case OBD_QC_CALLBACK:
1741 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1742 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1744 rc = target_handle_qc_callback(req);
1745 ldlm_callback_reply(req, rc);
1749 /* reply in handler */
1750 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1751 rc = target_handle_dqacq_callback(req);
1753 case LLOG_ORIGIN_HANDLE_CREATE:
1754 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1755 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1757 rc = llog_origin_handle_create(req);
1758 ldlm_callback_reply(req, rc);
1760 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1761 req_capsule_set(&req->rq_pill,
1762 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1763 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1765 rc = llog_origin_handle_next_block(req);
1766 ldlm_callback_reply(req, rc);
1768 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1769 req_capsule_set(&req->rq_pill,
1770 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1771 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1773 rc = llog_origin_handle_read_header(req);
1774 ldlm_callback_reply(req, rc);
1776 case LLOG_ORIGIN_HANDLE_CLOSE:
1777 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1779 rc = llog_origin_handle_close(req);
1780 ldlm_callback_reply(req, rc);
1783 CERROR("unknown opcode %u\n",
1784 lustre_msg_get_opc(req->rq_reqmsg));
1785 ldlm_callback_reply(req, -EPROTO);
1789 ns = req->rq_export->exp_obd->obd_namespace;
1790 LASSERT(ns != NULL);
1792 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1794 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1795 if (dlm_req == NULL) {
1796 ldlm_callback_reply(req, -EPROTO);
1800 /* Force a known safe race, send a cancel to the server for a lock
1801 * which the server has already started a blocking callback on. */
1802 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1803 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1804 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1806 CERROR("ldlm_cli_cancel: %d\n", rc);
1809 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1811 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1812 "disappeared\n", dlm_req->lock_handle[0].cookie);
1813 ldlm_callback_reply(req, -EINVAL);
1817 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1818 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1819 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1821 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1822 lock_res_and_lock(lock);
1823 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1824 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1825 /* If somebody cancels lock and cache is already droped,
1826 * or lock is failed before cp_ast received on client,
1827 * we can tell the server we have no lock. Otherwise, we
1828 * should send cancel after dropping the cache. */
1829 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1830 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1831 (lock->l_flags & LDLM_FL_FAILED)) {
1832 LDLM_DEBUG(lock, "callback on lock "
1833 LPX64" - lock disappeared\n",
1834 dlm_req->lock_handle[0].cookie);
1835 unlock_res_and_lock(lock);
1836 LDLM_LOCK_RELEASE(lock);
1837 ldlm_callback_reply(req, -EINVAL);
1840 /* BL_AST locks are not needed in lru.
1841 * let ldlm_cancel_lru() be fast. */
1842 ldlm_lock_remove_from_lru(lock);
1843 lock->l_flags |= LDLM_FL_BL_AST;
1845 unlock_res_and_lock(lock);
1847 /* We want the ost thread to get this reply so that it can respond
1848 * to ost requests (write cache writeback) that might be triggered
1851 * But we'd also like to be able to indicate in the reply that we're
1852 * cancelling right now, because it's unused, or have an intent result
1853 * in the reply, so we might have to push the responsibility for sending
1854 * the reply down into the AST handlers, alas. */
1856 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1857 case LDLM_BL_CALLBACK:
1858 CDEBUG(D_INODE, "blocking ast\n");
1859 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1860 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1861 ldlm_callback_reply(req, 0);
1862 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1863 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1865 case LDLM_CP_CALLBACK:
1866 CDEBUG(D_INODE, "completion ast\n");
1867 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1868 ldlm_callback_reply(req, 0);
1869 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1871 case LDLM_GL_CALLBACK:
1872 CDEBUG(D_INODE, "glimpse ast\n");
1873 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1874 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1877 LBUG(); /* checked above */
1883 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1888 /* Requests arrive in sender's byte order. The ptlrpc service
1889 * handler has already checked and, if necessary, byte-swapped the
1890 * incoming request message body, but I am responsible for the
1891 * message buffers. */
1893 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1895 if (req->rq_export == NULL) {
1896 struct ldlm_request *dlm_req;
1898 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1899 lustre_msg_get_opc(req->rq_reqmsg),
1900 libcfs_id2str(req->rq_peer),
1901 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1903 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1904 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1905 if (dlm_req != NULL)
1906 ldlm_lock_dump_handle(D_ERROR,
1907 &dlm_req->lock_handle[0]);
1908 ldlm_callback_reply(req, -ENOTCONN);
1912 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1914 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1916 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1917 CDEBUG(D_INODE, "cancel\n");
1918 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1920 rc = ldlm_handle_cancel(req);
1924 case OBD_LOG_CANCEL:
1925 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1926 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1928 rc = llog_origin_handle_cancel(req);
1929 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1931 ldlm_callback_reply(req, rc);
1934 CERROR("invalid opcode %d\n",
1935 lustre_msg_get_opc(req->rq_reqmsg));
1936 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1937 ldlm_callback_reply(req, -EINVAL);
1943 void ldlm_revoke_lock_cb(void *obj, void *data)
1945 struct list_head *rpc_list = data;
1946 struct ldlm_lock *lock = obj;
1948 lock_res_and_lock(lock);
1950 if (lock->l_req_mode != lock->l_granted_mode) {
1951 unlock_res_and_lock(lock);
1955 LASSERT(lock->l_resource);
1956 if (lock->l_resource->lr_type != LDLM_IBITS &&
1957 lock->l_resource->lr_type != LDLM_PLAIN) {
1958 unlock_res_and_lock(lock);
1962 if (lock->l_flags & LDLM_FL_AST_SENT) {
1963 unlock_res_and_lock(lock);
1967 LASSERT(lock->l_blocking_ast);
1968 LASSERT(!lock->l_blocking_lock);
1970 lock->l_flags |= LDLM_FL_AST_SENT;
1971 if (lock->l_export && lock->l_export->exp_lock_hash &&
1972 !hlist_unhashed(&lock->l_exp_hash))
1973 lustre_hash_del(lock->l_export->exp_lock_hash,
1974 &lock->l_remote_handle, &lock->l_exp_hash);
1975 list_add_tail(&lock->l_rk_ast, rpc_list);
1976 LDLM_LOCK_GET(lock);
1978 unlock_res_and_lock(lock);
1981 void ldlm_revoke_export_locks(struct obd_export *exp)
1983 struct list_head rpc_list;
1986 CFS_INIT_LIST_HEAD(&rpc_list);
1987 lustre_hash_for_each_empty(exp->exp_lock_hash,
1988 ldlm_revoke_lock_cb, &rpc_list);
1989 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1995 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1997 struct ldlm_bl_work_item *blwi = NULL;
1998 static unsigned int num_bl = 0;
2000 spin_lock(&blp->blp_lock);
2001 /* process a request from the blp_list at least every blp_num_threads */
2002 if (!list_empty(&blp->blp_list) &&
2003 (list_empty(&blp->blp_prio_list) || num_bl == 0))
2004 blwi = list_entry(blp->blp_list.next,
2005 struct ldlm_bl_work_item, blwi_entry);
2007 if (!list_empty(&blp->blp_prio_list))
2008 blwi = list_entry(blp->blp_prio_list.next,
2009 struct ldlm_bl_work_item, blwi_entry);
2012 if (++num_bl >= atomic_read(&blp->blp_num_threads))
2014 list_del(&blwi->blwi_entry);
2016 spin_unlock(&blp->blp_lock);
2021 /* This only contains temporary data until the thread starts */
2022 struct ldlm_bl_thread_data {
2023 char bltd_name[CFS_CURPROC_COMM_MAX];
2024 struct ldlm_bl_pool *bltd_blp;
2025 struct completion bltd_comp;
2029 static int ldlm_bl_thread_main(void *arg);
2031 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2033 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2036 init_completion(&bltd.bltd_comp);
2037 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
2039 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2040 atomic_read(&blp->blp_num_threads), rc);
2043 wait_for_completion(&bltd.bltd_comp);
2048 static int ldlm_bl_thread_main(void *arg)
2050 struct ldlm_bl_pool *blp;
2054 struct ldlm_bl_thread_data *bltd = arg;
2056 blp = bltd->bltd_blp;
2058 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
2059 atomic_inc(&blp->blp_busy_threads);
2061 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2062 "ldlm_bl_%02d", bltd->bltd_num);
2063 cfs_daemonize(bltd->bltd_name);
2065 complete(&bltd->bltd_comp);
2066 /* cannot use bltd after this, it is only on caller's stack */
2070 struct l_wait_info lwi = { 0 };
2071 struct ldlm_bl_work_item *blwi = NULL;
2073 blwi = ldlm_bl_get_work(blp);
2078 atomic_dec(&blp->blp_busy_threads);
2079 l_wait_event_exclusive(blp->blp_waitq,
2080 (blwi = ldlm_bl_get_work(blp)) != NULL,
2082 busy = atomic_inc_return(&blp->blp_busy_threads);
2084 if (blwi->blwi_ns == NULL)
2085 /* added by ldlm_cleanup() */
2088 /* Not fatal if racy and have a few too many threads */
2089 if (unlikely(busy < blp->blp_max_threads &&
2090 busy >= atomic_read(&blp->blp_num_threads)))
2091 /* discard the return value, we tried */
2092 ldlm_bl_thread_start(blp);
2094 if (blwi->blwi_ns == NULL)
2095 /* added by ldlm_cleanup() */
2099 if (blwi->blwi_count) {
2100 /* The special case when we cancel locks in lru
2101 * asynchronously, we pass the list of locks here.
2102 * Thus lock is marked LDLM_FL_CANCELING, and already
2103 * canceled locally. */
2104 ldlm_cli_cancel_list(&blwi->blwi_head,
2105 blwi->blwi_count, NULL, 0);
2107 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2110 OBD_FREE(blwi, sizeof(*blwi));
2113 atomic_dec(&blp->blp_busy_threads);
2114 atomic_dec(&blp->blp_num_threads);
2115 complete(&blp->blp_comp);
2121 static int ldlm_setup(void);
2122 static int ldlm_cleanup(void);
2124 int ldlm_get_ref(void)
2128 mutex_down(&ldlm_ref_sem);
2129 if (++ldlm_refcount == 1) {
2134 mutex_up(&ldlm_ref_sem);
2139 void ldlm_put_ref(void)
2142 mutex_down(&ldlm_ref_sem);
2143 if (ldlm_refcount == 1) {
2144 int rc = ldlm_cleanup();
2146 CERROR("ldlm_cleanup failed: %d\n", rc);
2152 mutex_up(&ldlm_ref_sem);
2158 * Export handle<->lock hash operations.
2161 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2163 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2167 ldlm_export_lock_key(struct hlist_node *hnode)
2169 struct ldlm_lock *lock;
2172 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2173 RETURN(&lock->l_remote_handle);
2177 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2180 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2184 ldlm_export_lock_get(struct hlist_node *hnode)
2186 struct ldlm_lock *lock;
2189 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2190 LDLM_LOCK_GET(lock);
2196 ldlm_export_lock_put(struct hlist_node *hnode)
2198 struct ldlm_lock *lock;
2201 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2202 LDLM_LOCK_RELEASE(lock);
2207 static lustre_hash_ops_t ldlm_export_lock_ops = {
2208 .lh_hash = ldlm_export_lock_hash,
2209 .lh_key = ldlm_export_lock_key,
2210 .lh_compare = ldlm_export_lock_compare,
2211 .lh_get = ldlm_export_lock_get,
2212 .lh_put = ldlm_export_lock_put
2215 int ldlm_init_export(struct obd_export *exp)
2219 exp->exp_lock_hash =
2220 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2221 7, 16, &ldlm_export_lock_ops, LH_REHASH);
2223 if (!exp->exp_lock_hash)
2228 EXPORT_SYMBOL(ldlm_init_export);
2230 void ldlm_destroy_export(struct obd_export *exp)
2233 lustre_hash_exit(exp->exp_lock_hash);
2234 exp->exp_lock_hash = NULL;
2237 EXPORT_SYMBOL(ldlm_destroy_export);
2239 static int ldlm_setup(void)
2241 struct ldlm_bl_pool *blp;
2243 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2244 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2250 if (ldlm_state != NULL)
2253 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2254 if (ldlm_state == NULL)
2258 rc = ldlm_proc_setup();
2264 if (ldlm_num_threads) {
2265 /* If ldlm_num_threads is set, it is the min and the max. */
2266 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2267 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2268 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2269 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2270 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2274 ldlm_state->ldlm_cb_service =
2275 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2276 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2277 LDLM_CB_REPLY_PORTAL, 1800,
2278 ldlm_callback_handler, "ldlm_cbd",
2279 ldlm_svc_proc_dir, NULL,
2280 ldlm_min_threads, ldlm_max_threads,
2282 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2284 if (!ldlm_state->ldlm_cb_service) {
2285 CERROR("failed to start service\n");
2286 GOTO(out_proc, rc = -ENOMEM);
2289 ldlm_state->ldlm_cancel_service =
2290 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2291 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2292 LDLM_CANCEL_REPLY_PORTAL, 6000,
2293 ldlm_cancel_handler, "ldlm_canceld",
2294 ldlm_svc_proc_dir, NULL,
2295 ldlm_min_threads, ldlm_max_threads,
2297 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2300 if (!ldlm_state->ldlm_cancel_service) {
2301 CERROR("failed to start service\n");
2302 GOTO(out_proc, rc = -ENOMEM);
2305 OBD_ALLOC(blp, sizeof(*blp));
2307 GOTO(out_proc, rc = -ENOMEM);
2308 ldlm_state->ldlm_bl_pool = blp;
2310 spin_lock_init(&blp->blp_lock);
2311 CFS_INIT_LIST_HEAD(&blp->blp_list);
2312 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2313 cfs_waitq_init(&blp->blp_waitq);
2314 atomic_set(&blp->blp_num_threads, 0);
2315 atomic_set(&blp->blp_busy_threads, 0);
2316 blp->blp_min_threads = ldlm_min_threads;
2317 blp->blp_max_threads = ldlm_max_threads;
2320 for (i = 0; i < blp->blp_min_threads; i++) {
2321 rc = ldlm_bl_thread_start(blp);
2323 GOTO(out_thread, rc);
2326 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2328 GOTO(out_thread, rc);
2330 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2332 GOTO(out_thread, rc);
2334 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2335 expired_lock_thread.elt_state = ELT_STOPPED;
2336 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2338 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2339 spin_lock_init(&waiting_locks_spinlock);
2340 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2342 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2344 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2345 GOTO(out_thread, rc);
2348 wait_event(expired_lock_thread.elt_waitq,
2349 expired_lock_thread.elt_state == ELT_READY);
2353 rc = ldlm_pools_init();
2355 GOTO(out_thread, rc);
2361 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2362 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2367 ldlm_proc_cleanup();
2370 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2375 static int ldlm_cleanup(void)
2378 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2382 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2383 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2384 CERROR("ldlm still has namespaces; clean these up first.\n");
2385 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2386 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2395 while (atomic_read(&blp->blp_num_threads) > 0) {
2396 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2398 init_completion(&blp->blp_comp);
2400 spin_lock(&blp->blp_lock);
2401 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2402 cfs_waitq_signal(&blp->blp_waitq);
2403 spin_unlock(&blp->blp_lock);
2405 wait_for_completion(&blp->blp_comp);
2407 OBD_FREE(blp, sizeof(*blp));
2409 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2410 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2411 ldlm_proc_cleanup();
2413 expired_lock_thread.elt_state = ELT_TERMINATE;
2414 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2415 wait_event(expired_lock_thread.elt_waitq,
2416 expired_lock_thread.elt_state == ELT_STOPPED);
2418 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2419 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2422 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2428 int __init ldlm_init(void)
2430 init_mutex(&ldlm_ref_sem);
2431 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2432 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2433 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2434 sizeof(struct ldlm_resource), 0,
2435 SLAB_HWCACHE_ALIGN);
2436 if (ldlm_resource_slab == NULL)
2439 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2440 sizeof(struct ldlm_lock), 0,
2441 SLAB_HWCACHE_ALIGN);
2442 if (ldlm_lock_slab == NULL) {
2443 cfs_mem_cache_destroy(ldlm_resource_slab);
2447 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2448 sizeof(struct ldlm_interval),
2449 0, SLAB_HWCACHE_ALIGN);
2450 if (ldlm_interval_slab == NULL) {
2451 cfs_mem_cache_destroy(ldlm_resource_slab);
2452 cfs_mem_cache_destroy(ldlm_lock_slab);
2459 void __exit ldlm_exit(void)
2463 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2464 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2465 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2466 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2467 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2468 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2469 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2473 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2476 EXPORT_SYMBOL(ldlm_get_processing_policy);
2477 EXPORT_SYMBOL(ldlm_lock2desc);
2478 EXPORT_SYMBOL(ldlm_register_intent);
2479 EXPORT_SYMBOL(ldlm_lockname);
2480 EXPORT_SYMBOL(ldlm_typename);
2481 EXPORT_SYMBOL(ldlm_lock2handle);
2482 EXPORT_SYMBOL(__ldlm_handle2lock);
2483 EXPORT_SYMBOL(ldlm_lock_get);
2484 EXPORT_SYMBOL(ldlm_lock_put);
2485 EXPORT_SYMBOL(ldlm_lock_match);
2486 EXPORT_SYMBOL(ldlm_lock_cancel);
2487 EXPORT_SYMBOL(ldlm_lock_addref);
2488 EXPORT_SYMBOL(ldlm_lock_addref_try);
2489 EXPORT_SYMBOL(ldlm_lock_decref);
2490 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2491 EXPORT_SYMBOL(ldlm_lock_change_resource);
2492 EXPORT_SYMBOL(ldlm_it2str);
2493 EXPORT_SYMBOL(ldlm_lock_dump);
2494 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2495 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2496 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2497 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2498 EXPORT_SYMBOL(ldlm_lock_allow_match);
2499 EXPORT_SYMBOL(ldlm_lock_downgrade);
2500 EXPORT_SYMBOL(ldlm_lock_convert);
2502 /* ldlm_request.c */
2503 EXPORT_SYMBOL(ldlm_completion_ast_async);
2504 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2505 EXPORT_SYMBOL(ldlm_completion_ast);
2506 EXPORT_SYMBOL(ldlm_blocking_ast);
2507 EXPORT_SYMBOL(ldlm_glimpse_ast);
2508 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2509 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2510 EXPORT_SYMBOL(ldlm_prep_elc_req);
2511 EXPORT_SYMBOL(ldlm_cli_convert);
2512 EXPORT_SYMBOL(ldlm_cli_enqueue);
2513 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2514 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2515 EXPORT_SYMBOL(ldlm_cli_cancel);
2516 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2517 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2518 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2519 EXPORT_SYMBOL(ldlm_replay_locks);
2520 EXPORT_SYMBOL(ldlm_resource_foreach);
2521 EXPORT_SYMBOL(ldlm_namespace_foreach);
2522 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2523 EXPORT_SYMBOL(ldlm_resource_iterate);
2524 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2525 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2528 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2529 EXPORT_SYMBOL(ldlm_server_completion_ast);
2530 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2531 EXPORT_SYMBOL(ldlm_handle_enqueue);
2532 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2533 EXPORT_SYMBOL(ldlm_handle_cancel);
2534 EXPORT_SYMBOL(ldlm_request_cancel);
2535 EXPORT_SYMBOL(ldlm_handle_convert);
2536 EXPORT_SYMBOL(ldlm_handle_convert0);
2537 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2538 EXPORT_SYMBOL(ldlm_get_ref);
2539 EXPORT_SYMBOL(ldlm_put_ref);
2540 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2541 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2543 /* ldlm_resource.c */
2544 EXPORT_SYMBOL(ldlm_namespace_new);
2545 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2546 EXPORT_SYMBOL(ldlm_namespace_free);
2547 EXPORT_SYMBOL(ldlm_namespace_dump);
2548 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2549 EXPORT_SYMBOL(ldlm_resource_get);
2550 EXPORT_SYMBOL(ldlm_resource_putref);
2551 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2554 EXPORT_SYMBOL(client_import_add_conn);
2555 EXPORT_SYMBOL(client_import_del_conn);
2556 EXPORT_SYMBOL(client_obd_setup);
2557 EXPORT_SYMBOL(client_obd_cleanup);
2558 EXPORT_SYMBOL(client_connect_import);
2559 EXPORT_SYMBOL(client_disconnect_export);
2560 EXPORT_SYMBOL(target_start_recovery_thread);
2561 EXPORT_SYMBOL(target_stop_recovery_thread);
2562 EXPORT_SYMBOL(target_handle_connect);
2563 EXPORT_SYMBOL(target_cleanup_recovery);
2564 EXPORT_SYMBOL(target_destroy_export);
2565 EXPORT_SYMBOL(target_cancel_recovery_timer);
2566 EXPORT_SYMBOL(target_send_reply);
2567 EXPORT_SYMBOL(target_queue_recovery_request);
2568 EXPORT_SYMBOL(target_handle_ping);
2569 EXPORT_SYMBOL(target_pack_pool_reply);
2570 EXPORT_SYMBOL(target_handle_disconnect);
2573 EXPORT_SYMBOL(lock_res_and_lock);
2574 EXPORT_SYMBOL(unlock_res_and_lock);