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");
207 export = class_export_get(lock->l_export);
208 spin_unlock_bh(&waiting_locks_spinlock);
211 class_fail_export(export);
212 class_export_put(export);
213 spin_lock_bh(&waiting_locks_spinlock);
215 spin_unlock_bh(&waiting_locks_spinlock);
217 if (do_dump && obd_dump_on_eviction) {
218 CERROR("dump the log upon eviction\n");
219 libcfs_debug_dumplog();
222 if (expired_lock_thread.elt_state == ELT_TERMINATE)
226 expired_lock_thread.elt_state = ELT_STOPPED;
227 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
231 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
233 /* This is called from within a timer interrupt and cannot schedule */
234 static void waiting_locks_callback(unsigned long unused)
236 struct ldlm_lock *lock, *last = NULL;
239 spin_lock_bh(&waiting_locks_spinlock);
240 while (!list_empty(&waiting_locks_list)) {
241 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
244 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
245 (lock->l_req_mode == LCK_GROUP))
248 if (ptlrpc_check_suspend()) {
249 /* there is a case when we talk to one mds, holding
250 * lock from another mds. this way we easily can get
251 * here, if second mds is being recovered. so, we
252 * suspend timeouts. bug 6019 */
254 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
255 lock->l_export->exp_client_uuid.uuid,
256 lock->l_export->exp_connection->c_remote_uuid.uuid,
257 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
259 list_del_init(&lock->l_pending_chain);
260 spin_unlock_bh(&waiting_locks_spinlock);
261 ldlm_add_waiting_lock(lock);
265 /* if timeout overlaps the activation time of suspended timeouts
266 * then extend it to give a chance for client to reconnect */
267 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
268 cfs_time_seconds(obd_timeout)/2),
269 ptlrpc_suspend_wakeup_time())) {
270 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
271 lock->l_export->exp_client_uuid.uuid,
272 lock->l_export->exp_connection->c_remote_uuid.uuid,
273 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
275 list_del_init(&lock->l_pending_chain);
276 spin_unlock_bh(&waiting_locks_spinlock);
277 ldlm_add_waiting_lock(lock);
281 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
282 "evicting client at %s ",
283 cfs_time_current_sec()- lock->l_enqueued_time.tv_sec,
285 lock->l_export->exp_connection->c_peer.nid));
289 list_del(&lock->l_pending_chain);
290 list_add(&lock->l_pending_chain,
291 &expired_lock_thread.elt_expired_locks);
294 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
295 if (obd_dump_on_timeout)
296 expired_lock_thread.elt_dump = __LINE__;
298 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
302 * Make sure the timer will fire again if we have any locks
305 if (!list_empty(&waiting_locks_list)) {
306 cfs_time_t timeout_rounded;
307 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
309 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
310 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
312 spin_unlock_bh(&waiting_locks_spinlock);
316 * Indicate that we're waiting for a client to call us back cancelling a given
317 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
318 * timer to fire appropriately. (We round up to the next second, to avoid
319 * floods of timer firings during periods of high lock contention and traffic).
321 * Called with the namespace lock held.
323 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
326 cfs_time_t timeout_rounded;
328 if (!list_empty(&lock->l_pending_chain))
331 timeout = ldlm_get_enq_timeout(lock);
333 lock->l_callback_timeout = cfs_time_shift(timeout);
335 timeout_rounded = round_timeout(lock->l_callback_timeout);
337 if (cfs_time_before(timeout_rounded,
338 cfs_timer_deadline(&waiting_locks_timer)) ||
339 !cfs_timer_is_armed(&waiting_locks_timer)) {
340 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
342 /* if the new lock has a shorter timeout than something earlier on
343 the list, we'll wait the longer amount of time; no big deal. */
344 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
348 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
352 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
354 spin_lock_bh(&waiting_locks_spinlock);
355 if (lock->l_destroyed) {
356 static cfs_time_t next;
357 spin_unlock_bh(&waiting_locks_spinlock);
358 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
359 if (cfs_time_after(cfs_time_current(), next)) {
360 next = cfs_time_shift(14400);
361 libcfs_debug_dumpstack(NULL);
366 ret = __ldlm_add_waiting_lock(lock);
367 spin_unlock_bh(&waiting_locks_spinlock);
369 LDLM_DEBUG(lock, "%sadding to wait list",
370 ret == 0 ? "not re-" : "");
375 * Remove a lock from the pending list, likely because it had its cancellation
376 * callback arrive without incident. This adjusts the lock-timeout timer if
377 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
379 * Called with namespace lock held.
381 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
383 struct list_head *list_next;
385 if (list_empty(&lock->l_pending_chain))
388 list_next = lock->l_pending_chain.next;
389 if (lock->l_pending_chain.prev == &waiting_locks_list) {
390 /* Removing the head of the list, adjust timer. */
391 if (list_next == &waiting_locks_list) {
392 /* No more, just cancel. */
393 cfs_timer_disarm(&waiting_locks_timer);
395 struct ldlm_lock *next;
396 next = list_entry(list_next, struct ldlm_lock,
398 cfs_timer_arm(&waiting_locks_timer,
399 round_timeout(next->l_callback_timeout));
402 list_del_init(&lock->l_pending_chain);
407 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
411 if (lock->l_export == NULL) {
412 /* We don't have a "waiting locks list" on clients. */
413 LDLM_DEBUG(lock, "client lock: no-op");
417 spin_lock_bh(&waiting_locks_spinlock);
418 ret = __ldlm_del_waiting_lock(lock);
419 spin_unlock_bh(&waiting_locks_spinlock);
421 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
428 * Called with namespace lock held.
430 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
432 if (lock->l_export == NULL) {
433 /* We don't have a "waiting locks list" on clients. */
434 LDLM_DEBUG(lock, "client lock: no-op");
438 spin_lock_bh(&waiting_locks_spinlock);
440 if (list_empty(&lock->l_pending_chain)) {
441 spin_unlock_bh(&waiting_locks_spinlock);
442 LDLM_DEBUG(lock, "wasn't waiting");
446 __ldlm_del_waiting_lock(lock);
447 __ldlm_add_waiting_lock(lock);
448 spin_unlock_bh(&waiting_locks_spinlock);
450 LDLM_DEBUG(lock, "refreshed");
454 #else /* !__KERNEL__ */
456 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
458 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
462 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
467 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
471 #endif /* __KERNEL__ */
473 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
474 const char *ast_type)
476 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
477 char *str = libcfs_nid2str(conn->c_peer.nid);
479 LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
480 "service %s.\n", str,
481 lock->l_export->exp_obd->obd_name);
483 LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
484 "resource %d\n", ast_type,
485 obd_export_nid2str(lock->l_export), rc);
487 if (obd_dump_on_timeout)
488 libcfs_debug_dumplog();
489 class_fail_export(lock->l_export);
492 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
493 struct ptlrpc_request *req, int rc,
494 const char *ast_type)
496 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
498 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
499 LASSERT(lock->l_export);
500 if (lock->l_export->exp_libclient) {
501 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
502 " timeout, just cancelling lock", ast_type,
503 libcfs_nid2str(peer.nid));
504 ldlm_lock_cancel(lock);
506 } else if (lock->l_flags & LDLM_FL_CANCEL) {
507 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
508 "cancel was received (AST reply lost?)",
509 ast_type, libcfs_nid2str(peer.nid));
510 ldlm_lock_cancel(lock);
513 ldlm_del_waiting_lock(lock);
514 ldlm_failed_ast(lock, rc, ast_type);
518 LDLM_DEBUG(lock, "client (nid %s) returned %d"
519 " from %s AST - normal race",
520 libcfs_nid2str(peer.nid),
522 lustre_msg_get_status(req->rq_repmsg) : -1,
525 LDLM_ERROR(lock, "client (nid %s) returned %d "
526 "from %s AST", libcfs_nid2str(peer.nid),
527 (req->rq_repmsg != NULL) ?
528 lustre_msg_get_status(req->rq_repmsg) : 0,
530 ldlm_lock_cancel(lock);
531 /* Server-side AST functions are called from ldlm_reprocess_all,
532 * which needs to be told to please restart its reprocessing. */
539 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
541 struct ldlm_cb_set_arg *arg;
542 struct ldlm_lock *lock;
545 LASSERT(data != NULL);
547 arg = req->rq_async_args.pointer_arg[0];
548 lock = req->rq_async_args.pointer_arg[1];
549 LASSERT(lock != NULL);
551 /* If client canceled the lock but the cancel has not
552 * been recieved yet, we need to update lvbo to have the
553 * proper attributes cached. */
554 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
555 ldlm_res_lvbo_update(lock->l_resource, NULL,
557 rc = ldlm_handle_ast_error(lock, req, rc,
558 arg->type == LDLM_BL_CALLBACK
559 ? "blocking" : "completion");
565 atomic_set(&arg->restart, 1);
570 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
571 struct ldlm_cb_set_arg *arg,
572 struct ldlm_lock *lock,
578 if (unlikely(instant_cancel)) {
579 rc = ptl_send_rpc(req, 1);
580 ptlrpc_req_finished(req);
582 /* If we cancelled the lock, we need to restart
583 * ldlm_reprocess_queue */
584 atomic_set(&arg->restart, 1);
587 ptlrpc_set_add_req(arg->set, req);
594 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
595 * enqueued server lock conflicts with given one.
597 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
598 * to wait for client response.
600 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
601 struct ldlm_lock_desc *desc,
602 void *data, int flag)
604 struct ldlm_cb_set_arg *arg = data;
605 struct ldlm_request *body;
606 struct ptlrpc_request *req;
607 int instant_cancel = 0;
611 if (flag == LDLM_CB_CANCELING)
612 /* Don't need to do anything here. */
616 LASSERT(data != NULL);
617 if (lock->l_export->exp_obd->obd_recovering != 0) {
618 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
619 ldlm_lock_dump(D_ERROR, lock, 0);
622 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
623 &RQF_LDLM_BL_CALLBACK,
624 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
628 req->rq_async_args.pointer_arg[0] = arg;
629 req->rq_async_args.pointer_arg[1] = lock;
630 req->rq_interpret_reply = ldlm_cb_interpret;
631 req->rq_no_resend = 1;
633 lock_res(lock->l_resource);
634 if (lock->l_granted_mode != lock->l_req_mode) {
635 /* this blocking AST will be communicated as part of the
636 * completion AST instead */
637 unlock_res(lock->l_resource);
638 ptlrpc_req_finished(req);
639 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
643 if (lock->l_destroyed) {
644 /* What's the point? */
645 unlock_res(lock->l_resource);
646 ptlrpc_req_finished(req);
650 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
653 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
654 body->lock_handle[0] = lock->l_remote_handle;
655 body->lock_desc = *desc;
656 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
658 LDLM_DEBUG(lock, "server preparing blocking AST");
660 ptlrpc_request_set_replen(req);
661 if (instant_cancel) {
662 unlock_res(lock->l_resource);
663 ldlm_lock_cancel(lock);
665 LASSERT(lock->l_granted_mode == lock->l_req_mode);
666 ldlm_add_waiting_lock(lock);
667 unlock_res(lock->l_resource);
670 req->rq_send_state = LUSTRE_IMP_FULL;
671 /* ptlrpc_prep_req already set timeout */
673 req->rq_timeout = ldlm_get_rq_timeout();
675 if (lock->l_export && lock->l_export->exp_ldlm_stats)
676 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
677 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
679 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
684 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
686 struct ldlm_cb_set_arg *arg = data;
687 struct ldlm_request *body;
688 struct ptlrpc_request *req;
689 struct timeval granted_time;
690 long total_enqueue_wait;
691 int instant_cancel = 0;
695 LASSERT(lock != NULL);
696 LASSERT(data != NULL);
698 do_gettimeofday(&granted_time);
699 total_enqueue_wait = cfs_timeval_sub(&granted_time,
700 &lock->l_enqueued_time, NULL);
702 if (total_enqueue_wait / ONE_MILLION > obd_timeout)
703 /* non-fatal with AT - change to LDLM_DEBUG? */
704 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
705 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
707 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
708 &RQF_LDLM_CP_CALLBACK);
712 lock_res_and_lock(lock);
713 if (lock->l_resource->lr_lvb_len)
714 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
715 lock->l_resource->lr_lvb_len);
716 unlock_res_and_lock(lock);
718 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
720 ptlrpc_request_free(req);
724 req->rq_async_args.pointer_arg[0] = arg;
725 req->rq_async_args.pointer_arg[1] = lock;
726 req->rq_interpret_reply = ldlm_cb_interpret;
727 req->rq_no_resend = 1;
728 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
730 body->lock_handle[0] = lock->l_remote_handle;
731 body->lock_flags = flags;
732 ldlm_lock2desc(lock, &body->lock_desc);
733 if (lock->l_resource->lr_lvb_len) {
734 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
736 lock_res_and_lock(lock);
737 memcpy(lvb, lock->l_resource->lr_lvb_data,
738 lock->l_resource->lr_lvb_len);
739 unlock_res_and_lock(lock);
742 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
745 /* Server-side enqueue wait time estimate, used in
746 __ldlm_add_waiting_lock to set future enqueue timers */
747 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
748 total_enqueue_wait / ONE_MILLION);
750 ptlrpc_request_set_replen(req);
752 req->rq_send_state = LUSTRE_IMP_FULL;
753 /* ptlrpc_prep_req already set timeout */
755 req->rq_timeout = ldlm_get_rq_timeout();
757 /* We only send real blocking ASTs after the lock is granted */
758 lock_res_and_lock(lock);
759 if (lock->l_flags & LDLM_FL_AST_SENT) {
760 body->lock_flags |= LDLM_FL_AST_SENT;
762 /* We might get here prior to ldlm_handle_enqueue setting
763 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
764 * into waiting list, but this is safe and similar code in
765 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
766 * that would not only cancel the lock, but will also remove
767 * it from waiting list */
768 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
769 unlock_res_and_lock(lock);
770 ldlm_lock_cancel(lock);
772 lock_res_and_lock(lock);
774 /* start the lock-timeout clock */
775 ldlm_add_waiting_lock(lock);
778 unlock_res_and_lock(lock);
780 if (lock->l_export && lock->l_export->exp_ldlm_stats)
781 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
782 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
784 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
789 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
791 struct ldlm_resource *res = lock->l_resource;
792 struct ldlm_request *body;
793 struct ptlrpc_request *req;
797 LASSERT(lock != NULL);
799 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
800 &RQF_LDLM_GL_CALLBACK,
801 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
806 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
807 body->lock_handle[0] = lock->l_remote_handle;
808 ldlm_lock2desc(lock, &body->lock_desc);
810 lock_res_and_lock(lock);
811 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
812 lock->l_resource->lr_lvb_len);
813 unlock_res_and_lock(lock);
814 res = lock->l_resource;
815 ptlrpc_request_set_replen(req);
818 req->rq_send_state = LUSTRE_IMP_FULL;
819 /* ptlrpc_prep_req already set timeout */
821 req->rq_timeout = ldlm_get_rq_timeout();
823 if (lock->l_export && lock->l_export->exp_ldlm_stats)
824 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
825 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
827 rc = ptlrpc_queue_wait(req);
828 if (rc == -ELDLM_NO_LOCK_DATA)
829 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
831 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
833 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
835 ptlrpc_req_finished(req);
837 ldlm_reprocess_all(res);
843 extern unsigned long long lu_time_stamp_get(void);
845 #define lu_time_stamp_get() time(NULL)
848 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
849 struct lprocfs_stats *srv_stats)
851 int lock_type = 0, op = 0;
853 lock_type = dlm_req->lock_desc.l_resource.lr_type;
857 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
860 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
861 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
863 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
866 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
869 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
877 lprocfs_counter_incr(srv_stats, op);
883 * Main server-side entry point into LDLM. This is called by ptlrpc service
884 * threads to carry out client lock enqueueing requests.
886 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
887 struct ptlrpc_request *req,
888 const struct ldlm_request *dlm_req,
889 const struct ldlm_callback_suite *cbs)
891 struct ldlm_reply *dlm_rep;
893 ldlm_error_t err = ELDLM_OK;
894 struct ldlm_lock *lock = NULL;
899 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
901 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
902 flags = dlm_req->lock_flags;
904 LASSERT(req->rq_export);
906 if (req->rq_rqbd->rqbd_service->srv_stats)
907 ldlm_svc_get_eopc(dlm_req,
908 req->rq_rqbd->rqbd_service->srv_stats);
910 if (req->rq_export->exp_ldlm_stats)
911 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
912 LDLM_ENQUEUE - LDLM_FIRST_OPC);
914 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
915 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
916 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
917 dlm_req->lock_desc.l_resource.lr_type);
918 GOTO(out, rc = -EFAULT);
921 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
922 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
923 dlm_req->lock_desc.l_req_mode &
924 (dlm_req->lock_desc.l_req_mode-1))) {
925 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
926 dlm_req->lock_desc.l_req_mode);
927 GOTO(out, rc = -EFAULT);
930 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
931 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
933 DEBUG_REQ(D_ERROR, req,
934 "PLAIN lock request from IBITS client?");
935 GOTO(out, rc = -EPROTO);
937 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
939 DEBUG_REQ(D_ERROR, req,
940 "IBITS lock request from unaware client?");
941 GOTO(out, rc = -EPROTO);
945 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
946 against server's _CONNECT_SUPPORTED flags? (I don't want to use
947 ibits for mgc/mgs) */
949 /* INODEBITS_INTEROP: Perform conversion from plain lock to
950 * inodebits lock if client does not support them. */
951 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
952 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
953 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
954 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
955 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
956 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
957 dlm_req->lock_desc.l_req_mode = LCK_CR;
961 if (unlikely(flags & LDLM_FL_REPLAY)) {
962 /* Find an existing lock in the per-export lock hash */
963 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
964 (void *)&dlm_req->lock_handle[0]);
966 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
967 LPX64, lock->l_handle.h_cookie);
968 GOTO(existing_lock, rc = 0);
972 /* The lock's callback data might be set in the policy function */
973 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
974 dlm_req->lock_desc.l_resource.lr_type,
975 dlm_req->lock_desc.l_req_mode,
976 cbs->lcs_blocking, cbs->lcs_completion,
977 cbs->lcs_glimpse, NULL, 0);
980 GOTO(out, rc = -ENOMEM);
982 do_gettimeofday(&lock->l_enqueued_time);
983 lock->l_remote_handle = dlm_req->lock_handle[0];
984 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
986 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
987 /* Don't enqueue a lock onto the export if it has already
988 * been evicted. Cancel it now instead. (bug 3822) */
989 if (req->rq_export->exp_failed) {
990 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
991 GOTO(out, rc = -ENOTCONN);
993 lock->l_export = class_export_get(req->rq_export);
995 if (lock->l_export->exp_lock_hash)
996 lustre_hash_add(lock->l_export->exp_lock_hash,
997 &lock->l_remote_handle,
1002 if (flags & LDLM_FL_HAS_INTENT) {
1003 /* In this case, the reply buffer is allocated deep in
1004 * local_lock_enqueue by the policy function. */
1007 lock_res_and_lock(lock);
1008 if (lock->l_resource->lr_lvb_len) {
1009 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1011 lock->l_resource->lr_lvb_len);
1013 unlock_res_and_lock(lock);
1015 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1016 GOTO(out, rc = -ENOMEM);
1018 rc = req_capsule_server_pack(&req->rq_pill);
1023 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1024 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1025 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1026 lock->l_req_extent = lock->l_policy_data.l_extent;
1028 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1032 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1033 dlm_rep->lock_flags = flags;
1035 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1036 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1038 /* We never send a blocking AST until the lock is granted, but
1039 * we can tell it right now */
1040 lock_res_and_lock(lock);
1042 /* Now take into account flags to be inherited from original lock
1043 request both in reply to client and in our own lock flags. */
1044 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1045 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1047 /* Don't move a pending lock onto the export if it has already
1048 * been evicted. Cancel it now instead. (bug 5683) */
1049 if (unlikely(req->rq_export->exp_failed ||
1050 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1051 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1053 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1054 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1055 if (lock->l_granted_mode == lock->l_req_mode) {
1057 * Only cancel lock if it was granted, because it would
1058 * be destroyed immediatelly and would never be granted
1059 * in the future, causing timeouts on client. Not
1060 * granted lock will be cancelled immediatelly after
1061 * sending completion AST.
1063 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1064 unlock_res_and_lock(lock);
1065 ldlm_lock_cancel(lock);
1066 lock_res_and_lock(lock);
1068 ldlm_add_waiting_lock(lock);
1071 /* Make sure we never ever grant usual metadata locks to liblustre
1073 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1074 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1075 req->rq_export->exp_libclient) {
1076 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1077 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1078 CERROR("Granting sync lock to libclient. "
1079 "req fl %d, rep fl %d, lock fl %d\n",
1080 dlm_req->lock_flags, dlm_rep->lock_flags,
1082 LDLM_ERROR(lock, "sync lock");
1083 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1084 struct ldlm_intent *it;
1086 it = req_capsule_client_get(&req->rq_pill,
1089 CERROR("This is intent %s ("LPU64")\n",
1090 ldlm_it2str(it->opc), it->opc);
1096 unlock_res_and_lock(lock);
1100 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1101 if (!req->rq_packed_final) {
1102 err = lustre_pack_reply(req, 1, NULL, NULL);
1107 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1108 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1110 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1111 "(err=%d, rc=%d)", err, rc);
1113 lock_res_and_lock(lock);
1115 if (lock->l_resource->lr_lvb_len > 0) {
1118 lvb = req_capsule_server_get(&req->rq_pill,
1120 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1123 memcpy(lvb, lock->l_resource->lr_lvb_data,
1124 lock->l_resource->lr_lvb_len);
1127 ldlm_resource_unlink_lock(lock);
1128 ldlm_lock_destroy_nolock(lock);
1130 unlock_res_and_lock(lock);
1132 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1133 ldlm_reprocess_all(lock->l_resource);
1135 LDLM_LOCK_PUT(lock);
1138 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1144 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1145 ldlm_completion_callback completion_callback,
1146 ldlm_blocking_callback blocking_callback,
1147 ldlm_glimpse_callback glimpse_callback)
1149 struct ldlm_request *dlm_req;
1150 struct ldlm_callback_suite cbs = {
1151 .lcs_completion = completion_callback,
1152 .lcs_blocking = blocking_callback,
1153 .lcs_glimpse = glimpse_callback
1157 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1158 if (dlm_req != NULL) {
1159 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1160 req, dlm_req, &cbs);
1167 int ldlm_handle_convert0(struct ptlrpc_request *req,
1168 const struct ldlm_request *dlm_req)
1170 struct ldlm_reply *dlm_rep;
1171 struct ldlm_lock *lock;
1175 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1176 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1177 LDLM_CONVERT - LDLM_FIRST_OPC);
1179 rc = req_capsule_server_pack(&req->rq_pill);
1183 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1184 dlm_rep->lock_flags = dlm_req->lock_flags;
1186 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1188 req->rq_status = EINVAL;
1192 LDLM_DEBUG(lock, "server-side convert handler START");
1194 do_gettimeofday(&lock->l_enqueued_time);
1195 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1196 &dlm_rep->lock_flags);
1198 if (ldlm_del_waiting_lock(lock))
1199 LDLM_DEBUG(lock, "converted waiting lock");
1202 req->rq_status = EDEADLOCK;
1207 if (!req->rq_status)
1208 ldlm_reprocess_all(lock->l_resource);
1209 LDLM_DEBUG(lock, "server-side convert handler END");
1210 LDLM_LOCK_PUT(lock);
1212 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1217 int ldlm_handle_convert(struct ptlrpc_request *req)
1220 struct ldlm_request *dlm_req;
1222 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1223 if (dlm_req != NULL) {
1224 rc = ldlm_handle_convert0(req, dlm_req);
1226 CERROR ("Can't unpack dlm_req\n");
1232 /* Cancel all the locks whos handles are packed into ldlm_request */
1233 int ldlm_request_cancel(struct ptlrpc_request *req,
1234 const struct ldlm_request *dlm_req, int first)
1236 struct ldlm_resource *res, *pres = NULL;
1237 struct ldlm_lock *lock;
1238 int i, count, done = 0;
1241 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1245 /* There is no lock on the server at the replay time,
1246 * skip lock cancelling to make replay tests to pass. */
1247 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1250 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1251 "starting at %d", count, first);
1253 for (i = first; i < count; i++) {
1254 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1256 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1257 "lock (cookie "LPU64")",
1258 dlm_req->lock_handle[i].cookie);
1262 res = lock->l_resource;
1267 ldlm_reprocess_all(pres);
1268 ldlm_resource_putref(pres);
1271 ldlm_resource_getref(res);
1272 ldlm_res_lvbo_update(res, NULL, 0, 1);
1276 ldlm_lock_cancel(lock);
1277 LDLM_LOCK_PUT(lock);
1280 ldlm_reprocess_all(pres);
1281 ldlm_resource_putref(pres);
1283 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1287 int ldlm_handle_cancel(struct ptlrpc_request *req)
1289 struct ldlm_request *dlm_req;
1293 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1294 if (dlm_req == NULL) {
1295 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1299 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1300 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1301 LDLM_CANCEL - LDLM_FIRST_OPC);
1303 rc = req_capsule_server_pack(&req->rq_pill);
1307 if (!ldlm_request_cancel(req, dlm_req, 0))
1308 req->rq_status = ESTALE;
1310 if (ptlrpc_reply(req) != 0)
1316 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1317 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1322 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1324 lock_res_and_lock(lock);
1325 lock->l_flags |= LDLM_FL_CBPENDING;
1327 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1328 lock->l_flags |= LDLM_FL_CANCEL;
1330 do_ast = (!lock->l_readers && !lock->l_writers);
1331 unlock_res_and_lock(lock);
1334 LDLM_DEBUG(lock, "already unused, calling "
1335 "callback (%p)", lock->l_blocking_ast);
1336 if (lock->l_blocking_ast != NULL)
1337 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1340 LDLM_DEBUG(lock, "Lock still has references, will be"
1341 " cancelled later");
1344 LDLM_DEBUG(lock, "client blocking callback handler END");
1345 LDLM_LOCK_PUT(lock);
1349 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1350 struct ldlm_namespace *ns,
1351 struct ldlm_request *dlm_req,
1352 struct ldlm_lock *lock)
1354 CFS_LIST_HEAD(ast_list);
1357 LDLM_DEBUG(lock, "client completion callback handler START");
1359 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1360 int to = cfs_time_seconds(1);
1362 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1363 if (lock->l_granted_mode == lock->l_req_mode ||
1369 lock_res_and_lock(lock);
1370 if (lock->l_destroyed ||
1371 lock->l_granted_mode == lock->l_req_mode) {
1372 /* bug 11300: the lock has already been granted */
1373 unlock_res_and_lock(lock);
1374 LDLM_DEBUG(lock, "Double grant race happened");
1375 LDLM_LOCK_PUT(lock);
1380 /* If we receive the completion AST before the actual enqueue returned,
1381 * then we might need to switch lock modes, resources, or extents. */
1382 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1383 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1384 LDLM_DEBUG(lock, "completion AST, new lock mode");
1387 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1388 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1389 LDLM_DEBUG(lock, "completion AST, new policy data");
1392 ldlm_resource_unlink_lock(lock);
1393 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1394 &lock->l_resource->lr_name,
1395 sizeof(lock->l_resource->lr_name)) != 0) {
1396 unlock_res_and_lock(lock);
1397 if (ldlm_lock_change_resource(ns, lock,
1398 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1399 LDLM_ERROR(lock, "Failed to allocate resource");
1400 LDLM_LOCK_PUT(lock);
1404 LDLM_DEBUG(lock, "completion AST, new resource");
1405 CERROR("change resource!\n");
1406 lock_res_and_lock(lock);
1409 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1410 /* BL_AST locks are not needed in lru.
1411 * let ldlm_cancel_lru() be fast. */
1412 ldlm_lock_remove_from_lru(lock);
1413 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1414 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1417 if (lock->l_lvb_len) {
1418 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1419 RCL_CLIENT) < lock->l_lvb_len) {
1420 LDLM_ERROR(lock, "completion AST did not contain "
1423 void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1425 (void *)lock->l_lvb_swabber);
1426 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1430 ldlm_grant_lock(lock, &ast_list);
1431 unlock_res_and_lock(lock);
1433 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1435 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1437 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1439 LDLM_LOCK_PUT(lock);
1443 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1444 struct ldlm_namespace *ns,
1445 struct ldlm_request *dlm_req,
1446 struct ldlm_lock *lock)
1451 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1453 if (lock->l_glimpse_ast != NULL)
1454 rc = lock->l_glimpse_ast(lock, req);
1456 if (req->rq_repmsg != NULL) {
1459 req->rq_status = rc;
1463 lock_res_and_lock(lock);
1464 if (lock->l_granted_mode == LCK_PW &&
1465 !lock->l_readers && !lock->l_writers &&
1466 cfs_time_after(cfs_time_current(),
1467 cfs_time_add(lock->l_last_used,
1468 cfs_time_seconds(10)))) {
1469 unlock_res_and_lock(lock);
1470 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1471 ldlm_handle_bl_callback(ns, NULL, lock);
1476 unlock_res_and_lock(lock);
1477 LDLM_LOCK_PUT(lock);
1481 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1483 if (req->rq_no_reply)
1486 req->rq_status = rc;
1487 if (!req->rq_packed_final) {
1488 rc = lustre_pack_reply(req, 1, NULL, NULL);
1492 return ptlrpc_reply(req);
1496 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1497 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1498 struct list_head *cancels, int count)
1500 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1501 struct ldlm_bl_work_item *blwi;
1504 if (cancels && count == 0)
1507 OBD_ALLOC(blwi, sizeof(*blwi));
1513 blwi->blwi_ld = *ld;
1515 list_add(&blwi->blwi_head, cancels);
1516 list_del_init(cancels);
1517 blwi->blwi_count = count;
1519 blwi->blwi_lock = lock;
1521 spin_lock(&blp->blp_lock);
1522 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1523 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1524 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1526 /* other blocking callbacks are added to the regular list */
1527 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1529 cfs_waitq_signal(&blp->blp_waitq);
1530 spin_unlock(&blp->blp_lock);
1536 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1537 struct ldlm_lock *lock)
1540 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1546 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1547 struct list_head *cancels, int count)
1550 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1556 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1557 static int ldlm_callback_handler(struct ptlrpc_request *req)
1559 struct ldlm_namespace *ns;
1560 struct ldlm_request *dlm_req;
1561 struct ldlm_lock *lock;
1565 /* Requests arrive in sender's byte order. The ptlrpc service
1566 * handler has already checked and, if necessary, byte-swapped the
1567 * incoming request message body, but I am responsible for the
1568 * message buffers. */
1570 /* do nothing for sec context finalize */
1571 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1574 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1576 if (req->rq_export == NULL) {
1577 struct ldlm_request *dlm_req;
1579 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1580 "export cookie "LPX64"; this is "
1581 "normal if this node rebooted with a lock held\n",
1582 lustre_msg_get_opc(req->rq_reqmsg),
1583 libcfs_id2str(req->rq_peer),
1584 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1586 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1587 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1588 if (dlm_req != NULL)
1589 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1590 dlm_req->lock_handle[0].cookie);
1592 ldlm_callback_reply(req, -ENOTCONN);
1596 LASSERT(req->rq_export != NULL);
1597 LASSERT(req->rq_export->exp_obd != NULL);
1599 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1600 case LDLM_BL_CALLBACK:
1601 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1604 case LDLM_CP_CALLBACK:
1605 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1608 case LDLM_GL_CALLBACK:
1609 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1612 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1613 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1614 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1616 rc = llog_origin_handle_cancel(req);
1617 ldlm_callback_reply(req, rc);
1619 case OBD_QC_CALLBACK:
1620 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1621 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1623 rc = target_handle_qc_callback(req);
1624 ldlm_callback_reply(req, rc);
1628 /* reply in handler */
1629 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1630 rc = target_handle_dqacq_callback(req);
1632 case LLOG_ORIGIN_HANDLE_CREATE:
1633 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1634 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1636 rc = llog_origin_handle_create(req);
1637 ldlm_callback_reply(req, rc);
1639 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1640 req_capsule_set(&req->rq_pill,
1641 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1642 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1644 rc = llog_origin_handle_next_block(req);
1645 ldlm_callback_reply(req, rc);
1647 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1648 req_capsule_set(&req->rq_pill,
1649 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1650 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1652 rc = llog_origin_handle_read_header(req);
1653 ldlm_callback_reply(req, rc);
1655 case LLOG_ORIGIN_HANDLE_CLOSE:
1656 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1658 rc = llog_origin_handle_close(req);
1659 ldlm_callback_reply(req, rc);
1662 CERROR("unknown opcode %u\n",
1663 lustre_msg_get_opc(req->rq_reqmsg));
1664 ldlm_callback_reply(req, -EPROTO);
1668 ns = req->rq_export->exp_obd->obd_namespace;
1669 LASSERT(ns != NULL);
1671 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1673 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1674 if (dlm_req == NULL) {
1675 ldlm_callback_reply(req, -EPROTO);
1679 /* Force a known safe race, send a cancel to the server for a lock
1680 * which the server has already started a blocking callback on. */
1681 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1682 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1683 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1685 CERROR("ldlm_cli_cancel: %d\n", rc);
1688 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1690 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1691 "disappeared\n", dlm_req->lock_handle[0].cookie);
1692 ldlm_callback_reply(req, -EINVAL);
1696 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1697 lock_res_and_lock(lock);
1698 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1699 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1700 /* If somebody cancels locks and cache is already droped,
1701 * we can tell the server we have no lock. Otherwise, we
1702 * should send cancel after dropping the cache. */
1703 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1704 (lock->l_flags & LDLM_FL_BL_DONE)) {
1705 LDLM_DEBUG(lock, "callback on lock "
1706 LPX64" - lock disappeared\n",
1707 dlm_req->lock_handle[0].cookie);
1708 unlock_res_and_lock(lock);
1709 LDLM_LOCK_PUT(lock);
1710 ldlm_callback_reply(req, -EINVAL);
1713 /* BL_AST locks are not needed in lru.
1714 * let ldlm_cancel_lru() be fast. */
1715 ldlm_lock_remove_from_lru(lock);
1716 lock->l_flags |= LDLM_FL_BL_AST;
1718 unlock_res_and_lock(lock);
1720 /* We want the ost thread to get this reply so that it can respond
1721 * to ost requests (write cache writeback) that might be triggered
1724 * But we'd also like to be able to indicate in the reply that we're
1725 * cancelling right now, because it's unused, or have an intent result
1726 * in the reply, so we might have to push the responsibility for sending
1727 * the reply down into the AST handlers, alas. */
1729 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1730 case LDLM_BL_CALLBACK:
1731 CDEBUG(D_INODE, "blocking ast\n");
1732 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1733 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1734 ldlm_callback_reply(req, 0);
1735 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1736 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1738 case LDLM_CP_CALLBACK:
1739 CDEBUG(D_INODE, "completion ast\n");
1740 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1741 ldlm_callback_reply(req, 0);
1742 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1744 case LDLM_GL_CALLBACK:
1745 CDEBUG(D_INODE, "glimpse ast\n");
1746 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1747 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1750 LBUG(); /* checked above */
1756 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1761 /* Requests arrive in sender's byte order. The ptlrpc service
1762 * handler has already checked and, if necessary, byte-swapped the
1763 * incoming request message body, but I am responsible for the
1764 * message buffers. */
1766 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1768 if (req->rq_export == NULL) {
1769 struct ldlm_request *dlm_req;
1771 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1772 lustre_msg_get_opc(req->rq_reqmsg),
1773 libcfs_id2str(req->rq_peer),
1774 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1776 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1777 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1778 if (dlm_req != NULL)
1779 ldlm_lock_dump_handle(D_ERROR,
1780 &dlm_req->lock_handle[0]);
1781 ldlm_callback_reply(req, -ENOTCONN);
1785 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1787 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1789 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1790 CDEBUG(D_INODE, "cancel\n");
1791 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1793 rc = ldlm_handle_cancel(req);
1797 case OBD_LOG_CANCEL:
1798 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1799 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1801 rc = llog_origin_handle_cancel(req);
1802 ldlm_callback_reply(req, rc);
1805 CERROR("invalid opcode %d\n",
1806 lustre_msg_get_opc(req->rq_reqmsg));
1807 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1808 ldlm_callback_reply(req, -EINVAL);
1814 void ldlm_revoke_lock_cb(void *obj, void *data)
1816 struct list_head *rpc_list = data;
1817 struct ldlm_lock *lock = obj;
1819 lock_res_and_lock(lock);
1821 if (lock->l_req_mode != lock->l_granted_mode) {
1822 unlock_res_and_lock(lock);
1826 LASSERT(lock->l_resource);
1827 if (lock->l_resource->lr_type != LDLM_IBITS &&
1828 lock->l_resource->lr_type != LDLM_PLAIN) {
1829 unlock_res_and_lock(lock);
1833 if (lock->l_flags & LDLM_FL_AST_SENT) {
1834 unlock_res_and_lock(lock);
1838 LASSERT(lock->l_blocking_ast);
1839 LASSERT(!lock->l_blocking_lock);
1841 lock->l_flags |= LDLM_FL_AST_SENT;
1842 if (lock->l_export && lock->l_export->exp_lock_hash)
1843 lustre_hash_del(lock->l_export->exp_lock_hash,
1844 &lock->l_remote_handle, &lock->l_exp_hash);
1845 list_add_tail(&lock->l_rk_ast, rpc_list);
1846 LDLM_LOCK_GET(lock);
1848 unlock_res_and_lock(lock);
1851 void ldlm_revoke_export_locks(struct obd_export *exp)
1853 struct list_head rpc_list;
1856 CFS_INIT_LIST_HEAD(&rpc_list);
1857 lustre_hash_for_each_empty(exp->exp_lock_hash,
1858 ldlm_revoke_lock_cb, &rpc_list);
1859 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1865 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1867 struct ldlm_bl_work_item *blwi = NULL;
1868 static unsigned int num_bl = 0;
1870 spin_lock(&blp->blp_lock);
1871 /* process a request from the blp_list at least every blp_num_threads */
1872 if (!list_empty(&blp->blp_list) &&
1873 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1874 blwi = list_entry(blp->blp_list.next,
1875 struct ldlm_bl_work_item, blwi_entry);
1877 if (!list_empty(&blp->blp_prio_list))
1878 blwi = list_entry(blp->blp_prio_list.next,
1879 struct ldlm_bl_work_item, blwi_entry);
1882 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1884 list_del(&blwi->blwi_entry);
1886 spin_unlock(&blp->blp_lock);
1891 /* This only contains temporary data until the thread starts */
1892 struct ldlm_bl_thread_data {
1893 char bltd_name[CFS_CURPROC_COMM_MAX];
1894 struct ldlm_bl_pool *bltd_blp;
1895 struct completion bltd_comp;
1899 static int ldlm_bl_thread_main(void *arg);
1901 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1903 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1906 init_completion(&bltd.bltd_comp);
1907 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1909 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1910 atomic_read(&blp->blp_num_threads), rc);
1913 wait_for_completion(&bltd.bltd_comp);
1918 static int ldlm_bl_thread_main(void *arg)
1920 struct ldlm_bl_pool *blp;
1924 struct ldlm_bl_thread_data *bltd = arg;
1926 blp = bltd->bltd_blp;
1928 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1929 atomic_inc(&blp->blp_busy_threads);
1931 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1932 "ldlm_bl_%02d", bltd->bltd_num);
1933 cfs_daemonize(bltd->bltd_name);
1935 complete(&bltd->bltd_comp);
1936 /* cannot use bltd after this, it is only on caller's stack */
1940 struct l_wait_info lwi = { 0 };
1941 struct ldlm_bl_work_item *blwi = NULL;
1943 blwi = ldlm_bl_get_work(blp);
1948 atomic_dec(&blp->blp_busy_threads);
1949 l_wait_event_exclusive(blp->blp_waitq,
1950 (blwi = ldlm_bl_get_work(blp)) != NULL,
1952 busy = atomic_inc_return(&blp->blp_busy_threads);
1954 if (blwi->blwi_ns == NULL)
1955 /* added by ldlm_cleanup() */
1958 /* Not fatal if racy and have a few too many threads */
1959 if (unlikely(busy < blp->blp_max_threads &&
1960 busy >= atomic_read(&blp->blp_num_threads)))
1961 /* discard the return value, we tried */
1962 ldlm_bl_thread_start(blp);
1964 if (blwi->blwi_ns == NULL)
1965 /* added by ldlm_cleanup() */
1969 if (blwi->blwi_count) {
1970 /* The special case when we cancel locks in lru
1971 * asynchronously, we pass the list of locks here.
1972 * Thus lock is marked LDLM_FL_CANCELING, and already
1973 * canceled locally. */
1974 ldlm_cli_cancel_list(&blwi->blwi_head,
1975 blwi->blwi_count, NULL, 0);
1977 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1980 OBD_FREE(blwi, sizeof(*blwi));
1983 atomic_dec(&blp->blp_busy_threads);
1984 atomic_dec(&blp->blp_num_threads);
1985 complete(&blp->blp_comp);
1991 static int ldlm_setup(void);
1992 static int ldlm_cleanup(void);
1994 int ldlm_get_ref(void)
1998 mutex_down(&ldlm_ref_sem);
1999 if (++ldlm_refcount == 1) {
2004 mutex_up(&ldlm_ref_sem);
2009 void ldlm_put_ref(void)
2012 mutex_down(&ldlm_ref_sem);
2013 if (ldlm_refcount == 1) {
2014 int rc = ldlm_cleanup();
2016 CERROR("ldlm_cleanup failed: %d\n", rc);
2022 mutex_up(&ldlm_ref_sem);
2028 * Export handle<->lock hash operations.
2031 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2033 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2037 ldlm_export_lock_key(struct hlist_node *hnode)
2039 struct ldlm_lock *lock;
2042 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2043 RETURN(&lock->l_remote_handle);
2047 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2050 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2054 ldlm_export_lock_get(struct hlist_node *hnode)
2056 struct ldlm_lock *lock;
2059 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2060 LDLM_LOCK_GET(lock);
2066 ldlm_export_lock_put(struct hlist_node *hnode)
2068 struct ldlm_lock *lock;
2071 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2072 LDLM_LOCK_PUT(lock);
2077 static lustre_hash_ops_t ldlm_export_lock_ops = {
2078 .lh_hash = ldlm_export_lock_hash,
2079 .lh_key = ldlm_export_lock_key,
2080 .lh_compare = ldlm_export_lock_compare,
2081 .lh_get = ldlm_export_lock_get,
2082 .lh_put = ldlm_export_lock_put
2085 int ldlm_init_export(struct obd_export *exp)
2089 exp->exp_lock_hash =
2090 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2091 128, 65536, &ldlm_export_lock_ops, LH_REHASH);
2093 if (!exp->exp_lock_hash)
2098 EXPORT_SYMBOL(ldlm_init_export);
2100 void ldlm_destroy_export(struct obd_export *exp)
2103 lustre_hash_exit(exp->exp_lock_hash);
2104 exp->exp_lock_hash = NULL;
2107 EXPORT_SYMBOL(ldlm_destroy_export);
2109 static int ldlm_setup(void)
2111 struct ldlm_bl_pool *blp;
2113 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2114 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2120 if (ldlm_state != NULL)
2123 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2124 if (ldlm_state == NULL)
2128 rc = ldlm_proc_setup();
2134 if (ldlm_num_threads) {
2135 /* If ldlm_num_threads is set, it is the min and the max. */
2136 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2137 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2138 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2139 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2140 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2144 ldlm_state->ldlm_cb_service =
2145 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2146 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2147 LDLM_CB_REPLY_PORTAL, 1800,
2148 ldlm_callback_handler, "ldlm_cbd",
2149 ldlm_svc_proc_dir, NULL,
2150 ldlm_min_threads, ldlm_max_threads,
2152 LCT_MD_THREAD|LCT_DT_THREAD);
2154 if (!ldlm_state->ldlm_cb_service) {
2155 CERROR("failed to start service\n");
2156 GOTO(out_proc, rc = -ENOMEM);
2159 ldlm_state->ldlm_cancel_service =
2160 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2161 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2162 LDLM_CANCEL_REPLY_PORTAL, 6000,
2163 ldlm_cancel_handler, "ldlm_canceld",
2164 ldlm_svc_proc_dir, NULL,
2165 ldlm_min_threads, ldlm_max_threads,
2167 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2169 if (!ldlm_state->ldlm_cancel_service) {
2170 CERROR("failed to start service\n");
2171 GOTO(out_proc, rc = -ENOMEM);
2174 OBD_ALLOC(blp, sizeof(*blp));
2176 GOTO(out_proc, rc = -ENOMEM);
2177 ldlm_state->ldlm_bl_pool = blp;
2179 spin_lock_init(&blp->blp_lock);
2180 CFS_INIT_LIST_HEAD(&blp->blp_list);
2181 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2182 cfs_waitq_init(&blp->blp_waitq);
2183 atomic_set(&blp->blp_num_threads, 0);
2184 atomic_set(&blp->blp_busy_threads, 0);
2185 blp->blp_min_threads = ldlm_min_threads;
2186 blp->blp_max_threads = ldlm_max_threads;
2189 for (i = 0; i < blp->blp_min_threads; i++) {
2190 rc = ldlm_bl_thread_start(blp);
2192 GOTO(out_thread, rc);
2195 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2197 GOTO(out_thread, rc);
2199 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2201 GOTO(out_thread, rc);
2203 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2204 expired_lock_thread.elt_state = ELT_STOPPED;
2205 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2207 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2208 spin_lock_init(&waiting_locks_spinlock);
2209 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2211 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2213 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2214 GOTO(out_thread, rc);
2217 wait_event(expired_lock_thread.elt_waitq,
2218 expired_lock_thread.elt_state == ELT_READY);
2222 rc = ldlm_pools_init();
2224 GOTO(out_thread, rc);
2230 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2231 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2236 ldlm_proc_cleanup();
2239 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2244 static int ldlm_cleanup(void)
2247 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2251 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2252 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2253 CERROR("ldlm still has namespaces; clean these up first.\n");
2254 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2255 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2264 while (atomic_read(&blp->blp_num_threads) > 0) {
2265 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2267 init_completion(&blp->blp_comp);
2269 spin_lock(&blp->blp_lock);
2270 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2271 cfs_waitq_signal(&blp->blp_waitq);
2272 spin_unlock(&blp->blp_lock);
2274 wait_for_completion(&blp->blp_comp);
2276 OBD_FREE(blp, sizeof(*blp));
2278 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2279 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2280 ldlm_proc_cleanup();
2282 expired_lock_thread.elt_state = ELT_TERMINATE;
2283 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2284 wait_event(expired_lock_thread.elt_waitq,
2285 expired_lock_thread.elt_state == ELT_STOPPED);
2287 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2288 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2291 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2297 int __init ldlm_init(void)
2299 init_mutex(&ldlm_ref_sem);
2300 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2301 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2302 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2303 sizeof(struct ldlm_resource), 0,
2304 SLAB_HWCACHE_ALIGN);
2305 if (ldlm_resource_slab == NULL)
2308 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2309 sizeof(struct ldlm_lock), 0,
2310 SLAB_HWCACHE_ALIGN);
2311 if (ldlm_lock_slab == NULL) {
2312 cfs_mem_cache_destroy(ldlm_resource_slab);
2316 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2317 sizeof(struct ldlm_interval),
2318 0, SLAB_HWCACHE_ALIGN);
2319 if (ldlm_interval_slab == NULL) {
2320 cfs_mem_cache_destroy(ldlm_resource_slab);
2321 cfs_mem_cache_destroy(ldlm_lock_slab);
2328 void __exit ldlm_exit(void)
2332 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2333 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2334 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2335 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2336 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2337 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2338 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2342 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2345 EXPORT_SYMBOL(ldlm_get_processing_policy);
2346 EXPORT_SYMBOL(ldlm_lock2desc);
2347 EXPORT_SYMBOL(ldlm_register_intent);
2348 EXPORT_SYMBOL(ldlm_lockname);
2349 EXPORT_SYMBOL(ldlm_typename);
2350 EXPORT_SYMBOL(ldlm_lock2handle);
2351 EXPORT_SYMBOL(__ldlm_handle2lock);
2352 EXPORT_SYMBOL(ldlm_lock_get);
2353 EXPORT_SYMBOL(ldlm_lock_put);
2354 EXPORT_SYMBOL(ldlm_lock_fast_match);
2355 EXPORT_SYMBOL(ldlm_lock_fast_release);
2356 EXPORT_SYMBOL(ldlm_lock_match);
2357 EXPORT_SYMBOL(ldlm_lock_cancel);
2358 EXPORT_SYMBOL(ldlm_lock_addref);
2359 EXPORT_SYMBOL(ldlm_lock_decref);
2360 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2361 EXPORT_SYMBOL(ldlm_lock_change_resource);
2362 EXPORT_SYMBOL(ldlm_lock_set_data);
2363 EXPORT_SYMBOL(ldlm_it2str);
2364 EXPORT_SYMBOL(ldlm_lock_dump);
2365 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2366 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2367 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2368 EXPORT_SYMBOL(ldlm_lock_allow_match);
2370 /* ldlm_request.c */
2371 EXPORT_SYMBOL(ldlm_completion_ast);
2372 EXPORT_SYMBOL(ldlm_blocking_ast);
2373 EXPORT_SYMBOL(ldlm_glimpse_ast);
2374 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2375 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2376 EXPORT_SYMBOL(ldlm_prep_elc_req);
2377 EXPORT_SYMBOL(ldlm_cli_convert);
2378 EXPORT_SYMBOL(ldlm_cli_enqueue);
2379 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2380 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2381 EXPORT_SYMBOL(ldlm_cli_cancel);
2382 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2383 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2384 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2385 EXPORT_SYMBOL(ldlm_cli_join_lru);
2386 EXPORT_SYMBOL(ldlm_replay_locks);
2387 EXPORT_SYMBOL(ldlm_resource_foreach);
2388 EXPORT_SYMBOL(ldlm_namespace_foreach);
2389 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2390 EXPORT_SYMBOL(ldlm_resource_iterate);
2391 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2392 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2395 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2396 EXPORT_SYMBOL(ldlm_server_completion_ast);
2397 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2398 EXPORT_SYMBOL(ldlm_handle_enqueue);
2399 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2400 EXPORT_SYMBOL(ldlm_handle_cancel);
2401 EXPORT_SYMBOL(ldlm_request_cancel);
2402 EXPORT_SYMBOL(ldlm_handle_convert);
2403 EXPORT_SYMBOL(ldlm_handle_convert0);
2404 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2405 EXPORT_SYMBOL(ldlm_get_ref);
2406 EXPORT_SYMBOL(ldlm_put_ref);
2407 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2408 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2410 /* ldlm_resource.c */
2411 EXPORT_SYMBOL(ldlm_namespace_new);
2412 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2413 EXPORT_SYMBOL(ldlm_namespace_free);
2414 EXPORT_SYMBOL(ldlm_namespace_dump);
2415 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2416 EXPORT_SYMBOL(ldlm_resource_get);
2417 EXPORT_SYMBOL(ldlm_resource_putref);
2418 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2421 EXPORT_SYMBOL(client_import_add_conn);
2422 EXPORT_SYMBOL(client_import_del_conn);
2423 EXPORT_SYMBOL(client_obd_setup);
2424 EXPORT_SYMBOL(client_obd_cleanup);
2425 EXPORT_SYMBOL(client_connect_import);
2426 EXPORT_SYMBOL(client_disconnect_export);
2427 EXPORT_SYMBOL(target_start_recovery_thread);
2428 EXPORT_SYMBOL(target_stop_recovery_thread);
2429 EXPORT_SYMBOL(target_handle_connect);
2430 EXPORT_SYMBOL(target_cleanup_recovery);
2431 EXPORT_SYMBOL(target_destroy_export);
2432 EXPORT_SYMBOL(target_cancel_recovery_timer);
2433 EXPORT_SYMBOL(target_send_reply);
2434 EXPORT_SYMBOL(target_queue_recovery_request);
2435 EXPORT_SYMBOL(target_handle_ping);
2436 EXPORT_SYMBOL(target_pack_pool_reply);
2437 EXPORT_SYMBOL(target_handle_disconnect);
2440 EXPORT_SYMBOL(lock_res_and_lock);
2441 EXPORT_SYMBOL(unlock_res_and_lock);