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();
490 spin_lock_bh(&waiting_locks_spinlock);
491 list_add(&lock->l_pending_chain, &expired_lock_thread.elt_expired_locks);
492 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
493 spin_unlock_bh(&waiting_locks_spinlock);
495 class_fail_export(lock->l_export);
499 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
500 struct ptlrpc_request *req, int rc,
501 const char *ast_type)
503 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
505 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
506 LASSERT(lock->l_export);
507 if (lock->l_export->exp_libclient) {
508 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
509 " timeout, just cancelling lock", ast_type,
510 libcfs_nid2str(peer.nid));
511 ldlm_lock_cancel(lock);
513 } else if (lock->l_flags & LDLM_FL_CANCEL) {
514 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
515 "cancel was received (AST reply lost?)",
516 ast_type, libcfs_nid2str(peer.nid));
517 ldlm_lock_cancel(lock);
520 ldlm_del_waiting_lock(lock);
521 ldlm_failed_ast(lock, rc, ast_type);
525 LDLM_DEBUG(lock, "client (nid %s) returned %d"
526 " from %s AST - normal race",
527 libcfs_nid2str(peer.nid),
529 lustre_msg_get_status(req->rq_repmsg) : -1,
532 LDLM_ERROR(lock, "client (nid %s) returned %d "
533 "from %s AST", libcfs_nid2str(peer.nid),
534 (req->rq_repmsg != NULL) ?
535 lustre_msg_get_status(req->rq_repmsg) : 0,
537 ldlm_lock_cancel(lock);
538 /* Server-side AST functions are called from ldlm_reprocess_all,
539 * which needs to be told to please restart its reprocessing. */
546 static int ldlm_cb_interpret(const struct lu_env *env,
547 struct ptlrpc_request *req, void *data, int rc)
549 struct ldlm_cb_set_arg *arg;
550 struct ldlm_lock *lock;
553 LASSERT(data != NULL);
555 arg = req->rq_async_args.pointer_arg[0];
556 lock = req->rq_async_args.pointer_arg[1];
557 LASSERT(lock != NULL);
559 /* If client canceled the lock but the cancel has not
560 * been recieved yet, we need to update lvbo to have the
561 * proper attributes cached. */
562 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
563 ldlm_res_lvbo_update(lock->l_resource, NULL,
565 rc = ldlm_handle_ast_error(lock, req, rc,
566 arg->type == LDLM_BL_CALLBACK
567 ? "blocking" : "completion");
570 LDLM_LOCK_RELEASE(lock);
573 atomic_set(&arg->restart, 1);
578 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
579 struct ldlm_cb_set_arg *arg,
580 struct ldlm_lock *lock,
586 if (unlikely(instant_cancel)) {
587 rc = ptl_send_rpc(req, 1);
588 ptlrpc_req_finished(req);
590 /* If we cancelled the lock, we need to restart
591 * ldlm_reprocess_queue */
592 atomic_set(&arg->restart, 1);
595 ptlrpc_set_add_req(arg->set, req);
602 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
603 * enqueued server lock conflicts with given one.
605 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
606 * to wait for client response.
608 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
609 struct ldlm_lock_desc *desc,
610 void *data, int flag)
612 struct ldlm_cb_set_arg *arg = data;
613 struct ldlm_request *body;
614 struct ptlrpc_request *req;
615 int instant_cancel = 0;
619 if (flag == LDLM_CB_CANCELING)
620 /* Don't need to do anything here. */
624 LASSERT(data != NULL);
625 if (lock->l_export->exp_obd->obd_recovering != 0) {
626 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
627 ldlm_lock_dump(D_ERROR, lock, 0);
630 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
631 &RQF_LDLM_BL_CALLBACK,
632 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
636 req->rq_async_args.pointer_arg[0] = arg;
637 req->rq_async_args.pointer_arg[1] = lock;
638 req->rq_interpret_reply = ldlm_cb_interpret;
639 req->rq_no_resend = 1;
641 lock_res(lock->l_resource);
642 if (lock->l_granted_mode != lock->l_req_mode) {
643 /* this blocking AST will be communicated as part of the
644 * completion AST instead */
645 unlock_res(lock->l_resource);
646 ptlrpc_req_finished(req);
647 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
651 if (lock->l_destroyed) {
652 /* What's the point? */
653 unlock_res(lock->l_resource);
654 ptlrpc_req_finished(req);
658 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
661 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
662 body->lock_handle[0] = lock->l_remote_handle;
663 body->lock_desc = *desc;
664 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
666 LDLM_DEBUG(lock, "server preparing blocking AST");
668 ptlrpc_request_set_replen(req);
669 if (instant_cancel) {
670 unlock_res(lock->l_resource);
671 ldlm_lock_cancel(lock);
673 LASSERT(lock->l_granted_mode == lock->l_req_mode);
674 ldlm_add_waiting_lock(lock);
675 unlock_res(lock->l_resource);
678 req->rq_send_state = LUSTRE_IMP_FULL;
679 /* ptlrpc_prep_req already set timeout */
681 req->rq_timeout = ldlm_get_rq_timeout();
683 if (lock->l_export && lock->l_export->exp_nid_stats &&
684 lock->l_export->exp_nid_stats->nid_ldlm_stats)
685 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
686 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
688 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
693 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
695 struct ldlm_cb_set_arg *arg = data;
696 struct ldlm_request *body;
697 struct ptlrpc_request *req;
698 struct timeval granted_time;
699 long total_enqueue_wait;
700 int instant_cancel = 0;
704 LASSERT(lock != NULL);
705 LASSERT(data != NULL);
707 do_gettimeofday(&granted_time);
708 total_enqueue_wait = cfs_timeval_sub(&granted_time,
709 &lock->l_enqueued_time, NULL);
711 if (total_enqueue_wait / ONE_MILLION > obd_timeout)
712 /* non-fatal with AT - change to LDLM_DEBUG? */
713 LDLM_ERROR(lock, "enqueue wait took %luus from "CFS_TIME_T,
714 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
716 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
717 &RQF_LDLM_CP_CALLBACK);
721 lock_res_and_lock(lock);
722 if (lock->l_resource->lr_lvb_len)
723 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
724 lock->l_resource->lr_lvb_len);
725 unlock_res_and_lock(lock);
727 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
729 ptlrpc_request_free(req);
733 req->rq_async_args.pointer_arg[0] = arg;
734 req->rq_async_args.pointer_arg[1] = lock;
735 req->rq_interpret_reply = ldlm_cb_interpret;
736 req->rq_no_resend = 1;
737 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
739 body->lock_handle[0] = lock->l_remote_handle;
740 body->lock_flags = flags;
741 ldlm_lock2desc(lock, &body->lock_desc);
742 if (lock->l_resource->lr_lvb_len) {
743 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
745 lock_res_and_lock(lock);
746 memcpy(lvb, lock->l_resource->lr_lvb_data,
747 lock->l_resource->lr_lvb_len);
748 unlock_res_and_lock(lock);
751 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
754 /* Server-side enqueue wait time estimate, used in
755 __ldlm_add_waiting_lock to set future enqueue timers */
756 at_add(&lock->l_resource->lr_namespace->ns_at_estimate,
757 total_enqueue_wait / ONE_MILLION);
759 ptlrpc_request_set_replen(req);
761 req->rq_send_state = LUSTRE_IMP_FULL;
762 /* ptlrpc_prep_req already set timeout */
764 req->rq_timeout = ldlm_get_rq_timeout();
766 /* We only send real blocking ASTs after the lock is granted */
767 lock_res_and_lock(lock);
768 if (lock->l_flags & LDLM_FL_AST_SENT) {
769 body->lock_flags |= LDLM_FL_AST_SENT;
771 /* We might get here prior to ldlm_handle_enqueue setting
772 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
773 * into waiting list, but this is safe and similar code in
774 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
775 * that would not only cancel the lock, but will also remove
776 * it from waiting list */
777 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
778 unlock_res_and_lock(lock);
779 ldlm_lock_cancel(lock);
781 lock_res_and_lock(lock);
783 /* start the lock-timeout clock */
784 ldlm_add_waiting_lock(lock);
787 unlock_res_and_lock(lock);
789 if (lock->l_export && lock->l_export->exp_nid_stats &&
790 lock->l_export->exp_nid_stats->nid_ldlm_stats)
791 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
792 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
794 rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
799 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
801 struct ldlm_resource *res = lock->l_resource;
802 struct ldlm_request *body;
803 struct ptlrpc_request *req;
807 LASSERT(lock != NULL);
809 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
810 &RQF_LDLM_GL_CALLBACK,
811 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK);
816 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
817 body->lock_handle[0] = lock->l_remote_handle;
818 ldlm_lock2desc(lock, &body->lock_desc);
820 lock_res_and_lock(lock);
821 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
822 lock->l_resource->lr_lvb_len);
823 unlock_res_and_lock(lock);
824 res = lock->l_resource;
825 ptlrpc_request_set_replen(req);
828 req->rq_send_state = LUSTRE_IMP_FULL;
829 /* ptlrpc_prep_req already set timeout */
831 req->rq_timeout = ldlm_get_rq_timeout();
833 if (lock->l_export && lock->l_export->exp_nid_stats &&
834 lock->l_export->exp_nid_stats->nid_ldlm_stats)
835 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
836 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
838 rc = ptlrpc_queue_wait(req);
839 if (rc == -ELDLM_NO_LOCK_DATA)
840 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
842 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
844 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
846 ptlrpc_req_finished(req);
848 ldlm_reprocess_all(res);
854 extern unsigned long long lu_time_stamp_get(void);
856 #define lu_time_stamp_get() time(NULL)
859 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
860 struct lprocfs_stats *srv_stats)
862 int lock_type = 0, op = 0;
864 lock_type = dlm_req->lock_desc.l_resource.lr_type;
868 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
871 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
872 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
874 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
877 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
880 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
888 lprocfs_counter_incr(srv_stats, op);
894 * Main server-side entry point into LDLM. This is called by ptlrpc service
895 * threads to carry out client lock enqueueing requests.
897 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
898 struct ptlrpc_request *req,
899 const struct ldlm_request *dlm_req,
900 const struct ldlm_callback_suite *cbs)
902 struct ldlm_reply *dlm_rep;
904 ldlm_error_t err = ELDLM_OK;
905 struct ldlm_lock *lock = NULL;
910 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
912 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
913 flags = dlm_req->lock_flags;
915 LASSERT(req->rq_export);
917 if (req->rq_rqbd->rqbd_service->srv_stats)
918 ldlm_svc_get_eopc(dlm_req,
919 req->rq_rqbd->rqbd_service->srv_stats);
921 if (req->rq_export && req->rq_export->exp_nid_stats &&
922 req->rq_export->exp_nid_stats->nid_ldlm_stats)
923 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
924 LDLM_ENQUEUE - LDLM_FIRST_OPC);
926 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
927 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
928 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
929 dlm_req->lock_desc.l_resource.lr_type);
930 GOTO(out, rc = -EFAULT);
933 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
934 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
935 dlm_req->lock_desc.l_req_mode &
936 (dlm_req->lock_desc.l_req_mode-1))) {
937 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
938 dlm_req->lock_desc.l_req_mode);
939 GOTO(out, rc = -EFAULT);
942 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
943 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
945 DEBUG_REQ(D_ERROR, req,
946 "PLAIN lock request from IBITS client?");
947 GOTO(out, rc = -EPROTO);
949 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
951 DEBUG_REQ(D_ERROR, req,
952 "IBITS lock request from unaware client?");
953 GOTO(out, rc = -EPROTO);
957 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
958 against server's _CONNECT_SUPPORTED flags? (I don't want to use
959 ibits for mgc/mgs) */
961 /* INODEBITS_INTEROP: Perform conversion from plain lock to
962 * inodebits lock if client does not support them. */
963 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
964 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
965 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
966 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
967 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
968 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
969 dlm_req->lock_desc.l_req_mode = LCK_CR;
973 if (unlikely(flags & LDLM_FL_REPLAY)) {
974 /* Find an existing lock in the per-export lock hash */
975 lock = lustre_hash_lookup(req->rq_export->exp_lock_hash,
976 (void *)&dlm_req->lock_handle[0]);
978 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
979 LPX64, lock->l_handle.h_cookie);
980 GOTO(existing_lock, rc = 0);
984 /* The lock's callback data might be set in the policy function */
985 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
986 dlm_req->lock_desc.l_resource.lr_type,
987 dlm_req->lock_desc.l_req_mode,
991 GOTO(out, rc = -ENOMEM);
993 do_gettimeofday(&lock->l_enqueued_time);
994 lock->l_remote_handle = dlm_req->lock_handle[0];
995 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
997 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
998 /* Don't enqueue a lock onto the export if it has already
999 * been evicted. Cancel it now instead. (bug 3822) */
1000 if (req->rq_export->exp_failed) {
1001 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1002 GOTO(out, rc = -ENOTCONN);
1004 lock->l_export = class_export_get(req->rq_export);
1006 if (lock->l_export->exp_lock_hash)
1007 lustre_hash_add(lock->l_export->exp_lock_hash,
1008 &lock->l_remote_handle,
1013 if (flags & LDLM_FL_HAS_INTENT) {
1014 /* In this case, the reply buffer is allocated deep in
1015 * local_lock_enqueue by the policy function. */
1018 lock_res_and_lock(lock);
1019 if (lock->l_resource->lr_lvb_len) {
1020 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1022 lock->l_resource->lr_lvb_len);
1024 unlock_res_and_lock(lock);
1026 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1027 GOTO(out, rc = -ENOMEM);
1029 rc = req_capsule_server_pack(&req->rq_pill);
1034 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1035 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1036 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1037 lock->l_req_extent = lock->l_policy_data.l_extent;
1039 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1043 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1044 dlm_rep->lock_flags = flags;
1046 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1047 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1049 /* We never send a blocking AST until the lock is granted, but
1050 * we can tell it right now */
1051 lock_res_and_lock(lock);
1053 /* Now take into account flags to be inherited from original lock
1054 request both in reply to client and in our own lock flags. */
1055 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1056 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1058 /* Don't move a pending lock onto the export if it has already
1059 * been evicted. Cancel it now instead. (bug 5683) */
1060 if (unlikely(req->rq_export->exp_failed ||
1061 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1062 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1064 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1065 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1066 if (lock->l_granted_mode == lock->l_req_mode) {
1068 * Only cancel lock if it was granted, because it would
1069 * be destroyed immediatelly and would never be granted
1070 * in the future, causing timeouts on client. Not
1071 * granted lock will be cancelled immediatelly after
1072 * sending completion AST.
1074 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1075 unlock_res_and_lock(lock);
1076 ldlm_lock_cancel(lock);
1077 lock_res_and_lock(lock);
1079 ldlm_add_waiting_lock(lock);
1082 /* Make sure we never ever grant usual metadata locks to liblustre
1084 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1085 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1086 req->rq_export->exp_libclient) {
1087 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1088 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1089 CERROR("Granting sync lock to libclient. "
1090 "req fl %d, rep fl %d, lock fl %d\n",
1091 dlm_req->lock_flags, dlm_rep->lock_flags,
1093 LDLM_ERROR(lock, "sync lock");
1094 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1095 struct ldlm_intent *it;
1097 it = req_capsule_client_get(&req->rq_pill,
1100 CERROR("This is intent %s ("LPU64")\n",
1101 ldlm_it2str(it->opc), it->opc);
1107 unlock_res_and_lock(lock);
1111 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1112 if (!req->rq_packed_final) {
1113 err = lustre_pack_reply(req, 1, NULL, NULL);
1118 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1119 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1121 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1122 "(err=%d, rc=%d)", err, rc);
1124 lock_res_and_lock(lock);
1126 if (lock->l_resource->lr_lvb_len > 0) {
1129 lvb = req_capsule_server_get(&req->rq_pill,
1131 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1134 memcpy(lvb, lock->l_resource->lr_lvb_data,
1135 lock->l_resource->lr_lvb_len);
1138 ldlm_resource_unlink_lock(lock);
1139 ldlm_lock_destroy_nolock(lock);
1141 unlock_res_and_lock(lock);
1143 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1144 ldlm_reprocess_all(lock->l_resource);
1146 LDLM_LOCK_RELEASE(lock);
1149 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1155 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1156 ldlm_completion_callback completion_callback,
1157 ldlm_blocking_callback blocking_callback,
1158 ldlm_glimpse_callback glimpse_callback)
1160 struct ldlm_request *dlm_req;
1161 struct ldlm_callback_suite cbs = {
1162 .lcs_completion = completion_callback,
1163 .lcs_blocking = blocking_callback,
1164 .lcs_glimpse = glimpse_callback
1168 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1169 if (dlm_req != NULL) {
1170 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1171 req, dlm_req, &cbs);
1178 int ldlm_handle_convert0(struct ptlrpc_request *req,
1179 const struct ldlm_request *dlm_req)
1181 struct ldlm_reply *dlm_rep;
1182 struct ldlm_lock *lock;
1186 if (req->rq_export && req->rq_export->exp_nid_stats &&
1187 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1188 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1189 LDLM_CONVERT - LDLM_FIRST_OPC);
1191 rc = req_capsule_server_pack(&req->rq_pill);
1195 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1196 dlm_rep->lock_flags = dlm_req->lock_flags;
1198 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1200 req->rq_status = EINVAL;
1204 LDLM_DEBUG(lock, "server-side convert handler START");
1206 do_gettimeofday(&lock->l_enqueued_time);
1207 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1208 &dlm_rep->lock_flags);
1210 if (ldlm_del_waiting_lock(lock))
1211 LDLM_DEBUG(lock, "converted waiting lock");
1214 req->rq_status = EDEADLOCK;
1219 if (!req->rq_status)
1220 ldlm_reprocess_all(lock->l_resource);
1221 LDLM_DEBUG(lock, "server-side convert handler END");
1222 LDLM_LOCK_PUT(lock);
1224 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1229 int ldlm_handle_convert(struct ptlrpc_request *req)
1232 struct ldlm_request *dlm_req;
1234 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1235 if (dlm_req != NULL) {
1236 rc = ldlm_handle_convert0(req, dlm_req);
1238 CERROR ("Can't unpack dlm_req\n");
1244 /* Cancel all the locks whos handles are packed into ldlm_request */
1245 int ldlm_request_cancel(struct ptlrpc_request *req,
1246 const struct ldlm_request *dlm_req, int first)
1248 struct ldlm_resource *res, *pres = NULL;
1249 struct ldlm_lock *lock;
1250 int i, count, done = 0;
1253 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1257 /* There is no lock on the server at the replay time,
1258 * skip lock cancelling to make replay tests to pass. */
1259 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1262 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1263 "starting at %d", count, first);
1265 for (i = first; i < count; i++) {
1266 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1268 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1269 "lock (cookie "LPU64")",
1270 dlm_req->lock_handle[i].cookie);
1274 res = lock->l_resource;
1279 ldlm_reprocess_all(pres);
1280 LDLM_RESOURCE_DELREF(pres);
1281 ldlm_resource_putref(pres);
1284 ldlm_resource_getref(res);
1285 LDLM_RESOURCE_ADDREF(res);
1286 ldlm_res_lvbo_update(res, NULL, 0, 1);
1290 ldlm_lock_cancel(lock);
1291 LDLM_LOCK_PUT(lock);
1294 ldlm_reprocess_all(pres);
1295 LDLM_RESOURCE_DELREF(pres);
1296 ldlm_resource_putref(pres);
1298 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1302 int ldlm_handle_cancel(struct ptlrpc_request *req)
1304 struct ldlm_request *dlm_req;
1308 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1309 if (dlm_req == NULL) {
1310 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1314 if (req->rq_export && req->rq_export->exp_nid_stats &&
1315 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1316 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1317 LDLM_CANCEL - LDLM_FIRST_OPC);
1319 rc = req_capsule_server_pack(&req->rq_pill);
1323 if (!ldlm_request_cancel(req, dlm_req, 0))
1324 req->rq_status = ESTALE;
1326 if (ptlrpc_reply(req) != 0)
1332 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1333 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1338 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1340 lock_res_and_lock(lock);
1341 lock->l_flags |= LDLM_FL_CBPENDING;
1343 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1344 lock->l_flags |= LDLM_FL_CANCEL;
1346 do_ast = (!lock->l_readers && !lock->l_writers);
1347 unlock_res_and_lock(lock);
1350 LDLM_DEBUG(lock, "already unused, calling "
1351 "callback (%p)", lock->l_blocking_ast);
1352 if (lock->l_blocking_ast != NULL)
1353 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1356 LDLM_DEBUG(lock, "Lock still has references, will be"
1357 " cancelled later");
1360 LDLM_DEBUG(lock, "client blocking callback handler END");
1361 LDLM_LOCK_RELEASE(lock);
1365 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1366 struct ldlm_namespace *ns,
1367 struct ldlm_request *dlm_req,
1368 struct ldlm_lock *lock)
1370 CFS_LIST_HEAD(ast_list);
1373 LDLM_DEBUG(lock, "client completion callback handler START");
1375 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1376 int to = cfs_time_seconds(1);
1378 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, to);
1379 if (lock->l_granted_mode == lock->l_req_mode ||
1385 lock_res_and_lock(lock);
1386 if (lock->l_destroyed ||
1387 lock->l_granted_mode == lock->l_req_mode) {
1388 /* bug 11300: the lock has already been granted */
1389 unlock_res_and_lock(lock);
1390 LDLM_DEBUG(lock, "Double grant race happened");
1391 LDLM_LOCK_RELEASE(lock);
1396 /* If we receive the completion AST before the actual enqueue returned,
1397 * then we might need to switch lock modes, resources, or extents. */
1398 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1399 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1400 LDLM_DEBUG(lock, "completion AST, new lock mode");
1403 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1404 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1405 LDLM_DEBUG(lock, "completion AST, new policy data");
1408 ldlm_resource_unlink_lock(lock);
1409 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1410 &lock->l_resource->lr_name,
1411 sizeof(lock->l_resource->lr_name)) != 0) {
1412 unlock_res_and_lock(lock);
1413 if (ldlm_lock_change_resource(ns, lock,
1414 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1415 LDLM_ERROR(lock, "Failed to allocate resource");
1416 LDLM_LOCK_RELEASE(lock);
1420 LDLM_DEBUG(lock, "completion AST, new resource");
1421 CERROR("change resource!\n");
1422 lock_res_and_lock(lock);
1425 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1426 /* BL_AST locks are not needed in lru.
1427 * let ldlm_cancel_lru() be fast. */
1428 ldlm_lock_remove_from_lru(lock);
1429 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1430 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1433 if (lock->l_lvb_len) {
1434 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1435 RCL_CLIENT) < lock->l_lvb_len) {
1436 LDLM_ERROR(lock, "completion AST did not contain "
1439 void *lvb = req_capsule_client_swab_get(&req->rq_pill,
1441 (void *)lock->l_lvb_swabber);
1442 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1446 ldlm_grant_lock(lock, &ast_list);
1447 unlock_res_and_lock(lock);
1449 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1451 ldlm_run_ast_work(&ast_list, LDLM_WORK_CP_AST);
1453 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1455 LDLM_LOCK_RELEASE(lock);
1459 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1460 struct ldlm_namespace *ns,
1461 struct ldlm_request *dlm_req,
1462 struct ldlm_lock *lock)
1467 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1469 if (lock->l_glimpse_ast != NULL)
1470 rc = lock->l_glimpse_ast(lock, req);
1472 if (req->rq_repmsg != NULL) {
1475 req->rq_status = rc;
1479 lock_res_and_lock(lock);
1480 if (lock->l_granted_mode == LCK_PW &&
1481 !lock->l_readers && !lock->l_writers &&
1482 cfs_time_after(cfs_time_current(),
1483 cfs_time_add(lock->l_last_used,
1484 cfs_time_seconds(10)))) {
1485 unlock_res_and_lock(lock);
1486 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1487 ldlm_handle_bl_callback(ns, NULL, lock);
1492 unlock_res_and_lock(lock);
1493 LDLM_LOCK_RELEASE(lock);
1497 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1499 if (req->rq_no_reply)
1502 req->rq_status = rc;
1503 if (!req->rq_packed_final) {
1504 rc = lustre_pack_reply(req, 1, NULL, NULL);
1508 return ptlrpc_reply(req);
1512 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1513 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1514 struct list_head *cancels, int count)
1516 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1517 struct ldlm_bl_work_item *blwi;
1520 if (cancels && count == 0)
1523 OBD_ALLOC(blwi, sizeof(*blwi));
1529 blwi->blwi_ld = *ld;
1531 list_add(&blwi->blwi_head, cancels);
1532 list_del_init(cancels);
1533 blwi->blwi_count = count;
1535 blwi->blwi_lock = lock;
1537 spin_lock(&blp->blp_lock);
1538 if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1539 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1540 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1542 /* other blocking callbacks are added to the regular list */
1543 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1545 cfs_waitq_signal(&blp->blp_waitq);
1546 spin_unlock(&blp->blp_lock);
1552 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1553 struct ldlm_lock *lock)
1556 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1562 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1563 struct list_head *cancels, int count)
1566 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1572 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1573 static int ldlm_callback_handler(struct ptlrpc_request *req)
1575 struct ldlm_namespace *ns;
1576 struct ldlm_request *dlm_req;
1577 struct ldlm_lock *lock;
1581 /* Requests arrive in sender's byte order. The ptlrpc service
1582 * handler has already checked and, if necessary, byte-swapped the
1583 * incoming request message body, but I am responsible for the
1584 * message buffers. */
1586 /* do nothing for sec context finalize */
1587 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1590 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1592 if (req->rq_export == NULL) {
1593 struct ldlm_request *dlm_req;
1595 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1596 "export cookie "LPX64"; this is "
1597 "normal if this node rebooted with a lock held\n",
1598 lustre_msg_get_opc(req->rq_reqmsg),
1599 libcfs_id2str(req->rq_peer),
1600 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1602 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1603 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1604 if (dlm_req != NULL)
1605 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1606 dlm_req->lock_handle[0].cookie);
1608 ldlm_callback_reply(req, -ENOTCONN);
1612 LASSERT(req->rq_export != NULL);
1613 LASSERT(req->rq_export->exp_obd != NULL);
1615 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1616 case LDLM_BL_CALLBACK:
1617 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1620 case LDLM_CP_CALLBACK:
1621 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1624 case LDLM_GL_CALLBACK:
1625 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1628 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1629 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1630 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1632 rc = llog_origin_handle_cancel(req);
1633 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1635 ldlm_callback_reply(req, rc);
1637 case OBD_QC_CALLBACK:
1638 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
1639 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1641 rc = target_handle_qc_callback(req);
1642 ldlm_callback_reply(req, rc);
1646 /* reply in handler */
1647 req_capsule_set(&req->rq_pill, &RQF_MDS_QUOTA_DQACQ);
1648 rc = target_handle_dqacq_callback(req);
1650 case LLOG_ORIGIN_HANDLE_CREATE:
1651 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1652 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1654 rc = llog_origin_handle_create(req);
1655 ldlm_callback_reply(req, rc);
1657 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1658 req_capsule_set(&req->rq_pill,
1659 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1660 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1662 rc = llog_origin_handle_next_block(req);
1663 ldlm_callback_reply(req, rc);
1665 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1666 req_capsule_set(&req->rq_pill,
1667 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1668 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1670 rc = llog_origin_handle_read_header(req);
1671 ldlm_callback_reply(req, rc);
1673 case LLOG_ORIGIN_HANDLE_CLOSE:
1674 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1676 rc = llog_origin_handle_close(req);
1677 ldlm_callback_reply(req, rc);
1680 CERROR("unknown opcode %u\n",
1681 lustre_msg_get_opc(req->rq_reqmsg));
1682 ldlm_callback_reply(req, -EPROTO);
1686 ns = req->rq_export->exp_obd->obd_namespace;
1687 LASSERT(ns != NULL);
1689 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1691 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1692 if (dlm_req == NULL) {
1693 ldlm_callback_reply(req, -EPROTO);
1697 /* Force a known safe race, send a cancel to the server for a lock
1698 * which the server has already started a blocking callback on. */
1699 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
1700 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1701 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
1703 CERROR("ldlm_cli_cancel: %d\n", rc);
1706 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
1708 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1709 "disappeared\n", dlm_req->lock_handle[0].cookie);
1710 ldlm_callback_reply(req, -EINVAL);
1714 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1715 lock_res_and_lock(lock);
1716 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1717 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1718 /* If somebody cancels locks and cache is already droped,
1719 * we can tell the server we have no lock. Otherwise, we
1720 * should send cancel after dropping the cache. */
1721 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1722 (lock->l_flags & LDLM_FL_BL_DONE)) {
1723 LDLM_DEBUG(lock, "callback on lock "
1724 LPX64" - lock disappeared\n",
1725 dlm_req->lock_handle[0].cookie);
1726 unlock_res_and_lock(lock);
1727 LDLM_LOCK_RELEASE(lock);
1728 ldlm_callback_reply(req, -EINVAL);
1731 /* BL_AST locks are not needed in lru.
1732 * let ldlm_cancel_lru() be fast. */
1733 ldlm_lock_remove_from_lru(lock);
1734 lock->l_flags |= LDLM_FL_BL_AST;
1736 unlock_res_and_lock(lock);
1738 /* We want the ost thread to get this reply so that it can respond
1739 * to ost requests (write cache writeback) that might be triggered
1742 * But we'd also like to be able to indicate in the reply that we're
1743 * cancelling right now, because it's unused, or have an intent result
1744 * in the reply, so we might have to push the responsibility for sending
1745 * the reply down into the AST handlers, alas. */
1747 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1748 case LDLM_BL_CALLBACK:
1749 CDEBUG(D_INODE, "blocking ast\n");
1750 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
1751 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1752 ldlm_callback_reply(req, 0);
1753 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1754 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1756 case LDLM_CP_CALLBACK:
1757 CDEBUG(D_INODE, "completion ast\n");
1758 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
1759 ldlm_callback_reply(req, 0);
1760 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1762 case LDLM_GL_CALLBACK:
1763 CDEBUG(D_INODE, "glimpse ast\n");
1764 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
1765 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1768 LBUG(); /* checked above */
1774 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1779 /* Requests arrive in sender's byte order. The ptlrpc service
1780 * handler has already checked and, if necessary, byte-swapped the
1781 * incoming request message body, but I am responsible for the
1782 * message buffers. */
1784 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1786 if (req->rq_export == NULL) {
1787 struct ldlm_request *dlm_req;
1789 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1790 lustre_msg_get_opc(req->rq_reqmsg),
1791 libcfs_id2str(req->rq_peer),
1792 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1794 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1795 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1796 if (dlm_req != NULL)
1797 ldlm_lock_dump_handle(D_ERROR,
1798 &dlm_req->lock_handle[0]);
1799 ldlm_callback_reply(req, -ENOTCONN);
1803 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1805 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1807 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1808 CDEBUG(D_INODE, "cancel\n");
1809 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1811 rc = ldlm_handle_cancel(req);
1815 case OBD_LOG_CANCEL:
1816 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1817 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1819 rc = llog_origin_handle_cancel(req);
1820 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1822 ldlm_callback_reply(req, rc);
1825 CERROR("invalid opcode %d\n",
1826 lustre_msg_get_opc(req->rq_reqmsg));
1827 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
1828 ldlm_callback_reply(req, -EINVAL);
1834 void ldlm_revoke_lock_cb(void *obj, void *data)
1836 struct list_head *rpc_list = data;
1837 struct ldlm_lock *lock = obj;
1839 lock_res_and_lock(lock);
1841 if (lock->l_req_mode != lock->l_granted_mode) {
1842 unlock_res_and_lock(lock);
1846 LASSERT(lock->l_resource);
1847 if (lock->l_resource->lr_type != LDLM_IBITS &&
1848 lock->l_resource->lr_type != LDLM_PLAIN) {
1849 unlock_res_and_lock(lock);
1853 if (lock->l_flags & LDLM_FL_AST_SENT) {
1854 unlock_res_and_lock(lock);
1858 LASSERT(lock->l_blocking_ast);
1859 LASSERT(!lock->l_blocking_lock);
1861 lock->l_flags |= LDLM_FL_AST_SENT;
1862 if (lock->l_export && lock->l_export->exp_lock_hash)
1863 lustre_hash_del(lock->l_export->exp_lock_hash,
1864 &lock->l_remote_handle, &lock->l_exp_hash);
1865 list_add_tail(&lock->l_rk_ast, rpc_list);
1866 LDLM_LOCK_GET(lock);
1868 unlock_res_and_lock(lock);
1871 void ldlm_revoke_export_locks(struct obd_export *exp)
1873 struct list_head rpc_list;
1876 CFS_INIT_LIST_HEAD(&rpc_list);
1877 lustre_hash_for_each_empty(exp->exp_lock_hash,
1878 ldlm_revoke_lock_cb, &rpc_list);
1879 ldlm_run_ast_work(&rpc_list, LDLM_WORK_REVOKE_AST);
1885 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1887 struct ldlm_bl_work_item *blwi = NULL;
1888 static unsigned int num_bl = 0;
1890 spin_lock(&blp->blp_lock);
1891 /* process a request from the blp_list at least every blp_num_threads */
1892 if (!list_empty(&blp->blp_list) &&
1893 (list_empty(&blp->blp_prio_list) || num_bl == 0))
1894 blwi = list_entry(blp->blp_list.next,
1895 struct ldlm_bl_work_item, blwi_entry);
1897 if (!list_empty(&blp->blp_prio_list))
1898 blwi = list_entry(blp->blp_prio_list.next,
1899 struct ldlm_bl_work_item, blwi_entry);
1902 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1904 list_del(&blwi->blwi_entry);
1906 spin_unlock(&blp->blp_lock);
1911 /* This only contains temporary data until the thread starts */
1912 struct ldlm_bl_thread_data {
1913 char bltd_name[CFS_CURPROC_COMM_MAX];
1914 struct ldlm_bl_pool *bltd_blp;
1915 struct completion bltd_comp;
1919 static int ldlm_bl_thread_main(void *arg);
1921 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1923 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1926 init_completion(&bltd.bltd_comp);
1927 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1929 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1930 atomic_read(&blp->blp_num_threads), rc);
1933 wait_for_completion(&bltd.bltd_comp);
1938 static int ldlm_bl_thread_main(void *arg)
1940 struct ldlm_bl_pool *blp;
1944 struct ldlm_bl_thread_data *bltd = arg;
1946 blp = bltd->bltd_blp;
1948 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1949 atomic_inc(&blp->blp_busy_threads);
1951 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1952 "ldlm_bl_%02d", bltd->bltd_num);
1953 cfs_daemonize(bltd->bltd_name);
1955 complete(&bltd->bltd_comp);
1956 /* cannot use bltd after this, it is only on caller's stack */
1960 struct l_wait_info lwi = { 0 };
1961 struct ldlm_bl_work_item *blwi = NULL;
1963 blwi = ldlm_bl_get_work(blp);
1968 atomic_dec(&blp->blp_busy_threads);
1969 l_wait_event_exclusive(blp->blp_waitq,
1970 (blwi = ldlm_bl_get_work(blp)) != NULL,
1972 busy = atomic_inc_return(&blp->blp_busy_threads);
1974 if (blwi->blwi_ns == NULL)
1975 /* added by ldlm_cleanup() */
1978 /* Not fatal if racy and have a few too many threads */
1979 if (unlikely(busy < blp->blp_max_threads &&
1980 busy >= atomic_read(&blp->blp_num_threads)))
1981 /* discard the return value, we tried */
1982 ldlm_bl_thread_start(blp);
1984 if (blwi->blwi_ns == NULL)
1985 /* added by ldlm_cleanup() */
1989 if (blwi->blwi_count) {
1990 /* The special case when we cancel locks in lru
1991 * asynchronously, we pass the list of locks here.
1992 * Thus lock is marked LDLM_FL_CANCELING, and already
1993 * canceled locally. */
1994 ldlm_cli_cancel_list(&blwi->blwi_head,
1995 blwi->blwi_count, NULL, 0);
1997 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2000 OBD_FREE(blwi, sizeof(*blwi));
2003 atomic_dec(&blp->blp_busy_threads);
2004 atomic_dec(&blp->blp_num_threads);
2005 complete(&blp->blp_comp);
2011 static int ldlm_setup(void);
2012 static int ldlm_cleanup(void);
2014 int ldlm_get_ref(void)
2018 mutex_down(&ldlm_ref_sem);
2019 if (++ldlm_refcount == 1) {
2024 mutex_up(&ldlm_ref_sem);
2029 void ldlm_put_ref(void)
2032 mutex_down(&ldlm_ref_sem);
2033 if (ldlm_refcount == 1) {
2034 int rc = ldlm_cleanup();
2036 CERROR("ldlm_cleanup failed: %d\n", rc);
2042 mutex_up(&ldlm_ref_sem);
2048 * Export handle<->lock hash operations.
2051 ldlm_export_lock_hash(lustre_hash_t *lh, void *key, unsigned mask)
2053 return lh_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2057 ldlm_export_lock_key(struct hlist_node *hnode)
2059 struct ldlm_lock *lock;
2062 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2063 RETURN(&lock->l_remote_handle);
2067 ldlm_export_lock_compare(void *key, struct hlist_node *hnode)
2070 RETURN(lustre_handle_equal(ldlm_export_lock_key(hnode), key));
2074 ldlm_export_lock_get(struct hlist_node *hnode)
2076 struct ldlm_lock *lock;
2079 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2080 LDLM_LOCK_GET(lock);
2086 ldlm_export_lock_put(struct hlist_node *hnode)
2088 struct ldlm_lock *lock;
2091 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2092 LDLM_LOCK_RELEASE(lock);
2097 static lustre_hash_ops_t ldlm_export_lock_ops = {
2098 .lh_hash = ldlm_export_lock_hash,
2099 .lh_key = ldlm_export_lock_key,
2100 .lh_compare = ldlm_export_lock_compare,
2101 .lh_get = ldlm_export_lock_get,
2102 .lh_put = ldlm_export_lock_put
2105 int ldlm_init_export(struct obd_export *exp)
2109 exp->exp_lock_hash =
2110 lustre_hash_init(obd_uuid2str(&exp->exp_client_uuid),
2111 128, 65536, &ldlm_export_lock_ops, LH_REHASH);
2113 if (!exp->exp_lock_hash)
2118 EXPORT_SYMBOL(ldlm_init_export);
2120 void ldlm_destroy_export(struct obd_export *exp)
2123 lustre_hash_exit(exp->exp_lock_hash);
2124 exp->exp_lock_hash = NULL;
2127 EXPORT_SYMBOL(ldlm_destroy_export);
2129 static int ldlm_setup(void)
2131 struct ldlm_bl_pool *blp;
2133 int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
2134 int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
2140 if (ldlm_state != NULL)
2143 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2144 if (ldlm_state == NULL)
2148 rc = ldlm_proc_setup();
2154 if (ldlm_num_threads) {
2155 /* If ldlm_num_threads is set, it is the min and the max. */
2156 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2157 ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2158 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2159 ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2160 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2164 ldlm_state->ldlm_cb_service =
2165 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2166 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2167 LDLM_CB_REPLY_PORTAL, 1800,
2168 ldlm_callback_handler, "ldlm_cbd",
2169 ldlm_svc_proc_dir, NULL,
2170 ldlm_min_threads, ldlm_max_threads,
2172 LCT_MD_THREAD|LCT_DT_THREAD);
2174 if (!ldlm_state->ldlm_cb_service) {
2175 CERROR("failed to start service\n");
2176 GOTO(out_proc, rc = -ENOMEM);
2179 ldlm_state->ldlm_cancel_service =
2180 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2181 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2182 LDLM_CANCEL_REPLY_PORTAL, 6000,
2183 ldlm_cancel_handler, "ldlm_canceld",
2184 ldlm_svc_proc_dir, NULL,
2185 ldlm_min_threads, ldlm_max_threads,
2187 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2189 if (!ldlm_state->ldlm_cancel_service) {
2190 CERROR("failed to start service\n");
2191 GOTO(out_proc, rc = -ENOMEM);
2194 OBD_ALLOC(blp, sizeof(*blp));
2196 GOTO(out_proc, rc = -ENOMEM);
2197 ldlm_state->ldlm_bl_pool = blp;
2199 spin_lock_init(&blp->blp_lock);
2200 CFS_INIT_LIST_HEAD(&blp->blp_list);
2201 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2202 cfs_waitq_init(&blp->blp_waitq);
2203 atomic_set(&blp->blp_num_threads, 0);
2204 atomic_set(&blp->blp_busy_threads, 0);
2205 blp->blp_min_threads = ldlm_min_threads;
2206 blp->blp_max_threads = ldlm_max_threads;
2209 for (i = 0; i < blp->blp_min_threads; i++) {
2210 rc = ldlm_bl_thread_start(blp);
2212 GOTO(out_thread, rc);
2215 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2217 GOTO(out_thread, rc);
2219 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2221 GOTO(out_thread, rc);
2223 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2224 expired_lock_thread.elt_state = ELT_STOPPED;
2225 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2227 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2228 spin_lock_init(&waiting_locks_spinlock);
2229 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2231 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2233 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2234 GOTO(out_thread, rc);
2237 wait_event(expired_lock_thread.elt_waitq,
2238 expired_lock_thread.elt_state == ELT_READY);
2242 rc = ldlm_pools_init();
2244 GOTO(out_thread, rc);
2250 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2251 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2256 ldlm_proc_cleanup();
2259 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2264 static int ldlm_cleanup(void)
2267 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2271 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2272 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2273 CERROR("ldlm still has namespaces; clean these up first.\n");
2274 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2275 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2284 while (atomic_read(&blp->blp_num_threads) > 0) {
2285 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2287 init_completion(&blp->blp_comp);
2289 spin_lock(&blp->blp_lock);
2290 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2291 cfs_waitq_signal(&blp->blp_waitq);
2292 spin_unlock(&blp->blp_lock);
2294 wait_for_completion(&blp->blp_comp);
2296 OBD_FREE(blp, sizeof(*blp));
2298 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2299 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2300 ldlm_proc_cleanup();
2302 expired_lock_thread.elt_state = ELT_TERMINATE;
2303 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2304 wait_event(expired_lock_thread.elt_waitq,
2305 expired_lock_thread.elt_state == ELT_STOPPED);
2307 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2308 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2311 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2317 int __init ldlm_init(void)
2319 init_mutex(&ldlm_ref_sem);
2320 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2321 init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2322 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2323 sizeof(struct ldlm_resource), 0,
2324 SLAB_HWCACHE_ALIGN);
2325 if (ldlm_resource_slab == NULL)
2328 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2329 sizeof(struct ldlm_lock), 0,
2330 SLAB_HWCACHE_ALIGN);
2331 if (ldlm_lock_slab == NULL) {
2332 cfs_mem_cache_destroy(ldlm_resource_slab);
2336 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2337 sizeof(struct ldlm_interval),
2338 0, SLAB_HWCACHE_ALIGN);
2339 if (ldlm_interval_slab == NULL) {
2340 cfs_mem_cache_destroy(ldlm_resource_slab);
2341 cfs_mem_cache_destroy(ldlm_lock_slab);
2348 void __exit ldlm_exit(void)
2352 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2353 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2354 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2355 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2356 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2357 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2358 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2362 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2365 EXPORT_SYMBOL(ldlm_get_processing_policy);
2366 EXPORT_SYMBOL(ldlm_lock2desc);
2367 EXPORT_SYMBOL(ldlm_register_intent);
2368 EXPORT_SYMBOL(ldlm_lockname);
2369 EXPORT_SYMBOL(ldlm_typename);
2370 EXPORT_SYMBOL(ldlm_lock2handle);
2371 EXPORT_SYMBOL(__ldlm_handle2lock);
2372 EXPORT_SYMBOL(ldlm_lock_get);
2373 EXPORT_SYMBOL(ldlm_lock_put);
2374 EXPORT_SYMBOL(ldlm_lock_fast_match);
2375 EXPORT_SYMBOL(ldlm_lock_fast_release);
2376 EXPORT_SYMBOL(ldlm_lock_match);
2377 EXPORT_SYMBOL(ldlm_lock_cancel);
2378 EXPORT_SYMBOL(ldlm_lock_addref);
2379 EXPORT_SYMBOL(ldlm_lock_addref_try);
2380 EXPORT_SYMBOL(ldlm_lock_decref);
2381 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2382 EXPORT_SYMBOL(ldlm_lock_change_resource);
2383 EXPORT_SYMBOL(ldlm_lock_set_data);
2384 EXPORT_SYMBOL(ldlm_it2str);
2385 EXPORT_SYMBOL(ldlm_lock_dump);
2386 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2387 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2388 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2389 EXPORT_SYMBOL(ldlm_lock_allow_match);
2391 /* ldlm_request.c */
2392 EXPORT_SYMBOL(ldlm_completion_ast_async);
2393 EXPORT_SYMBOL(ldlm_completion_ast);
2394 EXPORT_SYMBOL(ldlm_blocking_ast);
2395 EXPORT_SYMBOL(ldlm_glimpse_ast);
2396 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2397 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2398 EXPORT_SYMBOL(ldlm_prep_elc_req);
2399 EXPORT_SYMBOL(ldlm_cli_convert);
2400 EXPORT_SYMBOL(ldlm_cli_enqueue);
2401 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2402 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2403 EXPORT_SYMBOL(ldlm_cli_cancel);
2404 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2405 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2406 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2407 EXPORT_SYMBOL(ldlm_replay_locks);
2408 EXPORT_SYMBOL(ldlm_resource_foreach);
2409 EXPORT_SYMBOL(ldlm_namespace_foreach);
2410 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2411 EXPORT_SYMBOL(ldlm_resource_iterate);
2412 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2413 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2416 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2417 EXPORT_SYMBOL(ldlm_server_completion_ast);
2418 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2419 EXPORT_SYMBOL(ldlm_handle_enqueue);
2420 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2421 EXPORT_SYMBOL(ldlm_handle_cancel);
2422 EXPORT_SYMBOL(ldlm_request_cancel);
2423 EXPORT_SYMBOL(ldlm_handle_convert);
2424 EXPORT_SYMBOL(ldlm_handle_convert0);
2425 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2426 EXPORT_SYMBOL(ldlm_get_ref);
2427 EXPORT_SYMBOL(ldlm_put_ref);
2428 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2429 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2431 /* ldlm_resource.c */
2432 EXPORT_SYMBOL(ldlm_namespace_new);
2433 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2434 EXPORT_SYMBOL(ldlm_namespace_free);
2435 EXPORT_SYMBOL(ldlm_namespace_dump);
2436 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2437 EXPORT_SYMBOL(ldlm_resource_get);
2438 EXPORT_SYMBOL(ldlm_resource_putref);
2439 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2442 EXPORT_SYMBOL(client_import_add_conn);
2443 EXPORT_SYMBOL(client_import_del_conn);
2444 EXPORT_SYMBOL(client_obd_setup);
2445 EXPORT_SYMBOL(client_obd_cleanup);
2446 EXPORT_SYMBOL(client_connect_import);
2447 EXPORT_SYMBOL(client_disconnect_export);
2448 EXPORT_SYMBOL(target_start_recovery_thread);
2449 EXPORT_SYMBOL(target_stop_recovery_thread);
2450 EXPORT_SYMBOL(target_handle_connect);
2451 EXPORT_SYMBOL(target_cleanup_recovery);
2452 EXPORT_SYMBOL(target_destroy_export);
2453 EXPORT_SYMBOL(target_cancel_recovery_timer);
2454 EXPORT_SYMBOL(target_send_reply);
2455 EXPORT_SYMBOL(target_queue_recovery_request);
2456 EXPORT_SYMBOL(target_handle_ping);
2457 EXPORT_SYMBOL(target_pack_pool_reply);
2458 EXPORT_SYMBOL(target_handle_disconnect);
2461 EXPORT_SYMBOL(lock_res_and_lock);
2462 EXPORT_SYMBOL(unlock_res_and_lock);