1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lockd.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 # define EXPORT_SYMTAB
45 #define DEBUG_SUBSYSTEM S_LDLM
48 # include <libcfs/libcfs.h>
50 # include <liblustre.h>
53 #include <lustre_dlm.h>
54 #include <obd_class.h>
55 #include <libcfs/list.h>
56 #include "ldlm_internal.h"
59 static int ldlm_num_threads;
60 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
61 "number of DLM service threads to start");
64 extern cfs_mem_cache_t *ldlm_resource_slab;
65 extern cfs_mem_cache_t *ldlm_lock_slab;
66 static cfs_semaphore_t ldlm_ref_sem;
67 static int ldlm_refcount;
71 static struct ldlm_state *ldlm_state;
73 inline cfs_time_t round_timeout(cfs_time_t timeout)
75 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
78 /* timeout for initial callback (AST) reply (bz10399) */
79 static inline unsigned int ldlm_get_rq_timeout(void)
82 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
84 return timeout < 1 ? 1 : timeout;
88 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
89 static cfs_spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
90 static cfs_list_t waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
93 static struct expired_lock_thread {
94 cfs_waitq_t elt_waitq;
97 cfs_list_t elt_expired_locks;
98 } expired_lock_thread;
101 #define ELT_STOPPED 0
103 #define ELT_TERMINATE 2
105 struct ldlm_bl_pool {
106 cfs_spinlock_t blp_lock;
109 * blp_prio_list is used for callbacks that should be handled
110 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
113 cfs_list_t blp_prio_list;
116 * blp_list is used for all other callbacks which are likely
117 * to take longer to process.
121 cfs_waitq_t blp_waitq;
122 cfs_completion_t blp_comp;
123 cfs_atomic_t blp_num_threads;
124 cfs_atomic_t blp_busy_threads;
129 struct ldlm_bl_work_item {
130 cfs_list_t blwi_entry;
131 struct ldlm_namespace *blwi_ns;
132 struct ldlm_lock_desc blwi_ld;
133 struct ldlm_lock *blwi_lock;
134 cfs_list_t blwi_head;
136 cfs_completion_t blwi_comp;
137 cfs_atomic_t blwi_ref_count;
141 static inline void ldlm_bl_work_item_get(struct ldlm_bl_work_item *blwi)
143 cfs_atomic_inc(&blwi->blwi_ref_count);
146 static inline void ldlm_bl_work_item_put(struct ldlm_bl_work_item *blwi)
148 if (cfs_atomic_dec_and_test(&blwi->blwi_ref_count))
149 OBD_FREE(blwi, sizeof(*blwi));
152 static inline int have_expired_locks(void)
157 cfs_spin_lock_bh(&waiting_locks_spinlock);
158 need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
159 cfs_spin_unlock_bh(&waiting_locks_spinlock);
164 static int expired_lock_main(void *arg)
166 cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
167 struct l_wait_info lwi = { 0 };
171 cfs_daemonize("ldlm_elt");
173 expired_lock_thread.elt_state = ELT_READY;
174 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
177 l_wait_event(expired_lock_thread.elt_waitq,
178 have_expired_locks() ||
179 expired_lock_thread.elt_state == ELT_TERMINATE,
182 cfs_spin_lock_bh(&waiting_locks_spinlock);
183 if (expired_lock_thread.elt_dump) {
184 cfs_spin_unlock_bh(&waiting_locks_spinlock);
186 /* from waiting_locks_callback, but not in timer */
187 libcfs_debug_dumplog();
188 libcfs_run_lbug_upcall(__FILE__,
189 "waiting_locks_callback",
190 expired_lock_thread.elt_dump);
192 cfs_spin_lock_bh(&waiting_locks_spinlock);
193 expired_lock_thread.elt_dump = 0;
198 while (!cfs_list_empty(expired)) {
199 struct obd_export *export;
200 struct ldlm_lock *lock;
202 lock = cfs_list_entry(expired->next, struct ldlm_lock,
204 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
205 (void *)lock >= LP_POISON) {
206 cfs_spin_unlock_bh(&waiting_locks_spinlock);
207 CERROR("free lock on elt list %p\n", lock);
210 cfs_list_del_init(&lock->l_pending_chain);
211 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
212 (void *)lock->l_export >= LP_POISON) {
213 CERROR("lock with free export on elt list %p\n",
215 lock->l_export = NULL;
216 LDLM_ERROR(lock, "free export");
217 /* release extra ref grabbed by
218 * ldlm_add_waiting_lock() or
219 * ldlm_failed_ast() */
220 LDLM_LOCK_RELEASE(lock);
223 export = class_export_lock_get(lock->l_export, lock);
224 cfs_spin_unlock_bh(&waiting_locks_spinlock);
227 class_fail_export(export);
228 class_export_lock_put(export, lock);
230 /* release extra ref grabbed by ldlm_add_waiting_lock()
231 * or ldlm_failed_ast() */
232 LDLM_LOCK_RELEASE(lock);
234 cfs_spin_lock_bh(&waiting_locks_spinlock);
236 cfs_spin_unlock_bh(&waiting_locks_spinlock);
238 if (do_dump && obd_dump_on_eviction) {
239 CERROR("dump the log upon eviction\n");
240 libcfs_debug_dumplog();
243 if (expired_lock_thread.elt_state == ELT_TERMINATE)
247 expired_lock_thread.elt_state = ELT_STOPPED;
248 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
252 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
255 * Check if there is a request in the export request list
256 * which prevents the lock canceling.
258 static int ldlm_lock_busy(struct ldlm_lock *lock)
260 struct ptlrpc_request *req;
264 if (lock->l_export == NULL)
267 cfs_spin_lock(&lock->l_export->exp_lock);
268 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
270 if (req->rq_ops->hpreq_lock_match) {
271 match = req->rq_ops->hpreq_lock_match(req, lock);
276 cfs_spin_unlock(&lock->l_export->exp_lock);
280 /* This is called from within a timer interrupt and cannot schedule */
281 static void waiting_locks_callback(unsigned long unused)
283 struct ldlm_lock *lock, *last = NULL;
286 cfs_spin_lock_bh(&waiting_locks_spinlock);
287 while (!cfs_list_empty(&waiting_locks_list)) {
288 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
290 if (cfs_time_after(lock->l_callback_timeout,
291 cfs_time_current()) ||
292 (lock->l_req_mode == LCK_GROUP))
295 if (ptlrpc_check_suspend()) {
296 /* there is a case when we talk to one mds, holding
297 * lock from another mds. this way we easily can get
298 * here, if second mds is being recovered. so, we
299 * suspend timeouts. bug 6019 */
301 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
302 lock->l_export->exp_client_uuid.uuid,
303 lock->l_export->exp_connection->c_remote_uuid.uuid,
304 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
306 cfs_list_del_init(&lock->l_pending_chain);
307 cfs_spin_unlock_bh(&waiting_locks_spinlock);
308 ldlm_add_waiting_lock(lock);
312 /* if timeout overlaps the activation time of suspended timeouts
313 * then extend it to give a chance for client to reconnect */
314 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
315 cfs_time_seconds(obd_timeout)/2),
316 ptlrpc_suspend_wakeup_time())) {
317 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
318 lock->l_export->exp_client_uuid.uuid,
319 lock->l_export->exp_connection->c_remote_uuid.uuid,
320 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
322 cfs_list_del_init(&lock->l_pending_chain);
323 cfs_spin_unlock_bh(&waiting_locks_spinlock);
324 ldlm_add_waiting_lock(lock);
328 /* Check if we need to prolong timeout */
329 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
330 ldlm_lock_busy(lock)) {
333 if (lock->l_pending_chain.next == &waiting_locks_list)
338 cfs_spin_unlock_bh(&waiting_locks_spinlock);
339 LDLM_DEBUG(lock, "prolong the busy lock");
340 ldlm_refresh_waiting_lock(lock,
341 ldlm_get_enq_timeout(lock));
342 cfs_spin_lock_bh(&waiting_locks_spinlock);
345 LDLM_LOCK_RELEASE(lock);
349 LDLM_LOCK_RELEASE(lock);
352 lock->l_resource->lr_namespace->ns_timeouts++;
353 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
354 "evicting client at %s ",
355 cfs_time_current_sec()- lock->l_last_activity,
357 lock->l_export->exp_connection->c_peer.nid));
361 /* no needs to take an extra ref on the lock since it was in
362 * the waiting_locks_list and ldlm_add_waiting_lock()
363 * already grabbed a ref */
364 cfs_list_del(&lock->l_pending_chain);
365 cfs_list_add(&lock->l_pending_chain,
366 &expired_lock_thread.elt_expired_locks);
369 if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
370 if (obd_dump_on_timeout)
371 expired_lock_thread.elt_dump = __LINE__;
373 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
377 * Make sure the timer will fire again if we have any locks
380 if (!cfs_list_empty(&waiting_locks_list)) {
381 cfs_time_t timeout_rounded;
382 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
384 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
385 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
387 cfs_spin_unlock_bh(&waiting_locks_spinlock);
391 * Indicate that we're waiting for a client to call us back cancelling a given
392 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
393 * timer to fire appropriately. (We round up to the next second, to avoid
394 * floods of timer firings during periods of high lock contention and traffic).
395 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
396 * if it has been added to the waiting list (1 is returned).
398 * Called with the namespace lock held.
400 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
403 cfs_time_t timeout_rounded;
405 if (!cfs_list_empty(&lock->l_pending_chain))
408 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
409 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
412 timeout = cfs_time_shift(seconds);
413 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
414 lock->l_callback_timeout = timeout;
416 timeout_rounded = round_timeout(lock->l_callback_timeout);
418 if (cfs_time_before(timeout_rounded,
419 cfs_timer_deadline(&waiting_locks_timer)) ||
420 !cfs_timer_is_armed(&waiting_locks_timer)) {
421 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
423 /* if the new lock has a shorter timeout than something earlier on
424 the list, we'll wait the longer amount of time; no big deal. */
426 cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
430 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
433 int timeout = ldlm_get_enq_timeout(lock);
435 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
437 cfs_spin_lock_bh(&waiting_locks_spinlock);
438 if (lock->l_destroyed) {
439 static cfs_time_t next;
440 cfs_spin_unlock_bh(&waiting_locks_spinlock);
441 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
442 if (cfs_time_after(cfs_time_current(), next)) {
443 next = cfs_time_shift(14400);
444 libcfs_debug_dumpstack(NULL);
449 ret = __ldlm_add_waiting_lock(lock, timeout);
451 /* grab ref on the lock if it has been added to the
454 cfs_spin_unlock_bh(&waiting_locks_spinlock);
456 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
457 ret == 0 ? "not re-" : "", timeout,
458 AT_OFF ? "off" : "on");
463 * Remove a lock from the pending list, likely because it had its cancellation
464 * callback arrive without incident. This adjusts the lock-timeout timer if
465 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
466 * As done by ldlm_del_waiting_lock(), the caller must release the lock
467 * reference when the lock is removed from any list (1 is returned).
469 * Called with namespace lock held.
471 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
473 cfs_list_t *list_next;
475 if (cfs_list_empty(&lock->l_pending_chain))
478 list_next = lock->l_pending_chain.next;
479 if (lock->l_pending_chain.prev == &waiting_locks_list) {
480 /* Removing the head of the list, adjust timer. */
481 if (list_next == &waiting_locks_list) {
482 /* No more, just cancel. */
483 cfs_timer_disarm(&waiting_locks_timer);
485 struct ldlm_lock *next;
486 next = cfs_list_entry(list_next, struct ldlm_lock,
488 cfs_timer_arm(&waiting_locks_timer,
489 round_timeout(next->l_callback_timeout));
492 cfs_list_del_init(&lock->l_pending_chain);
497 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
501 if (lock->l_export == NULL) {
502 /* We don't have a "waiting locks list" on clients. */
503 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
507 cfs_spin_lock_bh(&waiting_locks_spinlock);
508 ret = __ldlm_del_waiting_lock(lock);
509 cfs_spin_unlock_bh(&waiting_locks_spinlock);
511 /* release lock ref if it has indeed been removed
513 LDLM_LOCK_RELEASE(lock);
515 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
522 * Called with namespace lock held.
524 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
526 if (lock->l_export == NULL) {
527 /* We don't have a "waiting locks list" on clients. */
528 LDLM_DEBUG(lock, "client lock: no-op");
532 cfs_spin_lock_bh(&waiting_locks_spinlock);
534 if (cfs_list_empty(&lock->l_pending_chain)) {
535 cfs_spin_unlock_bh(&waiting_locks_spinlock);
536 LDLM_DEBUG(lock, "wasn't waiting");
540 /* we remove/add the lock to the waiting list, so no needs to
541 * release/take a lock reference */
542 __ldlm_del_waiting_lock(lock);
543 __ldlm_add_waiting_lock(lock, timeout);
544 cfs_spin_unlock_bh(&waiting_locks_spinlock);
546 LDLM_DEBUG(lock, "refreshed");
549 #else /* !__KERNEL__ */
551 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
553 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
557 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
562 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
566 #endif /* __KERNEL__ */
568 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
569 const char *ast_type)
571 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
572 "to a lock %s callback time out: rc %d\n",
573 lock->l_export->exp_obd->obd_name,
574 obd_export_nid2str(lock->l_export), ast_type, rc);
576 if (obd_dump_on_timeout)
577 libcfs_debug_dumplog();
579 cfs_spin_lock_bh(&waiting_locks_spinlock);
580 if (__ldlm_del_waiting_lock(lock) == 0)
581 /* the lock was not in any list, grab an extra ref before adding
582 * the lock to the expired list */
584 cfs_list_add(&lock->l_pending_chain,
585 &expired_lock_thread.elt_expired_locks);
586 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
587 cfs_spin_unlock_bh(&waiting_locks_spinlock);
589 class_fail_export(lock->l_export);
593 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
594 struct ptlrpc_request *req, int rc,
595 const char *ast_type)
597 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
599 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
600 LASSERT(lock->l_export);
601 if (lock->l_export->exp_libclient) {
602 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
603 " timeout, just cancelling lock", ast_type,
604 libcfs_nid2str(peer.nid));
605 ldlm_lock_cancel(lock);
607 } else if (lock->l_flags & LDLM_FL_CANCEL) {
608 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
609 "cancel was received (AST reply lost?)",
610 ast_type, libcfs_nid2str(peer.nid));
611 ldlm_lock_cancel(lock);
614 ldlm_del_waiting_lock(lock);
615 ldlm_failed_ast(lock, rc, ast_type);
619 LDLM_DEBUG(lock, "client (nid %s) returned %d"
620 " from %s AST - normal race",
621 libcfs_nid2str(peer.nid),
623 lustre_msg_get_status(req->rq_repmsg) : -1,
626 LDLM_ERROR(lock, "client (nid %s) returned %d "
627 "from %s AST", libcfs_nid2str(peer.nid),
628 (req->rq_repmsg != NULL) ?
629 lustre_msg_get_status(req->rq_repmsg) : 0,
631 ldlm_lock_cancel(lock);
632 /* Server-side AST functions are called from ldlm_reprocess_all,
633 * which needs to be told to please restart its reprocessing. */
640 static int ldlm_cb_interpret(const struct lu_env *env,
641 struct ptlrpc_request *req, void *data, int rc)
643 struct ldlm_cb_set_arg *arg;
644 struct ldlm_lock *lock;
647 LASSERT(data != NULL);
649 arg = req->rq_async_args.pointer_arg[0];
650 lock = req->rq_async_args.pointer_arg[1];
651 LASSERT(lock != NULL);
653 /* If client canceled the lock but the cancel has not
654 * been received yet, we need to update lvbo to have the
655 * proper attributes cached. */
656 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
657 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
658 rc = ldlm_handle_ast_error(lock, req, rc,
659 arg->type == LDLM_BL_CALLBACK
660 ? "blocking" : "completion");
663 LDLM_LOCK_RELEASE(lock);
666 cfs_atomic_set(&arg->restart, 1);
671 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
672 struct ldlm_cb_set_arg *arg,
673 struct ldlm_lock *lock,
679 if (unlikely(instant_cancel)) {
680 rc = ptl_send_rpc(req, 1);
681 ptlrpc_req_finished(req);
683 /* If we cancelled the lock, we need to restart
684 * ldlm_reprocess_queue */
685 cfs_atomic_set(&arg->restart, 1);
688 ptlrpc_set_add_req(arg->set, req);
695 * Check if there are requests in the export request list which prevent
696 * the lock canceling and make these requests high priority ones.
698 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
700 struct ptlrpc_request *req;
703 if (lock->l_export == NULL) {
704 LDLM_DEBUG(lock, "client lock: no-op");
708 cfs_spin_lock(&lock->l_export->exp_lock);
709 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
711 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
712 req->rq_ops->hpreq_lock_match(req, lock))
713 ptlrpc_hpreq_reorder(req);
715 cfs_spin_unlock(&lock->l_export->exp_lock);
720 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
721 * enqueued server lock conflicts with given one.
723 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
724 * to wait for client response.
726 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
727 struct ldlm_lock_desc *desc,
728 void *data, int flag)
730 struct ldlm_cb_set_arg *arg = data;
731 struct ldlm_request *body;
732 struct ptlrpc_request *req;
733 int instant_cancel = 0;
737 if (flag == LDLM_CB_CANCELING)
738 /* Don't need to do anything here. */
742 LASSERT(data != NULL);
743 if (lock->l_export->exp_obd->obd_recovering != 0) {
744 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
745 ldlm_lock_dump(D_ERROR, lock, 0);
748 ldlm_lock_reorder_req(lock);
750 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
751 &RQF_LDLM_BL_CALLBACK,
752 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
756 req->rq_async_args.pointer_arg[0] = arg;
757 req->rq_async_args.pointer_arg[1] = lock;
758 req->rq_interpret_reply = ldlm_cb_interpret;
759 req->rq_no_resend = 1;
761 lock_res(lock->l_resource);
762 if (lock->l_granted_mode != lock->l_req_mode) {
763 /* this blocking AST will be communicated as part of the
764 * completion AST instead */
765 unlock_res(lock->l_resource);
766 ptlrpc_req_finished(req);
767 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
771 if (lock->l_destroyed) {
772 /* What's the point? */
773 unlock_res(lock->l_resource);
774 ptlrpc_req_finished(req);
778 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
781 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
782 body->lock_handle[0] = lock->l_remote_handle;
783 body->lock_desc = *desc;
784 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
786 LDLM_DEBUG(lock, "server preparing blocking AST");
788 ptlrpc_request_set_replen(req);
789 if (instant_cancel) {
790 unlock_res(lock->l_resource);
791 ldlm_lock_cancel(lock);
793 LASSERT(lock->l_granted_mode == lock->l_req_mode);
794 ldlm_add_waiting_lock(lock);
795 unlock_res(lock->l_resource);
798 req->rq_send_state = LUSTRE_IMP_FULL;
799 /* ptlrpc_request_alloc_pack already set timeout */
801 req->rq_timeout = ldlm_get_rq_timeout();
803 if (lock->l_export && lock->l_export->exp_nid_stats &&
804 lock->l_export->exp_nid_stats->nid_ldlm_stats)
805 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
806 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
808 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
813 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
815 struct ldlm_cb_set_arg *arg = data;
816 struct ldlm_request *body;
817 struct ptlrpc_request *req;
818 long total_enqueue_wait;
819 int instant_cancel = 0;
823 LASSERT(lock != NULL);
824 LASSERT(data != NULL);
826 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
827 lock->l_last_activity);
829 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
830 &RQF_LDLM_CP_CALLBACK);
834 lock_res_and_lock(lock);
835 if (lock->l_resource->lr_lvb_len)
836 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
837 lock->l_resource->lr_lvb_len);
838 unlock_res_and_lock(lock);
840 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
842 ptlrpc_request_free(req);
846 req->rq_async_args.pointer_arg[0] = arg;
847 req->rq_async_args.pointer_arg[1] = lock;
848 req->rq_interpret_reply = ldlm_cb_interpret;
849 req->rq_no_resend = 1;
850 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
852 body->lock_handle[0] = lock->l_remote_handle;
853 body->lock_flags = flags;
854 ldlm_lock2desc(lock, &body->lock_desc);
855 if (lock->l_resource->lr_lvb_len) {
856 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
858 lock_res_and_lock(lock);
859 memcpy(lvb, lock->l_resource->lr_lvb_data,
860 lock->l_resource->lr_lvb_len);
861 unlock_res_and_lock(lock);
864 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
867 /* Server-side enqueue wait time estimate, used in
868 __ldlm_add_waiting_lock to set future enqueue timers */
869 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
870 at_measured(&lock->l_resource->lr_namespace->ns_at_estimate,
873 /* bz18618. Don't add lock enqueue time we spend waiting for a
874 previous callback to fail. Locks waiting legitimately will
875 get extended by ldlm_refresh_waiting_lock regardless of the
876 estimate, so it's okay to underestimate here. */
877 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
878 "It is likely that a previous callback timed out.",
880 at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
882 ptlrpc_request_set_replen(req);
884 req->rq_send_state = LUSTRE_IMP_FULL;
885 /* ptlrpc_request_pack already set timeout */
887 req->rq_timeout = ldlm_get_rq_timeout();
889 /* We only send real blocking ASTs after the lock is granted */
890 lock_res_and_lock(lock);
891 if (lock->l_flags & LDLM_FL_AST_SENT) {
892 body->lock_flags |= LDLM_FL_AST_SENT;
893 /* copy ast flags like LDLM_FL_DISCARD_DATA */
894 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
896 /* We might get here prior to ldlm_handle_enqueue setting
897 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
898 * into waiting list, but this is safe and similar code in
899 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
900 * that would not only cancel the lock, but will also remove
901 * it from waiting list */
902 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
903 unlock_res_and_lock(lock);
904 ldlm_lock_cancel(lock);
906 lock_res_and_lock(lock);
908 /* start the lock-timeout clock */
909 ldlm_add_waiting_lock(lock);
912 unlock_res_and_lock(lock);
914 if (lock->l_export && lock->l_export->exp_nid_stats &&
915 lock->l_export->exp_nid_stats->nid_ldlm_stats)
916 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
917 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
919 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
924 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
926 struct ldlm_resource *res = lock->l_resource;
927 struct ldlm_request *body;
928 struct ptlrpc_request *req;
932 LASSERT(lock != NULL);
934 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
935 &RQF_LDLM_GL_CALLBACK,
936 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
941 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
942 body->lock_handle[0] = lock->l_remote_handle;
943 ldlm_lock2desc(lock, &body->lock_desc);
945 lock_res_and_lock(lock);
946 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
947 lock->l_resource->lr_lvb_len);
948 unlock_res_and_lock(lock);
949 res = lock->l_resource;
950 ptlrpc_request_set_replen(req);
953 req->rq_send_state = LUSTRE_IMP_FULL;
954 /* ptlrpc_request_alloc_pack already set timeout */
956 req->rq_timeout = ldlm_get_rq_timeout();
958 if (lock->l_export && lock->l_export->exp_nid_stats &&
959 lock->l_export->exp_nid_stats->nid_ldlm_stats)
960 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
961 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
963 rc = ptlrpc_queue_wait(req);
964 if (rc == -ELDLM_NO_LOCK_DATA)
965 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
967 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
969 rc = ldlm_res_lvbo_update(res, req, 1);
971 ptlrpc_req_finished(req);
973 ldlm_reprocess_all(res);
979 extern unsigned long long lu_time_stamp_get(void);
981 #define lu_time_stamp_get() time(NULL)
984 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
985 struct lprocfs_stats *srv_stats)
987 int lock_type = 0, op = 0;
989 lock_type = dlm_req->lock_desc.l_resource.lr_type;
993 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
996 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
997 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
999 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1002 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1005 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1013 lprocfs_counter_incr(srv_stats, op);
1019 * Main server-side entry point into LDLM. This is called by ptlrpc service
1020 * threads to carry out client lock enqueueing requests.
1022 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1023 struct ptlrpc_request *req,
1024 const struct ldlm_request *dlm_req,
1025 const struct ldlm_callback_suite *cbs)
1027 struct ldlm_reply *dlm_rep;
1029 ldlm_error_t err = ELDLM_OK;
1030 struct ldlm_lock *lock = NULL;
1031 void *cookie = NULL;
1035 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1037 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1038 flags = dlm_req->lock_flags;
1040 LASSERT(req->rq_export);
1042 if (req->rq_rqbd->rqbd_service->srv_stats)
1043 ldlm_svc_get_eopc(dlm_req,
1044 req->rq_rqbd->rqbd_service->srv_stats);
1046 if (req->rq_export && req->rq_export->exp_nid_stats &&
1047 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1048 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1049 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1051 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1052 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1053 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1054 dlm_req->lock_desc.l_resource.lr_type);
1055 GOTO(out, rc = -EFAULT);
1058 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1059 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1060 dlm_req->lock_desc.l_req_mode &
1061 (dlm_req->lock_desc.l_req_mode-1))) {
1062 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1063 dlm_req->lock_desc.l_req_mode);
1064 GOTO(out, rc = -EFAULT);
1067 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1068 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1070 DEBUG_REQ(D_ERROR, req,
1071 "PLAIN lock request from IBITS client?");
1072 GOTO(out, rc = -EPROTO);
1074 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1076 DEBUG_REQ(D_ERROR, req,
1077 "IBITS lock request from unaware client?");
1078 GOTO(out, rc = -EPROTO);
1082 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1083 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1084 ibits for mgc/mgs) */
1086 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1087 * inodebits lock if client does not support them. */
1088 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1089 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1090 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1091 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1092 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1093 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1094 dlm_req->lock_desc.l_req_mode = LCK_CR;
1098 if (unlikely(flags & LDLM_FL_REPLAY)) {
1099 /* Find an existing lock in the per-export lock hash */
1100 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1101 (void *)&dlm_req->lock_handle[0]);
1103 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1104 LPX64, lock->l_handle.h_cookie);
1105 GOTO(existing_lock, rc = 0);
1109 /* The lock's callback data might be set in the policy function */
1110 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1111 dlm_req->lock_desc.l_resource.lr_type,
1112 dlm_req->lock_desc.l_req_mode,
1116 GOTO(out, rc = -ENOMEM);
1118 lock->l_last_activity = cfs_time_current_sec();
1119 lock->l_remote_handle = dlm_req->lock_handle[0];
1120 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1122 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1123 /* Don't enqueue a lock onto the export if it has already
1124 * been evicted. Cancel it now instead. (bug 3822) */
1125 if (req->rq_export->exp_failed) {
1126 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1127 GOTO(out, rc = -ENOTCONN);
1130 lock->l_export = class_export_lock_get(req->rq_export, lock);
1131 if (lock->l_export->exp_lock_hash)
1132 cfs_hash_add(lock->l_export->exp_lock_hash,
1133 &lock->l_remote_handle,
1138 if (flags & LDLM_FL_HAS_INTENT) {
1139 /* In this case, the reply buffer is allocated deep in
1140 * local_lock_enqueue by the policy function. */
1143 lock_res_and_lock(lock);
1144 if (lock->l_resource->lr_lvb_len) {
1145 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1147 lock->l_resource->lr_lvb_len);
1149 unlock_res_and_lock(lock);
1151 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1152 GOTO(out, rc = -ENOMEM);
1154 rc = req_capsule_server_pack(&req->rq_pill);
1159 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1160 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1161 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1162 lock->l_req_extent = lock->l_policy_data.l_extent;
1164 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1168 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1169 dlm_rep->lock_flags = flags;
1171 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1172 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1174 /* We never send a blocking AST until the lock is granted, but
1175 * we can tell it right now */
1176 lock_res_and_lock(lock);
1178 /* Now take into account flags to be inherited from original lock
1179 request both in reply to client and in our own lock flags. */
1180 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1181 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1183 /* Don't move a pending lock onto the export if it has already
1184 * been evicted. Cancel it now instead. (bug 5683) */
1185 if (unlikely(req->rq_export->exp_failed ||
1186 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1187 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1189 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1190 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1191 if (lock->l_granted_mode == lock->l_req_mode) {
1193 * Only cancel lock if it was granted, because it would
1194 * be destroyed immediately and would never be granted
1195 * in the future, causing timeouts on client. Not
1196 * granted lock will be cancelled immediately after
1197 * sending completion AST.
1199 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1200 unlock_res_and_lock(lock);
1201 ldlm_lock_cancel(lock);
1202 lock_res_and_lock(lock);
1204 ldlm_add_waiting_lock(lock);
1207 /* Make sure we never ever grant usual metadata locks to liblustre
1209 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1210 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1211 req->rq_export->exp_libclient) {
1212 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1213 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1214 CERROR("Granting sync lock to libclient. "
1215 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1216 dlm_req->lock_flags, dlm_rep->lock_flags,
1218 LDLM_ERROR(lock, "sync lock");
1219 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1220 struct ldlm_intent *it;
1222 it = req_capsule_client_get(&req->rq_pill,
1225 CERROR("This is intent %s ("LPU64")\n",
1226 ldlm_it2str(it->opc), it->opc);
1232 unlock_res_and_lock(lock);
1236 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1237 if (!req->rq_packed_final) {
1238 err = lustre_pack_reply(req, 1, NULL, NULL);
1243 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1244 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1246 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1247 "(err=%d, rc=%d)", err, rc);
1249 lock_res_and_lock(lock);
1251 if (lock->l_resource->lr_lvb_len > 0) {
1254 lvb = req_capsule_server_get(&req->rq_pill,
1256 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1259 memcpy(lvb, lock->l_resource->lr_lvb_data,
1260 lock->l_resource->lr_lvb_len);
1263 ldlm_resource_unlink_lock(lock);
1264 ldlm_lock_destroy_nolock(lock);
1266 unlock_res_and_lock(lock);
1268 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1269 ldlm_reprocess_all(lock->l_resource);
1271 LDLM_LOCK_RELEASE(lock);
1274 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1280 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1281 ldlm_completion_callback completion_callback,
1282 ldlm_blocking_callback blocking_callback,
1283 ldlm_glimpse_callback glimpse_callback)
1285 struct ldlm_request *dlm_req;
1286 struct ldlm_callback_suite cbs = {
1287 .lcs_completion = completion_callback,
1288 .lcs_blocking = blocking_callback,
1289 .lcs_glimpse = glimpse_callback
1293 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1294 if (dlm_req != NULL) {
1295 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1296 req, dlm_req, &cbs);
1303 int ldlm_handle_convert0(struct ptlrpc_request *req,
1304 const struct ldlm_request *dlm_req)
1306 struct ldlm_reply *dlm_rep;
1307 struct ldlm_lock *lock;
1311 if (req->rq_export && req->rq_export->exp_nid_stats &&
1312 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1313 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1314 LDLM_CONVERT - LDLM_FIRST_OPC);
1316 rc = req_capsule_server_pack(&req->rq_pill);
1320 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1321 dlm_rep->lock_flags = dlm_req->lock_flags;
1323 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1325 req->rq_status = EINVAL;
1329 LDLM_DEBUG(lock, "server-side convert handler START");
1331 lock->l_last_activity = cfs_time_current_sec();
1332 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1333 &dlm_rep->lock_flags);
1335 if (ldlm_del_waiting_lock(lock))
1336 LDLM_DEBUG(lock, "converted waiting lock");
1339 req->rq_status = EDEADLOCK;
1344 if (!req->rq_status)
1345 ldlm_reprocess_all(lock->l_resource);
1346 LDLM_DEBUG(lock, "server-side convert handler END");
1347 LDLM_LOCK_PUT(lock);
1349 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1354 int ldlm_handle_convert(struct ptlrpc_request *req)
1357 struct ldlm_request *dlm_req;
1359 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1360 if (dlm_req != NULL) {
1361 rc = ldlm_handle_convert0(req, dlm_req);
1363 CERROR ("Can't unpack dlm_req\n");
1369 /* Cancel all the locks whos handles are packed into ldlm_request */
1370 int ldlm_request_cancel(struct ptlrpc_request *req,
1371 const struct ldlm_request *dlm_req, int first)
1373 struct ldlm_resource *res, *pres = NULL;
1374 struct ldlm_lock *lock;
1375 int i, count, done = 0;
1378 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1382 /* There is no lock on the server at the replay time,
1383 * skip lock cancelling to make replay tests to pass. */
1384 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1387 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1388 "starting at %d", count, first);
1390 for (i = first; i < count; i++) {
1391 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1393 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1394 "lock (cookie "LPU64")",
1395 dlm_req->lock_handle[i].cookie);
1399 res = lock->l_resource;
1404 ldlm_reprocess_all(pres);
1405 LDLM_RESOURCE_DELREF(pres);
1406 ldlm_resource_putref(pres);
1409 ldlm_resource_getref(res);
1410 LDLM_RESOURCE_ADDREF(res);
1411 ldlm_res_lvbo_update(res, NULL, 1);
1415 ldlm_lock_cancel(lock);
1416 LDLM_LOCK_PUT(lock);
1419 ldlm_reprocess_all(pres);
1420 LDLM_RESOURCE_DELREF(pres);
1421 ldlm_resource_putref(pres);
1423 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1427 int ldlm_handle_cancel(struct ptlrpc_request *req)
1429 struct ldlm_request *dlm_req;
1433 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1434 if (dlm_req == NULL) {
1435 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1439 if (req->rq_export && req->rq_export->exp_nid_stats &&
1440 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1441 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1442 LDLM_CANCEL - LDLM_FIRST_OPC);
1444 rc = req_capsule_server_pack(&req->rq_pill);
1448 if (!ldlm_request_cancel(req, dlm_req, 0))
1449 req->rq_status = ESTALE;
1451 if (ptlrpc_reply(req) != 0)
1457 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1458 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1463 LDLM_DEBUG(lock, "client blocking AST callback handler");
1465 lock_res_and_lock(lock);
1466 lock->l_flags |= LDLM_FL_CBPENDING;
1468 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1469 lock->l_flags |= LDLM_FL_CANCEL;
1471 do_ast = (!lock->l_readers && !lock->l_writers);
1472 unlock_res_and_lock(lock);
1475 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1476 lock, lock->l_blocking_ast);
1477 if (lock->l_blocking_ast != NULL)
1478 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1481 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1485 LDLM_DEBUG(lock, "client blocking callback handler END");
1486 LDLM_LOCK_RELEASE(lock);
1490 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1491 struct ldlm_namespace *ns,
1492 struct ldlm_request *dlm_req,
1493 struct ldlm_lock *lock)
1495 CFS_LIST_HEAD(ast_list);
1498 LDLM_DEBUG(lock, "client completion callback handler START");
1500 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1501 int to = cfs_time_seconds(1);
1503 cfs_schedule_timeout_and_set_state(
1504 CFS_TASK_INTERRUPTIBLE, to);
1505 if (lock->l_granted_mode == lock->l_req_mode ||
1511 lock_res_and_lock(lock);
1512 if (lock->l_destroyed ||
1513 lock->l_granted_mode == lock->l_req_mode) {
1514 /* bug 11300: the lock has already been granted */
1515 unlock_res_and_lock(lock);
1516 LDLM_DEBUG(lock, "Double grant race happened");
1517 LDLM_LOCK_RELEASE(lock);
1522 /* If we receive the completion AST before the actual enqueue returned,
1523 * then we might need to switch lock modes, resources, or extents. */
1524 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1525 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1526 LDLM_DEBUG(lock, "completion AST, new lock mode");
1529 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1530 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1531 LDLM_DEBUG(lock, "completion AST, new policy data");
1534 ldlm_resource_unlink_lock(lock);
1535 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1536 &lock->l_resource->lr_name,
1537 sizeof(lock->l_resource->lr_name)) != 0) {
1538 unlock_res_and_lock(lock);
1539 if (ldlm_lock_change_resource(ns, lock,
1540 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1541 LDLM_ERROR(lock, "Failed to allocate resource");
1542 LDLM_LOCK_RELEASE(lock);
1546 LDLM_DEBUG(lock, "completion AST, new resource");
1547 CERROR("change resource!\n");
1548 lock_res_and_lock(lock);
1551 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1552 /* BL_AST locks are not needed in lru.
1553 * let ldlm_cancel_lru() be fast. */
1554 ldlm_lock_remove_from_lru(lock);
1555 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1556 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1559 if (lock->l_lvb_len) {
1560 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1561 RCL_CLIENT) < lock->l_lvb_len) {
1562 LDLM_ERROR(lock, "completion AST did not contain "
1565 void *lvb = req_capsule_client_get(&req->rq_pill,
1567 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1571 ldlm_grant_lock(lock, &ast_list);
1572 unlock_res_and_lock(lock);
1574 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1576 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1578 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1580 LDLM_LOCK_RELEASE(lock);
1584 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1585 struct ldlm_namespace *ns,
1586 struct ldlm_request *dlm_req,
1587 struct ldlm_lock *lock)
1592 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1594 if (lock->l_glimpse_ast != NULL)
1595 rc = lock->l_glimpse_ast(lock, req);
1597 if (req->rq_repmsg != NULL) {
1600 req->rq_status = rc;
1604 lock_res_and_lock(lock);
1605 if (lock->l_granted_mode == LCK_PW &&
1606 !lock->l_readers && !lock->l_writers &&
1607 cfs_time_after(cfs_time_current(),
1608 cfs_time_add(lock->l_last_used,
1609 cfs_time_seconds(10)))) {
1610 unlock_res_and_lock(lock);
1611 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1612 ldlm_handle_bl_callback(ns, NULL, lock);
1617 unlock_res_and_lock(lock);
1618 LDLM_LOCK_RELEASE(lock);
1622 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1624 if (req->rq_no_reply)
1627 req->rq_status = rc;
1628 if (!req->rq_packed_final) {
1629 rc = lustre_pack_reply(req, 1, NULL, NULL);
1633 return ptlrpc_reply(req);
1637 static int __ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_bl_work_item *blwi,
1638 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1639 cfs_list_t *cancels, int count, int mode)
1641 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1644 if (cancels && count == 0) {
1645 if (mode == LDLM_ASYNC)
1646 OBD_FREE(blwi, sizeof(*blwi));
1650 cfs_init_completion(&blwi->blwi_comp);
1651 cfs_atomic_set(&blwi->blwi_ref_count, 1);
1655 blwi->blwi_ld = *ld;
1657 cfs_list_add(&blwi->blwi_head, cancels);
1658 cfs_list_del_init(cancels);
1659 blwi->blwi_count = count;
1661 blwi->blwi_lock = lock;
1664 cfs_spin_lock(&blp->blp_lock);
1665 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1666 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1667 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1669 /* other blocking callbacks are added to the regular list */
1670 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1672 cfs_spin_unlock(&blp->blp_lock);
1674 if (mode == LDLM_SYNC) {
1675 /* keep ref count as object is on this stack for SYNC call */
1676 ldlm_bl_work_item_get(blwi);
1677 cfs_waitq_signal(&blp->blp_waitq);
1678 cfs_wait_for_completion(&blwi->blwi_comp);
1680 cfs_waitq_signal(&blp->blp_waitq);
1686 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1687 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1688 struct list_head *cancels, int count, int mode)
1692 if (mode == LDLM_SYNC) {
1693 /* if it is synchronous call do minimum mem alloc, as it could
1694 * be triggered from kernel shrinker
1696 struct ldlm_bl_work_item blwi;
1697 memset(&blwi, 0, sizeof(blwi));
1698 /* have extra ref as this obj is on stack */
1699 RETURN(__ldlm_bl_to_thread(ns, &blwi, ld, lock, cancels, count, mode));
1701 struct ldlm_bl_work_item *blwi;
1702 OBD_ALLOC(blwi, sizeof(*blwi));
1706 RETURN(__ldlm_bl_to_thread(ns, blwi, ld, lock, cancels, count, mode));
1712 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1713 struct ldlm_lock *lock)
1716 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1722 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1723 cfs_list_t *cancels, int count, int mode)
1726 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1732 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1733 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1735 struct obd_device *obd = req->rq_export->exp_obd;
1742 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1744 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1746 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1748 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1751 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1753 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1755 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1758 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1761 /* We are responsible for swabbing contents of val */
1763 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1764 /* Pass it on to mdc (the "export" in this case) */
1765 rc = obd_set_info_async(req->rq_export,
1766 sizeof(KEY_HSM_COPYTOOL_SEND),
1767 KEY_HSM_COPYTOOL_SEND,
1770 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1775 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1776 const char *msg, int rc)
1778 CWARN("%s: [pid %d] [xid x"LPU64"] [nid %s] [opc %d] [rc %d].\n",
1779 msg, lustre_msg_get_status(req->rq_reqmsg),
1780 req->rq_xid, libcfs_id2str(req->rq_peer),
1781 lustre_msg_get_opc(req->rq_reqmsg), rc);
1782 if (req->rq_no_reply)
1783 CWARN("No reply was sent, maybe cause bug 21636.\n");
1785 CWARN("Send reply failed, maybe cause bug 21636.\n");
1788 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1789 static int ldlm_callback_handler(struct ptlrpc_request *req)
1791 struct ldlm_namespace *ns;
1792 struct ldlm_request *dlm_req;
1793 struct ldlm_lock *lock;
1797 /* Requests arrive in sender's byte order. The ptlrpc service
1798 * handler has already checked and, if necessary, byte-swapped the
1799 * incoming request message body, but I am responsible for the
1800 * message buffers. */
1802 /* do nothing for sec context finalize */
1803 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1806 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1808 if (req->rq_export == NULL) {
1809 rc = ldlm_callback_reply(req, -ENOTCONN);
1810 ldlm_callback_errmsg(req, "Operate on unconnected server", rc);
1814 LASSERT(req->rq_export != NULL);
1815 LASSERT(req->rq_export->exp_obd != NULL);
1817 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1818 case LDLM_BL_CALLBACK:
1819 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1822 case LDLM_CP_CALLBACK:
1823 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1826 case LDLM_GL_CALLBACK:
1827 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1831 rc = ldlm_handle_setinfo(req);
1832 ldlm_callback_reply(req, rc);
1834 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1835 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1836 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1837 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1839 rc = llog_origin_handle_cancel(req);
1840 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1842 ldlm_callback_reply(req, rc);
1844 case OBD_QC_CALLBACK:
1845 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1846 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1848 rc = target_handle_qc_callback(req);
1849 ldlm_callback_reply(req, rc);
1853 /* reply in handler */
1854 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1855 rc = target_handle_dqacq_callback(req);
1857 case LLOG_ORIGIN_HANDLE_CREATE:
1858 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1859 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1861 rc = llog_origin_handle_create(req);
1862 ldlm_callback_reply(req, rc);
1864 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1865 req_capsule_set(&req->rq_pill,
1866 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1867 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1869 rc = llog_origin_handle_next_block(req);
1870 ldlm_callback_reply(req, rc);
1872 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1873 req_capsule_set(&req->rq_pill,
1874 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1875 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1877 rc = llog_origin_handle_read_header(req);
1878 ldlm_callback_reply(req, rc);
1880 case LLOG_ORIGIN_HANDLE_CLOSE:
1881 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1883 rc = llog_origin_handle_close(req);
1884 ldlm_callback_reply(req, rc);
1887 CERROR("unknown opcode %u\n",
1888 lustre_msg_get_opc(req->rq_reqmsg));
1889 ldlm_callback_reply(req, -EPROTO);
1893 ns = req->rq_export->exp_obd->obd_namespace;
1894 LASSERT(ns != NULL);
1896 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1898 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1899 if (dlm_req == NULL) {
1900 rc = ldlm_callback_reply(req, -EPROTO);
1901 ldlm_callback_errmsg(req, "Operate without parameter", rc);
1905 /* Force a known safe race, send a cancel to the server for a lock
1906 * which the server has already started a blocking callback on. */
1907 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1908 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1909 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1911 CERROR("ldlm_cli_cancel: %d\n", rc);
1914 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1916 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1917 "disappeared\n", dlm_req->lock_handle[0].cookie);
1918 rc = ldlm_callback_reply(req, -EINVAL);
1919 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc);
1923 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1924 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1925 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1927 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1928 lock_res_and_lock(lock);
1929 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1930 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1931 /* If somebody cancels lock and cache is already dropped,
1932 * or lock is failed before cp_ast received on client,
1933 * we can tell the server we have no lock. Otherwise, we
1934 * should send cancel after dropping the cache. */
1935 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1936 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1937 (lock->l_flags & LDLM_FL_FAILED)) {
1938 LDLM_DEBUG(lock, "callback on lock "
1939 LPX64" - lock disappeared\n",
1940 dlm_req->lock_handle[0].cookie);
1941 unlock_res_and_lock(lock);
1942 LDLM_LOCK_RELEASE(lock);
1943 rc = ldlm_callback_reply(req, -EINVAL);
1944 ldlm_callback_errmsg(req, "Operate on stale lock", rc);
1947 /* BL_AST locks are not needed in lru.
1948 * let ldlm_cancel_lru() be fast. */
1949 ldlm_lock_remove_from_lru(lock);
1950 lock->l_flags |= LDLM_FL_BL_AST;
1952 unlock_res_and_lock(lock);
1954 /* We want the ost thread to get this reply so that it can respond
1955 * to ost requests (write cache writeback) that might be triggered
1958 * But we'd also like to be able to indicate in the reply that we're
1959 * cancelling right now, because it's unused, or have an intent result
1960 * in the reply, so we might have to push the responsibility for sending
1961 * the reply down into the AST handlers, alas. */
1963 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1964 case LDLM_BL_CALLBACK:
1965 CDEBUG(D_INODE, "blocking ast\n");
1966 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1967 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1968 rc = ldlm_callback_reply(req, 0);
1969 if (req->rq_no_reply || rc)
1970 ldlm_callback_errmsg(req, "Normal process", rc);
1972 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1973 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1975 case LDLM_CP_CALLBACK:
1976 CDEBUG(D_INODE, "completion ast\n");
1977 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1978 ldlm_callback_reply(req, 0);
1979 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1981 case LDLM_GL_CALLBACK:
1982 CDEBUG(D_INODE, "glimpse ast\n");
1983 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1984 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1987 LBUG(); /* checked above */
1993 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1998 /* Requests arrive in sender's byte order. The ptlrpc service
1999 * handler has already checked and, if necessary, byte-swapped the
2000 * incoming request message body, but I am responsible for the
2001 * message buffers. */
2003 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2005 if (req->rq_export == NULL) {
2006 struct ldlm_request *dlm_req;
2008 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
2009 lustre_msg_get_opc(req->rq_reqmsg),
2010 libcfs_id2str(req->rq_peer),
2011 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2013 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2014 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2015 dlm_req = req_capsule_client_get(&req->rq_pill,
2017 if (dlm_req != NULL)
2018 ldlm_lock_dump_handle(D_ERROR,
2019 &dlm_req->lock_handle[0]);
2021 ldlm_callback_reply(req, -ENOTCONN);
2025 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2027 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2029 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2030 CDEBUG(D_INODE, "cancel\n");
2031 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2033 rc = ldlm_handle_cancel(req);
2037 case OBD_LOG_CANCEL:
2038 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2039 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2041 rc = llog_origin_handle_cancel(req);
2042 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2044 ldlm_callback_reply(req, rc);
2047 CERROR("invalid opcode %d\n",
2048 lustre_msg_get_opc(req->rq_reqmsg));
2049 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2050 ldlm_callback_reply(req, -EINVAL);
2056 void ldlm_revoke_lock_cb(void *obj, void *data)
2058 cfs_list_t *rpc_list = data;
2059 struct ldlm_lock *lock = obj;
2061 lock_res_and_lock(lock);
2063 if (lock->l_req_mode != lock->l_granted_mode) {
2064 unlock_res_and_lock(lock);
2068 LASSERT(lock->l_resource);
2069 if (lock->l_resource->lr_type != LDLM_IBITS &&
2070 lock->l_resource->lr_type != LDLM_PLAIN) {
2071 unlock_res_and_lock(lock);
2075 if (lock->l_flags & LDLM_FL_AST_SENT) {
2076 unlock_res_and_lock(lock);
2080 LASSERT(lock->l_blocking_ast);
2081 LASSERT(!lock->l_blocking_lock);
2083 lock->l_flags |= LDLM_FL_AST_SENT;
2084 if (lock->l_export && lock->l_export->exp_lock_hash &&
2085 !cfs_hlist_unhashed(&lock->l_exp_hash))
2086 cfs_hash_del(lock->l_export->exp_lock_hash,
2087 &lock->l_remote_handle, &lock->l_exp_hash);
2088 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2089 LDLM_LOCK_GET(lock);
2091 unlock_res_and_lock(lock);
2094 void ldlm_revoke_export_locks(struct obd_export *exp)
2096 cfs_list_t rpc_list;
2099 CFS_INIT_LIST_HEAD(&rpc_list);
2100 cfs_hash_for_each_empty(exp->exp_lock_hash,
2101 ldlm_revoke_lock_cb, &rpc_list);
2102 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
2108 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2110 struct ldlm_bl_work_item *blwi = NULL;
2111 static unsigned int num_bl = 0;
2113 cfs_spin_lock(&blp->blp_lock);
2114 /* process a request from the blp_list at least every blp_num_threads */
2115 if (!cfs_list_empty(&blp->blp_list) &&
2116 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2117 blwi = cfs_list_entry(blp->blp_list.next,
2118 struct ldlm_bl_work_item, blwi_entry);
2120 if (!cfs_list_empty(&blp->blp_prio_list))
2121 blwi = cfs_list_entry(blp->blp_prio_list.next,
2122 struct ldlm_bl_work_item,
2126 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2128 cfs_list_del(&blwi->blwi_entry);
2130 cfs_spin_unlock(&blp->blp_lock);
2135 /* This only contains temporary data until the thread starts */
2136 struct ldlm_bl_thread_data {
2137 char bltd_name[CFS_CURPROC_COMM_MAX];
2138 struct ldlm_bl_pool *bltd_blp;
2139 cfs_completion_t bltd_comp;
2143 static int ldlm_bl_thread_main(void *arg);
2145 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2147 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2150 cfs_init_completion(&bltd.bltd_comp);
2151 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
2153 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2154 cfs_atomic_read(&blp->blp_num_threads), rc);
2157 cfs_wait_for_completion(&bltd.bltd_comp);
2162 static int ldlm_bl_thread_main(void *arg)
2164 struct ldlm_bl_pool *blp;
2168 struct ldlm_bl_thread_data *bltd = arg;
2170 blp = bltd->bltd_blp;
2173 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2174 cfs_atomic_inc(&blp->blp_busy_threads);
2176 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2177 "ldlm_bl_%02d", bltd->bltd_num);
2178 cfs_daemonize(bltd->bltd_name);
2180 cfs_complete(&bltd->bltd_comp);
2181 /* cannot use bltd after this, it is only on caller's stack */
2185 struct l_wait_info lwi = { 0 };
2186 struct ldlm_bl_work_item *blwi = NULL;
2188 blwi = ldlm_bl_get_work(blp);
2193 cfs_atomic_dec(&blp->blp_busy_threads);
2194 l_wait_event_exclusive(blp->blp_waitq,
2195 (blwi = ldlm_bl_get_work(blp)) != NULL,
2197 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2199 if (blwi->blwi_ns == NULL)
2200 /* added by ldlm_cleanup() */
2203 /* Not fatal if racy and have a few too many threads */
2204 if (unlikely(busy < blp->blp_max_threads &&
2205 busy >= cfs_atomic_read(&blp->blp_num_threads)))
2206 /* discard the return value, we tried */
2207 ldlm_bl_thread_start(blp);
2209 if (blwi->blwi_ns == NULL)
2210 /* added by ldlm_cleanup() */
2214 if (blwi->blwi_count) {
2215 /* The special case when we cancel locks in lru
2216 * asynchronously, we pass the list of locks here.
2217 * Thus lock is marked LDLM_FL_CANCELING, and already
2218 * canceled locally. */
2219 ldlm_cli_cancel_list(&blwi->blwi_head,
2220 blwi->blwi_count, NULL, 0);
2222 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2225 cfs_complete(&blwi->blwi_comp);
2226 ldlm_bl_work_item_put(blwi);
2229 cfs_atomic_dec(&blp->blp_busy_threads);
2230 cfs_atomic_dec(&blp->blp_num_threads);
2231 cfs_complete(&blp->blp_comp);
2237 static int ldlm_setup(void);
2238 static int ldlm_cleanup(void);
2240 int ldlm_get_ref(void)
2244 cfs_mutex_down(&ldlm_ref_sem);
2245 if (++ldlm_refcount == 1) {
2250 cfs_mutex_up(&ldlm_ref_sem);
2255 void ldlm_put_ref(void)
2258 cfs_mutex_down(&ldlm_ref_sem);
2259 if (ldlm_refcount == 1) {
2260 int rc = ldlm_cleanup();
2262 CERROR("ldlm_cleanup failed: %d\n", rc);
2268 cfs_mutex_up(&ldlm_ref_sem);
2274 * Export handle<->lock hash operations.
2277 ldlm_export_lock_hash(cfs_hash_t *hs, void *key, unsigned mask)
2279 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2283 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2285 struct ldlm_lock *lock;
2288 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2289 RETURN(&lock->l_remote_handle);
2293 ldlm_export_lock_compare(void *key, cfs_hlist_node_t *hnode)
2296 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2300 ldlm_export_lock_get(cfs_hlist_node_t *hnode)
2302 struct ldlm_lock *lock;
2305 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2306 LDLM_LOCK_GET(lock);
2312 ldlm_export_lock_put(cfs_hlist_node_t *hnode)
2314 struct ldlm_lock *lock;
2317 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2318 LDLM_LOCK_RELEASE(lock);
2323 static cfs_hash_ops_t ldlm_export_lock_ops = {
2324 .hs_hash = ldlm_export_lock_hash,
2325 .hs_key = ldlm_export_lock_key,
2326 .hs_compare = ldlm_export_lock_compare,
2327 .hs_get = ldlm_export_lock_get,
2328 .hs_put = ldlm_export_lock_put
2331 int ldlm_init_export(struct obd_export *exp)
2335 exp->exp_lock_hash =
2336 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2337 HASH_EXP_LOCK_CUR_BITS, HASH_EXP_LOCK_MAX_BITS,
2338 &ldlm_export_lock_ops, CFS_HASH_REHASH);
2340 if (!exp->exp_lock_hash)
2345 EXPORT_SYMBOL(ldlm_init_export);
2347 void ldlm_destroy_export(struct obd_export *exp)
2350 cfs_hash_putref(exp->exp_lock_hash);
2351 exp->exp_lock_hash = NULL;
2354 EXPORT_SYMBOL(ldlm_destroy_export);
2356 static int ldlm_setup(void)
2358 struct ldlm_bl_pool *blp;
2360 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2361 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2367 if (ldlm_state != NULL)
2370 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2371 if (ldlm_state == NULL)
2375 rc = ldlm_proc_setup();
2381 if (ldlm_num_threads) {
2382 /* If ldlm_num_threads is set, it is the min and the max. */
2383 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2384 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2385 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2386 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2387 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2391 ldlm_state->ldlm_cb_service =
2392 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2393 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2394 LDLM_CB_REPLY_PORTAL, 2,
2395 ldlm_callback_handler, "ldlm_cbd",
2396 ldlm_svc_proc_dir, NULL,
2397 ldlm_min_threads, ldlm_max_threads,
2399 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2401 if (!ldlm_state->ldlm_cb_service) {
2402 CERROR("failed to start service\n");
2403 GOTO(out_proc, rc = -ENOMEM);
2406 ldlm_state->ldlm_cancel_service =
2407 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2408 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2409 LDLM_CANCEL_REPLY_PORTAL, 6,
2410 ldlm_cancel_handler, "ldlm_canceld",
2411 ldlm_svc_proc_dir, NULL,
2412 ldlm_min_threads, ldlm_max_threads,
2414 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2417 if (!ldlm_state->ldlm_cancel_service) {
2418 CERROR("failed to start service\n");
2419 GOTO(out_proc, rc = -ENOMEM);
2422 OBD_ALLOC(blp, sizeof(*blp));
2424 GOTO(out_proc, rc = -ENOMEM);
2425 ldlm_state->ldlm_bl_pool = blp;
2427 cfs_spin_lock_init(&blp->blp_lock);
2428 CFS_INIT_LIST_HEAD(&blp->blp_list);
2429 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2430 cfs_waitq_init(&blp->blp_waitq);
2431 cfs_atomic_set(&blp->blp_num_threads, 0);
2432 cfs_atomic_set(&blp->blp_busy_threads, 0);
2433 blp->blp_min_threads = ldlm_min_threads;
2434 blp->blp_max_threads = ldlm_max_threads;
2437 for (i = 0; i < blp->blp_min_threads; i++) {
2438 rc = ldlm_bl_thread_start(blp);
2440 GOTO(out_thread, rc);
2443 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2445 GOTO(out_thread, rc);
2447 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2449 GOTO(out_thread, rc);
2451 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2452 expired_lock_thread.elt_state = ELT_STOPPED;
2453 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2455 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2456 cfs_spin_lock_init(&waiting_locks_spinlock);
2457 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2459 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2461 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2462 GOTO(out_thread, rc);
2465 cfs_wait_event(expired_lock_thread.elt_waitq,
2466 expired_lock_thread.elt_state == ELT_READY);
2470 rc = ldlm_pools_init();
2472 GOTO(out_thread, rc);
2478 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2479 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2484 ldlm_proc_cleanup();
2487 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2492 static int ldlm_cleanup(void)
2495 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2499 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2500 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2501 CERROR("ldlm still has namespaces; clean these up first.\n");
2502 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2503 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2512 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2513 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2515 cfs_init_completion(&blp->blp_comp);
2517 cfs_spin_lock(&blp->blp_lock);
2518 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2519 cfs_waitq_signal(&blp->blp_waitq);
2520 cfs_spin_unlock(&blp->blp_lock);
2522 cfs_wait_for_completion(&blp->blp_comp);
2524 OBD_FREE(blp, sizeof(*blp));
2526 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2527 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2528 ldlm_proc_cleanup();
2530 expired_lock_thread.elt_state = ELT_TERMINATE;
2531 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2532 cfs_wait_event(expired_lock_thread.elt_waitq,
2533 expired_lock_thread.elt_state == ELT_STOPPED);
2535 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2536 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2539 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2545 int __init ldlm_init(void)
2547 cfs_init_mutex(&ldlm_ref_sem);
2548 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2549 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2550 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2551 sizeof(struct ldlm_resource), 0,
2552 CFS_SLAB_HWCACHE_ALIGN);
2553 if (ldlm_resource_slab == NULL)
2556 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2557 sizeof(struct ldlm_lock), 0,
2558 CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2559 if (ldlm_lock_slab == NULL) {
2560 cfs_mem_cache_destroy(ldlm_resource_slab);
2564 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2565 sizeof(struct ldlm_interval),
2566 0, CFS_SLAB_HWCACHE_ALIGN);
2567 if (ldlm_interval_slab == NULL) {
2568 cfs_mem_cache_destroy(ldlm_resource_slab);
2569 cfs_mem_cache_destroy(ldlm_lock_slab);
2572 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2573 class_export_dump_hook = ldlm_dump_export_locks;
2578 void __exit ldlm_exit(void)
2582 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2583 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2584 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2586 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2587 * synchronize_rcu() to wait a grace period elapsed, so that
2588 * ldlm_lock_free() get a chance to be called. */
2591 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2592 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2593 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2594 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2598 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2601 EXPORT_SYMBOL(ldlm_get_processing_policy);
2602 EXPORT_SYMBOL(ldlm_lock2desc);
2603 EXPORT_SYMBOL(ldlm_register_intent);
2604 EXPORT_SYMBOL(ldlm_lockname);
2605 EXPORT_SYMBOL(ldlm_typename);
2606 EXPORT_SYMBOL(ldlm_lock2handle);
2607 EXPORT_SYMBOL(__ldlm_handle2lock);
2608 EXPORT_SYMBOL(ldlm_lock_get);
2609 EXPORT_SYMBOL(ldlm_lock_put);
2610 EXPORT_SYMBOL(ldlm_lock_match);
2611 EXPORT_SYMBOL(ldlm_lock_cancel);
2612 EXPORT_SYMBOL(ldlm_lock_addref);
2613 EXPORT_SYMBOL(ldlm_lock_addref_try);
2614 EXPORT_SYMBOL(ldlm_lock_decref);
2615 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2616 EXPORT_SYMBOL(ldlm_lock_change_resource);
2617 EXPORT_SYMBOL(ldlm_it2str);
2618 EXPORT_SYMBOL(ldlm_lock_dump);
2619 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2620 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2621 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2622 EXPORT_SYMBOL(ldlm_lock_allow_match);
2623 EXPORT_SYMBOL(ldlm_lock_downgrade);
2624 EXPORT_SYMBOL(ldlm_lock_convert);
2626 /* ldlm_request.c */
2627 EXPORT_SYMBOL(ldlm_completion_ast_async);
2628 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2629 EXPORT_SYMBOL(ldlm_completion_ast);
2630 EXPORT_SYMBOL(ldlm_blocking_ast);
2631 EXPORT_SYMBOL(ldlm_glimpse_ast);
2632 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2633 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2634 EXPORT_SYMBOL(ldlm_prep_elc_req);
2635 EXPORT_SYMBOL(ldlm_cli_convert);
2636 EXPORT_SYMBOL(ldlm_cli_enqueue);
2637 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2638 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2639 EXPORT_SYMBOL(ldlm_cli_cancel);
2640 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2641 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2642 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2643 EXPORT_SYMBOL(ldlm_replay_locks);
2644 EXPORT_SYMBOL(ldlm_resource_foreach);
2645 EXPORT_SYMBOL(ldlm_namespace_foreach);
2646 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2647 EXPORT_SYMBOL(ldlm_resource_iterate);
2648 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2649 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2652 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2653 EXPORT_SYMBOL(ldlm_server_completion_ast);
2654 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2655 EXPORT_SYMBOL(ldlm_handle_enqueue);
2656 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2657 EXPORT_SYMBOL(ldlm_handle_cancel);
2658 EXPORT_SYMBOL(ldlm_request_cancel);
2659 EXPORT_SYMBOL(ldlm_handle_convert);
2660 EXPORT_SYMBOL(ldlm_handle_convert0);
2661 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2662 EXPORT_SYMBOL(ldlm_get_ref);
2663 EXPORT_SYMBOL(ldlm_put_ref);
2664 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2665 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2667 /* ldlm_resource.c */
2668 EXPORT_SYMBOL(ldlm_namespace_new);
2669 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2670 EXPORT_SYMBOL(ldlm_namespace_free);
2671 EXPORT_SYMBOL(ldlm_namespace_dump);
2672 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2673 EXPORT_SYMBOL(ldlm_resource_get);
2674 EXPORT_SYMBOL(ldlm_resource_putref);
2675 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2678 EXPORT_SYMBOL(client_import_add_conn);
2679 EXPORT_SYMBOL(client_import_del_conn);
2680 EXPORT_SYMBOL(client_obd_setup);
2681 EXPORT_SYMBOL(client_obd_cleanup);
2682 EXPORT_SYMBOL(client_connect_import);
2683 EXPORT_SYMBOL(client_disconnect_export);
2684 EXPORT_SYMBOL(server_disconnect_export);
2685 EXPORT_SYMBOL(target_stop_recovery_thread);
2686 EXPORT_SYMBOL(target_handle_connect);
2687 EXPORT_SYMBOL(target_cleanup_recovery);
2688 EXPORT_SYMBOL(target_destroy_export);
2689 EXPORT_SYMBOL(target_cancel_recovery_timer);
2690 EXPORT_SYMBOL(target_send_reply);
2691 EXPORT_SYMBOL(target_queue_recovery_request);
2692 EXPORT_SYMBOL(target_handle_ping);
2693 EXPORT_SYMBOL(target_pack_pool_reply);
2694 EXPORT_SYMBOL(target_handle_disconnect);
2697 EXPORT_SYMBOL(lock_res_and_lock);
2698 EXPORT_SYMBOL(unlock_res_and_lock);