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;
106 static inline int have_expired_locks(void)
111 spin_lock_bh(&waiting_locks_spinlock);
112 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
113 spin_unlock_bh(&waiting_locks_spinlock);
118 static int expired_lock_main(void *arg)
120 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
121 struct l_wait_info lwi = { 0 };
125 cfs_daemonize("ldlm_elt");
127 expired_lock_thread.elt_state = ELT_READY;
128 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
131 l_wait_event(expired_lock_thread.elt_waitq,
132 have_expired_locks() ||
133 expired_lock_thread.elt_state == ELT_TERMINATE,
136 spin_lock_bh(&waiting_locks_spinlock);
137 if (expired_lock_thread.elt_dump) {
138 spin_unlock_bh(&waiting_locks_spinlock);
140 /* from waiting_locks_callback, but not in timer */
141 libcfs_debug_dumplog();
142 libcfs_run_lbug_upcall(__FILE__,
143 "waiting_locks_callback",
144 expired_lock_thread.elt_dump);
146 spin_lock_bh(&waiting_locks_spinlock);
147 expired_lock_thread.elt_dump = 0;
152 while (!list_empty(expired)) {
153 struct obd_export *export;
154 struct ldlm_lock *lock;
156 lock = list_entry(expired->next, struct ldlm_lock,
158 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
159 (void *)lock >= LP_POISON) {
160 spin_unlock_bh(&waiting_locks_spinlock);
161 CERROR("free lock on elt list %p\n", lock);
164 list_del_init(&lock->l_pending_chain);
165 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
166 (void *)lock->l_export >= LP_POISON) {
167 CERROR("lock with free export on elt list %p\n",
169 lock->l_export = NULL;
170 LDLM_ERROR(lock, "free export");
173 export = class_export_get(lock->l_export);
174 spin_unlock_bh(&waiting_locks_spinlock);
177 class_fail_export(export);
178 class_export_put(export);
179 spin_lock_bh(&waiting_locks_spinlock);
181 spin_unlock_bh(&waiting_locks_spinlock);
183 if (do_dump && obd_dump_on_eviction) {
184 CERROR("dump the log upon eviction\n");
185 libcfs_debug_dumplog();
188 if (expired_lock_thread.elt_state == ELT_TERMINATE)
192 expired_lock_thread.elt_state = ELT_STOPPED;
193 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
197 /* This is called from within a timer interrupt and cannot schedule */
198 static void waiting_locks_callback(unsigned long unused)
200 struct ldlm_lock *lock, *last = NULL;
202 spin_lock_bh(&waiting_locks_spinlock);
203 while (!list_empty(&waiting_locks_list)) {
204 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
207 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
208 (lock->l_req_mode == LCK_GROUP))
211 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
212 "%s@%s nid %s ",lock->l_export->exp_client_uuid.uuid,
213 lock->l_export->exp_connection->c_remote_uuid.uuid,
214 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
217 LDLM_ERROR(lock, "waiting on lock multiple times");
218 CERROR("wll %p n/p %p/%p, l_pending %p n/p %p/%p\n",
220 waiting_locks_list.next, waiting_locks_list.prev,
221 &lock->l_pending_chain,
222 lock->l_pending_chain.next,
223 lock->l_pending_chain.prev);
225 CFS_INIT_LIST_HEAD(&waiting_locks_list); /* HACK */
226 expired_lock_thread.elt_dump = __LINE__;
229 CEMERG("would be an LBUG, but isn't (bug 5653)\n");
230 libcfs_debug_dumpstack(NULL);
231 /*blocks* libcfs_debug_dumplog(); */
232 /*blocks* libcfs_run_lbug_upcall(file, func, line); */
237 list_del(&lock->l_pending_chain);
238 list_add(&lock->l_pending_chain,
239 &expired_lock_thread.elt_expired_locks);
242 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
243 if (obd_dump_on_timeout)
244 expired_lock_thread.elt_dump = __LINE__;
246 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
250 * Make sure the timer will fire again if we have any locks
253 if (!list_empty(&waiting_locks_list)) {
254 cfs_time_t timeout_rounded;
255 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
257 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
258 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
260 spin_unlock_bh(&waiting_locks_spinlock);
264 * Indicate that we're waiting for a client to call us back cancelling a given
265 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
266 * timer to fire appropriately. (We round up to the next second, to avoid
267 * floods of timer firings during periods of high lock contention and traffic).
269 * Called with the namespace lock held.
271 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
273 cfs_time_t timeout_rounded;
275 if (!list_empty(&lock->l_pending_chain))
278 lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
279 cfs_time_seconds(obd_timeout)/2);
281 timeout_rounded = round_timeout(lock->l_callback_timeout);
283 if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
284 !cfs_timer_is_armed(&waiting_locks_timer)) {
285 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
288 list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
292 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
296 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
298 spin_lock_bh(&waiting_locks_spinlock);
299 if (lock->l_destroyed) {
300 static cfs_time_t next;
301 spin_unlock_bh(&waiting_locks_spinlock);
302 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
303 if (cfs_time_after(cfs_time_current(), next)) {
304 next = cfs_time_shift(14400);
305 libcfs_debug_dumpstack(NULL);
310 ret = __ldlm_add_waiting_lock(lock);
311 spin_unlock_bh(&waiting_locks_spinlock);
313 LDLM_DEBUG(lock, "%sadding to wait list",
314 ret == 0 ? "not re-" : "");
319 * Remove a lock from the pending list, likely because it had its cancellation
320 * callback arrive without incident. This adjusts the lock-timeout timer if
321 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
323 * Called with namespace lock held.
325 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
327 struct list_head *list_next;
329 if (list_empty(&lock->l_pending_chain))
332 list_next = lock->l_pending_chain.next;
333 if (lock->l_pending_chain.prev == &waiting_locks_list) {
334 /* Removing the head of the list, adjust timer. */
335 if (list_next == &waiting_locks_list) {
336 /* No more, just cancel. */
337 cfs_timer_disarm(&waiting_locks_timer);
339 struct ldlm_lock *next;
340 next = list_entry(list_next, struct ldlm_lock,
342 cfs_timer_arm(&waiting_locks_timer,
343 round_timeout(next->l_callback_timeout));
346 list_del_init(&lock->l_pending_chain);
351 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
355 if (lock->l_export == NULL) {
356 /* We don't have a "waiting locks list" on clients. */
357 LDLM_DEBUG(lock, "client lock: no-op");
361 spin_lock_bh(&waiting_locks_spinlock);
362 ret = __ldlm_del_waiting_lock(lock);
363 spin_unlock_bh(&waiting_locks_spinlock);
365 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
372 * Called with namespace lock held.
374 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
376 if (lock->l_export == NULL) {
377 /* We don't have a "waiting locks list" on clients. */
378 LDLM_DEBUG(lock, "client lock: no-op");
382 spin_lock_bh(&waiting_locks_spinlock);
384 if (list_empty(&lock->l_pending_chain)) {
385 spin_unlock_bh(&waiting_locks_spinlock);
386 LDLM_DEBUG(lock, "wasn't waiting");
390 __ldlm_del_waiting_lock(lock);
391 __ldlm_add_waiting_lock(lock);
392 spin_unlock_bh(&waiting_locks_spinlock);
394 LDLM_DEBUG(lock, "refreshed");
398 #else /* !__KERNEL__ */
400 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
402 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
406 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
411 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
415 #endif /* __KERNEL__ */
417 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
418 const char *ast_type)
420 struct ptlrpc_connection *conn = lock->l_export->exp_connection;
421 char *str = libcfs_nid2str(conn->c_peer.nid);
423 LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
424 "service %s.\n", str,
425 lock->l_export->exp_obd->obd_name);
427 LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
428 "resource %d\n", ast_type,
429 obd_export_nid2str(lock->l_export), rc);
431 if (obd_dump_on_timeout)
432 libcfs_debug_dumplog();
433 class_fail_export(lock->l_export);
436 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
437 struct ptlrpc_request *req, int rc,
438 const char *ast_type)
440 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
442 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
443 LASSERT(lock->l_export);
444 if (lock->l_export->exp_libclient) {
445 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
446 " timeout, just cancelling lock", ast_type,
447 libcfs_nid2str(peer.nid));
448 ldlm_lock_cancel(lock);
450 } else if (lock->l_flags & LDLM_FL_CANCEL) {
451 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
452 "cancel was received (AST reply lost?)",
453 ast_type, libcfs_nid2str(peer.nid));
454 ldlm_lock_cancel(lock);
457 ldlm_del_waiting_lock(lock);
458 ldlm_failed_ast(lock, rc, ast_type);
462 LDLM_DEBUG(lock, "client (nid %s) returned %d"
463 " from %s AST - normal race",
464 libcfs_nid2str(peer.nid),
465 lustre_msg_get_status(req->rq_repmsg),
468 LDLM_ERROR(lock, "client (nid %s) returned %d "
469 "from %s AST", libcfs_nid2str(peer.nid),
470 (req->rq_repmsg != NULL) ?
471 lustre_msg_get_status(req->rq_repmsg) : 0,
473 ldlm_lock_cancel(lock);
474 /* Server-side AST functions are called from ldlm_reprocess_all,
475 * which needs to be told to please restart its reprocessing. */
482 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
483 struct ldlm_lock_desc *desc,
484 void *data, int flag)
486 struct ldlm_request *body;
487 struct ptlrpc_request *req;
488 int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
489 [DLM_LOCKREQ_OFF] = sizeof(*body) };
490 int instant_cancel = 0, rc = 0;
493 if (flag == LDLM_CB_CANCELING) {
494 /* Don't need to do anything here. */
500 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
501 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
506 lock_res(lock->l_resource);
507 if (lock->l_granted_mode != lock->l_req_mode) {
508 /* this blocking AST will be communicated as part of the
509 * completion AST instead */
510 unlock_res(lock->l_resource);
511 ptlrpc_req_finished(req);
512 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
516 if (lock->l_destroyed) {
517 /* What's the point? */
518 unlock_res(lock->l_resource);
519 ptlrpc_req_finished(req);
524 if (CURRENT_SECONDS - lock->l_export->exp_last_request_time > 30){
525 unlock_res(lock->l_resource);
526 ptlrpc_req_finished(req);
527 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
532 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
535 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
536 body->lock_handle[0] = lock->l_remote_handle;
537 body->lock_desc = *desc;
538 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
540 LDLM_DEBUG(lock, "server preparing blocking AST");
542 ptlrpc_req_set_repsize(req, 1, NULL);
543 if (instant_cancel) {
544 unlock_res(lock->l_resource);
545 ldlm_lock_cancel(lock);
546 } else if (lock->l_granted_mode == lock->l_req_mode) {
547 ldlm_add_waiting_lock(lock);
548 unlock_res(lock->l_resource);
551 req->rq_send_state = LUSTRE_IMP_FULL;
552 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
554 if (lock->l_export && lock->l_export->exp_ldlm_stats)
555 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
556 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
558 if (unlikely(instant_cancel)) {
559 rc = ptl_send_rpc(req, 1);
561 rc = ptlrpc_queue_wait(req);
562 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
565 /* If client canceled the lock but the cancel has not been
566 * recieved yet, we need to update lvbo to have the proper
567 * attributes cached. */
569 ldlm_res_lvbo_update(lock->l_resource, NULL, 0, 1);
570 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
573 ptlrpc_req_finished(req);
575 /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
576 if (!rc && instant_cancel)
582 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
584 struct ldlm_request *body;
585 struct ptlrpc_request *req;
586 struct timeval granted_time;
587 long total_enqueue_wait;
588 int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
589 [DLM_LOCKREQ_OFF] = sizeof(*body) };
590 int rc = 0, buffers = 2, instant_cancel = 0;
593 LASSERT(lock != NULL);
595 do_gettimeofday(&granted_time);
596 total_enqueue_wait = cfs_timeval_sub(&granted_time,
597 &lock->l_enqueued_time, NULL);
599 if (total_enqueue_wait / 1000000 > obd_timeout)
600 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
601 total_enqueue_wait, lock->l_enqueued_time.tv_sec);
603 lock_res_and_lock(lock);
604 if (lock->l_resource->lr_lvb_len) {
605 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
608 unlock_res_and_lock(lock);
610 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
611 LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
616 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
617 body->lock_handle[0] = lock->l_remote_handle;
618 body->lock_flags = flags;
619 ldlm_lock2desc(lock, &body->lock_desc);
624 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
625 lock->l_resource->lr_lvb_len);
626 lock_res_and_lock(lock);
627 memcpy(lvb, lock->l_resource->lr_lvb_data,
628 lock->l_resource->lr_lvb_len);
629 unlock_res_and_lock(lock);
632 LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
635 ptlrpc_req_set_repsize(req, 1, NULL);
637 req->rq_send_state = LUSTRE_IMP_FULL;
638 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
640 /* We only send real blocking ASTs after the lock is granted */
641 lock_res_and_lock(lock);
642 if (lock->l_flags & LDLM_FL_AST_SENT) {
643 body->lock_flags |= LDLM_FL_AST_SENT;
645 /* We might get here prior to ldlm_handle_enqueue setting
646 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
647 * into waiting list, but this is safe and similar code in
648 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
649 * that would not only cancel the lock, but will also remove
650 * it from waiting list */
651 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
652 unlock_res_and_lock(lock);
653 ldlm_lock_cancel(lock);
655 lock_res_and_lock(lock);
657 ldlm_add_waiting_lock(lock); /* start the lock-timeout
661 unlock_res_and_lock(lock);
663 if (lock->l_export && lock->l_export->exp_ldlm_stats)
664 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
665 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
667 rc = ptlrpc_queue_wait(req);
669 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
671 ptlrpc_req_finished(req);
673 /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
674 if (!rc && instant_cancel)
680 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
682 struct ldlm_resource *res = lock->l_resource;
683 struct ldlm_request *body;
684 struct ptlrpc_request *req;
685 int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
686 [DLM_LOCKREQ_OFF] = sizeof(*body) };
690 LASSERT(lock != NULL);
692 req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
693 LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
698 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
699 body->lock_handle[0] = lock->l_remote_handle;
700 ldlm_lock2desc(lock, &body->lock_desc);
702 lock_res_and_lock(lock);
703 size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
704 unlock_res_and_lock(lock);
705 res = lock->l_resource;
706 ptlrpc_req_set_repsize(req, 2, size);
708 req->rq_send_state = LUSTRE_IMP_FULL;
709 req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
711 if (lock->l_export && lock->l_export->exp_ldlm_stats)
712 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
713 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
715 rc = ptlrpc_queue_wait(req);
716 if (rc == -ELDLM_NO_LOCK_DATA)
717 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
719 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
721 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
723 ptlrpc_req_finished(req);
727 static struct ldlm_lock *
728 find_existing_lock(struct obd_export *exp, struct lustre_handle *remote_hdl)
730 struct list_head *iter;
732 spin_lock(&exp->exp_ldlm_data.led_lock);
733 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
734 struct ldlm_lock *lock;
735 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
736 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
738 spin_unlock(&exp->exp_ldlm_data.led_lock);
742 spin_unlock(&exp->exp_ldlm_data.led_lock);
748 * Main server-side entry point into LDLM. This is called by ptlrpc service
749 * threads to carry out client lock enqueueing requests.
751 int ldlm_handle_enqueue(struct ptlrpc_request *req,
752 ldlm_completion_callback completion_callback,
753 ldlm_blocking_callback blocking_callback,
754 ldlm_glimpse_callback glimpse_callback)
756 struct obd_device *obddev = req->rq_export->exp_obd;
757 struct ldlm_reply *dlm_rep;
758 struct ldlm_request *dlm_req;
759 int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
760 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
763 ldlm_error_t err = ELDLM_OK;
764 struct ldlm_lock *lock = NULL;
768 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
770 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
771 lustre_swab_ldlm_request);
772 if (dlm_req == NULL) {
773 CERROR ("Can't unpack dlm_req\n");
774 GOTO(out, rc = -EFAULT);
777 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
778 flags = dlm_req->lock_flags;
780 LASSERT(req->rq_export);
782 if (req->rq_export->exp_ldlm_stats)
783 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
784 LDLM_ENQUEUE - LDLM_FIRST_OPC);
786 if (dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
787 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE) {
788 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
789 dlm_req->lock_desc.l_resource.lr_type);
790 GOTO(out, rc = -EFAULT);
793 if (dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
794 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
795 dlm_req->lock_desc.l_req_mode & (dlm_req->lock_desc.l_req_mode-1)) {
796 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
797 dlm_req->lock_desc.l_req_mode);
798 GOTO(out, rc = -EFAULT);
801 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
802 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN) {
803 DEBUG_REQ(D_ERROR, req,
804 "PLAIN lock request from IBITS client?");
805 GOTO(out, rc = -EPROTO);
807 } else if (dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) {
808 DEBUG_REQ(D_ERROR, req,
809 "IBITS lock request from unaware client?");
810 GOTO(out, rc = -EPROTO);
814 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
815 against server's _CONNECT_SUPPORTED flags? (I don't want to use
816 ibits for mgc/mgs) */
818 /* INODEBITS_INTEROP: Perform conversion from plain lock to
819 * inodebits lock if client does not support them. */
820 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
821 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
822 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
823 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
824 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
825 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
826 dlm_req->lock_desc.l_req_mode = LCK_CR;
830 if (flags & LDLM_FL_REPLAY) {
831 lock = find_existing_lock(req->rq_export,
832 &dlm_req->lock_handle[0]);
834 DEBUG_REQ(D_HA, req, "found existing lock cookie "LPX64,
835 lock->l_handle.h_cookie);
836 GOTO(existing_lock, rc = 0);
840 /* The lock's callback data might be set in the policy function */
841 lock = ldlm_lock_create(obddev->obd_namespace,
842 dlm_req->lock_desc.l_resource.lr_name,
843 dlm_req->lock_desc.l_resource.lr_type,
844 dlm_req->lock_desc.l_req_mode,
845 blocking_callback, completion_callback,
846 glimpse_callback, NULL, 0);
848 GOTO(out, rc = -ENOMEM);
850 do_gettimeofday(&lock->l_enqueued_time);
851 lock->l_remote_handle = dlm_req->lock_handle[0];
852 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
854 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
855 /* Don't enqueue a lock onto the export if it has already
856 * been evicted. Cancel it now instead. (bug 3822) */
857 if (req->rq_export->exp_failed) {
858 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
859 GOTO(out, rc = -ENOTCONN);
861 lock->l_export = class_export_get(req->rq_export);
862 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
863 list_add(&lock->l_export_chain,
864 &lock->l_export->exp_ldlm_data.led_held_locks);
865 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
869 if (flags & LDLM_FL_HAS_INTENT) {
870 /* In this case, the reply buffer is allocated deep in
871 * local_lock_enqueue by the policy function. */
876 lock_res_and_lock(lock);
877 if (lock->l_resource->lr_lvb_len) {
878 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
881 unlock_res_and_lock(lock);
883 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
884 GOTO(out, rc = -ENOMEM);
886 rc = lustre_pack_reply(req, buffers, size, NULL);
891 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
892 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
893 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
894 lock->l_req_extent = lock->l_policy_data.l_extent;
896 err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, &flags);
900 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
902 dlm_rep->lock_flags = flags;
904 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
905 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
907 /* We never send a blocking AST until the lock is granted, but
908 * we can tell it right now */
909 lock_res_and_lock(lock);
911 /* Now take into account flags to be inherited from original lock
912 request both in reply to client and in our own lock flags. */
913 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
914 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
916 /* Don't move a pending lock onto the export if it has already
917 * been evicted. Cancel it now instead. (bug 5683) */
918 if (req->rq_export->exp_failed ||
919 OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT)) {
920 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
922 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
923 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
924 if (lock->l_granted_mode == lock->l_req_mode) {
925 /* Only cancel lock if it was granted, because it
926 * would be destroyed immediatelly and would never
927 * be granted in the future, causing timeouts on client.
928 * Not granted lock will be cancelled immediatelly after
929 * sending completion AST.
931 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
932 unlock_res_and_lock(lock);
933 ldlm_lock_cancel(lock);
934 lock_res_and_lock(lock);
935 } else if (lock->l_granted_mode == lock->l_req_mode)
936 ldlm_add_waiting_lock(lock);
939 /* Make sure we never ever grant usual metadata locks to liblustre
941 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
942 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
943 req->rq_export->exp_libclient) {
944 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
945 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
946 CERROR("Granting sync lock to libclient. "
947 "req fl %d, rep fl %d, lock fl %d\n",
948 dlm_req->lock_flags, dlm_rep->lock_flags,
950 LDLM_ERROR(lock, "sync lock");
951 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
952 struct ldlm_intent *it;
953 it = lustre_msg_buf(req->rq_reqmsg,
957 CERROR("This is intent %s ("LPU64")\n",
958 ldlm_it2str(it->opc), it->opc);
964 unlock_res_and_lock(lock);
968 req->rq_status = err;
969 if (req->rq_reply_state == NULL) {
970 err = lustre_pack_reply(req, 1, NULL, NULL);
976 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
977 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
979 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
980 "(err=%d, rc=%d)", err, rc);
983 lock_res_and_lock(lock);
984 size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
985 if (size[DLM_REPLY_REC_OFF] > 0) {
986 void *lvb = lustre_msg_buf(req->rq_repmsg,
988 size[DLM_REPLY_REC_OFF]);
989 LASSERTF(lvb != NULL, "req %p, lock %p\n",
992 memcpy(lvb, lock->l_resource->lr_lvb_data,
993 size[DLM_REPLY_REC_OFF]);
995 unlock_res_and_lock(lock);
997 lock_res_and_lock(lock);
998 ldlm_resource_unlink_lock(lock);
999 ldlm_lock_destroy_nolock(lock);
1000 unlock_res_and_lock(lock);
1003 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1004 ldlm_reprocess_all(lock->l_resource);
1006 LDLM_LOCK_PUT(lock);
1009 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1015 int ldlm_handle_convert(struct ptlrpc_request *req)
1017 struct ldlm_request *dlm_req;
1018 struct ldlm_reply *dlm_rep;
1019 struct ldlm_lock *lock;
1021 int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1022 [DLM_LOCKREPLY_OFF] = sizeof(*dlm_rep) };
1025 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1026 lustre_swab_ldlm_request);
1027 if (dlm_req == NULL) {
1028 CERROR ("Can't unpack dlm_req\n");
1032 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1033 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1034 LDLM_CONVERT - LDLM_FIRST_OPC);
1036 rc = lustre_pack_reply(req, 2, size, NULL);
1038 CERROR("out of memory\n");
1041 dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1043 dlm_rep->lock_flags = dlm_req->lock_flags;
1045 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1047 req->rq_status = EINVAL;
1051 LDLM_DEBUG(lock, "server-side convert handler START");
1053 do_gettimeofday(&lock->l_enqueued_time);
1054 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1055 &dlm_rep->lock_flags);
1057 if (ldlm_del_waiting_lock(lock))
1058 LDLM_DEBUG(lock, "converted waiting lock");
1061 req->rq_status = EDEADLOCK;
1066 if (!req->rq_status)
1067 ldlm_reprocess_all(lock->l_resource);
1068 LDLM_DEBUG(lock, "server-side convert handler END");
1069 LDLM_LOCK_PUT(lock);
1071 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1076 /* Cancel all the locks, which handles are packed into ldlm_request */
1077 int ldlm_request_cancel(struct ptlrpc_request *req,
1078 struct ldlm_request *dlm_req, int first)
1080 struct ldlm_resource *res, *pres = NULL;
1081 struct ldlm_lock *lock;
1082 int i, count, done = 0;
1085 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1086 "starting at %d", dlm_req->lock_count, first);
1087 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1091 /* There is no lock on the server at the replay time,
1092 * skip lock cancelling to make replay tests to pass. */
1093 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1096 for (i = first; i < count; i++) {
1097 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1099 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1100 "lock (cookie "LPU64")",
1101 dlm_req->lock_handle[i].cookie);
1106 res = lock->l_resource;
1109 ldlm_reprocess_all(pres);
1110 ldlm_resource_putref(pres);
1113 ldlm_resource_getref(res);
1114 ldlm_res_lvbo_update(res, NULL, 0, 1);
1118 ldlm_lock_cancel(lock);
1119 LDLM_LOCK_PUT(lock);
1122 ldlm_reprocess_all(pres);
1123 ldlm_resource_putref(pres);
1125 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1129 int ldlm_handle_cancel(struct ptlrpc_request *req)
1131 struct ldlm_request *dlm_req;
1135 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1136 lustre_swab_ldlm_request);
1137 if (dlm_req == NULL) {
1138 CERROR("bad request buffer for cancel\n");
1142 if (req->rq_export && req->rq_export->exp_ldlm_stats)
1143 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1144 LDLM_CANCEL - LDLM_FIRST_OPC);
1146 rc = lustre_pack_reply(req, 1, NULL, NULL);
1148 CERROR("out of memory\n");
1152 if (!ldlm_request_cancel(req, dlm_req, 0))
1153 req->rq_status = ESTALE;
1155 if (ptlrpc_reply(req) != 0)
1161 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1162 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1167 LDLM_DEBUG(lock, "client blocking AST callback handler START");
1169 lock_res_and_lock(lock);
1170 lock->l_flags |= LDLM_FL_CBPENDING;
1172 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1173 lock->l_flags |= LDLM_FL_CANCEL;
1175 do_ast = (!lock->l_readers && !lock->l_writers);
1176 unlock_res_and_lock(lock);
1179 LDLM_DEBUG(lock, "already unused, calling "
1180 "callback (%p)", lock->l_blocking_ast);
1181 if (lock->l_blocking_ast != NULL)
1182 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1185 LDLM_DEBUG(lock, "Lock still has references, will be"
1186 " cancelled later");
1189 LDLM_DEBUG(lock, "client blocking callback handler END");
1190 LDLM_LOCK_PUT(lock);
1194 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1195 struct ldlm_namespace *ns,
1196 struct ldlm_request *dlm_req,
1197 struct ldlm_lock *lock)
1199 CFS_LIST_HEAD(ast_list);
1202 LDLM_DEBUG(lock, "client completion callback handler START");
1204 lock_res_and_lock(lock);
1206 /* If we receive the completion AST before the actual enqueue returned,
1207 * then we might need to switch lock modes, resources, or extents. */
1208 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1209 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1210 LDLM_DEBUG(lock, "completion AST, new lock mode");
1213 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1214 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1215 LDLM_DEBUG(lock, "completion AST, new policy data");
1218 ldlm_resource_unlink_lock(lock);
1219 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1220 &lock->l_resource->lr_name,
1221 sizeof(lock->l_resource->lr_name)) != 0) {
1222 unlock_res_and_lock(lock);
1223 ldlm_lock_change_resource(ns, lock,
1224 dlm_req->lock_desc.l_resource.lr_name);
1225 LDLM_DEBUG(lock, "completion AST, new resource");
1226 CERROR("change resource!\n");
1227 lock_res_and_lock(lock);
1230 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1231 lock->l_flags |= LDLM_FL_CBPENDING;
1232 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1235 if (lock->l_lvb_len) {
1237 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1238 lock->l_lvb_swabber);
1240 LDLM_ERROR(lock, "completion AST did not contain "
1243 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1247 ldlm_grant_lock(lock, &ast_list);
1248 unlock_res_and_lock(lock);
1250 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1252 ldlm_run_cp_ast_work(&ast_list);
1254 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1256 LDLM_LOCK_PUT(lock);
1260 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1261 struct ldlm_namespace *ns,
1262 struct ldlm_request *dlm_req,
1263 struct ldlm_lock *lock)
1268 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1270 if (lock->l_glimpse_ast != NULL)
1271 rc = lock->l_glimpse_ast(lock, req);
1273 if (req->rq_repmsg != NULL) {
1276 req->rq_status = rc;
1280 lock_res_and_lock(lock);
1281 if (lock->l_granted_mode == LCK_PW &&
1282 !lock->l_readers && !lock->l_writers &&
1283 cfs_time_after(cfs_time_current(),
1284 cfs_time_add(lock->l_last_used, cfs_time_seconds(10)))) {
1285 unlock_res_and_lock(lock);
1286 if (ldlm_bl_to_thread(ns, NULL, lock, 0))
1287 ldlm_handle_bl_callback(ns, NULL, lock);
1292 unlock_res_and_lock(lock);
1293 LDLM_LOCK_PUT(lock);
1297 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1299 req->rq_status = rc;
1300 if (req->rq_reply_state == NULL) {
1301 rc = lustre_pack_reply(req, 1, NULL, NULL);
1305 return ptlrpc_reply(req);
1308 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1309 struct ldlm_lock *lock, int flags)
1312 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1313 struct ldlm_bl_work_item *blwi;
1316 OBD_ALLOC(blwi, sizeof(*blwi));
1322 blwi->blwi_ld = *ld;
1323 blwi->blwi_lock = lock;
1324 blwi->blwi_flags = flags;
1326 spin_lock(&blp->blp_lock);
1327 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1328 cfs_waitq_signal(&blp->blp_waitq);
1329 spin_unlock(&blp->blp_lock);
1337 static int ldlm_callback_handler(struct ptlrpc_request *req)
1339 struct ldlm_namespace *ns;
1340 struct ldlm_request *dlm_req;
1341 struct ldlm_lock *lock;
1345 /* Requests arrive in sender's byte order. The ptlrpc service
1346 * handler has already checked and, if necessary, byte-swapped the
1347 * incoming request message body, but I am responsible for the
1348 * message buffers. */
1350 if (req->rq_export == NULL) {
1351 struct ldlm_request *dlm_req;
1353 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1354 "export cookie "LPX64"; this is "
1355 "normal if this node rebooted with a lock held\n",
1356 lustre_msg_get_opc(req->rq_reqmsg),
1357 libcfs_id2str(req->rq_peer),
1358 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1360 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1362 lustre_swab_ldlm_request);
1363 if (dlm_req != NULL)
1364 CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1365 dlm_req->lock_handle[0].cookie);
1367 ldlm_callback_reply(req, -ENOTCONN);
1371 LASSERT(req->rq_export != NULL);
1372 LASSERT(req->rq_export->exp_obd != NULL);
1374 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1375 case LDLM_BL_CALLBACK:
1376 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1378 case LDLM_CP_CALLBACK:
1379 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
1381 case LDLM_GL_CALLBACK:
1382 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
1384 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1385 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1386 rc = llog_origin_handle_cancel(req);
1387 ldlm_callback_reply(req, rc);
1389 case OBD_QC_CALLBACK:
1390 OBD_FAIL_RETURN(OBD_FAIL_OBD_QC_CALLBACK_NET, 0);
1391 rc = target_handle_qc_callback(req);
1392 ldlm_callback_reply(req, rc);
1396 /* reply in handler */
1397 rc = target_handle_dqacq_callback(req);
1399 case LLOG_ORIGIN_HANDLE_CREATE:
1400 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1401 rc = llog_origin_handle_create(req);
1402 ldlm_callback_reply(req, rc);
1404 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1405 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1406 rc = llog_origin_handle_next_block(req);
1407 ldlm_callback_reply(req, rc);
1409 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1410 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1411 rc = llog_origin_handle_read_header(req);
1412 ldlm_callback_reply(req, rc);
1414 case LLOG_ORIGIN_HANDLE_CLOSE:
1415 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1416 rc = llog_origin_handle_close(req);
1417 ldlm_callback_reply(req, rc);
1420 CERROR("unknown opcode %u\n",
1421 lustre_msg_get_opc(req->rq_reqmsg));
1422 ldlm_callback_reply(req, -EPROTO);
1426 ns = req->rq_export->exp_obd->obd_namespace;
1427 LASSERT(ns != NULL);
1429 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1430 lustre_swab_ldlm_request);
1431 if (dlm_req == NULL) {
1432 CERROR ("can't unpack dlm_req\n");
1433 ldlm_callback_reply(req, -EPROTO);
1437 lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1439 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1440 "disappeared\n", dlm_req->lock_handle[0].cookie);
1441 ldlm_callback_reply(req, -EINVAL);
1445 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1446 lock_res_and_lock(lock);
1447 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1448 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1449 /* If somebody cancels locks and cache is already droped,
1450 * we can tell the server we have no lock. Otherwise, we
1451 * should send cancel after dropping the cache. */
1452 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1453 (lock->l_flags & LDLM_FL_BL_DONE)) {
1454 LDLM_DEBUG(lock, "callback on lock "
1455 LPX64" - lock disappeared\n",
1456 dlm_req->lock_handle[0].cookie);
1457 LDLM_LOCK_PUT(lock);
1458 unlock_res_and_lock(lock);
1459 ldlm_callback_reply(req, -EINVAL);
1462 lock->l_flags |= LDLM_FL_BL_AST;
1464 unlock_res_and_lock(lock);
1466 /* We want the ost thread to get this reply so that it can respond
1467 * to ost requests (write cache writeback) that might be triggered
1470 * But we'd also like to be able to indicate in the reply that we're
1471 * cancelling right now, because it's unused, or have an intent result
1472 * in the reply, so we might have to push the responsibility for sending
1473 * the reply down into the AST handlers, alas. */
1475 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1476 case LDLM_BL_CALLBACK:
1477 CDEBUG(D_INODE, "blocking ast\n");
1478 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1479 ldlm_callback_reply(req, 0);
1480 if (ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock, 0))
1481 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1483 case LDLM_CP_CALLBACK:
1484 CDEBUG(D_INODE, "completion ast\n");
1485 ldlm_callback_reply(req, 0);
1486 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1488 case LDLM_GL_CALLBACK:
1489 CDEBUG(D_INODE, "glimpse ast\n");
1490 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1493 LBUG(); /* checked above */
1499 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1504 /* Requests arrive in sender's byte order. The ptlrpc service
1505 * handler has already checked and, if necessary, byte-swapped the
1506 * incoming request message body, but I am responsible for the
1507 * message buffers. */
1509 if (req->rq_export == NULL) {
1510 struct ldlm_request *dlm_req;
1512 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1513 lustre_msg_get_opc(req->rq_reqmsg),
1514 libcfs_id2str(req->rq_peer),
1515 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1517 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1519 lustre_swab_ldlm_request);
1520 if (dlm_req != NULL)
1521 ldlm_lock_dump_handle(D_ERROR,
1522 &dlm_req->lock_handle[0]);
1524 ldlm_callback_reply(req, -ENOTCONN);
1528 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1530 /* XXX FIXME move this back to mds/handler.c, bug 249 */
1532 CDEBUG(D_INODE, "cancel\n");
1533 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1534 rc = ldlm_handle_cancel(req);
1538 case OBD_LOG_CANCEL:
1539 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1540 rc = llog_origin_handle_cancel(req);
1541 ldlm_callback_reply(req, rc);
1544 CERROR("invalid opcode %d\n",
1545 lustre_msg_get_opc(req->rq_reqmsg));
1546 ldlm_callback_reply(req, -EINVAL);
1553 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1555 struct ldlm_bl_work_item *blwi = NULL;
1557 spin_lock(&blp->blp_lock);
1558 if (!list_empty(&blp->blp_list)) {
1559 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
1561 list_del(&blwi->blwi_entry);
1563 spin_unlock(&blp->blp_lock);
1568 struct ldlm_bl_thread_data {
1570 struct ldlm_bl_pool *bltd_blp;
1573 static int ldlm_bl_thread_main(void *arg)
1575 struct ldlm_bl_thread_data *bltd = arg;
1576 struct ldlm_bl_pool *blp = bltd->bltd_blp;
1580 char name[CFS_CURPROC_COMM_MAX];
1581 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1583 cfs_daemonize(name);
1586 atomic_inc(&blp->blp_num_threads);
1587 complete(&blp->blp_comp);
1590 struct l_wait_info lwi = { 0 };
1591 struct ldlm_bl_work_item *blwi = NULL;
1593 l_wait_event_exclusive(blp->blp_waitq,
1594 (blwi = ldlm_bl_get_work(blp)) != NULL,
1597 if (blwi->blwi_ns == NULL)
1600 if (blwi->blwi_flags == LDLM_FL_CANCELING) {
1601 /* The special case when we cancel locks in lru
1602 * asynchronously, then we first remove the lock from
1603 * l_bl_ast explicitely in ldlm_cancel_lru before
1604 * sending it to this thread. Thus lock is marked
1605 * LDLM_FL_CANCELING, and already cancelled locally. */
1606 CFS_LIST_HEAD(head);
1607 LASSERT(list_empty(&blwi->blwi_lock->l_bl_ast));
1608 list_add(&blwi->blwi_lock->l_bl_ast, &head);
1609 ldlm_cli_cancel_req(blwi->blwi_lock->l_conn_export,
1611 LDLM_LOCK_PUT(blwi->blwi_lock);
1613 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1616 OBD_FREE(blwi, sizeof(*blwi));
1619 atomic_dec(&blp->blp_num_threads);
1620 complete(&blp->blp_comp);
1626 static int ldlm_setup(void);
1627 static int ldlm_cleanup(int force);
1629 int ldlm_get_ref(void)
1633 mutex_down(&ldlm_ref_sem);
1634 if (++ldlm_refcount == 1) {
1639 mutex_up(&ldlm_ref_sem);
1644 void ldlm_put_ref(int force)
1647 mutex_down(&ldlm_ref_sem);
1648 if (ldlm_refcount == 1) {
1649 int rc = ldlm_cleanup(force);
1651 CERROR("ldlm_cleanup failed: %d\n", rc);
1657 mutex_up(&ldlm_ref_sem);
1662 static int ldlm_setup(void)
1664 struct ldlm_bl_pool *blp;
1671 if (ldlm_state != NULL)
1674 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1675 if (ldlm_state == NULL)
1679 rc = ldlm_proc_setup();
1684 ldlm_state->ldlm_cb_service =
1685 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1686 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
1687 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
1688 ldlm_callback_handler, "ldlm_cbd",
1689 ldlm_svc_proc_dir, NULL,
1690 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1693 if (!ldlm_state->ldlm_cb_service) {
1694 CERROR("failed to start service\n");
1695 GOTO(out_proc, rc = -ENOMEM);
1698 ldlm_state->ldlm_cancel_service =
1699 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1700 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
1701 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
1702 ldlm_cancel_handler, "ldlm_canceld",
1703 ldlm_svc_proc_dir, NULL,
1704 LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
1707 if (!ldlm_state->ldlm_cancel_service) {
1708 CERROR("failed to start service\n");
1709 GOTO(out_proc, rc = -ENOMEM);
1712 OBD_ALLOC(blp, sizeof(*blp));
1714 GOTO(out_proc, rc = -ENOMEM);
1715 ldlm_state->ldlm_bl_pool = blp;
1717 atomic_set(&blp->blp_num_threads, 0);
1718 cfs_waitq_init(&blp->blp_waitq);
1719 spin_lock_init(&blp->blp_lock);
1721 CFS_INIT_LIST_HEAD(&blp->blp_list);
1724 for (i = 0; i < LDLM_BL_THREADS; i++) {
1725 struct ldlm_bl_thread_data bltd = {
1729 init_completion(&blp->blp_comp);
1730 rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1732 CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1733 GOTO(out_thread, rc);
1735 wait_for_completion(&blp->blp_comp);
1738 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
1740 GOTO(out_thread, rc);
1742 rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
1744 GOTO(out_thread, rc);
1746 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1747 expired_lock_thread.elt_state = ELT_STOPPED;
1748 cfs_waitq_init(&expired_lock_thread.elt_waitq);
1750 CFS_INIT_LIST_HEAD(&waiting_locks_list);
1751 spin_lock_init(&waiting_locks_spinlock);
1752 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
1754 rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
1756 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1757 GOTO(out_thread, rc);
1760 wait_event(expired_lock_thread.elt_waitq,
1761 expired_lock_thread.elt_state == ELT_READY);
1768 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1769 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1774 ldlm_proc_cleanup();
1777 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1782 static int ldlm_cleanup(int force)
1785 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1789 if (!list_empty(&ldlm_namespace_list)) {
1790 CERROR("ldlm still has namespaces; clean these up first.\n");
1791 ldlm_dump_all_namespaces(D_DLMTRACE);
1796 while (atomic_read(&blp->blp_num_threads) > 0) {
1797 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1799 init_completion(&blp->blp_comp);
1801 spin_lock(&blp->blp_lock);
1802 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1803 cfs_waitq_signal(&blp->blp_waitq);
1804 spin_unlock(&blp->blp_lock);
1806 wait_for_completion(&blp->blp_comp);
1808 OBD_FREE(blp, sizeof(*blp));
1810 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1811 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1812 ldlm_proc_cleanup();
1814 expired_lock_thread.elt_state = ELT_TERMINATE;
1815 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
1816 wait_event(expired_lock_thread.elt_waitq,
1817 expired_lock_thread.elt_state == ELT_STOPPED);
1819 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1820 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1823 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1829 int __init ldlm_init(void)
1831 init_mutex(&ldlm_ref_sem);
1832 init_mutex(&ldlm_namespace_lock);
1833 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
1834 sizeof(struct ldlm_resource), 0,
1835 SLAB_HWCACHE_ALIGN);
1836 if (ldlm_resource_slab == NULL)
1839 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
1840 sizeof(struct ldlm_lock), 0,
1841 SLAB_HWCACHE_ALIGN);
1842 if (ldlm_lock_slab == NULL) {
1843 cfs_mem_cache_destroy(ldlm_resource_slab);
1850 void __exit ldlm_exit(void)
1854 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1855 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
1856 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
1857 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
1858 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
1862 EXPORT_SYMBOL(ldlm_extent_shift_kms);
1865 EXPORT_SYMBOL(ldlm_get_processing_policy);
1866 EXPORT_SYMBOL(ldlm_lock2desc);
1867 EXPORT_SYMBOL(ldlm_register_intent);
1868 EXPORT_SYMBOL(ldlm_lockname);
1869 EXPORT_SYMBOL(ldlm_typename);
1870 EXPORT_SYMBOL(ldlm_lock2handle);
1871 EXPORT_SYMBOL(__ldlm_handle2lock);
1872 EXPORT_SYMBOL(ldlm_lock_get);
1873 EXPORT_SYMBOL(ldlm_lock_put);
1874 EXPORT_SYMBOL(ldlm_lock_match);
1875 EXPORT_SYMBOL(ldlm_lock_cancel);
1876 EXPORT_SYMBOL(ldlm_lock_addref);
1877 EXPORT_SYMBOL(ldlm_lock_decref);
1878 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1879 EXPORT_SYMBOL(ldlm_lock_change_resource);
1880 EXPORT_SYMBOL(ldlm_lock_set_data);
1881 EXPORT_SYMBOL(ldlm_it2str);
1882 EXPORT_SYMBOL(ldlm_lock_dump);
1883 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1884 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1885 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1886 EXPORT_SYMBOL(ldlm_lock_allow_match);
1888 /* ldlm_request.c */
1889 EXPORT_SYMBOL(ldlm_completion_ast);
1890 EXPORT_SYMBOL(ldlm_blocking_ast);
1891 EXPORT_SYMBOL(ldlm_glimpse_ast);
1892 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1893 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
1894 EXPORT_SYMBOL(ldlm_cli_convert);
1895 EXPORT_SYMBOL(ldlm_cli_enqueue);
1896 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
1897 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
1898 EXPORT_SYMBOL(ldlm_cli_cancel);
1899 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1900 EXPORT_SYMBOL(ldlm_cli_cancel_req);
1901 EXPORT_SYMBOL(ldlm_cli_join_lru);
1902 EXPORT_SYMBOL(ldlm_replay_locks);
1903 EXPORT_SYMBOL(ldlm_resource_foreach);
1904 EXPORT_SYMBOL(ldlm_namespace_foreach);
1905 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1906 EXPORT_SYMBOL(ldlm_resource_iterate);
1907 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1908 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1911 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1912 EXPORT_SYMBOL(ldlm_server_completion_ast);
1913 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1914 EXPORT_SYMBOL(ldlm_handle_enqueue);
1915 EXPORT_SYMBOL(ldlm_handle_cancel);
1916 EXPORT_SYMBOL(ldlm_request_cancel);
1917 EXPORT_SYMBOL(ldlm_handle_convert);
1918 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1919 EXPORT_SYMBOL(ldlm_get_ref);
1920 EXPORT_SYMBOL(ldlm_put_ref);
1921 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
1923 /* ldlm_resource.c */
1924 EXPORT_SYMBOL(ldlm_namespace_new);
1925 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1926 EXPORT_SYMBOL(ldlm_namespace_free);
1927 EXPORT_SYMBOL(ldlm_namespace_dump);
1928 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1929 EXPORT_SYMBOL(ldlm_resource_get);
1930 EXPORT_SYMBOL(ldlm_resource_putref);
1931 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1934 EXPORT_SYMBOL(client_import_add_conn);
1935 EXPORT_SYMBOL(client_import_del_conn);
1936 EXPORT_SYMBOL(client_obd_setup);
1937 EXPORT_SYMBOL(client_obd_cleanup);
1938 EXPORT_SYMBOL(client_connect_import);
1939 EXPORT_SYMBOL(client_disconnect_export);
1940 EXPORT_SYMBOL(target_abort_recovery);
1941 EXPORT_SYMBOL(target_cleanup_recovery);
1942 EXPORT_SYMBOL(target_handle_connect);
1943 EXPORT_SYMBOL(target_destroy_export);
1944 EXPORT_SYMBOL(target_cancel_recovery_timer);
1945 EXPORT_SYMBOL(target_send_reply);
1946 EXPORT_SYMBOL(target_queue_recovery_request);
1947 EXPORT_SYMBOL(target_handle_ping);
1948 EXPORT_SYMBOL(target_handle_disconnect);
1949 EXPORT_SYMBOL(target_queue_final_reply);
1952 EXPORT_SYMBOL(lock_res_and_lock);
1953 EXPORT_SYMBOL(unlock_res_and_lock);