1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lockd.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 # define EXPORT_SYMTAB
45 #define DEBUG_SUBSYSTEM S_LDLM
48 # include <libcfs/libcfs.h>
50 # include <liblustre.h>
53 #include <lustre_dlm.h>
54 #include <obd_class.h>
55 #include <libcfs/list.h>
56 #include "ldlm_internal.h"
59 static int ldlm_num_threads;
60 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
61 "number of DLM service threads to start");
64 extern cfs_mem_cache_t *ldlm_resource_slab;
65 extern cfs_mem_cache_t *ldlm_lock_slab;
66 static struct semaphore ldlm_ref_sem;
67 static int ldlm_refcount;
71 static struct ldlm_state *ldlm_state;
73 inline cfs_time_t round_timeout(cfs_time_t timeout)
75 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
78 /* timeout for initial callback (AST) reply (bz10399) */
79 static inline unsigned int ldlm_get_rq_timeout(void)
82 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
84 return timeout < 1 ? 1 : timeout;
88 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
89 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
90 static struct list_head waiting_locks_list;
91 static cfs_timer_t waiting_locks_timer;
93 static struct expired_lock_thread {
94 cfs_waitq_t elt_waitq;
97 struct list_head elt_expired_locks;
98 } expired_lock_thread;
101 #define ELT_STOPPED 0
103 #define ELT_TERMINATE 2
105 struct ldlm_bl_pool {
109 * blp_prio_list is used for callbacks that should be handled
110 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
113 struct list_head blp_prio_list;
116 * blp_list is used for all other callbacks which are likely
117 * to take longer to process.
119 struct list_head blp_list;
121 cfs_waitq_t blp_waitq;
122 struct completion blp_comp;
123 atomic_t blp_num_threads;
124 atomic_t blp_busy_threads;
129 struct ldlm_bl_work_item {
130 struct list_head blwi_entry;
131 struct ldlm_namespace *blwi_ns;
132 struct ldlm_lock_desc blwi_ld;
133 struct ldlm_lock *blwi_lock;
134 struct list_head blwi_head;
140 static inline int have_expired_locks(void)
145 spin_lock_bh(&waiting_locks_spinlock);
146 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
147 spin_unlock_bh(&waiting_locks_spinlock);
152 static int expired_lock_main(void *arg)
154 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
155 struct l_wait_info lwi = { 0 };
159 cfs_daemonize("ldlm_elt");
161 expired_lock_thread.elt_state = ELT_READY;
162 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
165 l_wait_event(expired_lock_thread.elt_waitq,
166 have_expired_locks() ||
167 expired_lock_thread.elt_state == ELT_TERMINATE,
170 spin_lock_bh(&waiting_locks_spinlock);
171 if (expired_lock_thread.elt_dump) {
172 spin_unlock_bh(&waiting_locks_spinlock);
174 /* from waiting_locks_callback, but not in timer */
175 libcfs_debug_dumplog();
176 libcfs_run_lbug_upcall(__FILE__,
177 "waiting_locks_callback",
178 expired_lock_thread.elt_dump);
180 spin_lock_bh(&waiting_locks_spinlock);
181 expired_lock_thread.elt_dump = 0;
186 while (!list_empty(expired)) {
187 struct obd_export *export;
188 struct ldlm_lock *lock;
190 lock = list_entry(expired->next, struct ldlm_lock,
192 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
193 (void *)lock >= LP_POISON) {
194 spin_unlock_bh(&waiting_locks_spinlock);
195 CERROR("free lock on elt list %p\n", lock);
198 list_del_init(&lock->l_pending_chain);
199 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
200 (void *)lock->l_export >= LP_POISON) {
201 CERROR("lock with free export on elt list %p\n",
203 lock->l_export = NULL;
204 LDLM_ERROR(lock, "free export");
205 /* release extra ref grabbed by
206 * ldlm_add_waiting_lock() or
207 * ldlm_failed_ast() */
211 export = class_export_get(lock->l_export);
212 spin_unlock_bh(&waiting_locks_spinlock);
214 /* release extra ref grabbed by ldlm_add_waiting_lock()
215 * or ldlm_failed_ast() */
219 class_fail_export(export);
220 class_export_put(export);
221 spin_lock_bh(&waiting_locks_spinlock);
223 spin_unlock_bh(&waiting_locks_spinlock);
225 if (do_dump && obd_dump_on_eviction) {
226 CERROR("dump the log upon eviction\n");
227 libcfs_debug_dumplog();
230 if (expired_lock_thread.elt_state == ELT_TERMINATE)
234 expired_lock_thread.elt_state = ELT_STOPPED;
235 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
239 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
241 /* This is called from within a timer interrupt and cannot schedule */
242 static void waiting_locks_callback(unsigned long unused)
244 struct ldlm_lock *lock, *last = NULL;
247 spin_lock_bh(&waiting_locks_spinlock);
248 while (!list_empty(&waiting_locks_list)) {
249 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
252 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
253 (lock->l_req_mode == LCK_GROUP))
256 if (ptlrpc_check_suspend()) {
257 /* there is a case when we talk to one mds, holding
258 * lock from another mds. this way we easily can get
259 * here, if second mds is being recovered. so, we
260 * suspend timeouts. bug 6019 */
262 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
263 lock->l_export->exp_client_uuid.uuid,
264 lock->l_export->exp_connection->c_remote_uuid.uuid,
265 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
267 list_del_init(&lock->l_pending_chain);
268 spin_unlock_bh(&waiting_locks_spinlock);
269 ldlm_add_waiting_lock(lock);
273 /* if timeout overlaps the activation time of suspended timeouts
274 * then extend it to give a chance for client to reconnect */
275 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
276 cfs_time_seconds(obd_timeout)/2),
277 ptlrpc_suspend_wakeup_time())) {
278 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
279 lock->l_export->exp_client_uuid.uuid,
280 lock->l_export->exp_connection->c_remote_uuid.uuid,
281 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
283 list_del_init(&lock->l_pending_chain);
284 spin_unlock_bh(&waiting_locks_spinlock);
285 ldlm_add_waiting_lock(lock);
289 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
290 "evicting client at %s ",
291 cfs_time_current_sec()- lock->l_enqueued_time.tv_sec,
293 lock->l_export->exp_connection->c_peer.nid));
297 /* no needs to take an extra ref on the lock since it was in
298 * the waiting_locks_list and ldlm_add_waiting_lock()
299 * already grabbed a ref */
300 list_del(&lock->l_pending_chain);
301 list_add(&lock->l_pending_chain,
302 &expired_lock_thread.elt_expired_locks);
305 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
306 if (obd_dump_on_timeout)
307 expired_lock_thread.elt_dump = __LINE__;
309 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
313 * Make sure the timer will fire again if we have any locks
316 if (!list_empty(&waiting_locks_list)) {
317 cfs_time_t timeout_rounded;
318 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
320 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
321 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
323 spin_unlock_bh(&waiting_locks_spinlock);
327 * Indicate that we're waiting for a client to call us back cancelling a given
328 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
329 * timer to fire appropriately. (We round up to the next second, to avoid
330 * floods of timer firings during periods of high lock contention and traffic).
331 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
332 * if it has been added to the waiting list (1 is returned).
334 * Called with the namespace lock held.
336 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
339 cfs_time_t timeout_rounded;
341 if (!list_empty(&lock->l_pending_chain))
344 timeout = ldlm_get_enq_timeout(lock);
346 lock->l_callback_timeout = cfs_time_shift(timeout);
348 timeout_rounded = round_timeout(lock->l_callback_timeout);
350 if (cfs_time_before(timeout_rounded,
351 cfs_timer_deadline(&waiting_locks_timer)) ||
352 !cfs_timer_is_armed(&waiting_locks_timer)) {
353 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
355 /* if the new lock has a shorter timeout than something earlier on
356 the list, we'll wait the longer amount of time; no big deal. */
357 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
361 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
365 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
367 spin_lock_bh(&waiting_locks_spinlock);
368 if (lock->l_destroyed) {
369 static cfs_time_t next;
370 spin_unlock_bh(&waiting_locks_spinlock);
371 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
372 if (cfs_time_after(cfs_time_current(), next)) {
373 next = cfs_time_shift(14400);
374 libcfs_debug_dumpstack(NULL);
379 ret = __ldlm_add_waiting_lock(lock);
381 /* grab ref on the lock if it has been added to the
384 spin_unlock_bh(&waiting_locks_spinlock);
386 LDLM_DEBUG(lock, "%sadding to wait list",
387 ret == 0 ? "not re-" : "");
392 * Remove a lock from the pending list, likely because it had its cancellation
393 * callback arrive without incident. This adjusts the lock-timeout timer if
394 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
395 * As done by ldlm_del_waiting_lock(), the caller must release the lock
396 * reference when the lock is removed from any list (1 is returned).
398 * Called with namespace lock held.
400 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
402 struct list_head *list_next;
404 if (list_empty(&lock->l_pending_chain))
407 list_next = lock->l_pending_chain.next;
408 if (lock->l_pending_chain.prev == &waiting_locks_list) {
409 /* Removing the head of the list, adjust timer. */
410 if (list_next == &waiting_locks_list) {
411 /* No more, just cancel. */
412 cfs_timer_disarm(&waiting_locks_timer);
414 struct ldlm_lock *next;
415 next = list_entry(list_next, struct ldlm_lock,
417 cfs_timer_arm(&waiting_locks_timer,
418 round_timeout(next->l_callback_timeout));
421 list_del_init(&lock->l_pending_chain);
426 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
430 if (lock->l_export == NULL) {
431 /* We don't have a "waiting locks list" on clients. */
432 LDLM_DEBUG(lock, "client lock: no-op");
436 spin_lock_bh(&waiting_locks_spinlock);
437 ret = __ldlm_del_waiting_lock(lock);
438 spin_unlock_bh(&waiting_locks_spinlock);
440 /* release lock ref if it has indeed been removed
444 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
451 * Called with namespace lock held.
453 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
455 if (lock->l_export == NULL) {
456 /* We don't have a "waiting locks list" on clients. */
457 LDLM_DEBUG(lock, "client lock: no-op");
461 spin_lock_bh(&waiting_locks_spinlock);
463 if (list_empty(&lock->l_pending_chain)) {
464 spin_unlock_bh(&waiting_locks_spinlock);
465 LDLM_DEBUG(lock, "wasn't waiting");
469 /* we remove/add the lock to the waiting list, so no needs to
470 * release/take a lock reference */
471 __ldlm_del_waiting_lock(lock);
472 __ldlm_add_waiting_lock(lock);
473 spin_unlock_bh(&waiting_locks_spinlock);
475 LDLM_DEBUG(lock, "refreshed");
479 #else /* !__KERNEL__ */
481 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
483 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
487 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
492 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
496 #endif /* __KERNEL__ */
498 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
499 const char *ast_type)
501 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
502 "to a lock %s callback time out: rc %d\n",
503 lock->l_export->exp_obd->obd_name,
504 obd_export_nid2str(lock->l_export), ast_type, rc);
506 if (obd_dump_on_timeout)
507 libcfs_debug_dumplog();
509 spin_lock_bh(&waiting_locks_spinlock);
510 if (__ldlm_del_waiting_lock(lock) == 0)
511 /* the lock was not in any list, grab an extra ref before adding
512 * the lock to the expired list */
514 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
515 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
516 spin_unlock_bh(&waiting_locks_spinlock);
518 class_fail_export(lock->l_export);
522 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
523 struct ptlrpc_request *req, int rc,
524 const char *ast_type)
526 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
528 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
529 LASSERT(lock->l_export);
530 if (lock->l_export->exp_libclient) {
531 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
532 " timeout, just cancelling lock", ast_type,
533 libcfs_nid2str(peer.nid));
534 ldlm_lock_cancel(lock);
536 } else if (lock->l_flags & LDLM_FL_CANCEL) {
537 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
538 "cancel was received (AST reply lost?)",
539 ast_type, libcfs_nid2str(peer.nid));
540 ldlm_lock_cancel(lock);
543 ldlm_del_waiting_lock(lock);
544 ldlm_failed_ast(lock, rc, ast_type);
548 LDLM_DEBUG(lock, "client (nid %s) returned %d"
549 " from %s AST - normal race",
550 libcfs_nid2str(peer.nid),
552 lustre_msg_get_status(req->rq_repmsg) : -1,
555 LDLM_ERROR(lock, "client (nid %s) returned %d "
556 "from %s AST", libcfs_nid2str(peer.nid),
557 (req->rq_repmsg != NULL) ?
558 lustre_msg_get_status(req->rq_repmsg) : 0,
560 ldlm_lock_cancel(lock);
561 /* Server-side AST functions are called from ldlm_reprocess_all,
562 * which needs to be told to please restart its reprocessing. */
569 static int ldlm_cb_interpret(const struct lu_env *env,
570 struct ptlrpc_request *req, void *data, int rc)
572 struct ldlm_cb_set_arg *arg;
573 struct ldlm_lock *lock;
576 LASSERT(data != NULL);
578 arg = req->rq_async_args.pointer_arg[0];
579 lock = req->rq_async_args.pointer_arg[1];
580 LASSERT(lock != NULL);
582 /* If client canceled the lock but the cancel has not
583 * been recieved yet, we need to update lvbo to have the
584 * proper attributes cached. */
585 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
586 ldlm_res_lvbo_update(lock->l_resource, NULL,
588 rc = ldlm_handle_ast_error(lock, req, rc,
589 arg->type == LDLM_BL_CALLBACK
590 ? "blocking" : "completion");
593 LDLM_LOCK_RELEASE(lock);
596 atomic_set(&arg->restart, 1);
601 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
602 struct ldlm_cb_set_arg *arg,
603 struct ldlm_lock *lock,
609 if (unlikely(instant_cancel)) {
610 rc = ptl_send_rpc(req, 1);
611 ptlrpc_req_finished(req);
613 /* If we cancelled the lock, we need to restart
614 * ldlm_reprocess_queue */
615 atomic_set(&arg->restart, 1);
618 ptlrpc_set_add_req(arg->set, req);
625 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
626 * enqueued server lock conflicts with given one.
628 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
629 * to wait for client response.
631 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
632 struct ldlm_lock_desc *desc,
633 void *data, int flag)
635 struct ldlm_cb_set_arg *arg = data;
636 struct ldlm_request *body;
637 struct ptlrpc_request *req;
638 int instant_cancel = 0;
642 if (flag == LDLM_CB_CANCELING)
643 /* Don't need to do anything here. */
647 LASSERT(data != NULL);
648 if (lock->l_export->exp_obd->obd_recovering != 0) {
649 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
650 ldlm_lock_dump(D_ERROR, lock, 0);
653 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
654 &RQF_LDLM_BL_CALLBACK,
655 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
659 req->rq_async_args.pointer_arg[0] = arg;
660 req->rq_async_args.pointer_arg[1] = lock;
661 req->rq_interpret_reply = ldlm_cb_interpret;
662 req->rq_no_resend = 1;
664 lock_res(lock->l_resource);
665 if (lock->l_granted_mode != lock->l_req_mode) {
666 /* this blocking AST will be communicated as part of the
667 * completion AST instead */
668 unlock_res(lock->l_resource);
669 ptlrpc_req_finished(req);
670 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
674 if (lock->l_destroyed) {
675 /* What's the point? */
676 unlock_res(lock->l_resource);
677 ptlrpc_req_finished(req);
681 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
684 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
685 body->lock_handle[0] = lock->l_remote_handle;
686 body->lock_desc = *desc;
687 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
689 LDLM_DEBUG(lock, "server preparing blocking AST");
691 ptlrpc_request_set_replen(req);
692 if (instant_cancel) {
693 unlock_res(lock->l_resource);
694 ldlm_lock_cancel(lock);
696 LASSERT(lock->l_granted_mode == lock->l_req_mode);
697 ldlm_add_waiting_lock(lock);
698 unlock_res(lock->l_resource);
701 req->rq_send_state = LUSTRE_IMP_FULL;
702 /* ptlrpc_prep_req already set timeout */
704 req->rq_timeout = ldlm_get_rq_timeout();
706 if (lock->l_export && lock->l_export->exp_nid_stats &&
707 lock->l_export->exp_nid_stats->nid_ldlm_stats)
708 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
709 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
711 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
716 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
718 struct ldlm_cb_set_arg *arg = data;
719 struct ldlm_request *body;
720 struct ptlrpc_request *req;
721 struct timeval granted_time;
722 long total_enqueue_wait;
723 int instant_cancel = 0;
727 LASSERT(lock != NULL);
728 LASSERT(data != NULL);
730 do_gettimeofday(&granted_time);
731 total_enqueue_wait = cfs_timeval_sub(&granted_time,
732 &lock->l_enqueued_time, NULL);
734 if (total_enqueue_wait / ONE_MILLION > obd_timeout)
735 /* non-fatal with AT - change to LDLM_DEBUG? */
736 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
737 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
739 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
740 &RQF_LDLM_CP_CALLBACK);
744 lock_res_and_lock(lock);
745 if (lock->l_resource->lr_lvb_len)
746 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
747 lock->l_resource->lr_lvb_len);
748 unlock_res_and_lock(lock);
750 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
752 ptlrpc_request_free(req);
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;
760 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
762 body->lock_handle[0] = lock->l_remote_handle;
763 body->lock_flags = flags;
764 ldlm_lock2desc(lock, &body->lock_desc);
765 if (lock->l_resource->lr_lvb_len) {
766 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
768 lock_res_and_lock(lock);
769 memcpy(lvb, lock->l_resource->lr_lvb_data,
770 lock->l_resource->lr_lvb_len);
771 unlock_res_and_lock(lock);
774 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
777 /* Server-side enqueue wait time estimate, used in
778 __ldlm_add_waiting_lock to set future enqueue timers */
779 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
780 total_enqueue_wait / ONE_MILLION);
782 ptlrpc_request_set_replen(req);
784 req->rq_send_state = LUSTRE_IMP_FULL;
785 /* ptlrpc_prep_req already set timeout */
787 req->rq_timeout = ldlm_get_rq_timeout();
789 /* We only send real blocking ASTs after the lock is granted */
790 lock_res_and_lock(lock);
791 if (lock->l_flags & LDLM_FL_AST_SENT) {
792 body->lock_flags |= LDLM_FL_AST_SENT;
794 /* We might get here prior to ldlm_handle_enqueue setting
795 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
796 * into waiting list, but this is safe and similar code in
797 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
798 * that would not only cancel the lock, but will also remove
799 * it from waiting list */
800 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
801 unlock_res_and_lock(lock);
802 ldlm_lock_cancel(lock);
804 lock_res_and_lock(lock);
806 /* start the lock-timeout clock */
807 ldlm_add_waiting_lock(lock);
810 unlock_res_and_lock(lock);
812 if (lock->l_export && lock->l_export->exp_nid_stats &&
813 lock->l_export->exp_nid_stats->nid_ldlm_stats)
814 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
815 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
817 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
822 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
824 struct ldlm_resource *res = lock->l_resource;
825 struct ldlm_request *body;
826 struct ptlrpc_request *req;
830 LASSERT(lock != NULL);
832 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
833 &RQF_LDLM_GL_CALLBACK,
834 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
839 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
840 body->lock_handle[0] = lock->l_remote_handle;
841 ldlm_lock2desc(lock, &body->lock_desc);
843 lock_res_and_lock(lock);
844 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
845 lock->l_resource->lr_lvb_len);
846 unlock_res_and_lock(lock);
847 res = lock->l_resource;
848 ptlrpc_request_set_replen(req);
851 req->rq_send_state = LUSTRE_IMP_FULL;
852 /* ptlrpc_prep_req already set timeout */
854 req->rq_timeout = ldlm_get_rq_timeout();
856 if (lock->l_export && lock->l_export->exp_nid_stats &&
857 lock->l_export->exp_nid_stats->nid_ldlm_stats)
858 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
859 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
861 rc = ptlrpc_queue_wait(req);
862 if (rc == -ELDLM_NO_LOCK_DATA)
863 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
865 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
867 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
869 ptlrpc_req_finished(req);
871 ldlm_reprocess_all(res);
877 extern unsigned long long lu_time_stamp_get(void);
879 #define lu_time_stamp_get() time(NULL)
882 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
883 struct lprocfs_stats *srv_stats)
885 int lock_type = 0, op = 0;
887 lock_type = dlm_req->lock_desc.l_resource.lr_type;
891 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
894 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
895 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
897 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
900 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
903 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
911 lprocfs_counter_incr(srv_stats, op);
917 * Main server-side entry point into LDLM. This is called by ptlrpc service
918 * threads to carry out client lock enqueueing requests.
920 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
921 struct ptlrpc_request *req,
922 const struct ldlm_request *dlm_req,
923 const struct ldlm_callback_suite *cbs)
925 struct ldlm_reply *dlm_rep;
927 ldlm_error_t err = ELDLM_OK;
928 struct ldlm_lock *lock = NULL;
933 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
935 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
936 flags = dlm_req->lock_flags;
938 LASSERT(req->rq_export);
940 if (req->rq_rqbd->rqbd_service->srv_stats)
941 ldlm_svc_get_eopc(dlm_req,
942 req->rq_rqbd->rqbd_service->srv_stats);
944 if (req->rq_export && req->rq_export->exp_nid_stats &&
945 req->rq_export->exp_nid_stats->nid_ldlm_stats)
946 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
947 LDLM_ENQUEUE - LDLM_FIRST_OPC);
949 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
950 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
951 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
952 dlm_req->lock_desc.l_resource.lr_type);
953 GOTO(out, rc = -EFAULT);
956 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
957 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
958 dlm_req->lock_desc.l_req_mode &
959 (dlm_req->lock_desc.l_req_mode-1))) {
960 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
961 dlm_req->lock_desc.l_req_mode);
962 GOTO(out, rc = -EFAULT);
965 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
966 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
968 DEBUG_REQ(D_ERROR, req,
969 "PLAIN lock request from IBITS client?");
970 GOTO(out, rc = -EPROTO);
972 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
974 DEBUG_REQ(D_ERROR, req,
975 "IBITS lock request from unaware client?");
976 GOTO(out, rc = -EPROTO);
980 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
981 against server's _CONNECT_SUPPORTED flags? (I don't want to use
982 ibits for mgc/mgs) */
984 /* INODEBITS_INTEROP: Perform conversion from plain lock to
985 * inodebits lock if client does not support them. */
986 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
987 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
988 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
989 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
990 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
991 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
992 dlm_req->lock_desc.l_req_mode = LCK_CR;
996 if (unlikely(flags & LDLM_FL_REPLAY)) {
997 /* Find an existing lock in the per-export lock hash */
998 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
999 (void *)&dlm_req->lock_handle[0]);
1001 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1002 LPX64, lock->l_handle.h_cookie);
1003 GOTO(existing_lock, rc = 0);
1007 /* The lock's callback data might be set in the policy function */
1008 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1009 dlm_req->lock_desc.l_resource.lr_type,
1010 dlm_req->lock_desc.l_req_mode,
1014 GOTO(out, rc = -ENOMEM);
1016 do_gettimeofday(&lock->l_enqueued_time);
1017 lock->l_remote_handle = dlm_req->lock_handle[0];
1018 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1020 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1021 /* Don't enqueue a lock onto the export if it has already
1022 * been evicted. Cancel it now instead. (bug 3822) */
1023 if (req->rq_export->exp_failed) {
1024 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1025 GOTO(out, rc = -ENOTCONN);
1027 lock->l_export = class_export_get(req->rq_export);
1029 if (lock->l_export->exp_lock_hash)
1030 lustre_hash_add(lock->l_export->exp_lock_hash,
1031 &lock->l_remote_handle,
1036 if (flags & LDLM_FL_HAS_INTENT) {
1037 /* In this case, the reply buffer is allocated deep in
1038 * local_lock_enqueue by the policy function. */
1041 lock_res_and_lock(lock);
1042 if (lock->l_resource->lr_lvb_len) {
1043 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1045 lock->l_resource->lr_lvb_len);
1047 unlock_res_and_lock(lock);
1049 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1050 GOTO(out, rc = -ENOMEM);
1052 rc = req_capsule_server_pack(&req->rq_pill);
1057 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1058 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1059 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1060 lock->l_req_extent = lock->l_policy_data.l_extent;
1062 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1066 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1067 dlm_rep->lock_flags = flags;
1069 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1070 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1072 /* We never send a blocking AST until the lock is granted, but
1073 * we can tell it right now */
1074 lock_res_and_lock(lock);
1076 /* Now take into account flags to be inherited from original lock
1077 request both in reply to client and in our own lock flags. */
1078 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1079 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1081 /* Don't move a pending lock onto the export if it has already
1082 * been evicted. Cancel it now instead. (bug 5683) */
1083 if (unlikely(req->rq_export->exp_failed ||
1084 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1085 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1087 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1088 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1089 if (lock->l_granted_mode == lock->l_req_mode) {
1091 * Only cancel lock if it was granted, because it would
1092 * be destroyed immediatelly and would never be granted
1093 * in the future, causing timeouts on client. Not
1094 * granted lock will be cancelled immediatelly after
1095 * sending completion AST.
1097 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1098 unlock_res_and_lock(lock);
1099 ldlm_lock_cancel(lock);
1100 lock_res_and_lock(lock);
1102 ldlm_add_waiting_lock(lock);
1105 /* Make sure we never ever grant usual metadata locks to liblustre
1107 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1108 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1109 req->rq_export->exp_libclient) {
1110 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1111 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1112 CERROR("Granting sync lock to libclient. "
1113 "req fl %d, rep fl %d, lock fl %d\n",
1114 dlm_req->lock_flags, dlm_rep->lock_flags,
1116 LDLM_ERROR(lock, "sync lock");
1117 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1118 struct ldlm_intent *it;
1120 it = req_capsule_client_get(&req->rq_pill,
1123 CERROR("This is intent %s ("LPU64")\n",
1124 ldlm_it2str(it->opc), it->opc);
1130 unlock_res_and_lock(lock);
1134 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1135 if (!req->rq_packed_final) {
1136 err = lustre_pack_reply(req, 1, NULL, NULL);
1141 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1142 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1144 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1145 "(err=%d, rc=%d)", err, rc);
1147 lock_res_and_lock(lock);
1149 if (lock->l_resource->lr_lvb_len > 0) {
1152 lvb = req_capsule_server_get(&req->rq_pill,
1154 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1157 memcpy(lvb, lock->l_resource->lr_lvb_data,
1158 lock->l_resource->lr_lvb_len);
1161 ldlm_resource_unlink_lock(lock);
1162 ldlm_lock_destroy_nolock(lock);
1164 unlock_res_and_lock(lock);
1166 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1167 ldlm_reprocess_all(lock->l_resource);
1169 LDLM_LOCK_RELEASE(lock);
1172 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1178 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1179 ldlm_completion_callback completion_callback,
1180 ldlm_blocking_callback blocking_callback,
1181 ldlm_glimpse_callback glimpse_callback)
1183 struct ldlm_request *dlm_req;
1184 struct ldlm_callback_suite cbs = {
1185 .lcs_completion = completion_callback,
1186 .lcs_blocking = blocking_callback,
1187 .lcs_glimpse = glimpse_callback
1191 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1192 if (dlm_req != NULL) {
1193 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1194 req, dlm_req, &cbs);
1201 int ldlm_handle_convert0(struct ptlrpc_request *req,
1202 const struct ldlm_request *dlm_req)
1204 struct ldlm_reply *dlm_rep;
1205 struct ldlm_lock *lock;
1209 if (req->rq_export && req->rq_export->exp_nid_stats &&
1210 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1211 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1212 LDLM_CONVERT - LDLM_FIRST_OPC);
1214 rc = req_capsule_server_pack(&req->rq_pill);
1218 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1219 dlm_rep->lock_flags = dlm_req->lock_flags;
1221 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1223 req->rq_status = EINVAL;
1227 LDLM_DEBUG(lock, "server-side convert handler START");
1229 do_gettimeofday(&lock->l_enqueued_time);
1230 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1231 &dlm_rep->lock_flags);
1233 if (ldlm_del_waiting_lock(lock))
1234 LDLM_DEBUG(lock, "converted waiting lock");
1237 req->rq_status = EDEADLOCK;
1242 if (!req->rq_status)
1243 ldlm_reprocess_all(lock->l_resource);
1244 LDLM_DEBUG(lock, "server-side convert handler END");
1245 LDLM_LOCK_PUT(lock);
1247 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1252 int ldlm_handle_convert(struct ptlrpc_request *req)
1255 struct ldlm_request *dlm_req;
1257 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1258 if (dlm_req != NULL) {
1259 rc = ldlm_handle_convert0(req, dlm_req);
1261 CERROR ("Can't unpack dlm_req\n");
1267 /* Cancel all the locks whos handles are packed into ldlm_request */
1268 int ldlm_request_cancel(struct ptlrpc_request *req,
1269 const struct ldlm_request *dlm_req, int first)
1271 struct ldlm_resource *res, *pres = NULL;
1272 struct ldlm_lock *lock;
1273 int i, count, done = 0;
1276 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1280 /* There is no lock on the server at the replay time,
1281 * skip lock cancelling to make replay tests to pass. */
1282 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1285 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1286 "starting at %d", count, first);
1288 for (i = first; i < count; i++) {
1289 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1291 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1292 "lock (cookie "LPU64")",
1293 dlm_req->lock_handle[i].cookie);
1297 res = lock->l_resource;
1302 ldlm_reprocess_all(pres);
1303 LDLM_RESOURCE_DELREF(pres);
1304 ldlm_resource_putref(pres);
1307 ldlm_resource_getref(res);
1308 LDLM_RESOURCE_ADDREF(res);
1309 ldlm_res_lvbo_update(res, NULL, 0, 1);
1313 ldlm_lock_cancel(lock);
1314 LDLM_LOCK_PUT(lock);
1317 ldlm_reprocess_all(pres);
1318 LDLM_RESOURCE_DELREF(pres);
1319 ldlm_resource_putref(pres);
1321 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1325 int ldlm_handle_cancel(struct ptlrpc_request *req)
1327 struct ldlm_request *dlm_req;
1331 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1332 if (dlm_req == NULL) {
1333 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1337 if (req->rq_export && req->rq_export->exp_nid_stats &&
1338 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1339 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1340 LDLM_CANCEL - LDLM_FIRST_OPC);
1342 rc = req_capsule_server_pack(&req->rq_pill);
1346 if (!ldlm_request_cancel(req, dlm_req, 0))
1347 req->rq_status = ESTALE;
1349 if (ptlrpc_reply(req) != 0)
1355 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1356 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1361 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1363 lock_res_and_lock(lock);
1364 lock->l_flags |= LDLM_FL_CBPENDING;
1366 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1367 lock->l_flags |= LDLM_FL_CANCEL;
1369 do_ast = (!lock->l_readers && !lock->l_writers);
1370 unlock_res_and_lock(lock);
1373 LDLM_DEBUG(lock, "already unused, calling "
1374 "callback (%p)", lock->l_blocking_ast);
1375 if (lock->l_blocking_ast != NULL)
1376 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1379 LDLM_DEBUG(lock, "Lock still has references, will be"
1380 " cancelled later");
1383 LDLM_DEBUG(lock, "client blocking callback handler END");
1384 LDLM_LOCK_RELEASE(lock);
1388 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1389 struct ldlm_namespace *ns,
1390 struct ldlm_request *dlm_req,
1391 struct ldlm_lock *lock)
1393 CFS_LIST_HEAD(ast_list);
1396 LDLM_DEBUG(lock, "client completion callback handler START");
1398 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1399 int to = cfs_time_seconds(1);
1401 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1402 if (lock->l_granted_mode == lock->l_req_mode ||
1408 lock_res_and_lock(lock);
1409 if (lock->l_destroyed ||
1410 lock->l_granted_mode == lock->l_req_mode) {
1411 /* bug 11300: the lock has already been granted */
1412 unlock_res_and_lock(lock);
1413 LDLM_DEBUG(lock, "Double grant race happened");
1414 LDLM_LOCK_RELEASE(lock);
1419 /* If we receive the completion AST before the actual enqueue returned,
1420 * then we might need to switch lock modes, resources, or extents. */
1421 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1422 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1423 LDLM_DEBUG(lock, "completion AST, new lock mode");
1426 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1427 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1428 LDLM_DEBUG(lock, "completion AST, new policy data");
1431 ldlm_resource_unlink_lock(lock);
1432 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1433 &lock->l_resource->lr_name,
1434 sizeof(lock->l_resource->lr_name)) != 0) {
1435 unlock_res_and_lock(lock);
1436 if (ldlm_lock_change_resource(ns, lock,
1437 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1438 LDLM_ERROR(lock, "Failed to allocate resource");
1439 LDLM_LOCK_RELEASE(lock);
1443 LDLM_DEBUG(lock, "completion AST, new resource");
1444 CERROR("change resource!\n");
1445 lock_res_and_lock(lock);
1448 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1449 /* BL_AST locks are not needed in lru.
1450 * let ldlm_cancel_lru() be fast. */
1451 ldlm_lock_remove_from_lru(lock);
1452 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1453 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1456 if (lock->l_lvb_len) {
1457 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1458 RCL_CLIENT) < lock->l_lvb_len) {
1459 LDLM_ERROR(lock, "completion AST did not contain "
1462 void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1464 (void *)lock->l_lvb_swabber);
1465 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1469 ldlm_grant_lock(lock, &ast_list);
1470 unlock_res_and_lock(lock);
1472 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1474 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1476 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1478 LDLM_LOCK_RELEASE(lock);
1482 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1483 struct ldlm_namespace *ns,
1484 struct ldlm_request *dlm_req,
1485 struct ldlm_lock *lock)
1490 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1492 if (lock->l_glimpse_ast != NULL)
1493 rc = lock->l_glimpse_ast(lock, req);
1495 if (req->rq_repmsg != NULL) {
1498 req->rq_status = rc;
1502 lock_res_and_lock(lock);
1503 if (lock->l_granted_mode == LCK_PW &&
1504 !lock->l_readers && !lock->l_writers &&
1505 cfs_time_after(cfs_time_current(),
1506 cfs_time_add(lock->l_last_used,
1507 cfs_time_seconds(10)))) {
1508 unlock_res_and_lock(lock);
1509 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1510 ldlm_handle_bl_callback(ns, NULL, lock);
1515 unlock_res_and_lock(lock);
1516 LDLM_LOCK_RELEASE(lock);
1520 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1522 if (req->rq_no_reply)
1525 req->rq_status = rc;
1526 if (!req->rq_packed_final) {
1527 rc = lustre_pack_reply(req, 1, NULL, NULL);
1531 return ptlrpc_reply(req);
1535 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1536 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1537 struct list_head *cancels, int count)
1539 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1540 struct ldlm_bl_work_item *blwi;
1543 if (cancels && count == 0)
1546 OBD_ALLOC(blwi, sizeof(*blwi));
1552 blwi->blwi_ld = *ld;
1554 list_add(&blwi->blwi_head, cancels);
1555 list_del_init(cancels);
1556 blwi->blwi_count = count;
1558 blwi->blwi_lock = lock;
1560 spin_lock(&blp->blp_lock);
1561 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1562 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1563 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1565 /* other blocking callbacks are added to the regular list */
1566 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1568 cfs_waitq_signal(&blp->blp_waitq);
1569 spin_unlock(&blp->blp_lock);
1575 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1576 struct ldlm_lock *lock)
1579 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1585 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1586 struct list_head *cancels, int count)
1589 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1595 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1596 static int ldlm_callback_handler(struct ptlrpc_request *req)
1598 struct ldlm_namespace *ns;
1599 struct ldlm_request *dlm_req;
1600 struct ldlm_lock *lock;
1604 /* Requests arrive in sender's byte order. The ptlrpc service
1605 * handler has already checked and, if necessary, byte-swapped the
1606 * incoming request message body, but I am responsible for the
1607 * message buffers. */
1609 /* do nothing for sec context finalize */
1610 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1613 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1615 if (req->rq_export == NULL) {
1616 struct ldlm_request *dlm_req;
1618 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1619 "export cookie "LPX64"; this is "
1620 "normal if this node rebooted with a lock held\n",
1621 lustre_msg_get_opc(req->rq_reqmsg),
1622 libcfs_id2str(req->rq_peer),
1623 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1625 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1626 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1627 if (dlm_req != NULL)
1628 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1629 dlm_req->lock_handle[0].cookie);
1631 ldlm_callback_reply(req, -ENOTCONN);
1635 LASSERT(req->rq_export != NULL);
1636 LASSERT(req->rq_export->exp_obd != NULL);
1638 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1639 case LDLM_BL_CALLBACK:
1640 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1643 case LDLM_CP_CALLBACK:
1644 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1647 case LDLM_GL_CALLBACK:
1648 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1651 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1652 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1653 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1655 rc = llog_origin_handle_cancel(req);
1656 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1658 ldlm_callback_reply(req, rc);
1660 case OBD_QC_CALLBACK:
1661 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1662 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1664 rc = target_handle_qc_callback(req);
1665 ldlm_callback_reply(req, rc);
1669 /* reply in handler */
1670 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1671 rc = target_handle_dqacq_callback(req);
1673 case LLOG_ORIGIN_HANDLE_CREATE:
1674 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1675 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1677 rc = llog_origin_handle_create(req);
1678 ldlm_callback_reply(req, rc);
1680 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1681 req_capsule_set(&req->rq_pill,
1682 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1683 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1685 rc = llog_origin_handle_next_block(req);
1686 ldlm_callback_reply(req, rc);
1688 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1689 req_capsule_set(&req->rq_pill,
1690 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1691 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1693 rc = llog_origin_handle_read_header(req);
1694 ldlm_callback_reply(req, rc);
1696 case LLOG_ORIGIN_HANDLE_CLOSE:
1697 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1699 rc = llog_origin_handle_close(req);
1700 ldlm_callback_reply(req, rc);
1703 CERROR("unknown opcode %u\n",
1704 lustre_msg_get_opc(req->rq_reqmsg));
1705 ldlm_callback_reply(req, -EPROTO);
1709 ns = req->rq_export->exp_obd->obd_namespace;
1710 LASSERT(ns != NULL);
1712 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1714 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1715 if (dlm_req == NULL) {
1716 ldlm_callback_reply(req, -EPROTO);
1720 /* Force a known safe race, send a cancel to the server for a lock
1721 * which the server has already started a blocking callback on. */
1722 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1723 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1724 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1726 CERROR("ldlm_cli_cancel: %d\n", rc);
1729 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1731 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1732 "disappeared\n", dlm_req->lock_handle[0].cookie);
1733 ldlm_callback_reply(req, -EINVAL);
1737 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1738 lock_res_and_lock(lock);
1739 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1740 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1741 /* If somebody cancels locks and cache is already droped,
1742 * we can tell the server we have no lock. Otherwise, we
1743 * should send cancel after dropping the cache. */
1744 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1745 (lock->l_flags & LDLM_FL_BL_DONE)) {
1746 LDLM_DEBUG(lock, "callback on lock "
1747 LPX64" - lock disappeared\n",
1748 dlm_req->lock_handle[0].cookie);
1749 unlock_res_and_lock(lock);
1750 LDLM_LOCK_RELEASE(lock);
1751 ldlm_callback_reply(req, -EINVAL);
1754 /* BL_AST locks are not needed in lru.
1755 * let ldlm_cancel_lru() be fast. */
1756 ldlm_lock_remove_from_lru(lock);
1757 lock->l_flags |= LDLM_FL_BL_AST;
1759 unlock_res_and_lock(lock);
1761 /* We want the ost thread to get this reply so that it can respond
1762 * to ost requests (write cache writeback) that might be triggered
1765 * But we'd also like to be able to indicate in the reply that we're
1766 * cancelling right now, because it's unused, or have an intent result
1767 * in the reply, so we might have to push the responsibility for sending
1768 * the reply down into the AST handlers, alas. */
1770 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1771 case LDLM_BL_CALLBACK:
1772 CDEBUG(D_INODE, "blocking ast\n");
1773 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1774 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1775 ldlm_callback_reply(req, 0);
1776 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1777 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1779 case LDLM_CP_CALLBACK:
1780 CDEBUG(D_INODE, "completion ast\n");
1781 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1782 ldlm_callback_reply(req, 0);
1783 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1785 case LDLM_GL_CALLBACK:
1786 CDEBUG(D_INODE, "glimpse ast\n");
1787 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1788 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1791 LBUG(); /* checked above */
1797 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1802 /* Requests arrive in sender's byte order. The ptlrpc service
1803 * handler has already checked and, if necessary, byte-swapped the
1804 * incoming request message body, but I am responsible for the
1805 * message buffers. */
1807 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1809 if (req->rq_export == NULL) {
1810 struct ldlm_request *dlm_req;
1812 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1813 lustre_msg_get_opc(req->rq_reqmsg),
1814 libcfs_id2str(req->rq_peer),
1815 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1817 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1818 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1819 if (dlm_req != NULL)
1820 ldlm_lock_dump_handle(D_ERROR,
1821 &dlm_req->lock_handle[0]);
1822 ldlm_callback_reply(req, -ENOTCONN);
1826 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1828 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1830 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1831 CDEBUG(D_INODE, "cancel\n");
1832 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1834 rc = ldlm_handle_cancel(req);
1838 case OBD_LOG_CANCEL:
1839 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1840 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1842 rc = llog_origin_handle_cancel(req);
1843 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1845 ldlm_callback_reply(req, rc);
1848 CERROR("invalid opcode %d\n",
1849 lustre_msg_get_opc(req->rq_reqmsg));
1850 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1851 ldlm_callback_reply(req, -EINVAL);
1857 void ldlm_revoke_lock_cb(void *obj, void *data)
1859 struct list_head *rpc_list = data;
1860 struct ldlm_lock *lock = obj;
1862 lock_res_and_lock(lock);
1864 if (lock->l_req_mode != lock->l_granted_mode) {
1865 unlock_res_and_lock(lock);
1869 LASSERT(lock->l_resource);
1870 if (lock->l_resource->lr_type != LDLM_IBITS &&
1871 lock->l_resource->lr_type != LDLM_PLAIN) {
1872 unlock_res_and_lock(lock);
1876 if (lock->l_flags & LDLM_FL_AST_SENT) {
1877 unlock_res_and_lock(lock);
1881 LASSERT(lock->l_blocking_ast);
1882 LASSERT(!lock->l_blocking_lock);
1884 lock->l_flags |= LDLM_FL_AST_SENT;
1885 if (lock->l_export && lock->l_export->exp_lock_hash)
1886 lustre_hash_del(lock->l_export->exp_lock_hash,
1887 &lock->l_remote_handle, &lock->l_exp_hash);
1888 list_add_tail(&lock->l_rk_ast, rpc_list);
1889 LDLM_LOCK_GET(lock);
1891 unlock_res_and_lock(lock);
1894 void ldlm_revoke_export_locks(struct obd_export *exp)
1896 struct list_head rpc_list;
1899 CFS_INIT_LIST_HEAD(&rpc_list);
1900 lustre_hash_for_each_empty(exp->exp_lock_hash,
1901 ldlm_revoke_lock_cb, &rpc_list);
1902 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1908 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1910 struct ldlm_bl_work_item *blwi = NULL;
1911 static unsigned int num_bl = 0;
1913 spin_lock(&blp->blp_lock);
1914 /* process a request from the blp_list at least every blp_num_threads */
1915 if (!list_empty(&blp->blp_list) &&
1916 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1917 blwi = list_entry(blp->blp_list.next,
1918 struct ldlm_bl_work_item, blwi_entry);
1920 if (!list_empty(&blp->blp_prio_list))
1921 blwi = list_entry(blp->blp_prio_list.next,
1922 struct ldlm_bl_work_item, blwi_entry);
1925 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1927 list_del(&blwi->blwi_entry);
1929 spin_unlock(&blp->blp_lock);
1934 /* This only contains temporary data until the thread starts */
1935 struct ldlm_bl_thread_data {
1936 char bltd_name[CFS_CURPROC_COMM_MAX];
1937 struct ldlm_bl_pool *bltd_blp;
1938 struct completion bltd_comp;
1942 static int ldlm_bl_thread_main(void *arg);
1944 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1946 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1949 init_completion(&bltd.bltd_comp);
1950 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1952 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1953 atomic_read(&blp->blp_num_threads), rc);
1956 wait_for_completion(&bltd.bltd_comp);
1961 static int ldlm_bl_thread_main(void *arg)
1963 struct ldlm_bl_pool *blp;
1967 struct ldlm_bl_thread_data *bltd = arg;
1969 blp = bltd->bltd_blp;
1971 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1972 atomic_inc(&blp->blp_busy_threads);
1974 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1975 "ldlm_bl_%02d", bltd->bltd_num);
1976 cfs_daemonize(bltd->bltd_name);
1978 complete(&bltd->bltd_comp);
1979 /* cannot use bltd after this, it is only on caller's stack */
1983 struct l_wait_info lwi = { 0 };
1984 struct ldlm_bl_work_item *blwi = NULL;
1986 blwi = ldlm_bl_get_work(blp);
1991 atomic_dec(&blp->blp_busy_threads);
1992 l_wait_event_exclusive(blp->blp_waitq,
1993 (blwi = ldlm_bl_get_work(blp)) != NULL,
1995 busy = atomic_inc_return(&blp->blp_busy_threads);
1997 if (blwi->blwi_ns == NULL)
1998 /* added by ldlm_cleanup() */
2001 /* Not fatal if racy and have a few too many threads */
2002 if (unlikely(busy < blp->blp_max_threads &&
2003 busy >= atomic_read(&blp->blp_num_threads)))
2004 /* discard the return value, we tried */
2005 ldlm_bl_thread_start(blp);
2007 if (blwi->blwi_ns == NULL)
2008 /* added by ldlm_cleanup() */
2012 if (blwi->blwi_count) {
2013 /* The special case when we cancel locks in lru
2014 * asynchronously, we pass the list of locks here.
2015 * Thus lock is marked LDLM_FL_CANCELING, and already
2016 * canceled locally. */
2017 ldlm_cli_cancel_list(&blwi->blwi_head,
2018 blwi->blwi_count, NULL, 0);
2020 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2023 OBD_FREE(blwi, sizeof(*blwi));
2026 atomic_dec(&blp->blp_busy_threads);
2027 atomic_dec(&blp->blp_num_threads);
2028 complete(&blp->blp_comp);
2034 static int ldlm_setup(void);
2035 static int ldlm_cleanup(void);
2037 int ldlm_get_ref(void)
2041 mutex_down(&ldlm_ref_sem);
2042 if (++ldlm_refcount == 1) {
2047 mutex_up(&ldlm_ref_sem);
2052 void ldlm_put_ref(void)
2055 mutex_down(&ldlm_ref_sem);
2056 if (ldlm_refcount == 1) {
2057 int rc = ldlm_cleanup();
2059 CERROR("ldlm_cleanup failed: %d\n", rc);
2065 mutex_up(&ldlm_ref_sem);
2071 * Export handle<->lock hash operations.
2074 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2076 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2080 ldlm_export_lock_key(struct hlist_node *hnode)
2082 struct ldlm_lock *lock;
2085 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2086 RETURN(&lock->l_remote_handle);
2090 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2093 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2097 ldlm_export_lock_get(struct hlist_node *hnode)
2099 struct ldlm_lock *lock;
2102 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2103 LDLM_LOCK_GET(lock);
2109 ldlm_export_lock_put(struct hlist_node *hnode)
2111 struct ldlm_lock *lock;
2114 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2115 LDLM_LOCK_RELEASE(lock);
2120 static lustre_hash_ops_t ldlm_export_lock_ops = {
2121 .lh_hash = ldlm_export_lock_hash,
2122 .lh_key = ldlm_export_lock_key,
2123 .lh_compare = ldlm_export_lock_compare,
2124 .lh_get = ldlm_export_lock_get,
2125 .lh_put = ldlm_export_lock_put
2128 int ldlm_init_export(struct obd_export *exp)
2132 exp->exp_lock_hash =
2133 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2134 128, 65536, &ldlm_export_lock_ops, LH_REHASH);
2136 if (!exp->exp_lock_hash)
2141 EXPORT_SYMBOL(ldlm_init_export);
2143 void ldlm_destroy_export(struct obd_export *exp)
2146 lustre_hash_exit(exp->exp_lock_hash);
2147 exp->exp_lock_hash = NULL;
2150 EXPORT_SYMBOL(ldlm_destroy_export);
2152 static int ldlm_setup(void)
2154 struct ldlm_bl_pool *blp;
2156 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2157 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2163 if (ldlm_state != NULL)
2166 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2167 if (ldlm_state == NULL)
2171 rc = ldlm_proc_setup();
2177 if (ldlm_num_threads) {
2178 /* If ldlm_num_threads is set, it is the min and the max. */
2179 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2180 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2181 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2182 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2183 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2187 ldlm_state->ldlm_cb_service =
2188 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2189 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2190 LDLM_CB_REPLY_PORTAL, 1800,
2191 ldlm_callback_handler, "ldlm_cbd",
2192 ldlm_svc_proc_dir, NULL,
2193 ldlm_min_threads, ldlm_max_threads,
2195 LCT_MD_THREAD|LCT_DT_THREAD);
2197 if (!ldlm_state->ldlm_cb_service) {
2198 CERROR("failed to start service\n");
2199 GOTO(out_proc, rc = -ENOMEM);
2202 ldlm_state->ldlm_cancel_service =
2203 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2204 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2205 LDLM_CANCEL_REPLY_PORTAL, 6000,
2206 ldlm_cancel_handler, "ldlm_canceld",
2207 ldlm_svc_proc_dir, NULL,
2208 ldlm_min_threads, ldlm_max_threads,
2210 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2212 if (!ldlm_state->ldlm_cancel_service) {
2213 CERROR("failed to start service\n");
2214 GOTO(out_proc, rc = -ENOMEM);
2217 OBD_ALLOC(blp, sizeof(*blp));
2219 GOTO(out_proc, rc = -ENOMEM);
2220 ldlm_state->ldlm_bl_pool = blp;
2222 spin_lock_init(&blp->blp_lock);
2223 CFS_INIT_LIST_HEAD(&blp->blp_list);
2224 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2225 cfs_waitq_init(&blp->blp_waitq);
2226 atomic_set(&blp->blp_num_threads, 0);
2227 atomic_set(&blp->blp_busy_threads, 0);
2228 blp->blp_min_threads = ldlm_min_threads;
2229 blp->blp_max_threads = ldlm_max_threads;
2232 for (i = 0; i < blp->blp_min_threads; i++) {
2233 rc = ldlm_bl_thread_start(blp);
2235 GOTO(out_thread, rc);
2238 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2240 GOTO(out_thread, rc);
2242 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2244 GOTO(out_thread, rc);
2246 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2247 expired_lock_thread.elt_state = ELT_STOPPED;
2248 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2250 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2251 spin_lock_init(&waiting_locks_spinlock);
2252 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2254 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2256 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2257 GOTO(out_thread, rc);
2260 wait_event(expired_lock_thread.elt_waitq,
2261 expired_lock_thread.elt_state == ELT_READY);
2265 rc = ldlm_pools_init();
2267 GOTO(out_thread, rc);
2273 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2274 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2279 ldlm_proc_cleanup();
2282 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2287 static int ldlm_cleanup(void)
2290 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2294 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2295 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2296 CERROR("ldlm still has namespaces; clean these up first.\n");
2297 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2298 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2307 while (atomic_read(&blp->blp_num_threads) > 0) {
2308 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2310 init_completion(&blp->blp_comp);
2312 spin_lock(&blp->blp_lock);
2313 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2314 cfs_waitq_signal(&blp->blp_waitq);
2315 spin_unlock(&blp->blp_lock);
2317 wait_for_completion(&blp->blp_comp);
2319 OBD_FREE(blp, sizeof(*blp));
2321 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2322 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2323 ldlm_proc_cleanup();
2325 expired_lock_thread.elt_state = ELT_TERMINATE;
2326 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2327 wait_event(expired_lock_thread.elt_waitq,
2328 expired_lock_thread.elt_state == ELT_STOPPED);
2330 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2331 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2334 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2340 int __init ldlm_init(void)
2342 init_mutex(&ldlm_ref_sem);
2343 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2344 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2345 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2346 sizeof(struct ldlm_resource), 0,
2347 SLAB_HWCACHE_ALIGN);
2348 if (ldlm_resource_slab == NULL)
2351 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2352 sizeof(struct ldlm_lock), 0,
2353 SLAB_HWCACHE_ALIGN);
2354 if (ldlm_lock_slab == NULL) {
2355 cfs_mem_cache_destroy(ldlm_resource_slab);
2359 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2360 sizeof(struct ldlm_interval),
2361 0, SLAB_HWCACHE_ALIGN);
2362 if (ldlm_interval_slab == NULL) {
2363 cfs_mem_cache_destroy(ldlm_resource_slab);
2364 cfs_mem_cache_destroy(ldlm_lock_slab);
2371 void __exit ldlm_exit(void)
2375 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2376 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2377 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2378 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2379 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2380 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2381 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2385 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2388 EXPORT_SYMBOL(ldlm_get_processing_policy);
2389 EXPORT_SYMBOL(ldlm_lock2desc);
2390 EXPORT_SYMBOL(ldlm_register_intent);
2391 EXPORT_SYMBOL(ldlm_lockname);
2392 EXPORT_SYMBOL(ldlm_typename);
2393 EXPORT_SYMBOL(ldlm_lock2handle);
2394 EXPORT_SYMBOL(__ldlm_handle2lock);
2395 EXPORT_SYMBOL(ldlm_lock_get);
2396 EXPORT_SYMBOL(ldlm_lock_put);
2397 EXPORT_SYMBOL(ldlm_lock_fast_match);
2398 EXPORT_SYMBOL(ldlm_lock_fast_release);
2399 EXPORT_SYMBOL(ldlm_lock_match);
2400 EXPORT_SYMBOL(ldlm_lock_cancel);
2401 EXPORT_SYMBOL(ldlm_lock_addref);
2402 EXPORT_SYMBOL(ldlm_lock_addref_try);
2403 EXPORT_SYMBOL(ldlm_lock_decref);
2404 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2405 EXPORT_SYMBOL(ldlm_lock_change_resource);
2406 EXPORT_SYMBOL(ldlm_lock_set_data);
2407 EXPORT_SYMBOL(ldlm_it2str);
2408 EXPORT_SYMBOL(ldlm_lock_dump);
2409 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2410 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2411 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2412 EXPORT_SYMBOL(ldlm_lock_allow_match);
2414 /* ldlm_request.c */
2415 EXPORT_SYMBOL(ldlm_completion_ast_async);
2416 EXPORT_SYMBOL(ldlm_completion_ast);
2417 EXPORT_SYMBOL(ldlm_blocking_ast);
2418 EXPORT_SYMBOL(ldlm_glimpse_ast);
2419 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2420 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2421 EXPORT_SYMBOL(ldlm_prep_elc_req);
2422 EXPORT_SYMBOL(ldlm_cli_convert);
2423 EXPORT_SYMBOL(ldlm_cli_enqueue);
2424 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2425 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2426 EXPORT_SYMBOL(ldlm_cli_cancel);
2427 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2428 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2429 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2430 EXPORT_SYMBOL(ldlm_replay_locks);
2431 EXPORT_SYMBOL(ldlm_resource_foreach);
2432 EXPORT_SYMBOL(ldlm_namespace_foreach);
2433 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2434 EXPORT_SYMBOL(ldlm_resource_iterate);
2435 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2436 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2439 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2440 EXPORT_SYMBOL(ldlm_server_completion_ast);
2441 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2442 EXPORT_SYMBOL(ldlm_handle_enqueue);
2443 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2444 EXPORT_SYMBOL(ldlm_handle_cancel);
2445 EXPORT_SYMBOL(ldlm_request_cancel);
2446 EXPORT_SYMBOL(ldlm_handle_convert);
2447 EXPORT_SYMBOL(ldlm_handle_convert0);
2448 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2449 EXPORT_SYMBOL(ldlm_get_ref);
2450 EXPORT_SYMBOL(ldlm_put_ref);
2451 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2452 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2454 /* ldlm_resource.c */
2455 EXPORT_SYMBOL(ldlm_namespace_new);
2456 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2457 EXPORT_SYMBOL(ldlm_namespace_free);
2458 EXPORT_SYMBOL(ldlm_namespace_dump);
2459 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2460 EXPORT_SYMBOL(ldlm_resource_get);
2461 EXPORT_SYMBOL(ldlm_resource_putref);
2462 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2465 EXPORT_SYMBOL(client_import_add_conn);
2466 EXPORT_SYMBOL(client_import_del_conn);
2467 EXPORT_SYMBOL(client_obd_setup);
2468 EXPORT_SYMBOL(client_obd_cleanup);
2469 EXPORT_SYMBOL(client_connect_import);
2470 EXPORT_SYMBOL(client_disconnect_export);
2471 EXPORT_SYMBOL(target_start_recovery_thread);
2472 EXPORT_SYMBOL(target_stop_recovery_thread);
2473 EXPORT_SYMBOL(target_handle_connect);
2474 EXPORT_SYMBOL(target_cleanup_recovery);
2475 EXPORT_SYMBOL(target_destroy_export);
2476 EXPORT_SYMBOL(target_cancel_recovery_timer);
2477 EXPORT_SYMBOL(target_send_reply);
2478 EXPORT_SYMBOL(target_queue_recovery_request);
2479 EXPORT_SYMBOL(target_handle_ping);
2480 EXPORT_SYMBOL(target_pack_pool_reply);
2481 EXPORT_SYMBOL(target_handle_disconnect);
2484 EXPORT_SYMBOL(lock_res_and_lock);
2485 EXPORT_SYMBOL(unlock_res_and_lock);