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 (c) 2002, 2010, Oracle and/or its affiliates. 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;
138 int blwi_mem_pressure;
143 static inline int have_expired_locks(void)
148 cfs_spin_lock_bh(&waiting_locks_spinlock);
149 need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
150 cfs_spin_unlock_bh(&waiting_locks_spinlock);
155 static int expired_lock_main(void *arg)
157 cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
158 struct l_wait_info lwi = { 0 };
162 cfs_daemonize("ldlm_elt");
164 expired_lock_thread.elt_state = ELT_READY;
165 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
168 l_wait_event(expired_lock_thread.elt_waitq,
169 have_expired_locks() ||
170 expired_lock_thread.elt_state == ELT_TERMINATE,
173 cfs_spin_lock_bh(&waiting_locks_spinlock);
174 if (expired_lock_thread.elt_dump) {
175 cfs_spin_unlock_bh(&waiting_locks_spinlock);
177 /* from waiting_locks_callback, but not in timer */
178 libcfs_debug_dumplog();
179 libcfs_run_lbug_upcall(__FILE__,
180 "waiting_locks_callback",
181 expired_lock_thread.elt_dump);
183 cfs_spin_lock_bh(&waiting_locks_spinlock);
184 expired_lock_thread.elt_dump = 0;
189 while (!cfs_list_empty(expired)) {
190 struct obd_export *export;
191 struct ldlm_lock *lock;
193 lock = cfs_list_entry(expired->next, struct ldlm_lock,
195 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
196 (void *)lock >= LP_POISON) {
197 cfs_spin_unlock_bh(&waiting_locks_spinlock);
198 CERROR("free lock on elt list %p\n", lock);
201 cfs_list_del_init(&lock->l_pending_chain);
202 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
203 (void *)lock->l_export >= LP_POISON) {
204 CERROR("lock with free export on elt list %p\n",
206 lock->l_export = NULL;
207 LDLM_ERROR(lock, "free export");
208 /* release extra ref grabbed by
209 * ldlm_add_waiting_lock() or
210 * ldlm_failed_ast() */
211 LDLM_LOCK_RELEASE(lock);
214 export = class_export_lock_get(lock->l_export, lock);
215 cfs_spin_unlock_bh(&waiting_locks_spinlock);
218 class_fail_export(export);
219 class_export_lock_put(export, lock);
221 /* release extra ref grabbed by ldlm_add_waiting_lock()
222 * or ldlm_failed_ast() */
223 LDLM_LOCK_RELEASE(lock);
225 cfs_spin_lock_bh(&waiting_locks_spinlock);
227 cfs_spin_unlock_bh(&waiting_locks_spinlock);
229 if (do_dump && obd_dump_on_eviction) {
230 CERROR("dump the log upon eviction\n");
231 libcfs_debug_dumplog();
234 if (expired_lock_thread.elt_state == ELT_TERMINATE)
238 expired_lock_thread.elt_state = ELT_STOPPED;
239 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
243 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
246 * Check if there is a request in the export request list
247 * which prevents the lock canceling.
249 static int ldlm_lock_busy(struct ldlm_lock *lock)
251 struct ptlrpc_request *req;
255 if (lock->l_export == NULL)
258 cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
259 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
261 if (req->rq_ops->hpreq_lock_match) {
262 match = req->rq_ops->hpreq_lock_match(req, lock);
267 cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
271 /* This is called from within a timer interrupt and cannot schedule */
272 static void waiting_locks_callback(unsigned long unused)
274 struct ldlm_lock *lock, *last = NULL;
277 cfs_spin_lock_bh(&waiting_locks_spinlock);
278 while (!cfs_list_empty(&waiting_locks_list)) {
279 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
281 if (cfs_time_after(lock->l_callback_timeout,
282 cfs_time_current()) ||
283 (lock->l_req_mode == LCK_GROUP))
286 if (ptlrpc_check_suspend()) {
287 /* there is a case when we talk to one mds, holding
288 * lock from another mds. this way we easily can get
289 * here, if second mds is being recovered. so, we
290 * suspend timeouts. bug 6019 */
292 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
293 lock->l_export->exp_client_uuid.uuid,
294 lock->l_export->exp_connection->c_remote_uuid.uuid,
295 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
297 cfs_list_del_init(&lock->l_pending_chain);
298 cfs_spin_unlock_bh(&waiting_locks_spinlock);
299 ldlm_add_waiting_lock(lock);
303 /* if timeout overlaps the activation time of suspended timeouts
304 * then extend it to give a chance for client to reconnect */
305 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
306 cfs_time_seconds(obd_timeout)/2),
307 ptlrpc_suspend_wakeup_time())) {
308 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
309 lock->l_export->exp_client_uuid.uuid,
310 lock->l_export->exp_connection->c_remote_uuid.uuid,
311 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
313 cfs_list_del_init(&lock->l_pending_chain);
314 cfs_spin_unlock_bh(&waiting_locks_spinlock);
315 ldlm_add_waiting_lock(lock);
319 /* Check if we need to prolong timeout */
320 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
321 ldlm_lock_busy(lock)) {
324 if (lock->l_pending_chain.next == &waiting_locks_list)
329 cfs_spin_unlock_bh(&waiting_locks_spinlock);
330 LDLM_DEBUG(lock, "prolong the busy lock");
331 ldlm_refresh_waiting_lock(lock,
332 ldlm_get_enq_timeout(lock));
333 cfs_spin_lock_bh(&waiting_locks_spinlock);
336 LDLM_LOCK_RELEASE(lock);
340 LDLM_LOCK_RELEASE(lock);
343 lock->l_resource->lr_namespace->ns_timeouts++;
344 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
345 "evicting client at %s ",
346 cfs_time_current_sec()- lock->l_last_activity,
348 lock->l_export->exp_connection->c_peer.nid));
352 /* no needs to take an extra ref on the lock since it was in
353 * the waiting_locks_list and ldlm_add_waiting_lock()
354 * already grabbed a ref */
355 cfs_list_del(&lock->l_pending_chain);
356 cfs_list_add(&lock->l_pending_chain,
357 &expired_lock_thread.elt_expired_locks);
360 if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
361 if (obd_dump_on_timeout)
362 expired_lock_thread.elt_dump = __LINE__;
364 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
368 * Make sure the timer will fire again if we have any locks
371 if (!cfs_list_empty(&waiting_locks_list)) {
372 cfs_time_t timeout_rounded;
373 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
375 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
376 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
378 cfs_spin_unlock_bh(&waiting_locks_spinlock);
382 * Indicate that we're waiting for a client to call us back cancelling a given
383 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
384 * timer to fire appropriately. (We round up to the next second, to avoid
385 * floods of timer firings during periods of high lock contention and traffic).
386 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
387 * if it has been added to the waiting list (1 is returned).
389 * Called with the namespace lock held.
391 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
394 cfs_time_t timeout_rounded;
396 if (!cfs_list_empty(&lock->l_pending_chain))
399 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
400 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
403 timeout = cfs_time_shift(seconds);
404 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
405 lock->l_callback_timeout = timeout;
407 timeout_rounded = round_timeout(lock->l_callback_timeout);
409 if (cfs_time_before(timeout_rounded,
410 cfs_timer_deadline(&waiting_locks_timer)) ||
411 !cfs_timer_is_armed(&waiting_locks_timer)) {
412 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
414 /* if the new lock has a shorter timeout than something earlier on
415 the list, we'll wait the longer amount of time; no big deal. */
417 cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
421 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
424 int timeout = ldlm_get_enq_timeout(lock);
426 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
428 cfs_spin_lock_bh(&waiting_locks_spinlock);
429 if (lock->l_destroyed) {
430 static cfs_time_t next;
431 cfs_spin_unlock_bh(&waiting_locks_spinlock);
432 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
433 if (cfs_time_after(cfs_time_current(), next)) {
434 next = cfs_time_shift(14400);
435 libcfs_debug_dumpstack(NULL);
440 ret = __ldlm_add_waiting_lock(lock, timeout);
442 /* grab ref on the lock if it has been added to the
445 cfs_spin_unlock_bh(&waiting_locks_spinlock);
447 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
448 ret == 0 ? "not re-" : "", timeout,
449 AT_OFF ? "off" : "on");
454 * Remove a lock from the pending list, likely because it had its cancellation
455 * callback arrive without incident. This adjusts the lock-timeout timer if
456 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
457 * As done by ldlm_del_waiting_lock(), the caller must release the lock
458 * reference when the lock is removed from any list (1 is returned).
460 * Called with namespace lock held.
462 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
464 cfs_list_t *list_next;
466 if (cfs_list_empty(&lock->l_pending_chain))
469 list_next = lock->l_pending_chain.next;
470 if (lock->l_pending_chain.prev == &waiting_locks_list) {
471 /* Removing the head of the list, adjust timer. */
472 if (list_next == &waiting_locks_list) {
473 /* No more, just cancel. */
474 cfs_timer_disarm(&waiting_locks_timer);
476 struct ldlm_lock *next;
477 next = cfs_list_entry(list_next, struct ldlm_lock,
479 cfs_timer_arm(&waiting_locks_timer,
480 round_timeout(next->l_callback_timeout));
483 cfs_list_del_init(&lock->l_pending_chain);
488 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
492 if (lock->l_export == NULL) {
493 /* We don't have a "waiting locks list" on clients. */
494 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
498 cfs_spin_lock_bh(&waiting_locks_spinlock);
499 ret = __ldlm_del_waiting_lock(lock);
500 cfs_spin_unlock_bh(&waiting_locks_spinlock);
502 /* release lock ref if it has indeed been removed
504 LDLM_LOCK_RELEASE(lock);
506 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
513 * Called with namespace lock held.
515 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
517 if (lock->l_export == NULL) {
518 /* We don't have a "waiting locks list" on clients. */
519 LDLM_DEBUG(lock, "client lock: no-op");
523 cfs_spin_lock_bh(&waiting_locks_spinlock);
525 if (cfs_list_empty(&lock->l_pending_chain)) {
526 cfs_spin_unlock_bh(&waiting_locks_spinlock);
527 LDLM_DEBUG(lock, "wasn't waiting");
531 /* we remove/add the lock to the waiting list, so no needs to
532 * release/take a lock reference */
533 __ldlm_del_waiting_lock(lock);
534 __ldlm_add_waiting_lock(lock, timeout);
535 cfs_spin_unlock_bh(&waiting_locks_spinlock);
537 LDLM_DEBUG(lock, "refreshed");
540 #else /* !__KERNEL__ */
542 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
544 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
548 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
553 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
557 #endif /* __KERNEL__ */
559 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
560 const char *ast_type)
562 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
563 "to a lock %s callback time out: rc %d\n",
564 lock->l_export->exp_obd->obd_name,
565 obd_export_nid2str(lock->l_export), ast_type, rc);
567 if (obd_dump_on_timeout)
568 libcfs_debug_dumplog();
570 cfs_spin_lock_bh(&waiting_locks_spinlock);
571 if (__ldlm_del_waiting_lock(lock) == 0)
572 /* the lock was not in any list, grab an extra ref before adding
573 * the lock to the expired list */
575 cfs_list_add(&lock->l_pending_chain,
576 &expired_lock_thread.elt_expired_locks);
577 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
578 cfs_spin_unlock_bh(&waiting_locks_spinlock);
580 class_fail_export(lock->l_export);
584 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
585 struct ptlrpc_request *req, int rc,
586 const char *ast_type)
588 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
590 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
591 LASSERT(lock->l_export);
592 if (lock->l_export->exp_libclient) {
593 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
594 " timeout, just cancelling lock", ast_type,
595 libcfs_nid2str(peer.nid));
596 ldlm_lock_cancel(lock);
598 } else if (lock->l_flags & LDLM_FL_CANCEL) {
599 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
600 "cancel was received (AST reply lost?)",
601 ast_type, libcfs_nid2str(peer.nid));
602 ldlm_lock_cancel(lock);
605 ldlm_del_waiting_lock(lock);
606 ldlm_failed_ast(lock, rc, ast_type);
610 struct ldlm_resource *res = lock->l_resource;
611 LDLM_DEBUG(lock, "client (nid %s) returned %d"
612 " from %s AST - normal race",
613 libcfs_nid2str(peer.nid),
615 lustre_msg_get_status(req->rq_repmsg) : -1,
618 /* update lvbo to return proper attributes.
620 ldlm_resource_getref(res);
621 ldlm_res_lvbo_update(res, NULL, 1);
622 ldlm_resource_putref(res);
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,
632 ldlm_lock_cancel(lock);
633 /* Server-side AST functions are called from ldlm_reprocess_all,
634 * which needs to be told to please restart its reprocessing. */
641 static int ldlm_cb_interpret(const struct lu_env *env,
642 struct ptlrpc_request *req, void *data, int rc)
644 struct ldlm_cb_set_arg *arg;
645 struct ldlm_lock *lock;
648 LASSERT(data != NULL);
650 arg = req->rq_async_args.pointer_arg[0];
651 lock = req->rq_async_args.pointer_arg[1];
652 LASSERT(lock != NULL);
654 rc = ldlm_handle_ast_error(lock, req, rc,
655 arg->type == LDLM_BL_CALLBACK
656 ? "blocking" : "completion");
659 LDLM_LOCK_RELEASE(lock);
662 cfs_atomic_set(&arg->restart, 1);
667 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
668 struct ldlm_cb_set_arg *arg,
669 struct ldlm_lock *lock,
675 if (unlikely(instant_cancel)) {
676 rc = ptl_send_rpc(req, 1);
677 ptlrpc_req_finished(req);
679 /* If we cancelled the lock, we need to restart
680 * ldlm_reprocess_queue */
681 cfs_atomic_set(&arg->restart, 1);
684 ptlrpc_set_add_req(arg->set, req);
691 * Check if there are requests in the export request list which prevent
692 * the lock canceling and make these requests high priority ones.
694 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
696 struct ptlrpc_request *req;
699 if (lock->l_export == NULL) {
700 LDLM_DEBUG(lock, "client lock: no-op");
704 cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
705 cfs_list_for_each_entry(req, &lock->l_export->exp_queued_rpc,
707 if (!req->rq_hp && req->rq_ops->hpreq_lock_match &&
708 req->rq_ops->hpreq_lock_match(req, lock))
709 ptlrpc_hpreq_reorder(req);
711 cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
716 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
717 * enqueued server lock conflicts with given one.
719 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
720 * to wait for client response.
722 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
723 struct ldlm_lock_desc *desc,
724 void *data, int flag)
726 struct ldlm_cb_set_arg *arg = data;
727 struct ldlm_request *body;
728 struct ptlrpc_request *req;
729 int instant_cancel = 0;
733 if (flag == LDLM_CB_CANCELING)
734 /* Don't need to do anything here. */
738 LASSERT(data != NULL);
739 if (lock->l_export->exp_obd->obd_recovering != 0) {
740 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
741 ldlm_lock_dump(D_ERROR, lock, 0);
744 ldlm_lock_reorder_req(lock);
746 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
747 &RQF_LDLM_BL_CALLBACK,
748 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
752 req->rq_async_args.pointer_arg[0] = arg;
753 req->rq_async_args.pointer_arg[1] = lock;
754 req->rq_interpret_reply = ldlm_cb_interpret;
755 req->rq_no_resend = 1;
757 lock_res(lock->l_resource);
758 if (lock->l_granted_mode != lock->l_req_mode) {
759 /* this blocking AST will be communicated as part of the
760 * completion AST instead */
761 unlock_res(lock->l_resource);
762 ptlrpc_req_finished(req);
763 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
767 if (lock->l_destroyed) {
768 /* What's the point? */
769 unlock_res(lock->l_resource);
770 ptlrpc_req_finished(req);
774 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
777 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
778 body->lock_handle[0] = lock->l_remote_handle;
779 body->lock_desc = *desc;
780 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
782 LDLM_DEBUG(lock, "server preparing blocking AST");
784 ptlrpc_request_set_replen(req);
785 if (instant_cancel) {
786 unlock_res(lock->l_resource);
787 ldlm_lock_cancel(lock);
789 LASSERT(lock->l_granted_mode == lock->l_req_mode);
790 ldlm_add_waiting_lock(lock);
791 unlock_res(lock->l_resource);
794 req->rq_send_state = LUSTRE_IMP_FULL;
795 /* ptlrpc_request_alloc_pack already set timeout */
797 req->rq_timeout = ldlm_get_rq_timeout();
799 if (lock->l_export && lock->l_export->exp_nid_stats &&
800 lock->l_export->exp_nid_stats->nid_ldlm_stats)
801 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
802 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
804 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
809 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
811 struct ldlm_cb_set_arg *arg = data;
812 struct ldlm_request *body;
813 struct ptlrpc_request *req;
814 long total_enqueue_wait;
815 int instant_cancel = 0;
819 LASSERT(lock != NULL);
820 LASSERT(data != NULL);
822 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
823 lock->l_last_activity);
825 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
826 &RQF_LDLM_CP_CALLBACK);
830 lock_res_and_lock(lock);
831 if (lock->l_resource->lr_lvb_len)
832 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
833 lock->l_resource->lr_lvb_len);
834 unlock_res_and_lock(lock);
836 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
838 ptlrpc_request_free(req);
842 req->rq_async_args.pointer_arg[0] = arg;
843 req->rq_async_args.pointer_arg[1] = lock;
844 req->rq_interpret_reply = ldlm_cb_interpret;
845 req->rq_no_resend = 1;
846 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
848 body->lock_handle[0] = lock->l_remote_handle;
849 body->lock_flags = flags;
850 ldlm_lock2desc(lock, &body->lock_desc);
851 if (lock->l_resource->lr_lvb_len) {
852 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
854 lock_res_and_lock(lock);
855 memcpy(lvb, lock->l_resource->lr_lvb_data,
856 lock->l_resource->lr_lvb_len);
857 unlock_res_and_lock(lock);
860 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
863 /* Server-side enqueue wait time estimate, used in
864 __ldlm_add_waiting_lock to set future enqueue timers */
865 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
866 at_measured(&lock->l_resource->lr_namespace->ns_at_estimate,
869 /* bz18618. Don't add lock enqueue time we spend waiting for a
870 previous callback to fail. Locks waiting legitimately will
871 get extended by ldlm_refresh_waiting_lock regardless of the
872 estimate, so it's okay to underestimate here. */
873 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
874 "It is likely that a previous callback timed out.",
876 at_get(&lock->l_resource->lr_namespace->ns_at_estimate));
878 ptlrpc_request_set_replen(req);
880 req->rq_send_state = LUSTRE_IMP_FULL;
881 /* ptlrpc_request_pack already set timeout */
883 req->rq_timeout = ldlm_get_rq_timeout();
885 /* We only send real blocking ASTs after the lock is granted */
886 lock_res_and_lock(lock);
887 if (lock->l_flags & LDLM_FL_AST_SENT) {
888 body->lock_flags |= LDLM_FL_AST_SENT;
889 /* copy ast flags like LDLM_FL_DISCARD_DATA */
890 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
892 /* We might get here prior to ldlm_handle_enqueue setting
893 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
894 * into waiting list, but this is safe and similar code in
895 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
896 * that would not only cancel the lock, but will also remove
897 * it from waiting list */
898 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
899 unlock_res_and_lock(lock);
900 ldlm_lock_cancel(lock);
902 lock_res_and_lock(lock);
904 /* start the lock-timeout clock */
905 ldlm_add_waiting_lock(lock);
908 unlock_res_and_lock(lock);
910 if (lock->l_export && lock->l_export->exp_nid_stats &&
911 lock->l_export->exp_nid_stats->nid_ldlm_stats)
912 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
913 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
915 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
920 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
922 struct ldlm_resource *res = lock->l_resource;
923 struct ldlm_request *body;
924 struct ptlrpc_request *req;
928 LASSERT(lock != NULL);
930 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
931 &RQF_LDLM_GL_CALLBACK,
932 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
937 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
938 body->lock_handle[0] = lock->l_remote_handle;
939 ldlm_lock2desc(lock, &body->lock_desc);
941 lock_res_and_lock(lock);
942 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
943 lock->l_resource->lr_lvb_len);
944 unlock_res_and_lock(lock);
945 res = lock->l_resource;
946 ptlrpc_request_set_replen(req);
949 req->rq_send_state = LUSTRE_IMP_FULL;
950 /* ptlrpc_request_alloc_pack already set timeout */
952 req->rq_timeout = ldlm_get_rq_timeout();
954 if (lock->l_export && lock->l_export->exp_nid_stats &&
955 lock->l_export->exp_nid_stats->nid_ldlm_stats)
956 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
957 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
959 rc = ptlrpc_queue_wait(req);
960 if (rc == -ELDLM_NO_LOCK_DATA)
961 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
963 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
965 rc = ldlm_res_lvbo_update(res, req, 1);
967 ptlrpc_req_finished(req);
969 ldlm_reprocess_all(res);
975 extern unsigned long long lu_time_stamp_get(void);
977 #define lu_time_stamp_get() time(NULL)
980 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
981 struct lprocfs_stats *srv_stats)
983 int lock_type = 0, op = 0;
985 lock_type = dlm_req->lock_desc.l_resource.lr_type;
989 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
992 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
993 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
995 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
998 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1001 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1009 lprocfs_counter_incr(srv_stats, op);
1015 * Main server-side entry point into LDLM. This is called by ptlrpc service
1016 * threads to carry out client lock enqueueing requests.
1018 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1019 struct ptlrpc_request *req,
1020 const struct ldlm_request *dlm_req,
1021 const struct ldlm_callback_suite *cbs)
1023 struct ldlm_reply *dlm_rep;
1025 ldlm_error_t err = ELDLM_OK;
1026 struct ldlm_lock *lock = NULL;
1027 void *cookie = NULL;
1031 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1033 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1034 flags = dlm_req->lock_flags;
1036 LASSERT(req->rq_export);
1038 if (req->rq_rqbd->rqbd_service->srv_stats)
1039 ldlm_svc_get_eopc(dlm_req,
1040 req->rq_rqbd->rqbd_service->srv_stats);
1042 if (req->rq_export && req->rq_export->exp_nid_stats &&
1043 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1044 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1045 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1047 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1048 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1049 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1050 dlm_req->lock_desc.l_resource.lr_type);
1051 GOTO(out, rc = -EFAULT);
1054 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1055 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1056 dlm_req->lock_desc.l_req_mode &
1057 (dlm_req->lock_desc.l_req_mode-1))) {
1058 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1059 dlm_req->lock_desc.l_req_mode);
1060 GOTO(out, rc = -EFAULT);
1063 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1064 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1066 DEBUG_REQ(D_ERROR, req,
1067 "PLAIN lock request from IBITS client?");
1068 GOTO(out, rc = -EPROTO);
1070 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1072 DEBUG_REQ(D_ERROR, req,
1073 "IBITS lock request from unaware client?");
1074 GOTO(out, rc = -EPROTO);
1078 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1079 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1080 ibits for mgc/mgs) */
1082 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1083 * inodebits lock if client does not support them. */
1084 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1085 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1086 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1087 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1088 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1089 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1090 dlm_req->lock_desc.l_req_mode = LCK_CR;
1094 if (unlikely(flags & LDLM_FL_REPLAY)) {
1095 /* Find an existing lock in the per-export lock hash */
1096 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1097 (void *)&dlm_req->lock_handle[0]);
1099 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1100 LPX64, lock->l_handle.h_cookie);
1101 GOTO(existing_lock, rc = 0);
1105 /* The lock's callback data might be set in the policy function */
1106 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1107 dlm_req->lock_desc.l_resource.lr_type,
1108 dlm_req->lock_desc.l_req_mode,
1112 GOTO(out, rc = -ENOMEM);
1114 lock->l_last_activity = cfs_time_current_sec();
1115 lock->l_remote_handle = dlm_req->lock_handle[0];
1116 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1118 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1119 /* Don't enqueue a lock onto the export if it has already
1120 * been evicted. Cancel it now instead. (bug 3822) */
1121 if (req->rq_export->exp_failed) {
1122 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1123 GOTO(out, rc = -ENOTCONN);
1126 lock->l_export = class_export_lock_get(req->rq_export, lock);
1127 if (lock->l_export->exp_lock_hash)
1128 cfs_hash_add(lock->l_export->exp_lock_hash,
1129 &lock->l_remote_handle,
1134 if (flags & LDLM_FL_HAS_INTENT) {
1135 /* In this case, the reply buffer is allocated deep in
1136 * local_lock_enqueue by the policy function. */
1139 lock_res_and_lock(lock);
1140 if (lock->l_resource->lr_lvb_len) {
1141 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1143 lock->l_resource->lr_lvb_len);
1145 unlock_res_and_lock(lock);
1147 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1148 GOTO(out, rc = -ENOMEM);
1150 rc = req_capsule_server_pack(&req->rq_pill);
1155 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1156 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1157 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1158 lock->l_req_extent = lock->l_policy_data.l_extent;
1160 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1164 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1165 dlm_rep->lock_flags = flags;
1167 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1168 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1170 /* We never send a blocking AST until the lock is granted, but
1171 * we can tell it right now */
1172 lock_res_and_lock(lock);
1174 /* Now take into account flags to be inherited from original lock
1175 request both in reply to client and in our own lock flags. */
1176 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1177 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1179 /* Don't move a pending lock onto the export if it has already
1180 * been evicted. Cancel it now instead. (bug 5683) */
1181 if (unlikely(req->rq_export->exp_failed ||
1182 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1183 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1185 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1186 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1187 if (lock->l_granted_mode == lock->l_req_mode) {
1189 * Only cancel lock if it was granted, because it would
1190 * be destroyed immediately and would never be granted
1191 * in the future, causing timeouts on client. Not
1192 * granted lock will be cancelled immediately after
1193 * sending completion AST.
1195 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1196 unlock_res_and_lock(lock);
1197 ldlm_lock_cancel(lock);
1198 lock_res_and_lock(lock);
1200 ldlm_add_waiting_lock(lock);
1203 /* Make sure we never ever grant usual metadata locks to liblustre
1205 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1206 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1207 req->rq_export->exp_libclient) {
1208 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1209 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1210 CERROR("Granting sync lock to libclient. "
1211 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1212 dlm_req->lock_flags, dlm_rep->lock_flags,
1214 LDLM_ERROR(lock, "sync lock");
1215 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1216 struct ldlm_intent *it;
1218 it = req_capsule_client_get(&req->rq_pill,
1221 CERROR("This is intent %s ("LPU64")\n",
1222 ldlm_it2str(it->opc), it->opc);
1228 unlock_res_and_lock(lock);
1232 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1233 if (!req->rq_packed_final) {
1234 err = lustre_pack_reply(req, 1, NULL, NULL);
1239 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1240 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1242 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1243 "(err=%d, rc=%d)", err, rc);
1245 lock_res_and_lock(lock);
1247 if (lock->l_resource->lr_lvb_len > 0) {
1250 lvb = req_capsule_server_get(&req->rq_pill,
1252 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1255 memcpy(lvb, lock->l_resource->lr_lvb_data,
1256 lock->l_resource->lr_lvb_len);
1259 ldlm_resource_unlink_lock(lock);
1260 ldlm_lock_destroy_nolock(lock);
1262 unlock_res_and_lock(lock);
1264 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1265 ldlm_reprocess_all(lock->l_resource);
1267 LDLM_LOCK_RELEASE(lock);
1270 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1276 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1277 ldlm_completion_callback completion_callback,
1278 ldlm_blocking_callback blocking_callback,
1279 ldlm_glimpse_callback glimpse_callback)
1281 struct ldlm_request *dlm_req;
1282 struct ldlm_callback_suite cbs = {
1283 .lcs_completion = completion_callback,
1284 .lcs_blocking = blocking_callback,
1285 .lcs_glimpse = glimpse_callback
1289 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1290 if (dlm_req != NULL) {
1291 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1292 req, dlm_req, &cbs);
1299 int ldlm_handle_convert0(struct ptlrpc_request *req,
1300 const struct ldlm_request *dlm_req)
1302 struct ldlm_reply *dlm_rep;
1303 struct ldlm_lock *lock;
1307 if (req->rq_export && req->rq_export->exp_nid_stats &&
1308 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1309 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1310 LDLM_CONVERT - LDLM_FIRST_OPC);
1312 rc = req_capsule_server_pack(&req->rq_pill);
1316 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1317 dlm_rep->lock_flags = dlm_req->lock_flags;
1319 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1321 req->rq_status = EINVAL;
1325 LDLM_DEBUG(lock, "server-side convert handler START");
1327 lock->l_last_activity = cfs_time_current_sec();
1328 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1329 &dlm_rep->lock_flags);
1331 if (ldlm_del_waiting_lock(lock))
1332 LDLM_DEBUG(lock, "converted waiting lock");
1335 req->rq_status = EDEADLOCK;
1340 if (!req->rq_status)
1341 ldlm_reprocess_all(lock->l_resource);
1342 LDLM_DEBUG(lock, "server-side convert handler END");
1343 LDLM_LOCK_PUT(lock);
1345 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1350 int ldlm_handle_convert(struct ptlrpc_request *req)
1353 struct ldlm_request *dlm_req;
1355 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1356 if (dlm_req != NULL) {
1357 rc = ldlm_handle_convert0(req, dlm_req);
1359 CERROR ("Can't unpack dlm_req\n");
1365 /* Cancel all the locks whos handles are packed into ldlm_request */
1366 int ldlm_request_cancel(struct ptlrpc_request *req,
1367 const struct ldlm_request *dlm_req, int first)
1369 struct ldlm_resource *res, *pres = NULL;
1370 struct ldlm_lock *lock;
1371 int i, count, done = 0;
1374 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1378 /* There is no lock on the server at the replay time,
1379 * skip lock cancelling to make replay tests to pass. */
1380 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1383 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1384 "starting at %d", count, first);
1386 for (i = first; i < count; i++) {
1387 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1389 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1390 "lock (cookie "LPU64")",
1391 dlm_req->lock_handle[i].cookie);
1395 res = lock->l_resource;
1400 ldlm_reprocess_all(pres);
1401 LDLM_RESOURCE_DELREF(pres);
1402 ldlm_resource_putref(pres);
1405 ldlm_resource_getref(res);
1406 LDLM_RESOURCE_ADDREF(res);
1407 ldlm_res_lvbo_update(res, NULL, 1);
1411 ldlm_lock_cancel(lock);
1412 LDLM_LOCK_PUT(lock);
1415 ldlm_reprocess_all(pres);
1416 LDLM_RESOURCE_DELREF(pres);
1417 ldlm_resource_putref(pres);
1419 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1423 int ldlm_handle_cancel(struct ptlrpc_request *req)
1425 struct ldlm_request *dlm_req;
1429 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1430 if (dlm_req == NULL) {
1431 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1435 if (req->rq_export && req->rq_export->exp_nid_stats &&
1436 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1437 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1438 LDLM_CANCEL - LDLM_FIRST_OPC);
1440 rc = req_capsule_server_pack(&req->rq_pill);
1444 if (!ldlm_request_cancel(req, dlm_req, 0))
1445 req->rq_status = ESTALE;
1447 RETURN(ptlrpc_reply(req));
1450 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1451 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1456 LDLM_DEBUG(lock, "client blocking AST callback handler");
1458 lock_res_and_lock(lock);
1459 lock->l_flags |= LDLM_FL_CBPENDING;
1461 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1462 lock->l_flags |= LDLM_FL_CANCEL;
1464 do_ast = (!lock->l_readers && !lock->l_writers);
1465 unlock_res_and_lock(lock);
1468 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1469 lock, lock->l_blocking_ast);
1470 if (lock->l_blocking_ast != NULL)
1471 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1474 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1478 LDLM_DEBUG(lock, "client blocking callback handler END");
1479 LDLM_LOCK_RELEASE(lock);
1483 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1484 struct ldlm_namespace *ns,
1485 struct ldlm_request *dlm_req,
1486 struct ldlm_lock *lock)
1488 CFS_LIST_HEAD(ast_list);
1491 LDLM_DEBUG(lock, "client completion callback handler START");
1493 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1494 int to = cfs_time_seconds(1);
1496 cfs_schedule_timeout_and_set_state(
1497 CFS_TASK_INTERRUPTIBLE, to);
1498 if (lock->l_granted_mode == lock->l_req_mode ||
1504 lock_res_and_lock(lock);
1505 if (lock->l_destroyed ||
1506 lock->l_granted_mode == lock->l_req_mode) {
1507 /* bug 11300: the lock has already been granted */
1508 unlock_res_and_lock(lock);
1509 LDLM_DEBUG(lock, "Double grant race happened");
1510 LDLM_LOCK_RELEASE(lock);
1515 /* If we receive the completion AST before the actual enqueue returned,
1516 * then we might need to switch lock modes, resources, or extents. */
1517 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1518 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1519 LDLM_DEBUG(lock, "completion AST, new lock mode");
1522 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1523 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1524 LDLM_DEBUG(lock, "completion AST, new policy data");
1527 ldlm_resource_unlink_lock(lock);
1528 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1529 &lock->l_resource->lr_name,
1530 sizeof(lock->l_resource->lr_name)) != 0) {
1531 unlock_res_and_lock(lock);
1532 if (ldlm_lock_change_resource(ns, lock,
1533 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1534 LDLM_ERROR(lock, "Failed to allocate resource");
1535 LDLM_LOCK_RELEASE(lock);
1539 LDLM_DEBUG(lock, "completion AST, new resource");
1540 CERROR("change resource!\n");
1541 lock_res_and_lock(lock);
1544 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1545 /* BL_AST locks are not needed in lru.
1546 * let ldlm_cancel_lru() be fast. */
1547 ldlm_lock_remove_from_lru(lock);
1548 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1549 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1552 if (lock->l_lvb_len) {
1553 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1554 RCL_CLIENT) < lock->l_lvb_len) {
1555 LDLM_ERROR(lock, "completion AST did not contain "
1558 void *lvb = req_capsule_client_get(&req->rq_pill,
1560 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1564 ldlm_grant_lock(lock, &ast_list);
1565 unlock_res_and_lock(lock);
1567 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1569 /* Let Enqueue to call osc_lock_upcall() and initialize
1571 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1573 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1575 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1577 LDLM_LOCK_RELEASE(lock);
1581 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1582 struct ldlm_namespace *ns,
1583 struct ldlm_request *dlm_req,
1584 struct ldlm_lock *lock)
1589 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1591 if (lock->l_glimpse_ast != NULL)
1592 rc = lock->l_glimpse_ast(lock, req);
1594 if (req->rq_repmsg != NULL) {
1597 req->rq_status = rc;
1601 lock_res_and_lock(lock);
1602 if (lock->l_granted_mode == LCK_PW &&
1603 !lock->l_readers && !lock->l_writers &&
1604 cfs_time_after(cfs_time_current(),
1605 cfs_time_add(lock->l_last_used,
1606 cfs_time_seconds(10)))) {
1607 unlock_res_and_lock(lock);
1608 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1609 ldlm_handle_bl_callback(ns, NULL, lock);
1614 unlock_res_and_lock(lock);
1615 LDLM_LOCK_RELEASE(lock);
1619 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1621 if (req->rq_no_reply)
1624 req->rq_status = rc;
1625 if (!req->rq_packed_final) {
1626 rc = lustre_pack_reply(req, 1, NULL, NULL);
1630 return ptlrpc_reply(req);
1634 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1636 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1639 cfs_spin_lock(&blp->blp_lock);
1640 if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1641 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1642 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1644 /* other blocking callbacks are added to the regular list */
1645 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1647 cfs_spin_unlock(&blp->blp_lock);
1649 cfs_waitq_signal(&blp->blp_waitq);
1651 /* can not use blwi->blwi_mode as blwi could be already freed in
1653 if (mode == LDLM_SYNC)
1654 cfs_wait_for_completion(&blwi->blwi_comp);
1659 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1660 struct ldlm_namespace *ns,
1661 struct ldlm_lock_desc *ld,
1662 cfs_list_t *cancels, int count,
1663 struct ldlm_lock *lock,
1666 cfs_init_completion(&blwi->blwi_comp);
1667 CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1669 if (cfs_memory_pressure_get())
1670 blwi->blwi_mem_pressure = 1;
1673 blwi->blwi_mode = mode;
1675 blwi->blwi_ld = *ld;
1677 cfs_list_add(&blwi->blwi_head, cancels);
1678 cfs_list_del_init(cancels);
1679 blwi->blwi_count = count;
1681 blwi->blwi_lock = lock;
1685 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1686 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1687 cfs_list_t *cancels, int count, int mode)
1691 if (cancels && count == 0)
1694 if (mode == LDLM_SYNC) {
1695 /* if it is synchronous call do minimum mem alloc, as it could
1696 * be triggered from kernel shrinker
1698 struct ldlm_bl_work_item blwi;
1699 memset(&blwi, 0, sizeof(blwi));
1700 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1701 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1703 struct ldlm_bl_work_item *blwi;
1704 OBD_ALLOC(blwi, sizeof(*blwi));
1707 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1709 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1715 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1716 struct ldlm_lock *lock)
1719 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1725 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1726 cfs_list_t *cancels, int count, int mode)
1729 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1735 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1736 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1738 struct obd_device *obd = req->rq_export->exp_obd;
1745 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1747 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1749 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1751 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1754 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1756 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1758 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1761 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1764 /* We are responsible for swabbing contents of val */
1766 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1767 /* Pass it on to mdc (the "export" in this case) */
1768 rc = obd_set_info_async(req->rq_export,
1769 sizeof(KEY_HSM_COPYTOOL_SEND),
1770 KEY_HSM_COPYTOOL_SEND,
1773 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1778 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1779 const char *msg, int rc,
1780 struct lustre_handle *handle)
1782 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1783 "%s: [nid %s] [rc %d] [lock "LPX64"]",
1784 msg, libcfs_id2str(req->rq_peer), rc,
1785 handle ? handle->cookie : 0);
1786 if (req->rq_no_reply)
1787 CWARN("No reply was sent, maybe cause bug 21636.\n");
1789 CWARN("Send reply failed, maybe cause bug 21636.\n");
1792 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1793 static int ldlm_callback_handler(struct ptlrpc_request *req)
1795 struct ldlm_namespace *ns;
1796 struct ldlm_request *dlm_req;
1797 struct ldlm_lock *lock;
1801 /* Requests arrive in sender's byte order. The ptlrpc service
1802 * handler has already checked and, if necessary, byte-swapped the
1803 * incoming request message body, but I am responsible for the
1804 * message buffers. */
1806 /* do nothing for sec context finalize */
1807 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1810 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1812 if (req->rq_export == NULL) {
1813 rc = ldlm_callback_reply(req, -ENOTCONN);
1814 ldlm_callback_errmsg(req, "Operate on unconnected server",
1819 LASSERT(req->rq_export != NULL);
1820 LASSERT(req->rq_export->exp_obd != NULL);
1822 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1823 case LDLM_BL_CALLBACK:
1824 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1827 case LDLM_CP_CALLBACK:
1828 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1831 case LDLM_GL_CALLBACK:
1832 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1836 rc = ldlm_handle_setinfo(req);
1837 ldlm_callback_reply(req, rc);
1839 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1840 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1841 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1842 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1844 rc = llog_origin_handle_cancel(req);
1845 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1847 ldlm_callback_reply(req, rc);
1849 case OBD_QC_CALLBACK:
1850 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1851 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1853 rc = target_handle_qc_callback(req);
1854 ldlm_callback_reply(req, rc);
1858 /* reply in handler */
1859 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1860 rc = target_handle_dqacq_callback(req);
1862 case LLOG_ORIGIN_HANDLE_CREATE:
1863 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1864 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1866 rc = llog_origin_handle_create(req);
1867 ldlm_callback_reply(req, rc);
1869 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1870 req_capsule_set(&req->rq_pill,
1871 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1872 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1874 rc = llog_origin_handle_next_block(req);
1875 ldlm_callback_reply(req, rc);
1877 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1878 req_capsule_set(&req->rq_pill,
1879 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1880 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1882 rc = llog_origin_handle_read_header(req);
1883 ldlm_callback_reply(req, rc);
1885 case LLOG_ORIGIN_HANDLE_CLOSE:
1886 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1888 rc = llog_origin_handle_close(req);
1889 ldlm_callback_reply(req, rc);
1892 CERROR("unknown opcode %u\n",
1893 lustre_msg_get_opc(req->rq_reqmsg));
1894 ldlm_callback_reply(req, -EPROTO);
1898 ns = req->rq_export->exp_obd->obd_namespace;
1899 LASSERT(ns != NULL);
1901 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1903 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1904 if (dlm_req == NULL) {
1905 rc = ldlm_callback_reply(req, -EPROTO);
1906 ldlm_callback_errmsg(req, "Operate without parameter", rc,
1911 /* Force a known safe race, send a cancel to the server for a lock
1912 * which the server has already started a blocking callback on. */
1913 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1914 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1915 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1917 CERROR("ldlm_cli_cancel: %d\n", rc);
1920 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1922 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1923 "disappeared\n", dlm_req->lock_handle[0].cookie);
1924 rc = ldlm_callback_reply(req, -EINVAL);
1925 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
1926 &dlm_req->lock_handle[0]);
1930 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
1931 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
1932 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
1934 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1935 lock_res_and_lock(lock);
1936 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1937 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1938 /* If somebody cancels lock and cache is already dropped,
1939 * or lock is failed before cp_ast received on client,
1940 * we can tell the server we have no lock. Otherwise, we
1941 * should send cancel after dropping the cache. */
1942 if (((lock->l_flags & LDLM_FL_CANCELING) &&
1943 (lock->l_flags & LDLM_FL_BL_DONE)) ||
1944 (lock->l_flags & LDLM_FL_FAILED)) {
1945 LDLM_DEBUG(lock, "callback on lock "
1946 LPX64" - lock disappeared\n",
1947 dlm_req->lock_handle[0].cookie);
1948 unlock_res_and_lock(lock);
1949 LDLM_LOCK_RELEASE(lock);
1950 rc = ldlm_callback_reply(req, -EINVAL);
1951 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
1952 &dlm_req->lock_handle[0]);
1955 /* BL_AST locks are not needed in lru.
1956 * let ldlm_cancel_lru() be fast. */
1957 ldlm_lock_remove_from_lru(lock);
1958 lock->l_flags |= LDLM_FL_BL_AST;
1960 unlock_res_and_lock(lock);
1962 /* We want the ost thread to get this reply so that it can respond
1963 * to ost requests (write cache writeback) that might be triggered
1966 * But we'd also like to be able to indicate in the reply that we're
1967 * cancelling right now, because it's unused, or have an intent result
1968 * in the reply, so we might have to push the responsibility for sending
1969 * the reply down into the AST handlers, alas. */
1971 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1972 case LDLM_BL_CALLBACK:
1973 CDEBUG(D_INODE, "blocking ast\n");
1974 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1975 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
1976 rc = ldlm_callback_reply(req, 0);
1977 if (req->rq_no_reply || rc)
1978 ldlm_callback_errmsg(req, "Normal process", rc,
1979 &dlm_req->lock_handle[0]);
1981 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1982 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1984 case LDLM_CP_CALLBACK:
1985 CDEBUG(D_INODE, "completion ast\n");
1986 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1987 ldlm_callback_reply(req, 0);
1988 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1990 case LDLM_GL_CALLBACK:
1991 CDEBUG(D_INODE, "glimpse ast\n");
1992 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1993 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1996 LBUG(); /* checked above */
2002 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2007 /* Requests arrive in sender's byte order. The ptlrpc service
2008 * handler has already checked and, if necessary, byte-swapped the
2009 * incoming request message body, but I am responsible for the
2010 * message buffers. */
2012 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2014 if (req->rq_export == NULL) {
2015 struct ldlm_request *dlm_req;
2017 CERROR("%s from %s arrived at %lu with bad export cookie "
2019 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2020 libcfs_nid2str(req->rq_peer.nid),
2021 req->rq_arrival_time.tv_sec,
2022 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2024 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2025 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2026 dlm_req = req_capsule_client_get(&req->rq_pill,
2028 if (dlm_req != NULL)
2029 ldlm_lock_dump_handle(D_ERROR,
2030 &dlm_req->lock_handle[0]);
2032 ldlm_callback_reply(req, -ENOTCONN);
2036 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2038 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2040 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2041 CDEBUG(D_INODE, "cancel\n");
2042 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2044 rc = ldlm_handle_cancel(req);
2048 case OBD_LOG_CANCEL:
2049 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2050 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2052 rc = llog_origin_handle_cancel(req);
2053 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2055 ldlm_callback_reply(req, rc);
2058 CERROR("invalid opcode %d\n",
2059 lustre_msg_get_opc(req->rq_reqmsg));
2060 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2061 ldlm_callback_reply(req, -EINVAL);
2067 void ldlm_revoke_lock_cb(void *obj, void *data)
2069 cfs_list_t *rpc_list = data;
2070 struct ldlm_lock *lock = obj;
2072 lock_res_and_lock(lock);
2074 if (lock->l_req_mode != lock->l_granted_mode) {
2075 unlock_res_and_lock(lock);
2079 LASSERT(lock->l_resource);
2080 if (lock->l_resource->lr_type != LDLM_IBITS &&
2081 lock->l_resource->lr_type != LDLM_PLAIN) {
2082 unlock_res_and_lock(lock);
2086 if (lock->l_flags & LDLM_FL_AST_SENT) {
2087 unlock_res_and_lock(lock);
2091 LASSERT(lock->l_blocking_ast);
2092 LASSERT(!lock->l_blocking_lock);
2094 lock->l_flags |= LDLM_FL_AST_SENT;
2095 if (lock->l_export && lock->l_export->exp_lock_hash &&
2096 !cfs_hlist_unhashed(&lock->l_exp_hash))
2097 cfs_hash_del(lock->l_export->exp_lock_hash,
2098 &lock->l_remote_handle, &lock->l_exp_hash);
2099 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2100 LDLM_LOCK_GET(lock);
2102 unlock_res_and_lock(lock);
2105 void ldlm_revoke_export_locks(struct obd_export *exp)
2107 cfs_list_t rpc_list;
2110 CFS_INIT_LIST_HEAD(&rpc_list);
2111 cfs_hash_for_each_empty(exp->exp_lock_hash,
2112 ldlm_revoke_lock_cb, &rpc_list);
2113 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
2119 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2121 struct ldlm_bl_work_item *blwi = NULL;
2122 static unsigned int num_bl = 0;
2124 cfs_spin_lock(&blp->blp_lock);
2125 /* process a request from the blp_list at least every blp_num_threads */
2126 if (!cfs_list_empty(&blp->blp_list) &&
2127 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2128 blwi = cfs_list_entry(blp->blp_list.next,
2129 struct ldlm_bl_work_item, blwi_entry);
2131 if (!cfs_list_empty(&blp->blp_prio_list))
2132 blwi = cfs_list_entry(blp->blp_prio_list.next,
2133 struct ldlm_bl_work_item,
2137 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2139 cfs_list_del(&blwi->blwi_entry);
2141 cfs_spin_unlock(&blp->blp_lock);
2146 /* This only contains temporary data until the thread starts */
2147 struct ldlm_bl_thread_data {
2148 char bltd_name[CFS_CURPROC_COMM_MAX];
2149 struct ldlm_bl_pool *bltd_blp;
2150 cfs_completion_t bltd_comp;
2154 static int ldlm_bl_thread_main(void *arg);
2156 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2158 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2161 cfs_init_completion(&bltd.bltd_comp);
2162 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
2164 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2165 cfs_atomic_read(&blp->blp_num_threads), rc);
2168 cfs_wait_for_completion(&bltd.bltd_comp);
2173 static int ldlm_bl_thread_main(void *arg)
2175 struct ldlm_bl_pool *blp;
2179 struct ldlm_bl_thread_data *bltd = arg;
2181 blp = bltd->bltd_blp;
2184 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2185 cfs_atomic_inc(&blp->blp_busy_threads);
2187 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2188 "ldlm_bl_%02d", bltd->bltd_num);
2189 cfs_daemonize(bltd->bltd_name);
2191 cfs_complete(&bltd->bltd_comp);
2192 /* cannot use bltd after this, it is only on caller's stack */
2196 struct l_wait_info lwi = { 0 };
2197 struct ldlm_bl_work_item *blwi = NULL;
2199 blwi = ldlm_bl_get_work(blp);
2204 cfs_atomic_dec(&blp->blp_busy_threads);
2205 l_wait_event_exclusive(blp->blp_waitq,
2206 (blwi = ldlm_bl_get_work(blp)) != NULL,
2208 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2210 if (blwi->blwi_ns == NULL)
2211 /* added by ldlm_cleanup() */
2214 /* Not fatal if racy and have a few too many threads */
2215 if (unlikely(busy < blp->blp_max_threads &&
2216 busy >= cfs_atomic_read(&blp->blp_num_threads)))
2217 /* discard the return value, we tried */
2218 ldlm_bl_thread_start(blp);
2220 if (blwi->blwi_ns == NULL)
2221 /* added by ldlm_cleanup() */
2224 if (blwi->blwi_mem_pressure)
2225 cfs_memory_pressure_set();
2227 if (blwi->blwi_count) {
2229 /* The special case when we cancel locks in lru
2230 * asynchronously, we pass the list of locks here.
2231 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2232 * canceled locally yet. */
2233 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2236 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2238 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2241 if (blwi->blwi_mem_pressure)
2242 cfs_memory_pressure_clr();
2244 if (blwi->blwi_mode == LDLM_ASYNC)
2245 OBD_FREE(blwi, sizeof(*blwi));
2247 cfs_complete(&blwi->blwi_comp);
2250 cfs_atomic_dec(&blp->blp_busy_threads);
2251 cfs_atomic_dec(&blp->blp_num_threads);
2252 cfs_complete(&blp->blp_comp);
2258 static int ldlm_setup(void);
2259 static int ldlm_cleanup(void);
2261 int ldlm_get_ref(void)
2265 cfs_mutex_down(&ldlm_ref_sem);
2266 if (++ldlm_refcount == 1) {
2271 cfs_mutex_up(&ldlm_ref_sem);
2276 void ldlm_put_ref(void)
2279 cfs_mutex_down(&ldlm_ref_sem);
2280 if (ldlm_refcount == 1) {
2281 int rc = ldlm_cleanup();
2283 CERROR("ldlm_cleanup failed: %d\n", rc);
2289 cfs_mutex_up(&ldlm_ref_sem);
2295 * Export handle<->lock hash operations.
2298 ldlm_export_lock_hash(cfs_hash_t *hs, void *key, unsigned mask)
2300 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2304 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2306 struct ldlm_lock *lock;
2309 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2310 RETURN(&lock->l_remote_handle);
2314 ldlm_export_lock_compare(void *key, cfs_hlist_node_t *hnode)
2317 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2321 ldlm_export_lock_get(cfs_hlist_node_t *hnode)
2323 struct ldlm_lock *lock;
2326 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2327 LDLM_LOCK_GET(lock);
2333 ldlm_export_lock_put(cfs_hlist_node_t *hnode)
2335 struct ldlm_lock *lock;
2338 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2339 LDLM_LOCK_RELEASE(lock);
2344 static cfs_hash_ops_t ldlm_export_lock_ops = {
2345 .hs_hash = ldlm_export_lock_hash,
2346 .hs_key = ldlm_export_lock_key,
2347 .hs_compare = ldlm_export_lock_compare,
2348 .hs_get = ldlm_export_lock_get,
2349 .hs_put = ldlm_export_lock_put
2352 int ldlm_init_export(struct obd_export *exp)
2356 exp->exp_lock_hash =
2357 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2358 HASH_EXP_LOCK_CUR_BITS, HASH_EXP_LOCK_MAX_BITS,
2359 &ldlm_export_lock_ops, CFS_HASH_REHASH);
2361 if (!exp->exp_lock_hash)
2366 EXPORT_SYMBOL(ldlm_init_export);
2368 void ldlm_destroy_export(struct obd_export *exp)
2371 cfs_hash_putref(exp->exp_lock_hash);
2372 exp->exp_lock_hash = NULL;
2375 EXPORT_SYMBOL(ldlm_destroy_export);
2377 static int ldlm_setup(void)
2379 struct ldlm_bl_pool *blp;
2381 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2382 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2388 if (ldlm_state != NULL)
2391 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2392 if (ldlm_state == NULL)
2396 rc = ldlm_proc_setup();
2402 if (ldlm_num_threads) {
2403 /* If ldlm_num_threads is set, it is the min and the max. */
2404 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2405 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2406 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2407 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2408 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2412 ldlm_state->ldlm_cb_service =
2413 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2414 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2415 LDLM_CB_REPLY_PORTAL, 2,
2416 ldlm_callback_handler, "ldlm_cbd",
2417 ldlm_svc_proc_dir, NULL,
2418 ldlm_min_threads, ldlm_max_threads,
2420 LCT_MD_THREAD|LCT_DT_THREAD, NULL);
2422 if (!ldlm_state->ldlm_cb_service) {
2423 CERROR("failed to start service\n");
2424 GOTO(out_proc, rc = -ENOMEM);
2427 ldlm_state->ldlm_cancel_service =
2428 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2429 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2430 LDLM_CANCEL_REPLY_PORTAL, 6,
2431 ldlm_cancel_handler, "ldlm_canceld",
2432 ldlm_svc_proc_dir, NULL,
2433 ldlm_min_threads, ldlm_max_threads,
2435 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
2438 if (!ldlm_state->ldlm_cancel_service) {
2439 CERROR("failed to start service\n");
2440 GOTO(out_proc, rc = -ENOMEM);
2443 OBD_ALLOC(blp, sizeof(*blp));
2445 GOTO(out_proc, rc = -ENOMEM);
2446 ldlm_state->ldlm_bl_pool = blp;
2448 cfs_spin_lock_init(&blp->blp_lock);
2449 CFS_INIT_LIST_HEAD(&blp->blp_list);
2450 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2451 cfs_waitq_init(&blp->blp_waitq);
2452 cfs_atomic_set(&blp->blp_num_threads, 0);
2453 cfs_atomic_set(&blp->blp_busy_threads, 0);
2454 blp->blp_min_threads = ldlm_min_threads;
2455 blp->blp_max_threads = ldlm_max_threads;
2458 for (i = 0; i < blp->blp_min_threads; i++) {
2459 rc = ldlm_bl_thread_start(blp);
2461 GOTO(out_thread, rc);
2464 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2466 GOTO(out_thread, rc);
2468 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2470 GOTO(out_thread, rc);
2472 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2473 expired_lock_thread.elt_state = ELT_STOPPED;
2474 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2476 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2477 cfs_spin_lock_init(&waiting_locks_spinlock);
2478 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2480 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2482 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2483 GOTO(out_thread, rc);
2486 cfs_wait_event(expired_lock_thread.elt_waitq,
2487 expired_lock_thread.elt_state == ELT_READY);
2491 rc = ldlm_pools_init();
2493 GOTO(out_thread, rc);
2499 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2500 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2505 ldlm_proc_cleanup();
2508 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2513 static int ldlm_cleanup(void)
2516 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2520 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2521 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2522 CERROR("ldlm still has namespaces; clean these up first.\n");
2523 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2524 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2533 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2534 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2536 cfs_init_completion(&blp->blp_comp);
2538 cfs_spin_lock(&blp->blp_lock);
2539 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2540 cfs_waitq_signal(&blp->blp_waitq);
2541 cfs_spin_unlock(&blp->blp_lock);
2543 cfs_wait_for_completion(&blp->blp_comp);
2545 OBD_FREE(blp, sizeof(*blp));
2547 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2548 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2549 ldlm_proc_cleanup();
2551 expired_lock_thread.elt_state = ELT_TERMINATE;
2552 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2553 cfs_wait_event(expired_lock_thread.elt_waitq,
2554 expired_lock_thread.elt_state == ELT_STOPPED);
2556 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2557 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2560 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2566 int __init ldlm_init(void)
2568 cfs_init_mutex(&ldlm_ref_sem);
2569 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2570 cfs_init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2571 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2572 sizeof(struct ldlm_resource), 0,
2573 CFS_SLAB_HWCACHE_ALIGN);
2574 if (ldlm_resource_slab == NULL)
2577 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2578 sizeof(struct ldlm_lock), 0,
2579 CFS_SLAB_HWCACHE_ALIGN | CFS_SLAB_DESTROY_BY_RCU);
2580 if (ldlm_lock_slab == NULL) {
2581 cfs_mem_cache_destroy(ldlm_resource_slab);
2585 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2586 sizeof(struct ldlm_interval),
2587 0, CFS_SLAB_HWCACHE_ALIGN);
2588 if (ldlm_interval_slab == NULL) {
2589 cfs_mem_cache_destroy(ldlm_resource_slab);
2590 cfs_mem_cache_destroy(ldlm_lock_slab);
2593 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2594 class_export_dump_hook = ldlm_dump_export_locks;
2599 void __exit ldlm_exit(void)
2603 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2604 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2605 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2607 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2608 * synchronize_rcu() to wait a grace period elapsed, so that
2609 * ldlm_lock_free() get a chance to be called. */
2612 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2613 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2614 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2615 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2619 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2622 EXPORT_SYMBOL(ldlm_get_processing_policy);
2623 EXPORT_SYMBOL(ldlm_lock2desc);
2624 EXPORT_SYMBOL(ldlm_register_intent);
2625 EXPORT_SYMBOL(ldlm_lockname);
2626 EXPORT_SYMBOL(ldlm_typename);
2627 EXPORT_SYMBOL(ldlm_lock2handle);
2628 EXPORT_SYMBOL(__ldlm_handle2lock);
2629 EXPORT_SYMBOL(ldlm_lock_get);
2630 EXPORT_SYMBOL(ldlm_lock_put);
2631 EXPORT_SYMBOL(ldlm_lock_match);
2632 EXPORT_SYMBOL(ldlm_lock_cancel);
2633 EXPORT_SYMBOL(ldlm_lock_addref);
2634 EXPORT_SYMBOL(ldlm_lock_addref_try);
2635 EXPORT_SYMBOL(ldlm_lock_decref);
2636 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2637 EXPORT_SYMBOL(ldlm_lock_change_resource);
2638 EXPORT_SYMBOL(ldlm_it2str);
2639 EXPORT_SYMBOL(ldlm_lock_dump);
2640 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2641 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2642 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2643 EXPORT_SYMBOL(ldlm_lock_allow_match);
2644 EXPORT_SYMBOL(ldlm_lock_downgrade);
2645 EXPORT_SYMBOL(ldlm_lock_convert);
2647 /* ldlm_request.c */
2648 EXPORT_SYMBOL(ldlm_completion_ast_async);
2649 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2650 EXPORT_SYMBOL(ldlm_completion_ast);
2651 EXPORT_SYMBOL(ldlm_blocking_ast);
2652 EXPORT_SYMBOL(ldlm_glimpse_ast);
2653 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2654 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2655 EXPORT_SYMBOL(ldlm_prep_elc_req);
2656 EXPORT_SYMBOL(ldlm_cli_convert);
2657 EXPORT_SYMBOL(ldlm_cli_enqueue);
2658 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2659 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2660 EXPORT_SYMBOL(ldlm_cli_cancel);
2661 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2662 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2663 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2664 EXPORT_SYMBOL(ldlm_replay_locks);
2665 EXPORT_SYMBOL(ldlm_resource_foreach);
2666 EXPORT_SYMBOL(ldlm_namespace_foreach);
2667 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2668 EXPORT_SYMBOL(ldlm_resource_iterate);
2669 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2670 EXPORT_SYMBOL(ldlm_cli_cancel_list_local);
2671 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2674 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2675 EXPORT_SYMBOL(ldlm_server_completion_ast);
2676 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2677 EXPORT_SYMBOL(ldlm_handle_enqueue);
2678 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2679 EXPORT_SYMBOL(ldlm_handle_cancel);
2680 EXPORT_SYMBOL(ldlm_request_cancel);
2681 EXPORT_SYMBOL(ldlm_handle_convert);
2682 EXPORT_SYMBOL(ldlm_handle_convert0);
2683 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2684 EXPORT_SYMBOL(ldlm_get_ref);
2685 EXPORT_SYMBOL(ldlm_put_ref);
2686 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2687 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2689 /* ldlm_resource.c */
2690 EXPORT_SYMBOL(ldlm_namespace_new);
2691 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2692 EXPORT_SYMBOL(ldlm_namespace_free);
2693 EXPORT_SYMBOL(ldlm_namespace_dump);
2694 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2695 EXPORT_SYMBOL(ldlm_resource_get);
2696 EXPORT_SYMBOL(ldlm_resource_putref);
2697 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2700 EXPORT_SYMBOL(client_import_add_conn);
2701 EXPORT_SYMBOL(client_import_del_conn);
2702 EXPORT_SYMBOL(client_obd_setup);
2703 EXPORT_SYMBOL(client_obd_cleanup);
2704 EXPORT_SYMBOL(client_connect_import);
2705 EXPORT_SYMBOL(client_disconnect_export);
2706 EXPORT_SYMBOL(server_disconnect_export);
2707 EXPORT_SYMBOL(target_stop_recovery_thread);
2708 EXPORT_SYMBOL(target_handle_connect);
2709 EXPORT_SYMBOL(target_cleanup_recovery);
2710 EXPORT_SYMBOL(target_destroy_export);
2711 EXPORT_SYMBOL(target_cancel_recovery_timer);
2712 EXPORT_SYMBOL(target_send_reply);
2713 EXPORT_SYMBOL(target_queue_recovery_request);
2714 EXPORT_SYMBOL(target_handle_ping);
2715 EXPORT_SYMBOL(target_pack_pool_reply);
2716 EXPORT_SYMBOL(target_handle_disconnect);
2719 EXPORT_SYMBOL(lock_res_and_lock);
2720 EXPORT_SYMBOL(unlock_res_and_lock);