1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002-2004 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
28 # define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_LDLM
33 # include <libcfs/libcfs.h>
35 # include <liblustre.h>
38 #include <lustre_dlm.h>
39 #include <obd_class.h>
40 #include <libcfs/list.h>
41 #include "ldlm_internal.h"
43 extern cfs_mem_cache_t *ldlm_resource_slab;
44 extern cfs_mem_cache_t *ldlm_lock_slab;
45 extern struct lustre_lock ldlm_handle_lock;
46 extern struct list_head ldlm_namespace_list;
48 extern struct semaphore ldlm_namespace_lock;
49 static struct semaphore ldlm_ref_sem;
50 static int ldlm_refcount;
54 static struct ldlm_state *ldlm_state;
56 inline cfs_time_t round_timeout(cfs_time_t timeout)
58 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
61 /* timeout for initial callback (AST) reply */
62 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
63 unsigned int obd_timeout)
65 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
67 return timeout < 1 ? 1 : timeout;
71 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
72 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
73 static struct list_head waiting_locks_list;
74 static cfs_timer_t waiting_locks_timer;
76 static struct expired_lock_thread {
77 cfs_waitq_t elt_waitq;
80 struct list_head elt_expired_locks;
81 } expired_lock_thread;
86 #define ELT_TERMINATE 2
90 struct list_head blp_list;
91 cfs_waitq_t blp_waitq;
92 atomic_t blp_num_threads;
93 struct completion blp_comp;
96 struct ldlm_bl_work_item {
97 struct list_head blwi_entry;
98 struct ldlm_namespace *blwi_ns;
99 struct ldlm_lock_desc blwi_ld;
100 struct ldlm_lock *blwi_lock;
105 static inline int have_expired_locks(void)
110 spin_lock_bh(&waiting_locks_spinlock);
111 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
112 spin_unlock_bh(&waiting_locks_spinlock);
117 static int expired_lock_main(void *arg)
119 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
120 struct l_wait_info lwi = { 0 };
124 cfs_daemonize("ldlm_elt");
126 expired_lock_thread.elt_state = ELT_READY;
127 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
130 l_wait_event(expired_lock_thread.elt_waitq,
131 have_expired_locks() ||
132 expired_lock_thread.elt_state == ELT_TERMINATE,
135 spin_lock_bh(&waiting_locks_spinlock);
136 if (expired_lock_thread.elt_dump) {
137 spin_unlock_bh(&waiting_locks_spinlock);
139 /* from waiting_locks_callback, but not in timer */
140 libcfs_debug_dumplog();
141 libcfs_run_lbug_upcall(__FILE__,
142 "waiting_locks_callback",
143 expired_lock_thread.elt_dump);
145 spin_lock_bh(&waiting_locks_spinlock);
146 expired_lock_thread.elt_dump = 0;
151 while (!list_empty(expired)) {
152 struct obd_export *export;
153 struct ldlm_lock *lock;
155 lock = list_entry(expired->next, struct ldlm_lock,
157 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
158 (void *)lock >= LP_POISON) {
159 spin_unlock_bh(&waiting_locks_spinlock);
160 CERROR("free lock on elt list %p\n", lock);
163 list_del_init(&lock->l_pending_chain);
164 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
165 (void *)lock->l_export >= LP_POISON) {
166 CERROR("lock with free export on elt list %p\n",
168 lock->l_export = NULL;
169 LDLM_ERROR(lock, "free export");
172 export = class_export_get(lock->l_export);
173 spin_unlock_bh(&waiting_locks_spinlock);
176 class_fail_export(export);
177 class_export_put(export);
178 spin_lock_bh(&waiting_locks_spinlock);
180 spin_unlock_bh(&waiting_locks_spinlock);
182 if (do_dump && obd_dump_on_eviction) {
183 CERROR("dump the log upon eviction\n");
184 libcfs_debug_dumplog();
187 if (expired_lock_thread.elt_state == ELT_TERMINATE)
191 expired_lock_thread.elt_state = ELT_STOPPED;
192 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
196 /* This is called from within a timer interrupt and cannot schedule */
197 static void waiting_locks_callback(unsigned long unused)
199 struct ldlm_lock *lock, *last = NULL;
201 spin_lock_bh(&waiting_locks_spinlock);
202 while (!list_empty(&waiting_locks_list)) {
203 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
206 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
207 (lock->l_req_mode == LCK_GROUP))
210 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
211 "%s@%s nid %s ",lock->l_export->exp_client_uuid.uuid,
212 lock->l_export->exp_connection->c_remote_uuid.uuid,
213 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
216 LDLM_ERROR(lock, "waiting on lock multiple times");
217 CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
219 waiting_locks_list.next, waiting_locks_list.prev,
220 &lock->l_pending_chain,
221 lock->l_pending_chain.next,
222 lock->l_pending_chain.prev);
224 CFS_INIT_LIST_HEAD(&waiting_locks_list); /* HACK */
225 expired_lock_thread.elt_dump = __LINE__;
228 CEMERG("would be an LBUG, but isn't (bug 5653)\n");
229 libcfs_debug_dumpstack(NULL);
230 /*blocks* libcfs_debug_dumplog(); */
231 /*blocks* libcfs_run_lbug_upcall(file, func, line); */
236 list_del(&lock->l_pending_chain);
237 list_add(&lock->l_pending_chain,
238 &expired_lock_thread.elt_expired_locks);
241 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
242 if (obd_dump_on_timeout)
243 expired_lock_thread.elt_dump = __LINE__;
245 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
249 * Make sure the timer will fire again if we have any locks
252 if (!list_empty(&waiting_locks_list)) {
253 cfs_time_t timeout_rounded;
254 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
256 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
257 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
259 spin_unlock_bh(&waiting_locks_spinlock);
263 * Indicate that we're waiting for a client to call us back cancelling a given
264 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
265 * timer to fire appropriately. (We round up to the next second, to avoid
266 * floods of timer firings during periods of high lock contention and traffic).
268 * Called with the namespace lock held.
270 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
272 cfs_time_t timeout_rounded;
274 if (!list_empty(&lock->l_pending_chain))
277 lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
278 cfs_time_seconds(obd_timeout)/2);
280 timeout_rounded = round_timeout(lock->l_callback_timeout);
282 if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
283 !cfs_timer_is_armed(&waiting_locks_timer)) {
284 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
287 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
291 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
295 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
297 spin_lock_bh(&waiting_locks_spinlock);
298 if (lock->l_destroyed) {
299 static cfs_time_t next;
300 spin_unlock_bh(&waiting_locks_spinlock);
301 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
302 if (cfs_time_after(cfs_time_current(), next)) {
303 next = cfs_time_shift(14400);
304 libcfs_debug_dumpstack(NULL);
309 ret = __ldlm_add_waiting_lock(lock);
310 spin_unlock_bh(&waiting_locks_spinlock);
312 LDLM_DEBUG(lock, "%sadding to wait list",
313 ret == 0 ? "not re-" : "");
318 * Remove a lock from the pending list, likely because it had its cancellation
319 * callback arrive without incident. This adjusts the lock-timeout timer if
320 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
322 * Called with namespace lock held.
324 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
326 struct list_head *list_next;
328 if (list_empty(&lock->l_pending_chain))
331 list_next = lock->l_pending_chain.next;
332 if (lock->l_pending_chain.prev == &waiting_locks_list) {
333 /* Removing the head of the list, adjust timer. */
334 if (list_next == &waiting_locks_list) {
335 /* No more, just cancel. */
336 cfs_timer_disarm(&waiting_locks_timer);
338 struct ldlm_lock *next;
339 next = list_entry(list_next, struct ldlm_lock,
341 cfs_timer_arm(&waiting_locks_timer,
342 round_timeout(next->l_callback_timeout));
345 list_del_init(&lock->l_pending_chain);
350 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
354 if (lock->l_export == NULL) {
355 /* We don't have a "waiting locks list" on clients. */
356 LDLM_DEBUG(lock, "client lock: no-op");
360 spin_lock_bh(&waiting_locks_spinlock);
361 ret = __ldlm_del_waiting_lock(lock);
362 spin_unlock_bh(&waiting_locks_spinlock);
364 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
371 * Called with namespace lock held.
373 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
375 if (lock->l_export == NULL) {
376 /* We don't have a "waiting locks list" on clients. */
377 LDLM_DEBUG(lock, "client lock: no-op");
381 spin_lock_bh(&waiting_locks_spinlock);
383 if (list_empty(&lock->l_pending_chain)) {
384 spin_unlock_bh(&waiting_locks_spinlock);
385 LDLM_DEBUG(lock, "wasn't waiting");
389 __ldlm_del_waiting_lock(lock);
390 __ldlm_add_waiting_lock(lock);
391 spin_unlock_bh(&waiting_locks_spinlock);
393 LDLM_DEBUG(lock, "refreshed");
397 #else /* !__KERNEL__ */
399 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
401 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
405 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
410 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
414 #endif /* __KERNEL__ */
416 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
417 const char *ast_type)
419 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
420 char *str = libcfs_nid2str(conn->c_peer.nid);
422 LCONSOLE_ERROR("A client on nid %s was evicted from service %s.\n",
423 str, lock->l_export->exp_obd->obd_name);
425 LDLM_ERROR(lock, "%s AST failed (%d): evicting client %s@%s NID %s"
426 " (%s)", ast_type, rc, lock->l_export->exp_client_uuid.uuid,
427 conn->c_remote_uuid.uuid, libcfs_nid2str(conn->c_peer.nid),
430 if (obd_dump_on_timeout)
431 libcfs_debug_dumplog();
432 class_fail_export(lock->l_export);
435 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
436 struct ptlrpc_request *req, int rc,
437 const char *ast_type)
439 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
441 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
442 LASSERT(lock->l_export);
443 if (lock->l_export->exp_libclient) {
444 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
445 " timeout, just cancelling lock", ast_type,
446 libcfs_nid2str(peer.nid));
447 ldlm_lock_cancel(lock);
449 } else if (lock->l_flags & LDLM_FL_CANCEL) {
450 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
451 "cancel was received (AST reply lost?)",
452 ast_type, libcfs_nid2str(peer.nid));
453 ldlm_lock_cancel(lock);
456 ldlm_del_waiting_lock(lock);
457 ldlm_failed_ast(lock, rc, ast_type);
461 LDLM_DEBUG(lock, "client (nid %s) returned %d"
462 " from %s AST - normal race",
463 libcfs_nid2str(peer.nid),
464 lustre_msg_get_status(req->rq_repmsg),
467 LDLM_ERROR(lock, "client (nid %s) returned %d "
468 "from %s AST", libcfs_nid2str(peer.nid),
469 (req->rq_repmsg != NULL) ?
470 lustre_msg_get_status(req->rq_repmsg) : 0,
472 ldlm_lock_cancel(lock);
473 /* Server-side AST functions are called from ldlm_reprocess_all,
474 * which needs to be told to please restart its reprocessing. */
481 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
482 struct ldlm_lock_desc *desc,
483 void *data, int flag)
485 struct ldlm_request *body;
486 struct ptlrpc_request *req;
487 int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
488 [DLM_LOCKREQ_OFF] = sizeof(*body) };
489 int instant_cancel = 0, rc = 0;
492 if (flag == LDLM_CB_CANCELING) {
493 /* Don't need to do anything here. */
499 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
500 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
505 lock_res(lock->l_resource);
506 if (lock->l_granted_mode != lock->l_req_mode) {
507 /* this blocking AST will be communicated as part of the
508 * completion AST instead */
509 unlock_res(lock->l_resource);
510 ptlrpc_req_finished(req);
511 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
515 if (lock->l_destroyed) {
516 /* What's the point? */
517 unlock_res(lock->l_resource);
518 ptlrpc_req_finished(req);
523 if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
524 unlock_res(lock->l_resource);
525 ptlrpc_req_finished(req);
526 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
531 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
534 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
535 body->lock_handle1 = lock->l_remote_handle;
536 body->lock_desc = *desc;
537 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
539 LDLM_DEBUG(lock, "server preparing blocking AST");
541 ptlrpc_req_set_repsize(req, 1, NULL);
542 if (instant_cancel) {
543 unlock_res(lock->l_resource);
544 ldlm_lock_cancel(lock);
545 } else if (lock->l_granted_mode == lock->l_req_mode) {
546 ldlm_add_waiting_lock(lock);
547 unlock_res(lock->l_resource);
550 req->rq_send_state = LUSTRE_IMP_FULL;
551 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
553 if (lock->l_export && lock->l_export->exp_ldlm_stats)
554 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
555 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
557 if (unlikely(instant_cancel)) {
558 rc = ptl_send_rpc(req, 1);
560 rc = ptlrpc_queue_wait(req);
561 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
564 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
566 ptlrpc_req_finished(req);
568 /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
569 if (!rc && instant_cancel)
575 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
577 struct ldlm_request *body;
578 struct ptlrpc_request *req;
579 struct timeval granted_time;
580 long total_enqueue_wait;
581 int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
582 [DLM_LOCKREQ_OFF] = sizeof(*body) };
583 int rc = 0, buffers = 2, instant_cancel = 0;
586 LASSERT(lock != NULL);
588 do_gettimeofday(&granted_time);
589 total_enqueue_wait = cfs_timeval_sub(&granted_time,
590 &lock->l_enqueued_time, NULL);
592 if (total_enqueue_wait / 1000000 > obd_timeout)
593 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
594 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
596 lock_res_and_lock(lock);
597 if (lock->l_resource->lr_lvb_len) {
598 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
601 unlock_res_and_lock(lock);
603 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
604 LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
609 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
610 body->lock_handle1 = lock->l_remote_handle;
611 body->lock_flags = flags;
612 ldlm_lock2desc(lock, &body->lock_desc);
617 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
618 lock->l_resource->lr_lvb_len);
619 lock_res_and_lock(lock);
620 memcpy(lvb, lock->l_resource->lr_lvb_data,
621 lock->l_resource->lr_lvb_len);
622 unlock_res_and_lock(lock);
625 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
628 ptlrpc_req_set_repsize(req, 1, NULL);
630 req->rq_send_state = LUSTRE_IMP_FULL;
631 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
633 /* We only send real blocking ASTs after the lock is granted */
634 lock_res_and_lock(lock);
635 if (lock->l_flags & LDLM_FL_AST_SENT) {
636 body->lock_flags |= LDLM_FL_AST_SENT;
638 /* We might get here prior to ldlm_handle_enqueue setting
639 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
640 * into waiting list, but this is safe and similar code in
641 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
642 * that would not only cancel the lock, but will also remove
643 * it from waiting list */
644 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
645 unlock_res_and_lock(lock);
646 ldlm_lock_cancel(lock);
648 lock_res_and_lock(lock);
650 ldlm_add_waiting_lock(lock); /* start the lock-timeout
654 unlock_res_and_lock(lock);
656 if (lock->l_export && lock->l_export->exp_ldlm_stats)
657 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
658 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
660 rc = ptlrpc_queue_wait(req);
662 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
664 ptlrpc_req_finished(req);
666 /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
667 if (!rc && instant_cancel)
673 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
675 struct ldlm_resource *res = lock->l_resource;
676 struct ldlm_request *body;
677 struct ptlrpc_request *req;
678 int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
679 [DLM_LOCKREQ_OFF] = sizeof(*body) };
683 LASSERT(lock != NULL);
685 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
686 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
691 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
692 body->lock_handle1 = lock->l_remote_handle;
693 ldlm_lock2desc(lock, &body->lock_desc);
695 lock_res_and_lock(lock);
696 size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
697 unlock_res_and_lock(lock);
698 res = lock->l_resource;
699 ptlrpc_req_set_repsize(req, 2, size);
701 req->rq_send_state = LUSTRE_IMP_FULL;
702 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
704 if (lock->l_export && lock->l_export->exp_ldlm_stats)
705 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
706 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
708 rc = ptlrpc_queue_wait(req);
709 if (rc == -ELDLM_NO_LOCK_DATA)
710 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
712 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
714 rc = res->lr_namespace->ns_lvbo->lvbo_update
715 (res, req->rq_repmsg, REPLY_REC_OFF, 1);
716 ptlrpc_req_finished(req);
720 static struct ldlm_lock *
721 find_existing_lock(struct obd_export *exp, struct lustre_handle *remote_hdl)
723 struct list_head *iter;
725 spin_lock(&exp->exp_ldlm_data.led_lock);
726 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
727 struct ldlm_lock *lock;
728 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
729 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
731 spin_unlock(&exp->exp_ldlm_data.led_lock);
735 spin_unlock(&exp->exp_ldlm_data.led_lock);
741 * Main server-side entry point into LDLM. This is called by ptlrpc service
742 * threads to carry out client lock enqueueing requests.
744 int ldlm_handle_enqueue(struct ptlrpc_request *req,
745 ldlm_completion_callback completion_callback,
746 ldlm_blocking_callback blocking_callback,
747 ldlm_glimpse_callback glimpse_callback)
749 struct obd_device *obddev = req->rq_export->exp_obd;
750 struct ldlm_reply *dlm_rep;
751 struct ldlm_request *dlm_req;
752 int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
753 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
756 ldlm_error_t err = ELDLM_OK;
757 struct ldlm_lock *lock = NULL;
761 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
763 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
764 lustre_swab_ldlm_request);
765 if (dlm_req == NULL) {
766 CERROR ("Can't unpack dlm_req\n");
767 GOTO(out, rc = -EFAULT);
770 flags = dlm_req->lock_flags;
772 LASSERT(req->rq_export);
774 if (req->rq_export->exp_ldlm_stats)
775 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
776 LDLM_ENQUEUE - LDLM_FIRST_OPC);
778 if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
779 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
780 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d\n",
781 dlm_req->lock_desc.l_resource.lr_type);
782 GOTO(out, rc = -EFAULT);
785 if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
786 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
787 dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
788 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d\n",
789 dlm_req->lock_desc.l_req_mode);
790 GOTO(out, rc = -EFAULT);
793 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
794 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
795 DEBUG_REQ(D_ERROR, req,
796 "PLAIN lock request from IBITS client?\n");
797 GOTO(out, rc = -EPROTO);
799 } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
800 DEBUG_REQ(D_ERROR, req,
801 "IBITS lock request from unaware client?\n");
802 GOTO(out, rc = -EPROTO);
806 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
807 against server's _CONNECT_SUPPORTED flags? (I don't want to use
808 ibits for mgc/mgs) */
810 /* INODEBITS_INTEROP: Perform conversion from plain lock to
811 * inodebits lock if client does not support them. */
812 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
813 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
814 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
815 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
816 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
817 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
818 dlm_req->lock_desc.l_req_mode = LCK_CR;
822 if (flags & LDLM_FL_REPLAY) {
823 lock = find_existing_lock(req->rq_export,
824 &dlm_req->lock_handle1);
826 DEBUG_REQ(D_HA, req, "found existing lock cookie "LPX64,
827 lock->l_handle.h_cookie);
828 GOTO(existing_lock, rc = 0);
832 /* The lock's callback data might be set in the policy function */
833 lock = ldlm_lock_create(obddev->obd_namespace, &dlm_req->lock_handle2,
834 dlm_req->lock_desc.l_resource.lr_name,
835 dlm_req->lock_desc.l_resource.lr_type,
836 dlm_req->lock_desc.l_req_mode,
837 blocking_callback, completion_callback,
838 glimpse_callback, NULL, 0);
840 GOTO(out, rc = -ENOMEM);
842 do_gettimeofday(&lock->l_enqueued_time);
843 lock->l_remote_handle = dlm_req->lock_handle1;
844 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
846 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
847 /* Don't enqueue a lock onto the export if it has already
848 * been evicted. Cancel it now instead. (bug 3822) */
849 if (req->rq_export->exp_failed) {
850 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
851 GOTO(out, rc = -ENOTCONN);
853 lock->l_export = class_export_get(req->rq_export);
854 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
855 list_add(&lock->l_export_chain,
856 &lock->l_export->exp_ldlm_data.led_held_locks);
857 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
861 if (flags & LDLM_FL_HAS_INTENT) {
862 /* In this case, the reply buffer is allocated deep in
863 * local_lock_enqueue by the policy function. */
868 lock_res_and_lock(lock);
869 if (lock->l_resource->lr_lvb_len) {
870 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
873 unlock_res_and_lock(lock);
875 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
876 GOTO(out, rc = -ENOMEM);
878 rc = lustre_pack_reply(req, buffers, size, NULL);
883 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
884 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
885 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
886 lock->l_req_extent = lock->l_policy_data.l_extent;
888 err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, &flags);
892 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
894 dlm_rep->lock_flags = flags;
896 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
897 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
899 /* We never send a blocking AST until the lock is granted, but
900 * we can tell it right now */
901 lock_res_and_lock(lock);
903 /* Now take into account flags to be inherited from original lock
904 request both in reply to client and in our own lock flags. */
905 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
906 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
908 /* Don't move a pending lock onto the export if it has already
909 * been evicted. Cancel it now instead. (bug 5683) */
910 if (req->rq_export->exp_failed ||
911 OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
912 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
914 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
915 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
916 if (lock->l_granted_mode == lock->l_req_mode) {
917 /* Only cancel lock if it was granted, because it
918 * would be destroyed immediatelly and would never
919 * be granted in the future, causing timeouts on client.
920 * Not granted lock will be cancelled immediatelly after
921 * sending completion AST.
923 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
924 unlock_res_and_lock(lock);
925 ldlm_lock_cancel(lock);
926 lock_res_and_lock(lock);
927 } else if (lock->l_granted_mode == lock->l_req_mode)
928 ldlm_add_waiting_lock(lock);
931 /* Make sure we never ever grant usual metadata locks to liblustre
933 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
934 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
935 req->rq_export->exp_libclient) {
936 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
937 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
938 CERROR("Granting sync lock to libclient. "
939 "req fl %d, rep fl %d, lock fl %d\n",
940 dlm_req->lock_flags, dlm_rep->lock_flags,
942 LDLM_ERROR(lock, "sync lock");
943 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
944 struct ldlm_intent *it;
945 it = lustre_msg_buf(req->rq_reqmsg,
949 CERROR("This is intent %s ("LPU64")\n",
950 ldlm_it2str(it->opc), it->opc);
956 unlock_res_and_lock(lock);
960 req->rq_status = err;
961 if (req->rq_reply_state == NULL) {
962 err = lustre_pack_reply(req, 1, NULL, NULL);
968 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
969 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
971 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
972 "(err=%d, rc=%d)", err, rc);
975 lock_res_and_lock(lock);
976 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
977 if (size[DLM_REPLY_REC_OFF] > 0) {
978 void *lvb = lustre_msg_buf(req->rq_repmsg,
980 size[DLM_REPLY_REC_OFF]);
981 LASSERTF(lvb != NULL, "req %p, lock %p\n",
984 memcpy(lvb, lock->l_resource->lr_lvb_data,
985 size[DLM_REPLY_REC_OFF]);
987 unlock_res_and_lock(lock);
989 lock_res_and_lock(lock);
990 ldlm_resource_unlink_lock(lock);
991 ldlm_lock_destroy_nolock(lock);
992 unlock_res_and_lock(lock);
995 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
996 ldlm_reprocess_all(lock->l_resource);
1001 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1007 int ldlm_handle_convert(struct ptlrpc_request *req)
1009 struct ldlm_request *dlm_req;
1010 struct ldlm_reply *dlm_rep;
1011 struct ldlm_lock *lock;
1013 int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1014 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
1017 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1018 lustre_swab_ldlm_request);
1019 if (dlm_req == NULL) {
1020 CERROR ("Can't unpack dlm_req\n");
1024 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1025 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1026 LDLM_CONVERT - LDLM_FIRST_OPC);
1028 rc = lustre_pack_reply(req, 2, size, NULL);
1030 CERROR("out of memory\n");
1033 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1035 dlm_rep->lock_flags = dlm_req->lock_flags;
1037 lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1039 req->rq_status = EINVAL;
1043 LDLM_DEBUG(lock, "server-side convert handler START");
1045 do_gettimeofday(&lock->l_enqueued_time);
1046 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1047 &dlm_rep->lock_flags);
1049 if (ldlm_del_waiting_lock(lock))
1050 LDLM_DEBUG(lock, "converted waiting lock");
1053 req->rq_status = EDEADLOCK;
1058 if (!req->rq_status)
1059 ldlm_reprocess_all(lock->l_resource);
1060 LDLM_DEBUG(lock, "server-side convert handler END");
1061 LDLM_LOCK_PUT(lock);
1063 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1068 int ldlm_handle_cancel(struct ptlrpc_request *req)
1070 struct ldlm_request *dlm_req;
1071 struct ldlm_lock *lock;
1072 struct ldlm_resource *res;
1076 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1077 lustre_swab_ldlm_request);
1078 if (dlm_req == NULL) {
1079 CERROR("bad request buffer for cancel\n");
1083 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1084 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1085 LDLM_CANCEL - LDLM_FIRST_OPC);
1087 rc = lustre_pack_reply(req, 1, NULL, NULL);
1089 CERROR("out of memory\n");
1093 lock = ldlm_handle2lock(&dlm_req->lock_handle1);
1095 CERROR("received cancel for unknown lock cookie "LPX64
1096 " from client %s id %s\n",
1097 dlm_req->lock_handle1.cookie,
1098 req->rq_export->exp_client_uuid.uuid,
1099 libcfs_id2str(req->rq_peer));
1100 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
1102 dlm_req->lock_handle1.cookie);
1103 req->rq_status = ESTALE;
1105 LDLM_DEBUG(lock, "server-side cancel handler START");
1106 res = lock->l_resource;
1107 if (res && res->lr_namespace->ns_lvbo &&
1108 res->lr_namespace->ns_lvbo->lvbo_update) {
1109 (void)res->lr_namespace->ns_lvbo->lvbo_update
1111 //(res, req->rq_reqmsg, 1, 1);
1114 ldlm_lock_cancel(lock);
1115 req->rq_status = rc;
1118 if (ptlrpc_reply(req) != 0)
1122 ldlm_reprocess_all(lock->l_resource);
1123 LDLM_DEBUG(lock, "server-side cancel handler END");
1124 LDLM_LOCK_PUT(lock);
1130 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1131 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1136 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1138 lock_res_and_lock(lock);
1139 lock->l_flags |= LDLM_FL_CBPENDING;
1141 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1142 lock->l_flags |= LDLM_FL_CANCEL;
1144 do_ast = (!lock->l_readers && !lock->l_writers);
1145 unlock_res_and_lock(lock);
1148 LDLM_DEBUG(lock, "already unused, calling "
1149 "callback (%p)", lock->l_blocking_ast);
1150 if (lock->l_blocking_ast != NULL)
1151 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1154 LDLM_DEBUG(lock, "Lock still has references, will be"
1155 " cancelled later");
1158 LDLM_DEBUG(lock, "client blocking callback handler END");
1159 LDLM_LOCK_PUT(lock);
1163 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1164 struct ldlm_namespace *ns,
1165 struct ldlm_request *dlm_req,
1166 struct ldlm_lock *lock)
1168 CFS_LIST_HEAD(ast_list);
1171 LDLM_DEBUG(lock, "client completion callback handler START");
1173 lock_res_and_lock(lock);
1175 /* If we receive the completion AST before the actual enqueue returned,
1176 * then we might need to switch lock modes, resources, or extents. */
1177 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1178 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1179 LDLM_DEBUG(lock, "completion AST, new lock mode");
1182 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1183 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1184 LDLM_DEBUG(lock, "completion AST, new policy data");
1187 ldlm_resource_unlink_lock(lock);
1188 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1189 &lock->l_resource->lr_name,
1190 sizeof(lock->l_resource->lr_name)) != 0) {
1191 unlock_res_and_lock(lock);
1192 ldlm_lock_change_resource(ns, lock,
1193 dlm_req->lock_desc.l_resource.lr_name);
1194 LDLM_DEBUG(lock, "completion AST, new resource");
1195 CERROR("change resource!\n");
1196 lock_res_and_lock(lock);
1199 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1200 lock->l_flags |= LDLM_FL_CBPENDING;
1201 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1204 if (lock->l_lvb_len) {
1206 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1207 lock->l_lvb_swabber);
1209 LDLM_ERROR(lock, "completion AST did not contain "
1212 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1216 ldlm_grant_lock(lock, &ast_list);
1217 unlock_res_and_lock(lock);
1219 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1221 ldlm_run_cp_ast_work(&ast_list);
1223 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1225 LDLM_LOCK_PUT(lock);
1229 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1230 struct ldlm_namespace *ns,
1231 struct ldlm_request *dlm_req,
1232 struct ldlm_lock *lock)
1237 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1239 if (lock->l_glimpse_ast != NULL)
1240 rc = lock->l_glimpse_ast(lock, req);
1242 if (req->rq_repmsg != NULL) {
1245 req->rq_status = rc;
1249 lock_res_and_lock(lock);
1250 if (lock->l_granted_mode == LCK_PW &&
1251 !lock->l_readers && !lock->l_writers &&
1252 cfs_time_after(cfs_time_current(),
1253 cfs_time_add(lock->l_last_used, cfs_time_seconds(10)))) {
1254 unlock_res_and_lock(lock);
1255 if (ldlm_bl_to_thread(ns, NULL, lock))
1256 ldlm_handle_bl_callback(ns, NULL, lock);
1261 unlock_res_and_lock(lock);
1262 LDLM_LOCK_PUT(lock);
1266 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1268 req->rq_status = rc;
1269 if (req->rq_reply_state == NULL) {
1270 rc = lustre_pack_reply(req, 1, NULL, NULL);
1274 return ptlrpc_reply(req);
1277 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1278 struct ldlm_lock *lock)
1281 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1282 struct ldlm_bl_work_item *blwi;
1285 OBD_ALLOC(blwi, sizeof(*blwi));
1291 blwi->blwi_ld = *ld;
1292 blwi->blwi_lock = lock;
1294 spin_lock(&blp->blp_lock);
1295 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1296 cfs_waitq_signal(&blp->blp_waitq);
1297 spin_unlock(&blp->blp_lock);
1305 static int ldlm_callback_handler(struct ptlrpc_request *req)
1307 struct ldlm_namespace *ns;
1308 struct ldlm_request *dlm_req;
1309 struct ldlm_lock *lock;
1313 /* Requests arrive in sender's byte order. The ptlrpc service
1314 * handler has already checked and, if necessary, byte-swapped the
1315 * incoming request message body, but I am responsible for the
1316 * message buffers. */
1318 if (req->rq_export == NULL) {
1319 struct ldlm_request *dlm_req;
1321 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1322 "export cookie "LPX64"; this is "
1323 "normal if this node rebooted with a lock held\n",
1324 lustre_msg_get_opc(req->rq_reqmsg),
1325 libcfs_id2str(req->rq_peer),
1326 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1328 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1330 lustre_swab_ldlm_request);
1331 if (dlm_req != NULL)
1332 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1333 dlm_req->lock_handle1.cookie);
1335 ldlm_callback_reply(req, -ENOTCONN);
1339 LASSERT(req->rq_export != NULL);
1340 LASSERT(req->rq_export->exp_obd != NULL);
1342 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1343 case LDLM_BL_CALLBACK:
1344 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1346 case LDLM_CP_CALLBACK:
1347 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1349 case LDLM_GL_CALLBACK:
1350 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1352 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1353 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1354 rc = llog_origin_handle_cancel(req);
1355 ldlm_callback_reply(req, rc);
1357 case OBD_QC_CALLBACK:
1358 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1359 rc = target_handle_qc_callback(req);
1360 ldlm_callback_reply(req, rc);
1364 /* reply in handler */
1365 rc = target_handle_dqacq_callback(req);
1367 case LLOG_ORIGIN_HANDLE_CREATE:
1368 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1369 rc = llog_origin_handle_create(req);
1370 ldlm_callback_reply(req, rc);
1372 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1373 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1374 rc = llog_origin_handle_next_block(req);
1375 ldlm_callback_reply(req, rc);
1377 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1378 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1379 rc = llog_origin_handle_read_header(req);
1380 ldlm_callback_reply(req, rc);
1382 case LLOG_ORIGIN_HANDLE_CLOSE:
1383 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1384 rc = llog_origin_handle_close(req);
1385 ldlm_callback_reply(req, rc);
1388 CERROR("unknown opcode %u\n",
1389 lustre_msg_get_opc(req->rq_reqmsg));
1390 ldlm_callback_reply(req, -EPROTO);
1394 ns = req->rq_export->exp_obd->obd_namespace;
1395 LASSERT(ns != NULL);
1397 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1398 lustre_swab_ldlm_request);
1399 if (dlm_req == NULL) {
1400 CERROR ("can't unpack dlm_req\n");
1401 ldlm_callback_reply(req, -EPROTO);
1405 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle1);
1407 CDEBUG(D_INODE, "callback on lock "LPX64" - lock disappeared\n",
1408 dlm_req->lock_handle1.cookie);
1409 ldlm_callback_reply(req, -EINVAL);
1413 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1414 lock_res_and_lock(lock);
1415 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1416 unlock_res_and_lock(lock);
1418 /* We want the ost thread to get this reply so that it can respond
1419 * to ost requests (write cache writeback) that might be triggered
1422 * But we'd also like to be able to indicate in the reply that we're
1423 * cancelling right now, because it's unused, or have an intent result
1424 * in the reply, so we might have to push the responsibility for sending
1425 * the reply down into the AST handlers, alas. */
1427 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1428 case LDLM_BL_CALLBACK:
1429 CDEBUG(D_INODE, "blocking ast\n");
1430 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1431 ldlm_callback_reply(req, 0);
1432 if (ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock))
1433 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1435 case LDLM_CP_CALLBACK:
1436 CDEBUG(D_INODE, "completion ast\n");
1437 ldlm_callback_reply(req, 0);
1438 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1440 case LDLM_GL_CALLBACK:
1441 CDEBUG(D_INODE, "glimpse ast\n");
1442 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1445 LBUG(); /* checked above */
1451 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1456 /* Requests arrive in sender's byte order. The ptlrpc service
1457 * handler has already checked and, if necessary, byte-swapped the
1458 * incoming request message body, but I am responsible for the
1459 * message buffers. */
1461 if (req->rq_export == NULL) {
1462 struct ldlm_request *dlm_req;
1464 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1465 lustre_msg_get_opc(req->rq_reqmsg),
1466 libcfs_id2str(req->rq_peer),
1467 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1469 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1471 lustre_swab_ldlm_request);
1472 if (dlm_req != NULL)
1473 ldlm_lock_dump_handle(D_ERROR, &dlm_req->lock_handle1);
1475 ldlm_callback_reply(req, -ENOTCONN);
1479 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1481 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1483 CDEBUG(D_INODE, "cancel\n");
1484 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1485 rc = ldlm_handle_cancel(req);
1489 case OBD_LOG_CANCEL:
1490 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1491 rc = llog_origin_handle_cancel(req);
1492 ldlm_callback_reply(req, rc);
1495 CERROR("invalid opcode %d\n",
1496 lustre_msg_get_opc(req->rq_reqmsg));
1497 ldlm_callback_reply(req, -EINVAL);
1504 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1506 struct ldlm_bl_work_item *blwi = NULL;
1508 spin_lock(&blp->blp_lock);
1509 if (!list_empty(&blp->blp_list)) {
1510 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
1512 list_del(&blwi->blwi_entry);
1514 spin_unlock(&blp->blp_lock);
1519 struct ldlm_bl_thread_data {
1521 struct ldlm_bl_pool *bltd_blp;
1524 static int ldlm_bl_thread_main(void *arg)
1526 struct ldlm_bl_thread_data *bltd = arg;
1527 struct ldlm_bl_pool *blp = bltd->bltd_blp;
1531 char name[CFS_CURPROC_COMM_MAX];
1532 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1534 cfs_daemonize(name);
1537 atomic_inc(&blp->blp_num_threads);
1538 complete(&blp->blp_comp);
1541 struct l_wait_info lwi = { 0 };
1542 struct ldlm_bl_work_item *blwi = NULL;
1544 l_wait_event_exclusive(blp->blp_waitq,
1545 (blwi = ldlm_bl_get_work(blp)) != NULL,
1548 if (blwi->blwi_ns == NULL)
1551 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1553 OBD_FREE(blwi, sizeof(*blwi));
1556 atomic_dec(&blp->blp_num_threads);
1557 complete(&blp->blp_comp);
1563 static int ldlm_setup(void);
1564 static int ldlm_cleanup(int force);
1566 int ldlm_get_ref(void)
1570 mutex_down(&ldlm_ref_sem);
1571 if (++ldlm_refcount == 1) {
1576 mutex_up(&ldlm_ref_sem);
1581 void ldlm_put_ref(int force)
1584 mutex_down(&ldlm_ref_sem);
1585 if (ldlm_refcount == 1) {
1586 int rc = ldlm_cleanup(force);
1588 CERROR("ldlm_cleanup failed: %d\n", rc);
1594 mutex_up(&ldlm_ref_sem);
1599 static int ldlm_setup(void)
1601 struct ldlm_bl_pool *blp;
1608 if (ldlm_state != NULL)
1611 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1612 if (ldlm_state == NULL)
1616 rc = ldlm_proc_setup();
1621 ldlm_state->ldlm_cb_service =
1622 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1623 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
1624 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
1625 ldlm_callback_handler, "ldlm_cbd",
1626 ldlm_svc_proc_dir, NULL,
1627 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1630 if (!ldlm_state->ldlm_cb_service) {
1631 CERROR("failed to start service\n");
1632 GOTO(out_proc, rc = -ENOMEM);
1635 ldlm_state->ldlm_cancel_service =
1636 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1637 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
1638 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
1639 ldlm_cancel_handler, "ldlm_canceld",
1640 ldlm_svc_proc_dir, NULL,
1641 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1644 if (!ldlm_state->ldlm_cancel_service) {
1645 CERROR("failed to start service\n");
1646 GOTO(out_proc, rc = -ENOMEM);
1649 OBD_ALLOC(blp, sizeof(*blp));
1651 GOTO(out_proc, rc = -ENOMEM);
1652 ldlm_state->ldlm_bl_pool = blp;
1654 atomic_set(&blp->blp_num_threads, 0);
1655 cfs_waitq_init(&blp->blp_waitq);
1656 spin_lock_init(&blp->blp_lock);
1658 CFS_INIT_LIST_HEAD(&blp->blp_list);
1661 for (i = 0; i < LDLM_BL_THREADS; i++) {
1662 struct ldlm_bl_thread_data bltd = {
1666 init_completion(&blp->blp_comp);
1667 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1669 CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1670 GOTO(out_thread, rc);
1672 wait_for_completion(&blp->blp_comp);
1675 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
1677 GOTO(out_thread, rc);
1679 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
1681 GOTO(out_thread, rc);
1683 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1684 expired_lock_thread.elt_state = ELT_STOPPED;
1685 cfs_waitq_init(&expired_lock_thread.elt_waitq);
1687 CFS_INIT_LIST_HEAD(&waiting_locks_list);
1688 spin_lock_init(&waiting_locks_spinlock);
1689 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
1691 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
1693 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1694 GOTO(out_thread, rc);
1697 wait_event(expired_lock_thread.elt_waitq,
1698 expired_lock_thread.elt_state == ELT_READY);
1705 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1706 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1711 ldlm_proc_cleanup();
1714 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1719 static int ldlm_cleanup(int force)
1722 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1726 if (!list_empty(&ldlm_namespace_list)) {
1727 CERROR("ldlm still has namespaces; clean these up first.\n");
1728 ldlm_dump_all_namespaces(D_DLMTRACE);
1733 while (atomic_read(&blp->blp_num_threads) > 0) {
1734 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1736 init_completion(&blp->blp_comp);
1738 spin_lock(&blp->blp_lock);
1739 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1740 cfs_waitq_signal(&blp->blp_waitq);
1741 spin_unlock(&blp->blp_lock);
1743 wait_for_completion(&blp->blp_comp);
1745 OBD_FREE(blp, sizeof(*blp));
1747 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1748 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1749 ldlm_proc_cleanup();
1751 expired_lock_thread.elt_state = ELT_TERMINATE;
1752 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
1753 wait_event(expired_lock_thread.elt_waitq,
1754 expired_lock_thread.elt_state == ELT_STOPPED);
1756 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1757 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1760 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1766 int __init ldlm_init(void)
1768 init_mutex(&ldlm_ref_sem);
1769 init_mutex(&ldlm_namespace_lock);
1770 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
1771 sizeof(struct ldlm_resource), 0,
1772 SLAB_HWCACHE_ALIGN);
1773 if (ldlm_resource_slab == NULL)
1776 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
1777 sizeof(struct ldlm_lock), 0,
1778 SLAB_HWCACHE_ALIGN);
1779 if (ldlm_lock_slab == NULL) {
1780 cfs_mem_cache_destroy(ldlm_resource_slab);
1787 void __exit ldlm_exit(void)
1791 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1792 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
1793 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
1794 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
1795 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
1799 EXPORT_SYMBOL(ldlm_extent_shift_kms);
1802 EXPORT_SYMBOL(ldlm_get_processing_policy);
1803 EXPORT_SYMBOL(ldlm_lock2desc);
1804 EXPORT_SYMBOL(ldlm_register_intent);
1805 EXPORT_SYMBOL(ldlm_lockname);
1806 EXPORT_SYMBOL(ldlm_typename);
1807 EXPORT_SYMBOL(ldlm_lock2handle);
1808 EXPORT_SYMBOL(__ldlm_handle2lock);
1809 EXPORT_SYMBOL(ldlm_lock_get);
1810 EXPORT_SYMBOL(ldlm_lock_put);
1811 EXPORT_SYMBOL(ldlm_lock_match);
1812 EXPORT_SYMBOL(ldlm_lock_cancel);
1813 EXPORT_SYMBOL(ldlm_lock_addref);
1814 EXPORT_SYMBOL(ldlm_lock_decref);
1815 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1816 EXPORT_SYMBOL(ldlm_lock_change_resource);
1817 EXPORT_SYMBOL(ldlm_lock_set_data);
1818 EXPORT_SYMBOL(ldlm_it2str);
1819 EXPORT_SYMBOL(ldlm_lock_dump);
1820 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1821 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1822 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1823 EXPORT_SYMBOL(ldlm_lock_allow_match);
1825 /* ldlm_request.c */
1826 EXPORT_SYMBOL(ldlm_completion_ast);
1827 EXPORT_SYMBOL(ldlm_blocking_ast);
1828 EXPORT_SYMBOL(ldlm_glimpse_ast);
1829 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1830 EXPORT_SYMBOL(ldlm_cli_convert);
1831 EXPORT_SYMBOL(ldlm_cli_enqueue);
1832 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
1833 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
1834 EXPORT_SYMBOL(ldlm_cli_cancel);
1835 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1836 EXPORT_SYMBOL(ldlm_cli_join_lru);
1837 EXPORT_SYMBOL(ldlm_replay_locks);
1838 EXPORT_SYMBOL(ldlm_resource_foreach);
1839 EXPORT_SYMBOL(ldlm_namespace_foreach);
1840 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1841 EXPORT_SYMBOL(ldlm_resource_iterate);
1844 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1845 EXPORT_SYMBOL(ldlm_server_completion_ast);
1846 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1847 EXPORT_SYMBOL(ldlm_handle_enqueue);
1848 EXPORT_SYMBOL(ldlm_handle_cancel);
1849 EXPORT_SYMBOL(ldlm_handle_convert);
1850 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1851 EXPORT_SYMBOL(ldlm_get_ref);
1852 EXPORT_SYMBOL(ldlm_put_ref);
1853 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
1855 /* ldlm_resource.c */
1856 EXPORT_SYMBOL(ldlm_namespace_new);
1857 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1858 EXPORT_SYMBOL(ldlm_namespace_free);
1859 EXPORT_SYMBOL(ldlm_namespace_dump);
1860 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1861 EXPORT_SYMBOL(ldlm_resource_get);
1862 EXPORT_SYMBOL(ldlm_resource_putref);
1863 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1866 EXPORT_SYMBOL(client_import_add_conn);
1867 EXPORT_SYMBOL(client_import_del_conn);
1868 EXPORT_SYMBOL(client_obd_setup);
1869 EXPORT_SYMBOL(client_obd_cleanup);
1870 EXPORT_SYMBOL(client_connect_import);
1871 EXPORT_SYMBOL(client_disconnect_export);
1872 EXPORT_SYMBOL(target_abort_recovery);
1873 EXPORT_SYMBOL(target_cleanup_recovery);
1874 EXPORT_SYMBOL(target_handle_connect);
1875 EXPORT_SYMBOL(target_destroy_export);
1876 EXPORT_SYMBOL(target_cancel_recovery_timer);
1877 EXPORT_SYMBOL(target_send_reply);
1878 EXPORT_SYMBOL(target_queue_recovery_request);
1879 EXPORT_SYMBOL(target_handle_ping);
1880 EXPORT_SYMBOL(target_handle_disconnect);
1881 EXPORT_SYMBOL(target_queue_final_reply);
1884 EXPORT_SYMBOL(lock_res_and_lock);
1885 EXPORT_SYMBOL(unlock_res_and_lock);