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_lock_get(lock->l_export, lock);
212 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 spin_lock_bh(&waiting_locks_spinlock);
224 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 spin_lock(&lock->l_export->exp_lock);
256 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
257 if (req->rq_ops->hpreq_lock_match) {
258 match = req->rq_ops->hpreq_lock_match(req, lock);
263 spin_unlock(&lock->l_export->exp_lock);
267 /* This is called from within a timer interrupt and cannot schedule */
268 static void waiting_locks_callback(unsigned long unused)
270 struct ldlm_lock *lock, *last = NULL;
273 spin_lock_bh(&waiting_locks_spinlock);
274 while (!list_empty(&waiting_locks_list)) {
275 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
277 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
278 (lock->l_req_mode == LCK_GROUP))
281 if (ptlrpc_check_suspend()) {
282 /* there is a case when we talk to one mds, holding
283 * lock from another mds. this way we easily can get
284 * here, if second mds is being recovered. so, we
285 * suspend timeouts. bug 6019 */
287 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
288 lock->l_export->exp_client_uuid.uuid,
289 lock->l_export->exp_connection->c_remote_uuid.uuid,
290 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
292 list_del_init(&lock->l_pending_chain);
293 spin_unlock_bh(&waiting_locks_spinlock);
294 ldlm_add_waiting_lock(lock);
298 /* if timeout overlaps the activation time of suspended timeouts
299 * then extend it to give a chance for client to reconnect */
300 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
301 cfs_time_seconds(obd_timeout)/2),
302 ptlrpc_suspend_wakeup_time())) {
303 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
304 lock->l_export->exp_client_uuid.uuid,
305 lock->l_export->exp_connection->c_remote_uuid.uuid,
306 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
308 list_del_init(&lock->l_pending_chain);
309 spin_unlock_bh(&waiting_locks_spinlock);
310 ldlm_add_waiting_lock(lock);
314 /* Check if we need to prolong timeout */
315 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
316 ldlm_lock_busy(lock)) {
319 if (lock->l_pending_chain.next == &waiting_locks_list)
324 spin_unlock_bh(&waiting_locks_spinlock);
325 LDLM_DEBUG(lock, "prolong the busy lock");
326 ldlm_refresh_waiting_lock(lock,
327 ldlm_get_enq_timeout(lock));
328 spin_lock_bh(&waiting_locks_spinlock);
331 LDLM_LOCK_RELEASE(lock);
335 LDLM_LOCK_RELEASE(lock);
338 lock->l_resource->lr_namespace->ns_timeouts++;
339 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
340 "evicting client at %s ",
341 cfs_time_current_sec()- lock->l_last_activity,
343 lock->l_export->exp_connection->c_peer.nid));
347 /* no needs to take an extra ref on the lock since it was in
348 * the waiting_locks_list and ldlm_add_waiting_lock()
349 * already grabbed a ref */
350 list_del(&lock->l_pending_chain);
351 list_add(&lock->l_pending_chain,
352 &expired_lock_thread.elt_expired_locks);
355 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
356 if (obd_dump_on_timeout)
357 expired_lock_thread.elt_dump = __LINE__;
359 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
363 * Make sure the timer will fire again if we have any locks
366 if (!list_empty(&waiting_locks_list)) {
367 cfs_time_t timeout_rounded;
368 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
370 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
371 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
373 spin_unlock_bh(&waiting_locks_spinlock);
377 * Indicate that we're waiting for a client to call us back cancelling a given
378 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
379 * timer to fire appropriately. (We round up to the next second, to avoid
380 * floods of timer firings during periods of high lock contention and traffic).
381 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
382 * if it has been added to the waiting list (1 is returned).
384 * Called with the namespace lock held.
386 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
389 cfs_time_t timeout_rounded;
391 if (!list_empty(&lock->l_pending_chain))
394 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
395 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
398 timeout = cfs_time_shift(seconds);
399 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
400 lock->l_callback_timeout = timeout;
402 timeout_rounded = round_timeout(lock->l_callback_timeout);
404 if (cfs_time_before(timeout_rounded,
405 cfs_timer_deadline(&waiting_locks_timer)) ||
406 !cfs_timer_is_armed(&waiting_locks_timer)) {
407 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
409 /* if the new lock has a shorter timeout than something earlier on
410 the list, we'll wait the longer amount of time; no big deal. */
411 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
415 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
418 int timeout = ldlm_get_enq_timeout(lock);
420 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
422 spin_lock_bh(&waiting_locks_spinlock);
423 if (lock->l_destroyed) {
424 static cfs_time_t next;
425 spin_unlock_bh(&waiting_locks_spinlock);
426 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
427 if (cfs_time_after(cfs_time_current(), next)) {
428 next = cfs_time_shift(14400);
429 libcfs_debug_dumpstack(NULL);
434 ret = __ldlm_add_waiting_lock(lock, timeout);
436 /* grab ref on the lock if it has been added to the
439 spin_unlock_bh(&waiting_locks_spinlock);
441 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
442 ret == 0 ? "not re-" : "", timeout,
443 AT_OFF ? "off" : "on");
448 * Remove a lock from the pending list, likely because it had its cancellation
449 * callback arrive without incident. This adjusts the lock-timeout timer if
450 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
451 * As done by ldlm_del_waiting_lock(), the caller must release the lock
452 * reference when the lock is removed from any list (1 is returned).
454 * Called with namespace lock held.
456 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
458 struct list_head *list_next;
460 if (list_empty(&lock->l_pending_chain))
463 list_next = lock->l_pending_chain.next;
464 if (lock->l_pending_chain.prev == &waiting_locks_list) {
465 /* Removing the head of the list, adjust timer. */
466 if (list_next == &waiting_locks_list) {
467 /* No more, just cancel. */
468 cfs_timer_disarm(&waiting_locks_timer);
470 struct ldlm_lock *next;
471 next = list_entry(list_next, struct ldlm_lock,
473 cfs_timer_arm(&waiting_locks_timer,
474 round_timeout(next->l_callback_timeout));
477 list_del_init(&lock->l_pending_chain);
482 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
486 if (lock->l_export == NULL) {
487 /* We don't have a "waiting locks list" on clients. */
488 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
492 spin_lock_bh(&waiting_locks_spinlock);
493 ret = __ldlm_del_waiting_lock(lock);
494 spin_unlock_bh(&waiting_locks_spinlock);
496 /* release lock ref if it has indeed been removed
498 LDLM_LOCK_RELEASE(lock);
500 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
507 * Called with namespace lock held.
509 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
511 if (lock->l_export == NULL) {
512 /* We don't have a "waiting locks list" on clients. */
513 LDLM_DEBUG(lock, "client lock: no-op");
517 spin_lock_bh(&waiting_locks_spinlock);
519 if (list_empty(&lock->l_pending_chain)) {
520 spin_unlock_bh(&waiting_locks_spinlock);
521 LDLM_DEBUG(lock, "wasn't waiting");
525 /* we remove/add the lock to the waiting list, so no needs to
526 * release/take a lock reference */
527 __ldlm_del_waiting_lock(lock);
528 __ldlm_add_waiting_lock(lock, timeout);
529 spin_unlock_bh(&waiting_locks_spinlock);
531 LDLM_DEBUG(lock, "refreshed");
534 #else /* !__KERNEL__ */
536 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
538 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
542 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
547 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
551 #endif /* __KERNEL__ */
553 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
554 const char *ast_type)
556 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
557 "to a lock %s callback time out: rc %d\n",
558 lock->l_export->exp_obd->obd_name,
559 obd_export_nid2str(lock->l_export), ast_type, rc);
561 if (obd_dump_on_timeout)
562 libcfs_debug_dumplog();
564 spin_lock_bh(&waiting_locks_spinlock);
565 if (__ldlm_del_waiting_lock(lock) == 0)
566 /* the lock was not in any list, grab an extra ref before adding
567 * the lock to the expired list */
569 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
570 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
571 spin_unlock_bh(&waiting_locks_spinlock);
573 class_fail_export(lock->l_export);
577 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
578 struct ptlrpc_request *req, int rc,
579 const char *ast_type)
581 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
583 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
584 LASSERT(lock->l_export);
585 if (lock->l_export->exp_libclient) {
586 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
587 " timeout, just cancelling lock", ast_type,
588 libcfs_nid2str(peer.nid));
589 ldlm_lock_cancel(lock);
591 } else if (lock->l_flags & LDLM_FL_CANCEL) {
592 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
593 "cancel was received (AST reply lost?)",
594 ast_type, libcfs_nid2str(peer.nid));
595 ldlm_lock_cancel(lock);
598 ldlm_del_waiting_lock(lock);
599 ldlm_failed_ast(lock, rc, ast_type);
603 LDLM_DEBUG(lock, "client (nid %s) returned %d"
604 " from %s AST - normal race",
605 libcfs_nid2str(peer.nid),
607 lustre_msg_get_status(req->rq_repmsg) : -1,
610 LDLM_ERROR(lock, "client (nid %s) returned %d "
611 "from %s AST", libcfs_nid2str(peer.nid),
612 (req->rq_repmsg != NULL) ?
613 lustre_msg_get_status(req->rq_repmsg) : 0,
615 ldlm_lock_cancel(lock);
616 /* Server-side AST functions are called from ldlm_reprocess_all,
617 * which needs to be told to please restart its reprocessing. */
624 static int ldlm_cb_interpret(const struct lu_env *env,
625 struct ptlrpc_request *req, void *data, int rc)
627 struct ldlm_cb_set_arg *arg;
628 struct ldlm_lock *lock;
631 LASSERT(data != NULL);
633 arg = req->rq_async_args.pointer_arg[0];
634 lock = req->rq_async_args.pointer_arg[1];
635 LASSERT(lock != NULL);
637 /* If client canceled the lock but the cancel has not
638 * been received yet, we need to update lvbo to have the
639 * proper attributes cached. */
640 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
641 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
642 rc = ldlm_handle_ast_error(lock, req, rc,
643 arg->type == LDLM_BL_CALLBACK
644 ? "blocking" : "completion");
647 LDLM_LOCK_RELEASE(lock);
650 atomic_set(&arg->restart, 1);
655 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
656 struct ldlm_cb_set_arg *arg,
657 struct ldlm_lock *lock,
663 if (unlikely(instant_cancel)) {
664 rc = ptl_send_rpc(req, 1);
665 ptlrpc_req_finished(req);
667 /* If we cancelled the lock, we need to restart
668 * ldlm_reprocess_queue */
669 atomic_set(&arg->restart, 1);
672 ptlrpc_set_add_req(arg->set, req);
679 * Check if there are requests in the export request list which prevent
680 * the lock canceling and make these requests high priority ones.
682 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
684 struct ptlrpc_request *req;
687 if (lock->l_export == NULL) {
688 LDLM_DEBUG(lock, "client lock: no-op");
692 spin_lock(&lock->l_export->exp_lock);
693 list_for_each_entry(req, &lock->l_export->exp_queued_rpc, rq_exp_list) {
694 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
695 req->rq_ops->hpreq_lock_match(req, lock))
696 ptlrpc_hpreq_reorder(req);
698 spin_unlock(&lock->l_export->exp_lock);
703 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
704 * enqueued server lock conflicts with given one.
706 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
707 * to wait for client response.
709 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
710 struct ldlm_lock_desc *desc,
711 void *data, int flag)
713 struct ldlm_cb_set_arg *arg = data;
714 struct ldlm_request *body;
715 struct ptlrpc_request *req;
716 int instant_cancel = 0;
720 if (flag == LDLM_CB_CANCELING)
721 /* Don't need to do anything here. */
725 LASSERT(data != NULL);
726 if (lock->l_export->exp_obd->obd_recovering != 0) {
727 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
728 ldlm_lock_dump(D_ERROR, lock, 0);
731 ldlm_lock_reorder_req(lock);
733 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
734 &RQF_LDLM_BL_CALLBACK,
735 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
739 req->rq_async_args.pointer_arg[0] = arg;
740 req->rq_async_args.pointer_arg[1] = lock;
741 req->rq_interpret_reply = ldlm_cb_interpret;
742 req->rq_no_resend = 1;
744 lock_res(lock->l_resource);
745 if (lock->l_granted_mode != lock->l_req_mode) {
746 /* this blocking AST will be communicated as part of the
747 * completion AST instead */
748 unlock_res(lock->l_resource);
749 ptlrpc_req_finished(req);
750 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
754 if (lock->l_destroyed) {
755 /* What's the point? */
756 unlock_res(lock->l_resource);
757 ptlrpc_req_finished(req);
761 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
764 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
765 body->lock_handle[0] = lock->l_remote_handle;
766 body->lock_desc = *desc;
767 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
769 LDLM_DEBUG(lock, "server preparing blocking AST");
771 ptlrpc_request_set_replen(req);
772 if (instant_cancel) {
773 unlock_res(lock->l_resource);
774 ldlm_lock_cancel(lock);
776 LASSERT(lock->l_granted_mode == lock->l_req_mode);
777 ldlm_add_waiting_lock(lock);
778 unlock_res(lock->l_resource);
781 req->rq_send_state = LUSTRE_IMP_FULL;
782 /* ptlrpc_request_alloc_pack already set timeout */
784 req->rq_timeout = ldlm_get_rq_timeout();
786 if (lock->l_export && lock->l_export->exp_nid_stats &&
787 lock->l_export->exp_nid_stats->nid_ldlm_stats)
788 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
789 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
791 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
796 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
798 struct ldlm_cb_set_arg *arg = data;
799 struct ldlm_request *body;
800 struct ptlrpc_request *req;
801 long total_enqueue_wait;
802 int instant_cancel = 0;
806 LASSERT(lock != NULL);
807 LASSERT(data != NULL);
809 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
810 lock->l_last_activity);
812 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
813 &RQF_LDLM_CP_CALLBACK);
817 lock_res_and_lock(lock);
818 if (lock->l_resource->lr_lvb_len)
819 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
820 lock->l_resource->lr_lvb_len);
821 unlock_res_and_lock(lock);
823 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
825 ptlrpc_request_free(req);
829 req->rq_async_args.pointer_arg[0] = arg;
830 req->rq_async_args.pointer_arg[1] = lock;
831 req->rq_interpret_reply = ldlm_cb_interpret;
832 req->rq_no_resend = 1;
833 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
835 body->lock_handle[0] = lock->l_remote_handle;
836 body->lock_flags = flags;
837 ldlm_lock2desc(lock, &body->lock_desc);
838 if (lock->l_resource->lr_lvb_len) {
839 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
841 lock_res_and_lock(lock);
842 memcpy(lvb, lock->l_resource->lr_lvb_data,
843 lock->l_resource->lr_lvb_len);
844 unlock_res_and_lock(lock);
847 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
850 /* Server-side enqueue wait time estimate, used in
851 __ldlm_add_waiting_lock to set future enqueue timers */
852 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
853 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
856 /* bz18618. Don't add lock enqueue time we spend waiting for a
857 previous callback to fail. Locks waiting legitimately will
858 get extended by ldlm_refresh_waiting_lock regardless of the
859 estimate, so it's okay to underestimate here. */
860 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
861 "It is likely that a previous callback timed out.",
863 at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
865 ptlrpc_request_set_replen(req);
867 req->rq_send_state = LUSTRE_IMP_FULL;
868 /* ptlrpc_request_pack already set timeout */
870 req->rq_timeout = ldlm_get_rq_timeout();
872 /* We only send real blocking ASTs after the lock is granted */
873 lock_res_and_lock(lock);
874 if (lock->l_flags & LDLM_FL_AST_SENT) {
875 body->lock_flags |= LDLM_FL_AST_SENT;
876 /* copy ast flags like LDLM_FL_DISCARD_DATA */
877 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
879 /* We might get here prior to ldlm_handle_enqueue setting
880 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
881 * into waiting list, but this is safe and similar code in
882 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
883 * that would not only cancel the lock, but will also remove
884 * it from waiting list */
885 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
886 unlock_res_and_lock(lock);
887 ldlm_lock_cancel(lock);
889 lock_res_and_lock(lock);
891 /* start the lock-timeout clock */
892 ldlm_add_waiting_lock(lock);
895 unlock_res_and_lock(lock);
897 if (lock->l_export && lock->l_export->exp_nid_stats &&
898 lock->l_export->exp_nid_stats->nid_ldlm_stats)
899 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
900 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
902 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
907 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
909 struct ldlm_resource *res = lock->l_resource;
910 struct ldlm_request *body;
911 struct ptlrpc_request *req;
915 LASSERT(lock != NULL);
917 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
918 &RQF_LDLM_GL_CALLBACK,
919 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
924 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
925 body->lock_handle[0] = lock->l_remote_handle;
926 ldlm_lock2desc(lock, &body->lock_desc);
928 lock_res_and_lock(lock);
929 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
930 lock->l_resource->lr_lvb_len);
931 unlock_res_and_lock(lock);
932 res = lock->l_resource;
933 ptlrpc_request_set_replen(req);
936 req->rq_send_state = LUSTRE_IMP_FULL;
937 /* ptlrpc_request_alloc_pack already set timeout */
939 req->rq_timeout = ldlm_get_rq_timeout();
941 if (lock->l_export && lock->l_export->exp_nid_stats &&
942 lock->l_export->exp_nid_stats->nid_ldlm_stats)
943 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
944 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
946 rc = ptlrpc_queue_wait(req);
947 if (rc == -ELDLM_NO_LOCK_DATA)
948 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
950 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
952 rc = ldlm_res_lvbo_update(res, req, 1);
954 ptlrpc_req_finished(req);
956 ldlm_reprocess_all(res);
962 extern unsigned long long lu_time_stamp_get(void);
964 #define lu_time_stamp_get() time(NULL)
967 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
968 struct lprocfs_stats *srv_stats)
970 int lock_type = 0, op = 0;
972 lock_type = dlm_req->lock_desc.l_resource.lr_type;
976 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
979 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
980 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
982 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
985 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
988 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
996 lprocfs_counter_incr(srv_stats, op);
1002 * Main server-side entry point into LDLM. This is called by ptlrpc service
1003 * threads to carry out client lock enqueueing requests.
1005 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1006 struct ptlrpc_request *req,
1007 const struct ldlm_request *dlm_req,
1008 const struct ldlm_callback_suite *cbs)
1010 struct ldlm_reply *dlm_rep;
1012 ldlm_error_t err = ELDLM_OK;
1013 struct ldlm_lock *lock = NULL;
1014 void *cookie = NULL;
1018 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1020 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1021 flags = dlm_req->lock_flags;
1023 LASSERT(req->rq_export);
1025 if (req->rq_rqbd->rqbd_service->srv_stats)
1026 ldlm_svc_get_eopc(dlm_req,
1027 req->rq_rqbd->rqbd_service->srv_stats);
1029 if (req->rq_export && req->rq_export->exp_nid_stats &&
1030 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1031 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1032 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1034 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1035 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1036 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1037 dlm_req->lock_desc.l_resource.lr_type);
1038 GOTO(out, rc = -EFAULT);
1041 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1042 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1043 dlm_req->lock_desc.l_req_mode &
1044 (dlm_req->lock_desc.l_req_mode-1))) {
1045 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1046 dlm_req->lock_desc.l_req_mode);
1047 GOTO(out, rc = -EFAULT);
1050 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1051 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1053 DEBUG_REQ(D_ERROR, req,
1054 "PLAIN lock request from IBITS client?");
1055 GOTO(out, rc = -EPROTO);
1057 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1059 DEBUG_REQ(D_ERROR, req,
1060 "IBITS lock request from unaware client?");
1061 GOTO(out, rc = -EPROTO);
1065 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1066 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1067 ibits for mgc/mgs) */
1069 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1070 * inodebits lock if client does not support them. */
1071 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1072 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1073 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1074 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1075 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1076 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1077 dlm_req->lock_desc.l_req_mode = LCK_CR;
1081 if (unlikely(flags & LDLM_FL_REPLAY)) {
1082 /* Find an existing lock in the per-export lock hash */
1083 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1084 (void *)&dlm_req->lock_handle[0]);
1086 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1087 LPX64, lock->l_handle.h_cookie);
1088 GOTO(existing_lock, rc = 0);
1092 /* The lock's callback data might be set in the policy function */
1093 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1094 dlm_req->lock_desc.l_resource.lr_type,
1095 dlm_req->lock_desc.l_req_mode,
1099 GOTO(out, rc = -ENOMEM);
1101 lock->l_last_activity = cfs_time_current_sec();
1102 lock->l_remote_handle = dlm_req->lock_handle[0];
1103 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1105 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1106 /* Don't enqueue a lock onto the export if it has already
1107 * been evicted. Cancel it now instead. (bug 3822) */
1108 if (req->rq_export->exp_failed) {
1109 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1110 GOTO(out, rc = -ENOTCONN);
1113 lock->l_export = class_export_lock_get(req->rq_export, lock);
1114 if (lock->l_export->exp_lock_hash)
1115 cfs_hash_add(lock->l_export->exp_lock_hash,
1116 &lock->l_remote_handle,
1121 if (flags & LDLM_FL_HAS_INTENT) {
1122 /* In this case, the reply buffer is allocated deep in
1123 * local_lock_enqueue by the policy function. */
1126 lock_res_and_lock(lock);
1127 if (lock->l_resource->lr_lvb_len) {
1128 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1130 lock->l_resource->lr_lvb_len);
1132 unlock_res_and_lock(lock);
1134 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1135 GOTO(out, rc = -ENOMEM);
1137 rc = req_capsule_server_pack(&req->rq_pill);
1142 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1143 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1144 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1145 lock->l_req_extent = lock->l_policy_data.l_extent;
1147 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1151 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1152 dlm_rep->lock_flags = flags;
1154 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1155 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1157 /* We never send a blocking AST until the lock is granted, but
1158 * we can tell it right now */
1159 lock_res_and_lock(lock);
1161 /* Now take into account flags to be inherited from original lock
1162 request both in reply to client and in our own lock flags. */
1163 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1164 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1166 /* Don't move a pending lock onto the export if it has already
1167 * been evicted. Cancel it now instead. (bug 5683) */
1168 if (unlikely(req->rq_export->exp_failed ||
1169 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1170 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1172 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1173 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1174 if (lock->l_granted_mode == lock->l_req_mode) {
1176 * Only cancel lock if it was granted, because it would
1177 * be destroyed immediately and would never be granted
1178 * in the future, causing timeouts on client. Not
1179 * granted lock will be cancelled immediately after
1180 * sending completion AST.
1182 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1183 unlock_res_and_lock(lock);
1184 ldlm_lock_cancel(lock);
1185 lock_res_and_lock(lock);
1187 ldlm_add_waiting_lock(lock);
1190 /* Make sure we never ever grant usual metadata locks to liblustre
1192 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1193 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1194 req->rq_export->exp_libclient) {
1195 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1196 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1197 CERROR("Granting sync lock to libclient. "
1198 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1199 dlm_req->lock_flags, dlm_rep->lock_flags,
1201 LDLM_ERROR(lock, "sync lock");
1202 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1203 struct ldlm_intent *it;
1205 it = req_capsule_client_get(&req->rq_pill,
1208 CERROR("This is intent %s ("LPU64")\n",
1209 ldlm_it2str(it->opc), it->opc);
1215 unlock_res_and_lock(lock);
1219 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1220 if (!req->rq_packed_final) {
1221 err = lustre_pack_reply(req, 1, NULL, NULL);
1226 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1227 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1229 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1230 "(err=%d, rc=%d)", err, rc);
1232 lock_res_and_lock(lock);
1234 if (lock->l_resource->lr_lvb_len > 0) {
1237 lvb = req_capsule_server_get(&req->rq_pill,
1239 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1242 memcpy(lvb, lock->l_resource->lr_lvb_data,
1243 lock->l_resource->lr_lvb_len);
1246 ldlm_resource_unlink_lock(lock);
1247 ldlm_lock_destroy_nolock(lock);
1249 unlock_res_and_lock(lock);
1251 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1252 ldlm_reprocess_all(lock->l_resource);
1254 LDLM_LOCK_RELEASE(lock);
1257 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1263 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1264 ldlm_completion_callback completion_callback,
1265 ldlm_blocking_callback blocking_callback,
1266 ldlm_glimpse_callback glimpse_callback)
1268 struct ldlm_request *dlm_req;
1269 struct ldlm_callback_suite cbs = {
1270 .lcs_completion = completion_callback,
1271 .lcs_blocking = blocking_callback,
1272 .lcs_glimpse = glimpse_callback
1276 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1277 if (dlm_req != NULL) {
1278 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1279 req, dlm_req, &cbs);
1286 int ldlm_handle_convert0(struct ptlrpc_request *req,
1287 const struct ldlm_request *dlm_req)
1289 struct ldlm_reply *dlm_rep;
1290 struct ldlm_lock *lock;
1294 if (req->rq_export && req->rq_export->exp_nid_stats &&
1295 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1296 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1297 LDLM_CONVERT - LDLM_FIRST_OPC);
1299 rc = req_capsule_server_pack(&req->rq_pill);
1303 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1304 dlm_rep->lock_flags = dlm_req->lock_flags;
1306 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1308 req->rq_status = EINVAL;
1312 LDLM_DEBUG(lock, "server-side convert handler START");
1314 lock->l_last_activity = cfs_time_current_sec();
1315 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1316 &dlm_rep->lock_flags);
1318 if (ldlm_del_waiting_lock(lock))
1319 LDLM_DEBUG(lock, "converted waiting lock");
1322 req->rq_status = EDEADLOCK;
1327 if (!req->rq_status)
1328 ldlm_reprocess_all(lock->l_resource);
1329 LDLM_DEBUG(lock, "server-side convert handler END");
1330 LDLM_LOCK_PUT(lock);
1332 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1337 int ldlm_handle_convert(struct ptlrpc_request *req)
1340 struct ldlm_request *dlm_req;
1342 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1343 if (dlm_req != NULL) {
1344 rc = ldlm_handle_convert0(req, dlm_req);
1346 CERROR ("Can't unpack dlm_req\n");
1352 /* Cancel all the locks whos handles are packed into ldlm_request */
1353 int ldlm_request_cancel(struct ptlrpc_request *req,
1354 const struct ldlm_request *dlm_req, int first)
1356 struct ldlm_resource *res, *pres = NULL;
1357 struct ldlm_lock *lock;
1358 int i, count, done = 0;
1361 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1365 /* There is no lock on the server at the replay time,
1366 * skip lock cancelling to make replay tests to pass. */
1367 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1370 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1371 "starting at %d", count, first);
1373 for (i = first; i < count; i++) {
1374 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1376 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1377 "lock (cookie "LPU64")",
1378 dlm_req->lock_handle[i].cookie);
1382 res = lock->l_resource;
1387 ldlm_reprocess_all(pres);
1388 LDLM_RESOURCE_DELREF(pres);
1389 ldlm_resource_putref(pres);
1392 ldlm_resource_getref(res);
1393 LDLM_RESOURCE_ADDREF(res);
1394 ldlm_res_lvbo_update(res, NULL, 1);
1398 ldlm_lock_cancel(lock);
1399 LDLM_LOCK_PUT(lock);
1402 ldlm_reprocess_all(pres);
1403 LDLM_RESOURCE_DELREF(pres);
1404 ldlm_resource_putref(pres);
1406 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1410 int ldlm_handle_cancel(struct ptlrpc_request *req)
1412 struct ldlm_request *dlm_req;
1416 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1417 if (dlm_req == NULL) {
1418 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1422 if (req->rq_export && req->rq_export->exp_nid_stats &&
1423 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1424 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1425 LDLM_CANCEL - LDLM_FIRST_OPC);
1427 rc = req_capsule_server_pack(&req->rq_pill);
1431 if (!ldlm_request_cancel(req, dlm_req, 0))
1432 req->rq_status = ESTALE;
1434 if (ptlrpc_reply(req) != 0)
1440 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1441 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1446 LDLM_DEBUG(lock, "client blocking AST callback handler");
1448 lock_res_and_lock(lock);
1449 lock->l_flags |= LDLM_FL_CBPENDING;
1451 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1452 lock->l_flags |= LDLM_FL_CANCEL;
1454 do_ast = (!lock->l_readers && !lock->l_writers);
1455 unlock_res_and_lock(lock);
1458 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1459 lock, lock->l_blocking_ast);
1460 if (lock->l_blocking_ast != NULL)
1461 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1464 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1468 LDLM_DEBUG(lock, "client blocking callback handler END");
1469 LDLM_LOCK_RELEASE(lock);
1473 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1474 struct ldlm_namespace *ns,
1475 struct ldlm_request *dlm_req,
1476 struct ldlm_lock *lock)
1478 CFS_LIST_HEAD(ast_list);
1481 LDLM_DEBUG(lock, "client completion callback handler START");
1483 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1484 int to = cfs_time_seconds(1);
1486 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1487 if (lock->l_granted_mode == lock->l_req_mode ||
1493 lock_res_and_lock(lock);
1494 if (lock->l_destroyed ||
1495 lock->l_granted_mode == lock->l_req_mode) {
1496 /* bug 11300: the lock has already been granted */
1497 unlock_res_and_lock(lock);
1498 LDLM_DEBUG(lock, "Double grant race happened");
1499 LDLM_LOCK_RELEASE(lock);
1504 /* If we receive the completion AST before the actual enqueue returned,
1505 * then we might need to switch lock modes, resources, or extents. */
1506 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1507 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1508 LDLM_DEBUG(lock, "completion AST, new lock mode");
1511 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1512 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1513 LDLM_DEBUG(lock, "completion AST, new policy data");
1516 ldlm_resource_unlink_lock(lock);
1517 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1518 &lock->l_resource->lr_name,
1519 sizeof(lock->l_resource->lr_name)) != 0) {
1520 unlock_res_and_lock(lock);
1521 if (ldlm_lock_change_resource(ns, lock,
1522 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1523 LDLM_ERROR(lock, "Failed to allocate resource");
1524 LDLM_LOCK_RELEASE(lock);
1528 LDLM_DEBUG(lock, "completion AST, new resource");
1529 CERROR("change resource!\n");
1530 lock_res_and_lock(lock);
1533 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1534 /* BL_AST locks are not needed in lru.
1535 * let ldlm_cancel_lru() be fast. */
1536 ldlm_lock_remove_from_lru(lock);
1537 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1538 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1541 if (lock->l_lvb_len) {
1542 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1543 RCL_CLIENT) < lock->l_lvb_len) {
1544 LDLM_ERROR(lock, "completion AST did not contain "
1547 void *lvb = req_capsule_client_get(&req->rq_pill,
1549 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1553 ldlm_grant_lock(lock, &ast_list);
1554 unlock_res_and_lock(lock);
1556 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1558 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1560 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1562 LDLM_LOCK_RELEASE(lock);
1566 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1567 struct ldlm_namespace *ns,
1568 struct ldlm_request *dlm_req,
1569 struct ldlm_lock *lock)
1574 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1576 if (lock->l_glimpse_ast != NULL)
1577 rc = lock->l_glimpse_ast(lock, req);
1579 if (req->rq_repmsg != NULL) {
1582 req->rq_status = rc;
1586 lock_res_and_lock(lock);
1587 if (lock->l_granted_mode == LCK_PW &&
1588 !lock->l_readers && !lock->l_writers &&
1589 cfs_time_after(cfs_time_current(),
1590 cfs_time_add(lock->l_last_used,
1591 cfs_time_seconds(10)))) {
1592 unlock_res_and_lock(lock);
1593 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1594 ldlm_handle_bl_callback(ns, NULL, lock);
1599 unlock_res_and_lock(lock);
1600 LDLM_LOCK_RELEASE(lock);
1604 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1606 if (req->rq_no_reply)
1609 req->rq_status = rc;
1610 if (!req->rq_packed_final) {
1611 rc = lustre_pack_reply(req, 1, NULL, NULL);
1615 return ptlrpc_reply(req);
1619 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1620 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1621 struct list_head *cancels, int count)
1623 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1624 struct ldlm_bl_work_item *blwi;
1627 if (cancels && count == 0)
1630 OBD_ALLOC(blwi, sizeof(*blwi));
1636 blwi->blwi_ld = *ld;
1638 list_add(&blwi->blwi_head, cancels);
1639 list_del_init(cancels);
1640 blwi->blwi_count = count;
1642 blwi->blwi_lock = lock;
1644 spin_lock(&blp->blp_lock);
1645 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1646 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1647 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1649 /* other blocking callbacks are added to the regular list */
1650 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1652 cfs_waitq_signal(&blp->blp_waitq);
1653 spin_unlock(&blp->blp_lock);
1659 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1660 struct ldlm_lock *lock)
1663 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1669 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1670 struct list_head *cancels, int count)
1673 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1679 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1680 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1682 struct obd_device *obd = req->rq_export->exp_obd;
1689 DEBUG_REQ(D_ERROR, req, "%s: handle setinfo\n", obd->obd_name);
1691 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1693 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1695 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1698 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1700 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1702 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1705 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1708 /* We are responsible for swabbing contents of val */
1710 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1711 /* Pass it on to mdc (the "export" in this case) */
1712 rc = obd_set_info_async(req->rq_export,
1713 sizeof(KEY_HSM_COPYTOOL_SEND),
1714 KEY_HSM_COPYTOOL_SEND,
1717 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1722 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1723 static int ldlm_callback_handler(struct ptlrpc_request *req)
1725 struct ldlm_namespace *ns;
1726 struct ldlm_request *dlm_req;
1727 struct ldlm_lock *lock;
1731 /* Requests arrive in sender's byte order. The ptlrpc service
1732 * handler has already checked and, if necessary, byte-swapped the
1733 * incoming request message body, but I am responsible for the
1734 * message buffers. */
1736 /* do nothing for sec context finalize */
1737 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1740 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1742 if (req->rq_export == NULL) {
1743 ldlm_callback_reply(req, -ENOTCONN);
1747 LASSERT(req->rq_export != NULL);
1748 LASSERT(req->rq_export->exp_obd != NULL);
1750 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1751 case LDLM_BL_CALLBACK:
1752 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1755 case LDLM_CP_CALLBACK:
1756 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1759 case LDLM_GL_CALLBACK:
1760 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1764 rc = ldlm_handle_setinfo(req);
1765 ldlm_callback_reply(req, rc);
1767 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1768 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1769 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1770 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1772 rc = llog_origin_handle_cancel(req);
1773 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1775 ldlm_callback_reply(req, rc);
1777 case OBD_QC_CALLBACK:
1778 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1779 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1781 rc = target_handle_qc_callback(req);
1782 ldlm_callback_reply(req, rc);
1786 /* reply in handler */
1787 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1788 rc = target_handle_dqacq_callback(req);
1790 case LLOG_ORIGIN_HANDLE_CREATE:
1791 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1792 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1794 rc = llog_origin_handle_create(req);
1795 ldlm_callback_reply(req, rc);
1797 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1798 req_capsule_set(&req->rq_pill,
1799 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1800 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1802 rc = llog_origin_handle_next_block(req);
1803 ldlm_callback_reply(req, rc);
1805 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1806 req_capsule_set(&req->rq_pill,
1807 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1808 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1810 rc = llog_origin_handle_read_header(req);
1811 ldlm_callback_reply(req, rc);
1813 case LLOG_ORIGIN_HANDLE_CLOSE:
1814 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1816 rc = llog_origin_handle_close(req);
1817 ldlm_callback_reply(req, rc);
1820 CERROR("unknown opcode %u\n",
1821 lustre_msg_get_opc(req->rq_reqmsg));
1822 ldlm_callback_reply(req, -EPROTO);
1826 ns = req->rq_export->exp_obd->obd_namespace;
1827 LASSERT(ns != NULL);
1829 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1831 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1832 if (dlm_req == NULL) {
1833 ldlm_callback_reply(req, -EPROTO);
1837 /* Force a known safe race, send a cancel to the server for a lock
1838 * which the server has already started a blocking callback on. */
1839 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1840 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1841 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1843 CERROR("ldlm_cli_cancel: %d\n", rc);
1846 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1848 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1849 "disappeared\n", dlm_req->lock_handle[0].cookie);
1850 ldlm_callback_reply(req, -EINVAL);
1854 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1855 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1856 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1858 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1859 lock_res_and_lock(lock);
1860 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1861 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1862 /* If somebody cancels lock and cache is already dropped,
1863 * or lock is failed before cp_ast received on client,
1864 * we can tell the server we have no lock. Otherwise, we
1865 * should send cancel after dropping the cache. */
1866 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1867 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1868 (lock->l_flags & LDLM_FL_FAILED)) {
1869 LDLM_DEBUG(lock, "callback on lock "
1870 LPX64" - lock disappeared\n",
1871 dlm_req->lock_handle[0].cookie);
1872 unlock_res_and_lock(lock);
1873 LDLM_LOCK_RELEASE(lock);
1874 ldlm_callback_reply(req, -EINVAL);
1877 /* BL_AST locks are not needed in lru.
1878 * let ldlm_cancel_lru() be fast. */
1879 ldlm_lock_remove_from_lru(lock);
1880 lock->l_flags |= LDLM_FL_BL_AST;
1882 unlock_res_and_lock(lock);
1884 /* We want the ost thread to get this reply so that it can respond
1885 * to ost requests (write cache writeback) that might be triggered
1888 * But we'd also like to be able to indicate in the reply that we're
1889 * cancelling right now, because it's unused, or have an intent result
1890 * in the reply, so we might have to push the responsibility for sending
1891 * the reply down into the AST handlers, alas. */
1893 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1894 case LDLM_BL_CALLBACK:
1895 CDEBUG(D_INODE, "blocking ast\n");
1896 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1897 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1898 ldlm_callback_reply(req, 0);
1899 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1900 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1902 case LDLM_CP_CALLBACK:
1903 CDEBUG(D_INODE, "completion ast\n");
1904 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1905 ldlm_callback_reply(req, 0);
1906 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1908 case LDLM_GL_CALLBACK:
1909 CDEBUG(D_INODE, "glimpse ast\n");
1910 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1911 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1914 LBUG(); /* checked above */
1920 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1925 /* Requests arrive in sender's byte order. The ptlrpc service
1926 * handler has already checked and, if necessary, byte-swapped the
1927 * incoming request message body, but I am responsible for the
1928 * message buffers. */
1930 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1932 if (req->rq_export == NULL) {
1933 struct ldlm_request *dlm_req;
1935 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1936 lustre_msg_get_opc(req->rq_reqmsg),
1937 libcfs_id2str(req->rq_peer),
1938 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1940 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
1941 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1942 dlm_req = req_capsule_client_get(&req->rq_pill,
1944 if (dlm_req != NULL)
1945 ldlm_lock_dump_handle(D_ERROR,
1946 &dlm_req->lock_handle[0]);
1948 ldlm_callback_reply(req, -ENOTCONN);
1952 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1954 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1956 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1957 CDEBUG(D_INODE, "cancel\n");
1958 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1960 rc = ldlm_handle_cancel(req);
1964 case OBD_LOG_CANCEL:
1965 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1966 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1968 rc = llog_origin_handle_cancel(req);
1969 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1971 ldlm_callback_reply(req, rc);
1974 CERROR("invalid opcode %d\n",
1975 lustre_msg_get_opc(req->rq_reqmsg));
1976 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1977 ldlm_callback_reply(req, -EINVAL);
1983 void ldlm_revoke_lock_cb(void *obj, void *data)
1985 struct list_head *rpc_list = data;
1986 struct ldlm_lock *lock = obj;
1988 lock_res_and_lock(lock);
1990 if (lock->l_req_mode != lock->l_granted_mode) {
1991 unlock_res_and_lock(lock);
1995 LASSERT(lock->l_resource);
1996 if (lock->l_resource->lr_type != LDLM_IBITS &&
1997 lock->l_resource->lr_type != LDLM_PLAIN) {
1998 unlock_res_and_lock(lock);
2002 if (lock->l_flags & LDLM_FL_AST_SENT) {
2003 unlock_res_and_lock(lock);
2007 LASSERT(lock->l_blocking_ast);
2008 LASSERT(!lock->l_blocking_lock);
2010 lock->l_flags |= LDLM_FL_AST_SENT;
2011 if (lock->l_export && lock->l_export->exp_lock_hash &&
2012 !hlist_unhashed(&lock->l_exp_hash))
2013 cfs_hash_del(lock->l_export->exp_lock_hash,
2014 &lock->l_remote_handle, &lock->l_exp_hash);
2015 list_add_tail(&lock->l_rk_ast, rpc_list);
2016 LDLM_LOCK_GET(lock);
2018 unlock_res_and_lock(lock);
2021 void ldlm_revoke_export_locks(struct obd_export *exp)
2023 struct list_head rpc_list;
2026 CFS_INIT_LIST_HEAD(&rpc_list);
2027 cfs_hash_for_each_empty(exp->exp_lock_hash,
2028 ldlm_revoke_lock_cb, &rpc_list);
2029 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
2035 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2037 struct ldlm_bl_work_item *blwi = NULL;
2038 static unsigned int num_bl = 0;
2040 spin_lock(&blp->blp_lock);
2041 /* process a request from the blp_list at least every blp_num_threads */
2042 if (!list_empty(&blp->blp_list) &&
2043 (list_empty(&blp->blp_prio_list) || num_bl == 0))
2044 blwi = list_entry(blp->blp_list.next,
2045 struct ldlm_bl_work_item, blwi_entry);
2047 if (!list_empty(&blp->blp_prio_list))
2048 blwi = list_entry(blp->blp_prio_list.next,
2049 struct ldlm_bl_work_item, blwi_entry);
2052 if (++num_bl >= atomic_read(&blp->blp_num_threads))
2054 list_del(&blwi->blwi_entry);
2056 spin_unlock(&blp->blp_lock);
2061 /* This only contains temporary data until the thread starts */
2062 struct ldlm_bl_thread_data {
2063 char bltd_name[CFS_CURPROC_COMM_MAX];
2064 struct ldlm_bl_pool *bltd_blp;
2065 struct completion bltd_comp;
2069 static int ldlm_bl_thread_main(void *arg);
2071 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2073 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2076 init_completion(&bltd.bltd_comp);
2077 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
2079 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2080 atomic_read(&blp->blp_num_threads), rc);
2083 wait_for_completion(&bltd.bltd_comp);
2088 static int ldlm_bl_thread_main(void *arg)
2090 struct ldlm_bl_pool *blp;
2094 struct ldlm_bl_thread_data *bltd = arg;
2096 blp = bltd->bltd_blp;
2098 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
2099 atomic_inc(&blp->blp_busy_threads);
2101 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2102 "ldlm_bl_%02d", bltd->bltd_num);
2103 cfs_daemonize(bltd->bltd_name);
2105 complete(&bltd->bltd_comp);
2106 /* cannot use bltd after this, it is only on caller's stack */
2110 struct l_wait_info lwi = { 0 };
2111 struct ldlm_bl_work_item *blwi = NULL;
2113 blwi = ldlm_bl_get_work(blp);
2118 atomic_dec(&blp->blp_busy_threads);
2119 l_wait_event_exclusive(blp->blp_waitq,
2120 (blwi = ldlm_bl_get_work(blp)) != NULL,
2122 busy = atomic_inc_return(&blp->blp_busy_threads);
2124 if (blwi->blwi_ns == NULL)
2125 /* added by ldlm_cleanup() */
2128 /* Not fatal if racy and have a few too many threads */
2129 if (unlikely(busy < blp->blp_max_threads &&
2130 busy >= atomic_read(&blp->blp_num_threads)))
2131 /* discard the return value, we tried */
2132 ldlm_bl_thread_start(blp);
2134 if (blwi->blwi_ns == NULL)
2135 /* added by ldlm_cleanup() */
2139 if (blwi->blwi_count) {
2140 /* The special case when we cancel locks in lru
2141 * asynchronously, we pass the list of locks here.
2142 * Thus lock is marked LDLM_FL_CANCELING, and already
2143 * canceled locally. */
2144 ldlm_cli_cancel_list(&blwi->blwi_head,
2145 blwi->blwi_count, NULL, 0);
2147 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2150 OBD_FREE(blwi, sizeof(*blwi));
2153 atomic_dec(&blp->blp_busy_threads);
2154 atomic_dec(&blp->blp_num_threads);
2155 complete(&blp->blp_comp);
2161 static int ldlm_setup(void);
2162 static int ldlm_cleanup(void);
2164 int ldlm_get_ref(void)
2168 mutex_down(&ldlm_ref_sem);
2169 if (++ldlm_refcount == 1) {
2174 mutex_up(&ldlm_ref_sem);
2179 void ldlm_put_ref(void)
2182 mutex_down(&ldlm_ref_sem);
2183 if (ldlm_refcount == 1) {
2184 int rc = ldlm_cleanup();
2186 CERROR("ldlm_cleanup failed: %d\n", rc);
2192 mutex_up(&ldlm_ref_sem);
2198 * Export handle<->lock hash operations.
2201 ldlm_export_lock_hash(cfs_hash_t *hs, void *key, unsigned mask)
2203 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2207 ldlm_export_lock_key(struct hlist_node *hnode)
2209 struct ldlm_lock *lock;
2212 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2213 RETURN(&lock->l_remote_handle);
2217 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2220 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2224 ldlm_export_lock_get(struct hlist_node *hnode)
2226 struct ldlm_lock *lock;
2229 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2230 LDLM_LOCK_GET(lock);
2236 ldlm_export_lock_put(struct hlist_node *hnode)
2238 struct ldlm_lock *lock;
2241 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2242 LDLM_LOCK_RELEASE(lock);
2247 static cfs_hash_ops_t ldlm_export_lock_ops = {
2248 .hs_hash = ldlm_export_lock_hash,
2249 .hs_key = ldlm_export_lock_key,
2250 .hs_compare = ldlm_export_lock_compare,
2251 .hs_get = ldlm_export_lock_get,
2252 .hs_put = ldlm_export_lock_put
2255 int ldlm_init_export(struct obd_export *exp)
2259 exp->exp_lock_hash =
2260 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2261 HASH_EXP_LOCK_CUR_BITS, HASH_EXP_LOCK_MAX_BITS,
2262 &ldlm_export_lock_ops, CFS_HASH_REHASH);
2264 if (!exp->exp_lock_hash)
2269 EXPORT_SYMBOL(ldlm_init_export);
2271 void ldlm_destroy_export(struct obd_export *exp)
2274 cfs_hash_destroy(exp->exp_lock_hash);
2275 exp->exp_lock_hash = NULL;
2278 EXPORT_SYMBOL(ldlm_destroy_export);
2280 static int ldlm_setup(void)
2282 struct ldlm_bl_pool *blp;
2284 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2285 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2291 if (ldlm_state != NULL)
2294 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2295 if (ldlm_state == NULL)
2299 rc = ldlm_proc_setup();
2305 if (ldlm_num_threads) {
2306 /* If ldlm_num_threads is set, it is the min and the max. */
2307 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2308 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2309 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2310 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2311 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2315 ldlm_state->ldlm_cb_service =
2316 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2317 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2318 LDLM_CB_REPLY_PORTAL, 2,
2319 ldlm_callback_handler, "ldlm_cbd",
2320 ldlm_svc_proc_dir, NULL,
2321 ldlm_min_threads, ldlm_max_threads,
2323 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2325 if (!ldlm_state->ldlm_cb_service) {
2326 CERROR("failed to start service\n");
2327 GOTO(out_proc, rc = -ENOMEM);
2330 ldlm_state->ldlm_cancel_service =
2331 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2332 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2333 LDLM_CANCEL_REPLY_PORTAL, 6,
2334 ldlm_cancel_handler, "ldlm_canceld",
2335 ldlm_svc_proc_dir, NULL,
2336 ldlm_min_threads, ldlm_max_threads,
2338 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2341 if (!ldlm_state->ldlm_cancel_service) {
2342 CERROR("failed to start service\n");
2343 GOTO(out_proc, rc = -ENOMEM);
2346 OBD_ALLOC(blp, sizeof(*blp));
2348 GOTO(out_proc, rc = -ENOMEM);
2349 ldlm_state->ldlm_bl_pool = blp;
2351 spin_lock_init(&blp->blp_lock);
2352 CFS_INIT_LIST_HEAD(&blp->blp_list);
2353 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2354 cfs_waitq_init(&blp->blp_waitq);
2355 atomic_set(&blp->blp_num_threads, 0);
2356 atomic_set(&blp->blp_busy_threads, 0);
2357 blp->blp_min_threads = ldlm_min_threads;
2358 blp->blp_max_threads = ldlm_max_threads;
2361 for (i = 0; i < blp->blp_min_threads; i++) {
2362 rc = ldlm_bl_thread_start(blp);
2364 GOTO(out_thread, rc);
2367 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2369 GOTO(out_thread, rc);
2371 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2373 GOTO(out_thread, rc);
2375 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2376 expired_lock_thread.elt_state = ELT_STOPPED;
2377 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2379 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2380 spin_lock_init(&waiting_locks_spinlock);
2381 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2383 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2385 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2386 GOTO(out_thread, rc);
2389 wait_event(expired_lock_thread.elt_waitq,
2390 expired_lock_thread.elt_state == ELT_READY);
2394 rc = ldlm_pools_init();
2396 GOTO(out_thread, rc);
2402 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2403 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2408 ldlm_proc_cleanup();
2411 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2416 static int ldlm_cleanup(void)
2419 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2423 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2424 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2425 CERROR("ldlm still has namespaces; clean these up first.\n");
2426 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2427 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2436 while (atomic_read(&blp->blp_num_threads) > 0) {
2437 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2439 init_completion(&blp->blp_comp);
2441 spin_lock(&blp->blp_lock);
2442 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2443 cfs_waitq_signal(&blp->blp_waitq);
2444 spin_unlock(&blp->blp_lock);
2446 wait_for_completion(&blp->blp_comp);
2448 OBD_FREE(blp, sizeof(*blp));
2450 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2451 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2452 ldlm_proc_cleanup();
2454 expired_lock_thread.elt_state = ELT_TERMINATE;
2455 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2456 wait_event(expired_lock_thread.elt_waitq,
2457 expired_lock_thread.elt_state == ELT_STOPPED);
2459 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2460 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2463 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2469 int __init ldlm_init(void)
2471 init_mutex(&ldlm_ref_sem);
2472 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2473 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2474 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2475 sizeof(struct ldlm_resource), 0,
2476 SLAB_HWCACHE_ALIGN);
2477 if (ldlm_resource_slab == NULL)
2480 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2481 sizeof(struct ldlm_lock), 0,
2482 SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
2483 if (ldlm_lock_slab == NULL) {
2484 cfs_mem_cache_destroy(ldlm_resource_slab);
2488 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2489 sizeof(struct ldlm_interval),
2490 0, SLAB_HWCACHE_ALIGN);
2491 if (ldlm_interval_slab == NULL) {
2492 cfs_mem_cache_destroy(ldlm_resource_slab);
2493 cfs_mem_cache_destroy(ldlm_lock_slab);
2496 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2497 class_export_dump_hook = ldlm_dump_export_locks;
2502 void __exit ldlm_exit(void)
2506 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2507 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2508 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2510 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2511 * synchronize_rcu() to wait a grace period elapsed, so that
2512 * ldlm_lock_free() get a chance to be called. */
2515 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2516 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2517 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2518 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2522 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2525 EXPORT_SYMBOL(ldlm_get_processing_policy);
2526 EXPORT_SYMBOL(ldlm_lock2desc);
2527 EXPORT_SYMBOL(ldlm_register_intent);
2528 EXPORT_SYMBOL(ldlm_lockname);
2529 EXPORT_SYMBOL(ldlm_typename);
2530 EXPORT_SYMBOL(ldlm_lock2handle);
2531 EXPORT_SYMBOL(__ldlm_handle2lock);
2532 EXPORT_SYMBOL(ldlm_lock_get);
2533 EXPORT_SYMBOL(ldlm_lock_put);
2534 EXPORT_SYMBOL(ldlm_lock_match);
2535 EXPORT_SYMBOL(ldlm_lock_cancel);
2536 EXPORT_SYMBOL(ldlm_lock_addref);
2537 EXPORT_SYMBOL(ldlm_lock_addref_try);
2538 EXPORT_SYMBOL(ldlm_lock_decref);
2539 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2540 EXPORT_SYMBOL(ldlm_lock_change_resource);
2541 EXPORT_SYMBOL(ldlm_it2str);
2542 EXPORT_SYMBOL(ldlm_lock_dump);
2543 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2544 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2545 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2546 EXPORT_SYMBOL(ldlm_lock_allow_match);
2547 EXPORT_SYMBOL(ldlm_lock_downgrade);
2548 EXPORT_SYMBOL(ldlm_lock_convert);
2550 /* ldlm_request.c */
2551 EXPORT_SYMBOL(ldlm_completion_ast_async);
2552 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2553 EXPORT_SYMBOL(ldlm_completion_ast);
2554 EXPORT_SYMBOL(ldlm_blocking_ast);
2555 EXPORT_SYMBOL(ldlm_glimpse_ast);
2556 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2557 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2558 EXPORT_SYMBOL(ldlm_prep_elc_req);
2559 EXPORT_SYMBOL(ldlm_cli_convert);
2560 EXPORT_SYMBOL(ldlm_cli_enqueue);
2561 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2562 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2563 EXPORT_SYMBOL(ldlm_cli_cancel);
2564 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2565 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2566 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2567 EXPORT_SYMBOL(ldlm_replay_locks);
2568 EXPORT_SYMBOL(ldlm_resource_foreach);
2569 EXPORT_SYMBOL(ldlm_namespace_foreach);
2570 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2571 EXPORT_SYMBOL(ldlm_resource_iterate);
2572 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2573 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2576 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2577 EXPORT_SYMBOL(ldlm_server_completion_ast);
2578 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2579 EXPORT_SYMBOL(ldlm_handle_enqueue);
2580 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2581 EXPORT_SYMBOL(ldlm_handle_cancel);
2582 EXPORT_SYMBOL(ldlm_request_cancel);
2583 EXPORT_SYMBOL(ldlm_handle_convert);
2584 EXPORT_SYMBOL(ldlm_handle_convert0);
2585 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2586 EXPORT_SYMBOL(ldlm_get_ref);
2587 EXPORT_SYMBOL(ldlm_put_ref);
2588 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2589 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2591 /* ldlm_resource.c */
2592 EXPORT_SYMBOL(ldlm_namespace_new);
2593 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2594 EXPORT_SYMBOL(ldlm_namespace_free);
2595 EXPORT_SYMBOL(ldlm_namespace_dump);
2596 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2597 EXPORT_SYMBOL(ldlm_resource_get);
2598 EXPORT_SYMBOL(ldlm_resource_putref);
2599 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2602 EXPORT_SYMBOL(client_import_add_conn);
2603 EXPORT_SYMBOL(client_import_del_conn);
2604 EXPORT_SYMBOL(client_obd_setup);
2605 EXPORT_SYMBOL(client_obd_cleanup);
2606 EXPORT_SYMBOL(client_connect_import);
2607 EXPORT_SYMBOL(client_disconnect_export);
2608 EXPORT_SYMBOL(server_disconnect_export);
2609 EXPORT_SYMBOL(target_stop_recovery_thread);
2610 EXPORT_SYMBOL(target_handle_connect);
2611 EXPORT_SYMBOL(target_cleanup_recovery);
2612 EXPORT_SYMBOL(target_destroy_export);
2613 EXPORT_SYMBOL(target_cancel_recovery_timer);
2614 EXPORT_SYMBOL(target_send_reply);
2615 EXPORT_SYMBOL(target_queue_recovery_request);
2616 EXPORT_SYMBOL(target_handle_ping);
2617 EXPORT_SYMBOL(target_pack_pool_reply);
2618 EXPORT_SYMBOL(target_handle_disconnect);
2621 EXPORT_SYMBOL(lock_res_and_lock);
2622 EXPORT_SYMBOL(unlock_res_and_lock);