4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lockd.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
44 #include <libcfs/libcfs.h>
45 #include <lustre_dlm.h>
46 #include <obd_class.h>
47 #include <libcfs/list.h>
48 #include "ldlm_internal.h"
50 static int ldlm_num_threads;
51 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
52 "number of DLM service threads to start");
54 static char *ldlm_cpts;
55 CFS_MODULE_PARM(ldlm_cpts, "s", charp, 0444,
56 "CPU partitions ldlm threads should run on");
58 extern struct kmem_cache *ldlm_resource_slab;
59 extern struct kmem_cache *ldlm_lock_slab;
60 static struct mutex ldlm_ref_mutex;
61 static int ldlm_refcount;
63 struct ldlm_cb_async_args {
64 struct ldlm_cb_set_arg *ca_set_arg;
65 struct ldlm_lock *ca_lock;
70 static struct ldlm_state *ldlm_state;
72 inline cfs_time_t round_timeout(cfs_time_t timeout)
74 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
77 /* timeout for initial callback (AST) reply (bz10399) */
78 static inline unsigned int ldlm_get_rq_timeout(void)
81 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
83 return timeout < 1 ? 1 : timeout;
88 #define ELT_TERMINATE 2
94 * blp_prio_list is used for callbacks that should be handled
95 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
98 struct list_head blp_prio_list;
101 * blp_list is used for all other callbacks which are likely
102 * to take longer to process.
104 struct list_head blp_list;
106 wait_queue_head_t blp_waitq;
107 struct completion blp_comp;
108 atomic_t blp_num_threads;
109 atomic_t blp_busy_threads;
114 struct ldlm_bl_work_item {
115 struct list_head blwi_entry;
116 struct ldlm_namespace *blwi_ns;
117 struct ldlm_lock_desc blwi_ld;
118 struct ldlm_lock *blwi_lock;
119 struct list_head blwi_head;
121 struct completion blwi_comp;
122 ldlm_cancel_flags_t blwi_flags;
123 int blwi_mem_pressure;
126 #ifdef HAVE_SERVER_SUPPORT
129 * Protects both waiting_locks_list and expired_lock_thread.
131 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
134 * List for contended locks.
136 * As soon as a lock is contended, it gets placed on this list and
137 * expected time to get a response is filled in the lock. A special
138 * thread walks the list looking for locks that should be released and
139 * schedules client evictions for those that have not been released in
142 * All access to it should be under waiting_locks_spinlock.
144 static struct list_head waiting_locks_list;
145 static struct timer_list waiting_locks_timer;
147 static struct expired_lock_thread {
148 wait_queue_head_t elt_waitq;
151 struct list_head elt_expired_locks;
152 } expired_lock_thread;
154 static inline int have_expired_locks(void)
159 spin_lock_bh(&waiting_locks_spinlock);
160 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
161 spin_unlock_bh(&waiting_locks_spinlock);
167 * Check expired lock list for expired locks and time them out.
169 static int expired_lock_main(void *arg)
171 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
172 struct l_wait_info lwi = { 0 };
177 expired_lock_thread.elt_state = ELT_READY;
178 wake_up(&expired_lock_thread.elt_waitq);
181 l_wait_event(expired_lock_thread.elt_waitq,
182 have_expired_locks() ||
183 expired_lock_thread.elt_state == ELT_TERMINATE,
186 spin_lock_bh(&waiting_locks_spinlock);
187 if (expired_lock_thread.elt_dump) {
188 struct libcfs_debug_msg_data msgdata = {
189 .msg_file = __FILE__,
190 .msg_fn = "waiting_locks_callback",
191 .msg_line = expired_lock_thread.elt_dump };
192 spin_unlock_bh(&waiting_locks_spinlock);
194 /* from waiting_locks_callback, but not in timer */
195 libcfs_debug_dumplog();
196 libcfs_run_lbug_upcall(&msgdata);
198 spin_lock_bh(&waiting_locks_spinlock);
199 expired_lock_thread.elt_dump = 0;
204 while (!list_empty(expired)) {
205 struct obd_export *export;
206 struct ldlm_lock *lock;
208 lock = list_entry(expired->next, struct ldlm_lock,
210 if ((void *)lock < LP_POISON + PAGE_CACHE_SIZE &&
211 (void *)lock >= LP_POISON) {
212 spin_unlock_bh(&waiting_locks_spinlock);
213 CERROR("free lock on elt list %p\n", lock);
216 list_del_init(&lock->l_pending_chain);
217 if ((void *)lock->l_export <
218 LP_POISON + PAGE_CACHE_SIZE &&
219 (void *)lock->l_export >= LP_POISON) {
220 CERROR("lock with free export on elt list %p\n",
222 lock->l_export = NULL;
223 LDLM_ERROR(lock, "free export");
224 /* release extra ref grabbed by
225 * ldlm_add_waiting_lock() or
226 * ldlm_failed_ast() */
227 LDLM_LOCK_RELEASE(lock);
231 if (ldlm_is_destroyed(lock)) {
232 /* release the lock refcount where
233 * waiting_locks_callback() founds */
234 LDLM_LOCK_RELEASE(lock);
237 export = class_export_lock_get(lock->l_export, lock);
238 spin_unlock_bh(&waiting_locks_spinlock);
240 spin_lock_bh(&export->exp_bl_list_lock);
241 list_del_init(&lock->l_exp_list);
242 spin_unlock_bh(&export->exp_bl_list_lock);
245 class_fail_export(export);
246 class_export_lock_put(export, lock);
248 /* release extra ref grabbed by ldlm_add_waiting_lock()
249 * or ldlm_failed_ast() */
250 LDLM_LOCK_RELEASE(lock);
252 spin_lock_bh(&waiting_locks_spinlock);
254 spin_unlock_bh(&waiting_locks_spinlock);
256 if (do_dump && obd_dump_on_eviction) {
257 CERROR("dump the log upon eviction\n");
258 libcfs_debug_dumplog();
261 if (expired_lock_thread.elt_state == ELT_TERMINATE)
265 expired_lock_thread.elt_state = ELT_STOPPED;
266 wake_up(&expired_lock_thread.elt_waitq);
270 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
271 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
274 * Check if there is a request in the export request list
275 * which prevents the lock canceling.
277 static int ldlm_lock_busy(struct ldlm_lock *lock)
279 struct ptlrpc_request *req;
283 if (lock->l_export == NULL)
286 spin_lock_bh(&lock->l_export->exp_rpc_lock);
287 list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
289 if (req->rq_ops->hpreq_lock_match) {
290 match = req->rq_ops->hpreq_lock_match(req, lock);
295 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
299 /* This is called from within a timer interrupt and cannot schedule */
300 static void waiting_locks_callback(unsigned long unused)
302 struct ldlm_lock *lock;
305 spin_lock_bh(&waiting_locks_spinlock);
306 while (!list_empty(&waiting_locks_list)) {
307 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
309 if (cfs_time_after(lock->l_callback_timeout,
310 cfs_time_current()) ||
311 (lock->l_req_mode == LCK_GROUP))
314 /* Check if we need to prolong timeout */
315 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
316 ldlm_lock_busy(lock)) {
319 if (lock->l_pending_chain.next == &waiting_locks_list)
324 spin_unlock_bh(&waiting_locks_spinlock);
325 LDLM_DEBUG(lock, "prolong the busy lock");
326 ldlm_refresh_waiting_lock(lock,
327 ldlm_bl_timeout(lock) >> 1);
328 spin_lock_bh(&waiting_locks_spinlock);
331 LDLM_LOCK_RELEASE(lock);
335 LDLM_LOCK_RELEASE(lock);
338 ldlm_lock_to_ns(lock)->ns_timeouts++;
339 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
340 "evicting client at %s ",
341 cfs_time_current_sec() - lock->l_last_activity,
343 lock->l_export->exp_connection->c_peer.nid));
345 /* no needs to take an extra ref on the lock since it was in
346 * the waiting_locks_list and ldlm_add_waiting_lock()
347 * already grabbed a ref */
348 list_del(&lock->l_pending_chain);
349 list_add(&lock->l_pending_chain,
350 &expired_lock_thread.elt_expired_locks);
354 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
355 if (obd_dump_on_timeout && need_dump)
356 expired_lock_thread.elt_dump = __LINE__;
358 wake_up(&expired_lock_thread.elt_waitq);
362 * Make sure the timer will fire again if we have any locks
365 if (!list_empty(&waiting_locks_list)) {
366 cfs_time_t timeout_rounded;
367 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
369 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
370 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
372 spin_unlock_bh(&waiting_locks_spinlock);
376 * Add lock to the list of contended locks.
378 * Indicate that we're waiting for a client to call us back cancelling a given
379 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
380 * timer to fire appropriately. (We round up to the next second, to avoid
381 * floods of timer firings during periods of high lock contention and traffic).
382 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
383 * if it has been added to the waiting list (1 is returned).
385 * Called with the namespace lock held.
387 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
390 cfs_time_t timeout_rounded;
392 if (!list_empty(&lock->l_pending_chain))
395 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
396 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
399 timeout = cfs_time_shift(seconds);
400 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
401 lock->l_callback_timeout = timeout;
403 timeout_rounded = round_timeout(lock->l_callback_timeout);
405 if (cfs_time_before(timeout_rounded,
406 cfs_timer_deadline(&waiting_locks_timer)) ||
407 !cfs_timer_is_armed(&waiting_locks_timer)) {
408 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
410 /* if the new lock has a shorter timeout than something earlier on
411 the list, we'll wait the longer amount of time; no big deal. */
413 list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
417 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
420 int timeout = ldlm_bl_timeout(lock);
422 /* NB: must be called with hold of lock_res_and_lock() */
423 LASSERT(ldlm_is_res_locked(lock));
424 ldlm_set_waited(lock);
426 LASSERT(!ldlm_is_cancel_on_block(lock));
428 spin_lock_bh(&waiting_locks_spinlock);
429 if (ldlm_is_destroyed(lock)) {
430 static cfs_time_t next;
431 spin_unlock_bh(&waiting_locks_spinlock);
432 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
433 if (cfs_time_after(cfs_time_current(), next)) {
434 next = cfs_time_shift(14400);
435 libcfs_debug_dumpstack(NULL);
440 lock->l_last_activity = cfs_time_current_sec();
441 ret = __ldlm_add_waiting_lock(lock, timeout);
443 /* grab ref on the lock if it has been added to the
447 spin_unlock_bh(&waiting_locks_spinlock);
450 spin_lock_bh(&lock->l_export->exp_bl_list_lock);
451 if (list_empty(&lock->l_exp_list))
452 list_add(&lock->l_exp_list,
453 &lock->l_export->exp_bl_list);
454 spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
457 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
458 ret == 0 ? "not re-" : "", timeout,
459 AT_OFF ? "off" : "on");
464 * Remove a lock from the pending list, likely because it had its cancellation
465 * callback arrive without incident. This adjusts the lock-timeout timer if
466 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
467 * As done by ldlm_del_waiting_lock(), the caller must release the lock
468 * reference when the lock is removed from any list (1 is returned).
470 * Called with namespace lock held.
472 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
474 struct list_head *list_next;
476 if (list_empty(&lock->l_pending_chain))
479 list_next = lock->l_pending_chain.next;
480 if (lock->l_pending_chain.prev == &waiting_locks_list) {
481 /* Removing the head of the list, adjust timer. */
482 if (list_next == &waiting_locks_list) {
483 /* No more, just cancel. */
484 cfs_timer_disarm(&waiting_locks_timer);
486 struct ldlm_lock *next;
487 next = list_entry(list_next, struct ldlm_lock,
489 cfs_timer_arm(&waiting_locks_timer,
490 round_timeout(next->l_callback_timeout));
493 list_del_init(&lock->l_pending_chain);
498 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
502 if (lock->l_export == NULL) {
503 /* We don't have a "waiting locks list" on clients. */
504 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
508 spin_lock_bh(&waiting_locks_spinlock);
509 ret = __ldlm_del_waiting_lock(lock);
510 spin_unlock_bh(&waiting_locks_spinlock);
512 /* remove the lock out of export blocking list */
513 spin_lock_bh(&lock->l_export->exp_bl_list_lock);
514 list_del_init(&lock->l_exp_list);
515 spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
518 /* release lock ref if it has indeed been removed
520 LDLM_LOCK_RELEASE(lock);
523 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
526 EXPORT_SYMBOL(ldlm_del_waiting_lock);
529 * Prolong the contended lock waiting time.
531 * Called with namespace lock held.
533 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
535 if (lock->l_export == NULL) {
536 /* We don't have a "waiting locks list" on clients. */
537 LDLM_DEBUG(lock, "client lock: no-op");
541 spin_lock_bh(&waiting_locks_spinlock);
543 if (list_empty(&lock->l_pending_chain)) {
544 spin_unlock_bh(&waiting_locks_spinlock);
545 LDLM_DEBUG(lock, "wasn't waiting");
549 /* we remove/add the lock to the waiting list, so no needs to
550 * release/take a lock reference */
551 __ldlm_del_waiting_lock(lock);
552 __ldlm_add_waiting_lock(lock, timeout);
553 spin_unlock_bh(&waiting_locks_spinlock);
555 LDLM_DEBUG(lock, "refreshed");
558 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
560 #else /* HAVE_SERVER_SUPPORT */
562 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
567 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
572 #endif /* !HAVE_SERVER_SUPPORT */
574 #ifdef HAVE_SERVER_SUPPORT
577 * Calculate the per-export Blocking timeout (covering BL AST, data flush,
578 * lock cancel, and their replies). Used for lock callback timeout and AST
581 * \param[in] lock lock which is getting the blocking callback
583 * \retval timeout in seconds to wait for the client reply
585 unsigned int ldlm_bl_timeout(struct ldlm_lock *lock)
587 unsigned int timeout;
590 return obd_timeout / 2;
592 /* Since these are non-updating timeouts, we should be conservative.
593 * Take more than usually, 150%
594 * It would be nice to have some kind of "early reply" mechanism for
595 * lock callbacks too... */
596 timeout = at_get(&lock->l_export->exp_bl_lock_at);
597 return max(timeout + (timeout >> 1), ldlm_enqueue_min);
599 EXPORT_SYMBOL(ldlm_bl_timeout);
602 * Perform lock cleanup if AST sending failed.
604 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
605 const char *ast_type)
607 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
608 "to a lock %s callback time out: rc %d\n",
609 lock->l_export->exp_obd->obd_name,
610 obd_export_nid2str(lock->l_export), ast_type, rc);
612 if (obd_dump_on_timeout)
613 libcfs_debug_dumplog();
614 spin_lock_bh(&waiting_locks_spinlock);
615 if (__ldlm_del_waiting_lock(lock) == 0)
616 /* the lock was not in any list, grab an extra ref before adding
617 * the lock to the expired list */
619 list_add(&lock->l_pending_chain,
620 &expired_lock_thread.elt_expired_locks);
621 wake_up(&expired_lock_thread.elt_waitq);
622 spin_unlock_bh(&waiting_locks_spinlock);
626 * Perform lock cleanup if AST reply came with error.
628 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
629 struct ptlrpc_request *req, int rc,
630 const char *ast_type)
632 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
634 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
635 LASSERT(lock->l_export);
636 if (lock->l_export->exp_libclient) {
637 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
638 " timeout, just cancelling lock", ast_type,
639 libcfs_nid2str(peer.nid));
640 ldlm_lock_cancel(lock);
642 } else if (ldlm_is_cancel(lock)) {
643 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
644 "cancel was received (AST reply lost?)",
645 ast_type, libcfs_nid2str(peer.nid));
646 ldlm_lock_cancel(lock);
649 ldlm_del_waiting_lock(lock);
650 ldlm_failed_ast(lock, rc, ast_type);
654 struct ldlm_resource *res = lock->l_resource;
655 LDLM_DEBUG(lock, "client (nid %s) returned %d"
656 " from %s AST - normal race",
657 libcfs_nid2str(peer.nid),
659 lustre_msg_get_status(req->rq_repmsg) : -1,
662 /* update lvbo to return proper attributes.
664 ldlm_resource_getref(res);
665 ldlm_res_lvbo_update(res, NULL, 1);
666 ldlm_resource_putref(res);
670 LDLM_ERROR(lock, "client (nid %s) returned %d: rc = %d "
671 "from %s AST", libcfs_nid2str(peer.nid),
672 (req->rq_repmsg != NULL) ?
673 lustre_msg_get_status(req->rq_repmsg) : 0,
676 ldlm_lock_cancel(lock);
677 /* Server-side AST functions are called from ldlm_reprocess_all,
678 * which needs to be told to please restart its reprocessing. */
685 static int ldlm_cb_interpret(const struct lu_env *env,
686 struct ptlrpc_request *req, void *data, int rc)
688 struct ldlm_cb_async_args *ca = data;
689 struct ldlm_lock *lock = ca->ca_lock;
690 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
693 LASSERT(lock != NULL);
696 case LDLM_GL_CALLBACK:
697 /* Update the LVB from disk if the AST failed
698 * (this is a legal race)
700 * - Glimpse callback of local lock just returns
701 * -ELDLM_NO_LOCK_DATA.
702 * - Glimpse callback of remote lock might return
703 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
705 if (rc == -ELDLM_NO_LOCK_DATA) {
706 LDLM_DEBUG(lock, "lost race - client has a lock but no "
708 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
709 } else if (rc != 0) {
710 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
712 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
715 case LDLM_BL_CALLBACK:
717 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
719 case LDLM_CP_CALLBACK:
721 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
724 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
729 /* release extra reference taken in ldlm_ast_fini() */
730 LDLM_LOCK_RELEASE(lock);
733 atomic_inc(&arg->restart);
738 static void ldlm_update_resend(struct ptlrpc_request *req, void *data)
740 struct ldlm_cb_async_args *ca = data;
741 struct ldlm_lock *lock = ca->ca_lock;
743 ldlm_refresh_waiting_lock(lock, ldlm_bl_timeout(lock));
746 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
747 struct ldlm_cb_set_arg *arg,
748 struct ldlm_lock *lock,
754 if (unlikely(instant_cancel)) {
755 rc = ptl_send_rpc(req, 1);
756 ptlrpc_req_finished(req);
758 atomic_inc(&arg->restart);
761 ptlrpc_set_add_req(arg->set, req);
768 * Check if there are requests in the export request list which prevent
769 * the lock canceling and make these requests high priority ones.
771 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
773 struct ptlrpc_request *req;
776 if (lock->l_export == NULL) {
777 LDLM_DEBUG(lock, "client lock: no-op");
781 spin_lock_bh(&lock->l_export->exp_rpc_lock);
782 list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
784 /* Do not process requests that were not yet added to there
785 * incoming queue or were already removed from there for
786 * processing. We evaluate ptlrpc_nrs_req_can_move() without
787 * holding svcpt->scp_req_lock, and then redo the check with
788 * the lock held once we need to obtain a reliable result.
790 if (ptlrpc_nrs_req_can_move(req) &&
791 req->rq_ops->hpreq_lock_match &&
792 req->rq_ops->hpreq_lock_match(req, lock))
793 ptlrpc_nrs_req_hp_move(req);
795 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
800 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
801 * enqueued server lock conflicts with given one.
803 * Sends blocking AST RPC to the client owning that lock; arms timeout timer
804 * to wait for client response.
806 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
807 struct ldlm_lock_desc *desc,
808 void *data, int flag)
810 struct ldlm_cb_async_args *ca;
811 struct ldlm_cb_set_arg *arg = data;
812 struct ldlm_request *body;
813 struct ptlrpc_request *req;
814 int instant_cancel = 0;
818 if (flag == LDLM_CB_CANCELING)
819 /* Don't need to do anything here. */
823 LASSERT(data != NULL);
824 if (lock->l_export->exp_obd->obd_recovering != 0)
825 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
827 ldlm_lock_reorder_req(lock);
829 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
830 &RQF_LDLM_BL_CALLBACK,
831 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
835 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
836 ca = ptlrpc_req_async_args(req);
837 ca->ca_set_arg = arg;
840 req->rq_interpret_reply = ldlm_cb_interpret;
842 lock_res_and_lock(lock);
843 if (lock->l_granted_mode != lock->l_req_mode) {
844 /* this blocking AST will be communicated as part of the
845 * completion AST instead */
846 unlock_res_and_lock(lock);
848 ptlrpc_req_finished(req);
849 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
853 if (ldlm_is_destroyed(lock)) {
854 /* What's the point? */
855 unlock_res_and_lock(lock);
856 ptlrpc_req_finished(req);
860 if (ldlm_is_cancel_on_block(lock))
863 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
864 body->lock_handle[0] = lock->l_remote_handle;
865 body->lock_desc = *desc;
866 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags & LDLM_FL_AST_MASK);
868 LDLM_DEBUG(lock, "server preparing blocking AST");
870 ptlrpc_request_set_replen(req);
871 if (instant_cancel) {
872 unlock_res_and_lock(lock);
873 ldlm_lock_cancel(lock);
875 req->rq_no_resend = 1;
877 LASSERT(lock->l_granted_mode == lock->l_req_mode);
878 ldlm_add_waiting_lock(lock);
879 unlock_res_and_lock(lock);
881 /* Do not resend after lock callback timeout */
882 req->rq_delay_limit = ldlm_bl_timeout(lock);
883 req->rq_resend_cb = ldlm_update_resend;
886 req->rq_send_state = LUSTRE_IMP_FULL;
887 /* ptlrpc_request_alloc_pack already set timeout */
889 req->rq_timeout = ldlm_get_rq_timeout();
891 lock->l_last_activity = cfs_time_current_sec();
893 if (lock->l_export && lock->l_export->exp_nid_stats &&
894 lock->l_export->exp_nid_stats->nid_ldlm_stats)
895 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
896 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
898 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
902 EXPORT_SYMBOL(ldlm_server_blocking_ast);
905 * ->l_completion_ast callback for a remote lock in server namespace.
907 * Sends AST to the client notifying it of lock granting. If initial
908 * lock response was not sent yet, instead of sending another RPC, just
909 * mark the lock as granted and client will understand
911 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
913 struct ldlm_cb_set_arg *arg = data;
914 struct ldlm_request *body;
915 struct ptlrpc_request *req;
916 struct ldlm_cb_async_args *ca;
917 int instant_cancel = 0;
922 LASSERT(lock != NULL);
923 LASSERT(data != NULL);
925 if (OBD_FAIL_PRECHECK(OBD_FAIL_OST_LDLM_REPLY_NET)) {
926 LDLM_DEBUG(lock, "dropping CP AST");
930 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
931 &RQF_LDLM_CP_CALLBACK);
935 /* server namespace, doesn't need lock */
936 lvb_len = ldlm_lvbo_size(lock);
937 /* LU-3124 & LU-2187: to not return layout in completion AST because
938 * it may deadlock for LU-2187, or client may not have enough space
939 * for large layout. The layout will be returned to client with an
940 * extra RPC to fetch xattr.lov */
941 if (ldlm_has_layout(lock))
944 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT, lvb_len);
945 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
947 ptlrpc_request_free(req);
951 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
952 ca = ptlrpc_req_async_args(req);
953 ca->ca_set_arg = arg;
956 req->rq_interpret_reply = ldlm_cb_interpret;
957 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
959 body->lock_handle[0] = lock->l_remote_handle;
960 body->lock_flags = ldlm_flags_to_wire(flags);
961 ldlm_lock2desc(lock, &body->lock_desc);
963 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
965 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
967 /* We still need to send the RPC to wake up the blocked
968 * enqueue thread on the client.
970 * Consider old client, there is no better way to notify
971 * the failure, just zero-sized the LVB, then the client
972 * will fail out as "-EPROTO". */
973 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, 0,
977 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len,
982 lock->l_last_activity = cfs_time_current_sec();
984 LDLM_DEBUG(lock, "server preparing completion AST");
986 ptlrpc_request_set_replen(req);
988 req->rq_send_state = LUSTRE_IMP_FULL;
989 /* ptlrpc_request_pack already set timeout */
991 req->rq_timeout = ldlm_get_rq_timeout();
993 /* We only send real blocking ASTs after the lock is granted */
994 lock_res_and_lock(lock);
995 if (ldlm_is_ast_sent(lock)) {
996 body->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
997 /* Copy AST flags like LDLM_FL_DISCARD_DATA. */
998 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags &
1001 /* We might get here prior to ldlm_handle_enqueue setting
1002 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
1003 * into waiting list, but this is safe and similar code in
1004 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
1005 * that would not only cancel the lock, but will also remove
1006 * it from waiting list */
1007 if (ldlm_is_cancel_on_block(lock)) {
1008 unlock_res_and_lock(lock);
1009 ldlm_lock_cancel(lock);
1012 req->rq_no_resend = 1;
1014 lock_res_and_lock(lock);
1016 /* start the lock-timeout clock */
1017 ldlm_add_waiting_lock(lock);
1018 /* Do not resend after lock callback timeout */
1019 req->rq_delay_limit = ldlm_bl_timeout(lock);
1020 req->rq_resend_cb = ldlm_update_resend;
1023 unlock_res_and_lock(lock);
1025 if (lock->l_export && lock->l_export->exp_nid_stats &&
1026 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1027 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1028 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1030 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1032 RETURN(lvb_len < 0 ? lvb_len : rc);
1034 EXPORT_SYMBOL(ldlm_server_completion_ast);
1037 * Server side ->l_glimpse_ast handler for client locks.
1039 * Sends glimpse AST to the client and waits for reply. Then updates
1040 * lvbo with the result.
1042 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1044 struct ldlm_cb_set_arg *arg = data;
1045 struct ldlm_request *body;
1046 struct ptlrpc_request *req;
1047 struct ldlm_cb_async_args *ca;
1049 struct req_format *req_fmt;
1052 LASSERT(lock != NULL);
1054 if (arg->gl_desc != NULL)
1055 /* There is a glimpse descriptor to pack */
1056 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1058 req_fmt = &RQF_LDLM_GL_CALLBACK;
1060 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1061 req_fmt, LUSTRE_DLM_VERSION,
1067 if (arg->gl_desc != NULL) {
1068 /* copy the GL descriptor */
1069 union ldlm_gl_desc *desc;
1070 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1071 *desc = *arg->gl_desc;
1074 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1075 body->lock_handle[0] = lock->l_remote_handle;
1076 ldlm_lock2desc(lock, &body->lock_desc);
1078 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1079 ca = ptlrpc_req_async_args(req);
1080 ca->ca_set_arg = arg;
1083 /* server namespace, doesn't need lock */
1084 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1085 ldlm_lvbo_size(lock));
1086 ptlrpc_request_set_replen(req);
1088 req->rq_send_state = LUSTRE_IMP_FULL;
1089 /* ptlrpc_request_alloc_pack already set timeout */
1091 req->rq_timeout = ldlm_get_rq_timeout();
1093 lock->l_last_activity = cfs_time_current_sec();
1095 req->rq_interpret_reply = ldlm_cb_interpret;
1097 if (lock->l_export && lock->l_export->exp_nid_stats &&
1098 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1099 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1100 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1102 rc = ldlm_ast_fini(req, arg, lock, 0);
1106 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1108 int ldlm_glimpse_locks(struct ldlm_resource *res,
1109 struct list_head *gl_work_list)
1114 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1116 if (rc == -ERESTART)
1117 ldlm_reprocess_all(res);
1121 EXPORT_SYMBOL(ldlm_glimpse_locks);
1123 /* return LDLM lock associated with a lock callback request */
1124 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req)
1126 struct ldlm_cb_async_args *ca;
1127 struct ldlm_lock *lock;
1130 ca = ptlrpc_req_async_args(req);
1133 RETURN(ERR_PTR(-EFAULT));
1137 EXPORT_SYMBOL(ldlm_request_lock);
1139 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1140 struct lprocfs_stats *srv_stats)
1142 int lock_type = 0, op = 0;
1144 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1146 switch (lock_type) {
1148 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1151 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1152 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1154 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1157 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1160 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1168 lprocfs_counter_incr(srv_stats, op);
1174 * Main server-side entry point into LDLM for enqueue. This is called by ptlrpc
1175 * service threads to carry out client lock enqueueing requests.
1177 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1178 struct ptlrpc_request *req,
1179 const struct ldlm_request *dlm_req,
1180 const struct ldlm_callback_suite *cbs)
1182 struct ldlm_reply *dlm_rep;
1184 ldlm_error_t err = ELDLM_OK;
1185 struct ldlm_lock *lock = NULL;
1186 void *cookie = NULL;
1188 struct ldlm_resource *res = NULL;
1191 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1193 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF, LATF_SKIP);
1194 flags = ldlm_flags_from_wire(dlm_req->lock_flags);
1196 LASSERT(req->rq_export);
1198 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1199 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1201 if (req->rq_export && req->rq_export->exp_nid_stats &&
1202 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1203 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1204 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1206 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1207 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1208 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1209 dlm_req->lock_desc.l_resource.lr_type);
1210 GOTO(out, rc = -EFAULT);
1213 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1214 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1215 dlm_req->lock_desc.l_req_mode &
1216 (dlm_req->lock_desc.l_req_mode-1))) {
1217 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1218 dlm_req->lock_desc.l_req_mode);
1219 GOTO(out, rc = -EFAULT);
1222 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) {
1223 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1225 DEBUG_REQ(D_ERROR, req,
1226 "PLAIN lock request from IBITS client?");
1227 GOTO(out, rc = -EPROTO);
1229 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1231 DEBUG_REQ(D_ERROR, req,
1232 "IBITS lock request from unaware client?");
1233 GOTO(out, rc = -EPROTO);
1237 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1238 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1239 ibits for mgc/mgs) */
1241 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1242 * inodebits lock if client does not support them. */
1243 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) &&
1244 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1245 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1246 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1247 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1248 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1249 dlm_req->lock_desc.l_req_mode = LCK_CR;
1253 if (unlikely((flags & LDLM_FL_REPLAY) ||
1254 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))) {
1255 /* Find an existing lock in the per-export lock hash */
1256 /* In the function below, .hs_keycmp resolves to
1257 * ldlm_export_lock_keycmp() */
1258 /* coverity[overrun-buffer-val] */
1259 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1260 (void *)&dlm_req->lock_handle[0]);
1262 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1263 LPX64, lock->l_handle.h_cookie);
1264 flags |= LDLM_FL_RESENT;
1265 GOTO(existing_lock, rc = 0);
1269 /* The lock's callback data might be set in the policy function */
1270 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1271 dlm_req->lock_desc.l_resource.lr_type,
1272 dlm_req->lock_desc.l_req_mode,
1273 cbs, NULL, 0, LVB_T_NONE);
1280 lock->l_remote_handle = dlm_req->lock_handle[0];
1281 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1283 /* Initialize resource lvb but not for a lock being replayed since
1284 * Client already got lvb sent in this case.
1285 * This must occur early since some policy methods assume resource
1286 * lvb is available (lr_lvb_data != NULL).
1288 res = lock->l_resource;
1289 if (!(flags & LDLM_FL_REPLAY)) {
1290 /* non-replayed lock, delayed lvb init may need to be done */
1291 rc = ldlm_lvbo_init(res);
1293 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", rc);
1298 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1299 /* Don't enqueue a lock onto the export if it is been disonnected
1300 * due to eviction (bug 3822) or server umount (bug 24324).
1301 * Cancel it now instead. */
1302 if (req->rq_export->exp_disconnected) {
1303 LDLM_ERROR(lock, "lock on disconnected export %p",
1305 GOTO(out, rc = -ENOTCONN);
1308 lock->l_export = class_export_lock_get(req->rq_export, lock);
1309 if (lock->l_export->exp_lock_hash)
1310 cfs_hash_add(lock->l_export->exp_lock_hash,
1311 &lock->l_remote_handle,
1314 /* Inherit the enqueue flags before the operation, because we do not
1315 * keep the res lock on return and next operations (BL AST) may proceed
1317 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
1318 LDLM_FL_INHERIT_MASK);
1321 if (flags & LDLM_FL_HAS_INTENT) {
1322 /* In this case, the reply buffer is allocated deep in
1323 * local_lock_enqueue by the policy function. */
1326 /* based on the assumption that lvb size never changes during
1327 * resource life time otherwise it need resource->lr_lock's
1329 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1330 RCL_SERVER, ldlm_lvbo_size(lock));
1332 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1333 GOTO(out, rc = -ENOMEM);
1335 rc = req_capsule_server_pack(&req->rq_pill);
1340 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1341 ldlm_convert_policy_to_local(req->rq_export,
1342 dlm_req->lock_desc.l_resource.lr_type,
1343 &dlm_req->lock_desc.l_policy_data,
1344 &lock->l_policy_data);
1345 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1346 lock->l_req_extent = lock->l_policy_data.l_extent;
1348 err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
1355 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1357 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1358 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1360 /* We never send a blocking AST until the lock is granted, but
1361 * we can tell it right now */
1362 lock_res_and_lock(lock);
1364 /* Now take into account flags to be inherited from original lock
1365 request both in reply to client and in our own lock flags. */
1366 dlm_rep->lock_flags = ldlm_flags_to_wire(flags);
1367 lock->l_flags |= flags & LDLM_FL_INHERIT_MASK;
1369 /* Don't move a pending lock onto the export if it has already been
1370 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1371 * Cancel it now instead. */
1372 if (unlikely(req->rq_export->exp_disconnected ||
1373 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1374 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1376 } else if (ldlm_is_ast_sent(lock)) {
1377 dlm_rep->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1378 if (lock->l_granted_mode == lock->l_req_mode) {
1380 * Only cancel lock if it was granted, because it would
1381 * be destroyed immediately and would never be granted
1382 * in the future, causing timeouts on client. Not
1383 * granted lock will be cancelled immediately after
1384 * sending completion AST.
1386 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1387 unlock_res_and_lock(lock);
1388 ldlm_lock_cancel(lock);
1389 lock_res_and_lock(lock);
1391 ldlm_add_waiting_lock(lock);
1394 /* Make sure we never ever grant usual metadata locks to liblustre
1396 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1397 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1398 req->rq_export->exp_libclient) {
1399 if (unlikely(!ldlm_is_cancel_on_block(lock) ||
1400 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1401 CERROR("Granting sync lock to libclient. "
1402 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1403 dlm_req->lock_flags, dlm_rep->lock_flags,
1405 LDLM_ERROR(lock, "sync lock");
1406 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1407 struct ldlm_intent *it;
1409 it = req_capsule_client_get(&req->rq_pill,
1412 CERROR("This is intent %s ("LPU64")\n",
1413 ldlm_it2str(it->opc), it->opc);
1419 unlock_res_and_lock(lock);
1423 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1424 if (!req->rq_packed_final) {
1425 err = lustre_pack_reply(req, 1, NULL, NULL);
1430 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1431 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1433 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1434 "(err=%d, rc=%d)", err, rc);
1437 if (req_capsule_has_field(&req->rq_pill, &RMF_DLM_LVB,
1439 ldlm_lvbo_size(lock) > 0) {
1443 buf = req_capsule_server_get(&req->rq_pill,
1445 LASSERTF(buf != NULL, "req %p, lock %p\n",
1447 buflen = req_capsule_get_size(&req->rq_pill,
1448 &RMF_DLM_LVB, RCL_SERVER);
1449 /* non-replayed lock, delayed lvb init may
1450 * need to be occur now */
1451 if ((buflen > 0) && !(flags & LDLM_FL_REPLAY)) {
1452 buflen = ldlm_lvbo_fill(lock, buf,
1458 buflen, RCL_SERVER);
1461 } else if (flags & LDLM_FL_REPLAY) {
1462 /* no LVB resend upon replay */
1477 lock_res_and_lock(lock);
1478 ldlm_resource_unlink_lock(lock);
1479 ldlm_lock_destroy_nolock(lock);
1480 unlock_res_and_lock(lock);
1483 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1484 ldlm_reprocess_all(lock->l_resource);
1486 LDLM_LOCK_RELEASE(lock);
1489 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1494 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1497 * Old-style LDLM main entry point for server code enqueue.
1499 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1500 ldlm_completion_callback completion_callback,
1501 ldlm_blocking_callback blocking_callback,
1502 ldlm_glimpse_callback glimpse_callback)
1504 struct ldlm_request *dlm_req;
1505 struct ldlm_callback_suite cbs = {
1506 .lcs_completion = completion_callback,
1507 .lcs_blocking = blocking_callback,
1508 .lcs_glimpse = glimpse_callback
1512 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1513 if (dlm_req != NULL) {
1514 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1515 req, dlm_req, &cbs);
1521 EXPORT_SYMBOL(ldlm_handle_enqueue);
1524 * Main LDLM entry point for server code to process lock conversion requests.
1526 int ldlm_handle_convert0(struct ptlrpc_request *req,
1527 const struct ldlm_request *dlm_req)
1529 struct ldlm_reply *dlm_rep;
1530 struct ldlm_lock *lock;
1534 if (req->rq_export && req->rq_export->exp_nid_stats &&
1535 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1536 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1537 LDLM_CONVERT - LDLM_FIRST_OPC);
1539 rc = req_capsule_server_pack(&req->rq_pill);
1543 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1544 dlm_rep->lock_flags = dlm_req->lock_flags;
1546 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1548 req->rq_status = LUSTRE_EINVAL;
1552 LDLM_DEBUG(lock, "server-side convert handler START");
1554 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1555 &dlm_rep->lock_flags);
1557 if (ldlm_del_waiting_lock(lock))
1558 LDLM_DEBUG(lock, "converted waiting lock");
1561 req->rq_status = LUSTRE_EDEADLK;
1566 if (!req->rq_status)
1567 ldlm_reprocess_all(lock->l_resource);
1568 LDLM_DEBUG(lock, "server-side convert handler END");
1569 LDLM_LOCK_PUT(lock);
1571 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1575 EXPORT_SYMBOL(ldlm_handle_convert0);
1578 * Old-style main LDLM entry point for server code to process lock conversion
1581 int ldlm_handle_convert(struct ptlrpc_request *req)
1584 struct ldlm_request *dlm_req;
1586 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1587 if (dlm_req != NULL) {
1588 rc = ldlm_handle_convert0(req, dlm_req);
1590 CERROR ("Can't unpack dlm_req\n");
1595 EXPORT_SYMBOL(ldlm_handle_convert);
1598 * Cancel all the locks whose handles are packed into ldlm_request
1600 * Called by server code expecting such combined cancel activity
1603 int ldlm_request_cancel(struct ptlrpc_request *req,
1604 const struct ldlm_request *dlm_req,
1605 int first, enum lustre_at_flags flags)
1607 struct ldlm_resource *res, *pres = NULL;
1608 struct ldlm_lock *lock;
1609 int i, count, done = 0;
1612 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1616 if (count == 1 && dlm_req->lock_handle[0].cookie == 0)
1619 /* There is no lock on the server at the replay time,
1620 * skip lock cancelling to make replay tests to pass. */
1621 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1624 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1625 "starting at %d", count, first);
1627 for (i = first; i < count; i++) {
1628 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1630 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1631 "lock (cookie "LPU64")",
1632 dlm_req->lock_handle[i].cookie);
1636 res = lock->l_resource;
1639 /* This code is an optimization to only attempt lock
1640 * granting on the resource (that could be CPU-expensive)
1641 * after we are done cancelling lock in that resource. */
1644 ldlm_reprocess_all(pres);
1645 LDLM_RESOURCE_DELREF(pres);
1646 ldlm_resource_putref(pres);
1649 ldlm_resource_getref(res);
1650 LDLM_RESOURCE_ADDREF(res);
1651 ldlm_res_lvbo_update(res, NULL, 1);
1656 if ((flags & LATF_STATS) && ldlm_is_ast_sent(lock)) {
1657 long delay = cfs_time_sub(cfs_time_current_sec(),
1658 lock->l_last_activity);
1659 LDLM_DEBUG(lock, "server cancels blocked lock after "
1660 CFS_DURATION_T"s", delay);
1661 at_measured(&lock->l_export->exp_bl_lock_at, delay);
1663 ldlm_lock_cancel(lock);
1664 LDLM_LOCK_PUT(lock);
1667 ldlm_reprocess_all(pres);
1668 LDLM_RESOURCE_DELREF(pres);
1669 ldlm_resource_putref(pres);
1671 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1674 EXPORT_SYMBOL(ldlm_request_cancel);
1677 * Main LDLM entry point for server code to cancel locks.
1679 * Typically gets called from service handler on LDLM_CANCEL opc.
1681 int ldlm_handle_cancel(struct ptlrpc_request *req)
1683 struct ldlm_request *dlm_req;
1687 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1688 if (dlm_req == NULL) {
1689 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1693 if (req->rq_export && req->rq_export->exp_nid_stats &&
1694 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1695 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1696 LDLM_CANCEL - LDLM_FIRST_OPC);
1698 rc = req_capsule_server_pack(&req->rq_pill);
1702 if (!ldlm_request_cancel(req, dlm_req, 0, LATF_STATS))
1703 req->rq_status = LUSTRE_ESTALE;
1705 RETURN(ptlrpc_reply(req));
1707 EXPORT_SYMBOL(ldlm_handle_cancel);
1708 #endif /* HAVE_SERVER_SUPPORT */
1711 * Callback handler for receiving incoming blocking ASTs.
1713 * This can only happen on client side.
1715 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1716 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1721 LDLM_DEBUG(lock, "client blocking AST callback handler");
1723 lock_res_and_lock(lock);
1724 ldlm_set_cbpending(lock);
1726 if (ldlm_is_cancel_on_block(lock))
1727 ldlm_set_cancel(lock);
1729 do_ast = (!lock->l_readers && !lock->l_writers);
1730 unlock_res_and_lock(lock);
1733 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1734 lock, lock->l_blocking_ast);
1735 if (lock->l_blocking_ast != NULL)
1736 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1739 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1743 LDLM_DEBUG(lock, "client blocking callback handler END");
1744 LDLM_LOCK_RELEASE(lock);
1749 * Callback handler for receiving incoming completion ASTs.
1751 * This only can happen on client side.
1753 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1754 struct ldlm_namespace *ns,
1755 struct ldlm_request *dlm_req,
1756 struct ldlm_lock *lock)
1758 struct list_head ast_list;
1763 LDLM_DEBUG(lock, "client completion callback handler START");
1765 INIT_LIST_HEAD(&ast_list);
1766 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1767 int to = cfs_time_seconds(1);
1769 schedule_timeout_and_set_state(
1770 TASK_INTERRUPTIBLE, to);
1771 if (lock->l_granted_mode == lock->l_req_mode ||
1772 ldlm_is_destroyed(lock))
1777 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1779 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", lvb_len);
1780 GOTO(out, rc = lvb_len);
1781 } else if (lvb_len > 0) {
1782 if (lock->l_lvb_len > 0) {
1783 /* for extent lock, lvb contains ost_lvb{}. */
1784 LASSERT(lock->l_lvb_data != NULL);
1786 if (unlikely(lock->l_lvb_len < lvb_len)) {
1787 LDLM_ERROR(lock, "Replied LVB is larger than "
1788 "expectation, expected = %d, "
1790 lock->l_lvb_len, lvb_len);
1791 GOTO(out, rc = -EINVAL);
1793 } else if (ldlm_has_layout(lock)) { /* for layout lock, lvb has
1794 * variable length */
1797 OBD_ALLOC_LARGE(lvb_data, lvb_len);
1798 if (lvb_data == NULL) {
1799 LDLM_ERROR(lock, "No memory: %d.\n", lvb_len);
1800 GOTO(out, rc = -ENOMEM);
1803 lock_res_and_lock(lock);
1804 LASSERT(lock->l_lvb_data == NULL);
1805 lock->l_lvb_type = LVB_T_LAYOUT;
1806 lock->l_lvb_data = lvb_data;
1807 lock->l_lvb_len = lvb_len;
1808 unlock_res_and_lock(lock);
1812 lock_res_and_lock(lock);
1813 if (ldlm_is_destroyed(lock) ||
1814 lock->l_granted_mode == lock->l_req_mode) {
1815 /* bug 11300: the lock has already been granted */
1816 unlock_res_and_lock(lock);
1817 LDLM_DEBUG(lock, "Double grant race happened");
1821 /* If we receive the completion AST before the actual enqueue returned,
1822 * then we might need to switch lock modes, resources, or extents. */
1823 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1824 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1825 LDLM_DEBUG(lock, "completion AST, new lock mode");
1828 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1829 ldlm_convert_policy_to_local(req->rq_export,
1830 dlm_req->lock_desc.l_resource.lr_type,
1831 &dlm_req->lock_desc.l_policy_data,
1832 &lock->l_policy_data);
1833 LDLM_DEBUG(lock, "completion AST, new policy data");
1836 ldlm_resource_unlink_lock(lock);
1837 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1838 &lock->l_resource->lr_name,
1839 sizeof(lock->l_resource->lr_name)) != 0) {
1840 unlock_res_and_lock(lock);
1841 rc = ldlm_lock_change_resource(ns, lock,
1842 &dlm_req->lock_desc.l_resource.lr_name);
1844 LDLM_ERROR(lock, "Failed to allocate resource");
1847 LDLM_DEBUG(lock, "completion AST, new resource");
1848 CERROR("change resource!\n");
1849 lock_res_and_lock(lock);
1852 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1853 /* BL_AST locks are not needed in LRU.
1854 * Let ldlm_cancel_lru() be fast. */
1855 ldlm_lock_remove_from_lru(lock);
1856 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1857 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1860 if (lock->l_lvb_len > 0) {
1861 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_CLIENT,
1862 lock->l_lvb_data, lvb_len);
1864 unlock_res_and_lock(lock);
1869 ldlm_grant_lock(lock, &ast_list);
1870 unlock_res_and_lock(lock);
1872 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1874 /* Let Enqueue to call osc_lock_upcall() and initialize
1876 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1878 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1880 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1886 lock_res_and_lock(lock);
1887 ldlm_set_failed(lock);
1888 unlock_res_and_lock(lock);
1889 wake_up(&lock->l_waitq);
1891 LDLM_LOCK_RELEASE(lock);
1895 * Callback handler for receiving incoming glimpse ASTs.
1897 * This only can happen on client side. After handling the glimpse AST
1898 * we also consider dropping the lock here if it is unused locally for a
1901 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1902 struct ldlm_namespace *ns,
1903 struct ldlm_request *dlm_req,
1904 struct ldlm_lock *lock)
1909 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1911 if (lock->l_glimpse_ast != NULL)
1912 rc = lock->l_glimpse_ast(lock, req);
1914 if (req->rq_repmsg != NULL) {
1917 req->rq_status = rc;
1921 lock_res_and_lock(lock);
1922 if (lock->l_granted_mode == LCK_PW &&
1923 !lock->l_readers && !lock->l_writers &&
1924 cfs_time_after(cfs_time_current(),
1925 cfs_time_add(lock->l_last_used,
1926 cfs_time_seconds(10)))) {
1927 unlock_res_and_lock(lock);
1928 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1929 ldlm_handle_bl_callback(ns, NULL, lock);
1934 unlock_res_and_lock(lock);
1935 LDLM_LOCK_RELEASE(lock);
1939 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1941 if (req->rq_no_reply)
1944 req->rq_status = rc;
1945 if (!req->rq_packed_final) {
1946 rc = lustre_pack_reply(req, 1, NULL, NULL);
1950 return ptlrpc_reply(req);
1953 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi,
1954 ldlm_cancel_flags_t cancel_flags)
1956 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1959 spin_lock(&blp->blp_lock);
1960 if (blwi->blwi_lock &&
1961 ldlm_is_discard_data(blwi->blwi_lock)) {
1962 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1963 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1965 /* other blocking callbacks are added to the regular list */
1966 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1968 spin_unlock(&blp->blp_lock);
1970 wake_up(&blp->blp_waitq);
1972 /* can not check blwi->blwi_flags as blwi could be already freed in
1974 if (!(cancel_flags & LCF_ASYNC))
1975 wait_for_completion(&blwi->blwi_comp);
1980 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1981 struct ldlm_namespace *ns,
1982 struct ldlm_lock_desc *ld,
1983 struct list_head *cancels, int count,
1984 struct ldlm_lock *lock,
1985 ldlm_cancel_flags_t cancel_flags)
1987 init_completion(&blwi->blwi_comp);
1988 INIT_LIST_HEAD(&blwi->blwi_head);
1990 if (memory_pressure_get())
1991 blwi->blwi_mem_pressure = 1;
1994 blwi->blwi_flags = cancel_flags;
1996 blwi->blwi_ld = *ld;
1998 list_add(&blwi->blwi_head, cancels);
1999 list_del_init(cancels);
2000 blwi->blwi_count = count;
2002 blwi->blwi_lock = lock;
2007 * Queues a list of locks \a cancels containing \a count locks
2008 * for later processing by a blocking thread. If \a count is zero,
2009 * then the lock referenced as \a lock is queued instead.
2011 * The blocking thread would then call ->l_blocking_ast callback in the lock.
2012 * If list addition fails an error is returned and caller is supposed to
2013 * call ->l_blocking_ast itself.
2015 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
2016 struct ldlm_lock_desc *ld,
2017 struct ldlm_lock *lock,
2018 struct list_head *cancels, int count,
2019 ldlm_cancel_flags_t cancel_flags)
2023 if (cancels && count == 0)
2026 if (cancel_flags & LCF_ASYNC) {
2027 struct ldlm_bl_work_item *blwi;
2029 OBD_ALLOC(blwi, sizeof(*blwi));
2032 init_blwi(blwi, ns, ld, cancels, count, lock, cancel_flags);
2034 RETURN(__ldlm_bl_to_thread(blwi, cancel_flags));
2036 /* if it is synchronous call do minimum mem alloc, as it could
2037 * be triggered from kernel shrinker
2039 struct ldlm_bl_work_item blwi;
2041 memset(&blwi, 0, sizeof(blwi));
2042 init_blwi(&blwi, ns, ld, cancels, count, lock, cancel_flags);
2043 RETURN(__ldlm_bl_to_thread(&blwi, cancel_flags));
2048 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
2049 struct ldlm_lock *lock)
2051 return ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LCF_ASYNC);
2054 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
2055 struct list_head *cancels, int count,
2056 ldlm_cancel_flags_t cancel_flags)
2058 return ldlm_bl_to_thread(ns, ld, NULL, cancels, count, cancel_flags);
2061 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
2062 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
2064 struct obd_device *obd = req->rq_export->exp_obd;
2071 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
2073 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2075 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
2077 DEBUG_REQ(D_IOCTL, req, "no set_info key");
2080 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
2082 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
2084 DEBUG_REQ(D_IOCTL, req, "no set_info val");
2087 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
2090 /* We are responsible for swabbing contents of val */
2092 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
2093 /* Pass it on to mdc (the "export" in this case) */
2094 rc = obd_set_info_async(req->rq_svc_thread->t_env,
2096 sizeof(KEY_HSM_COPYTOOL_SEND),
2097 KEY_HSM_COPYTOOL_SEND,
2100 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
2105 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
2106 const char *msg, int rc,
2107 struct lustre_handle *handle)
2109 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
2110 "%s: [nid %s] [rc %d] [lock "LPX64"]",
2111 msg, libcfs_id2str(req->rq_peer), rc,
2112 handle ? handle->cookie : 0);
2113 if (req->rq_no_reply)
2114 CWARN("No reply was sent, maybe cause bug 21636.\n");
2116 CWARN("Send reply failed, maybe cause bug 21636.\n");
2119 static int ldlm_handle_qc_callback(struct ptlrpc_request *req)
2121 struct obd_quotactl *oqctl;
2122 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2124 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2125 if (oqctl == NULL) {
2126 CERROR("Can't unpack obd_quotactl\n");
2130 oqctl->qc_stat = ptlrpc_status_ntoh(oqctl->qc_stat);
2132 cli->cl_qchk_stat = oqctl->qc_stat;
2136 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2137 static int ldlm_callback_handler(struct ptlrpc_request *req)
2139 struct ldlm_namespace *ns;
2140 struct ldlm_request *dlm_req;
2141 struct ldlm_lock *lock;
2145 /* Requests arrive in sender's byte order. The ptlrpc service
2146 * handler has already checked and, if necessary, byte-swapped the
2147 * incoming request message body, but I am responsible for the
2148 * message buffers. */
2150 /* do nothing for sec context finalize */
2151 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
2154 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2156 if (req->rq_export == NULL) {
2157 rc = ldlm_callback_reply(req, -ENOTCONN);
2158 ldlm_callback_errmsg(req, "Operate on unconnected server",
2163 LASSERT(req->rq_export != NULL);
2164 LASSERT(req->rq_export->exp_obd != NULL);
2166 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2167 case LDLM_BL_CALLBACK:
2168 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
2171 case LDLM_CP_CALLBACK:
2172 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
2175 case LDLM_GL_CALLBACK:
2176 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
2180 rc = ldlm_handle_setinfo(req);
2181 ldlm_callback_reply(req, rc);
2183 case LLOG_ORIGIN_HANDLE_CREATE:
2184 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2185 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2187 rc = llog_origin_handle_open(req);
2188 ldlm_callback_reply(req, rc);
2190 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2191 req_capsule_set(&req->rq_pill,
2192 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2193 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2195 rc = llog_origin_handle_next_block(req);
2196 ldlm_callback_reply(req, rc);
2198 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2199 req_capsule_set(&req->rq_pill,
2200 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2201 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2203 rc = llog_origin_handle_read_header(req);
2204 ldlm_callback_reply(req, rc);
2206 case LLOG_ORIGIN_HANDLE_CLOSE:
2207 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2209 rc = llog_origin_handle_close(req);
2210 ldlm_callback_reply(req, rc);
2212 case OBD_QC_CALLBACK:
2213 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
2214 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
2216 rc = ldlm_handle_qc_callback(req);
2217 ldlm_callback_reply(req, rc);
2220 CERROR("unknown opcode %u\n",
2221 lustre_msg_get_opc(req->rq_reqmsg));
2222 ldlm_callback_reply(req, -EPROTO);
2226 ns = req->rq_export->exp_obd->obd_namespace;
2227 LASSERT(ns != NULL);
2229 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2231 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2232 if (dlm_req == NULL) {
2233 rc = ldlm_callback_reply(req, -EPROTO);
2234 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2239 /* Force a known safe race, send a cancel to the server for a lock
2240 * which the server has already started a blocking callback on. */
2241 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2242 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2243 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0], 0);
2245 CERROR("ldlm_cli_cancel: %d\n", rc);
2248 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2250 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2251 "disappeared\n", dlm_req->lock_handle[0].cookie);
2252 rc = ldlm_callback_reply(req, -EINVAL);
2253 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2254 &dlm_req->lock_handle[0]);
2258 if (ldlm_is_fail_loc(lock) &&
2259 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2260 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2262 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2263 lock_res_and_lock(lock);
2264 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
2266 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2267 /* If somebody cancels lock and cache is already dropped,
2268 * or lock is failed before cp_ast received on client,
2269 * we can tell the server we have no lock. Otherwise, we
2270 * should send cancel after dropping the cache. */
2271 if ((ldlm_is_canceling(lock) && ldlm_is_bl_done(lock)) ||
2272 ldlm_is_failed(lock)) {
2273 LDLM_DEBUG(lock, "callback on lock "
2274 LPX64" - lock disappeared\n",
2275 dlm_req->lock_handle[0].cookie);
2276 unlock_res_and_lock(lock);
2277 LDLM_LOCK_RELEASE(lock);
2278 rc = ldlm_callback_reply(req, -EINVAL);
2279 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2280 &dlm_req->lock_handle[0]);
2283 /* BL_AST locks are not needed in LRU.
2284 * Let ldlm_cancel_lru() be fast. */
2285 ldlm_lock_remove_from_lru(lock);
2286 ldlm_set_bl_ast(lock);
2288 unlock_res_and_lock(lock);
2290 /* We want the ost thread to get this reply so that it can respond
2291 * to ost requests (write cache writeback) that might be triggered
2294 * But we'd also like to be able to indicate in the reply that we're
2295 * cancelling right now, because it's unused, or have an intent result
2296 * in the reply, so we might have to push the responsibility for sending
2297 * the reply down into the AST handlers, alas. */
2299 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2300 case LDLM_BL_CALLBACK:
2301 CDEBUG(D_INODE, "blocking ast\n");
2302 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2303 if (!ldlm_is_cancel_on_block(lock)) {
2304 rc = ldlm_callback_reply(req, 0);
2305 if (req->rq_no_reply || rc)
2306 ldlm_callback_errmsg(req, "Normal process", rc,
2307 &dlm_req->lock_handle[0]);
2309 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2310 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2312 case LDLM_CP_CALLBACK:
2313 CDEBUG(D_INODE, "completion ast\n");
2314 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2315 ldlm_callback_reply(req, 0);
2316 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2318 case LDLM_GL_CALLBACK:
2319 CDEBUG(D_INODE, "glimpse ast\n");
2320 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2321 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2324 LBUG(); /* checked above */
2330 #ifdef HAVE_SERVER_SUPPORT
2332 * Main handler for canceld thread.
2334 * Separated into its own thread to avoid deadlocks.
2336 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2341 /* Requests arrive in sender's byte order. The ptlrpc service
2342 * handler has already checked and, if necessary, byte-swapped the
2343 * incoming request message body, but I am responsible for the
2344 * message buffers. */
2346 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2348 if (req->rq_export == NULL) {
2349 struct ldlm_request *dlm_req;
2351 CERROR("%s from %s arrived at %lu with bad export cookie "
2353 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2354 libcfs_nid2str(req->rq_peer.nid),
2355 req->rq_arrival_time.tv_sec,
2356 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2358 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2359 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2360 dlm_req = req_capsule_client_get(&req->rq_pill,
2362 if (dlm_req != NULL)
2363 ldlm_lock_dump_handle(D_ERROR,
2364 &dlm_req->lock_handle[0]);
2366 ldlm_callback_reply(req, -ENOTCONN);
2370 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2372 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2374 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2375 CDEBUG(D_INODE, "cancel\n");
2376 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET) ||
2377 CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))
2379 rc = ldlm_handle_cancel(req);
2384 CERROR("invalid opcode %d\n",
2385 lustre_msg_get_opc(req->rq_reqmsg));
2386 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2387 ldlm_callback_reply(req, -EINVAL);
2393 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2394 struct ldlm_lock *lock)
2396 struct ldlm_request *dlm_req;
2397 struct lustre_handle lockh;
2402 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2403 if (dlm_req == NULL)
2406 ldlm_lock2handle(lock, &lockh);
2407 for (i = 0; i < dlm_req->lock_count; i++) {
2408 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2410 DEBUG_REQ(D_RPCTRACE, req,
2411 "Prio raised by lock "LPX64".", lockh.cookie);
2422 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2424 struct ldlm_request *dlm_req;
2429 /* no prolong in recovery */
2430 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2433 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2434 if (dlm_req == NULL)
2437 for (i = 0; i < dlm_req->lock_count; i++) {
2438 struct ldlm_lock *lock;
2440 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2444 rc = ldlm_is_ast_sent(lock) ? 1 : 0;
2446 LDLM_DEBUG(lock, "hpreq cancel lock");
2447 LDLM_LOCK_PUT(lock);
2456 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2457 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2458 .hpreq_check = ldlm_cancel_hpreq_check,
2462 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2466 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2468 if (req->rq_export == NULL)
2471 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2472 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2473 req->rq_ops = &ldlm_cancel_hpreq_ops;
2478 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2479 struct hlist_node *hnode, void *data)
2482 struct list_head *rpc_list = data;
2483 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2485 lock_res_and_lock(lock);
2487 if (lock->l_req_mode != lock->l_granted_mode) {
2488 unlock_res_and_lock(lock);
2492 LASSERT(lock->l_resource);
2493 if (lock->l_resource->lr_type != LDLM_IBITS &&
2494 lock->l_resource->lr_type != LDLM_PLAIN) {
2495 unlock_res_and_lock(lock);
2499 if (ldlm_is_ast_sent(lock)) {
2500 unlock_res_and_lock(lock);
2504 LASSERT(lock->l_blocking_ast);
2505 LASSERT(!lock->l_blocking_lock);
2507 ldlm_set_ast_sent(lock);
2508 if (lock->l_export && lock->l_export->exp_lock_hash) {
2509 /* NB: it's safe to call cfs_hash_del() even lock isn't
2510 * in exp_lock_hash. */
2511 /* In the function below, .hs_keycmp resolves to
2512 * ldlm_export_lock_keycmp() */
2513 /* coverity[overrun-buffer-val] */
2514 cfs_hash_del(lock->l_export->exp_lock_hash,
2515 &lock->l_remote_handle, &lock->l_exp_hash);
2518 list_add_tail(&lock->l_rk_ast, rpc_list);
2519 LDLM_LOCK_GET(lock);
2521 unlock_res_and_lock(lock);
2525 void ldlm_revoke_export_locks(struct obd_export *exp)
2527 struct list_head rpc_list;
2530 INIT_LIST_HEAD(&rpc_list);
2531 cfs_hash_for_each_empty(exp->exp_lock_hash,
2532 ldlm_revoke_lock_cb, &rpc_list);
2533 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2534 LDLM_WORK_REVOKE_AST);
2538 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2539 #endif /* HAVE_SERVER_SUPPORT */
2541 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2543 struct ldlm_bl_work_item *blwi = NULL;
2544 static unsigned int num_bl = 0;
2546 spin_lock(&blp->blp_lock);
2547 /* process a request from the blp_list at least every blp_num_threads */
2548 if (!list_empty(&blp->blp_list) &&
2549 (list_empty(&blp->blp_prio_list) || num_bl == 0))
2550 blwi = list_entry(blp->blp_list.next,
2551 struct ldlm_bl_work_item, blwi_entry);
2553 if (!list_empty(&blp->blp_prio_list))
2554 blwi = list_entry(blp->blp_prio_list.next,
2555 struct ldlm_bl_work_item,
2559 if (++num_bl >= atomic_read(&blp->blp_num_threads))
2561 list_del(&blwi->blwi_entry);
2563 spin_unlock(&blp->blp_lock);
2568 /* This only contains temporary data until the thread starts */
2569 struct ldlm_bl_thread_data {
2570 char bltd_name[CFS_CURPROC_COMM_MAX];
2571 struct ldlm_bl_pool *bltd_blp;
2572 struct completion bltd_comp;
2576 static int ldlm_bl_thread_main(void *arg);
2578 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2580 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2581 struct task_struct *task;
2583 init_completion(&bltd.bltd_comp);
2584 bltd.bltd_num = atomic_read(&blp->blp_num_threads);
2585 snprintf(bltd.bltd_name, sizeof(bltd.bltd_name) - 1,
2586 "ldlm_bl_%02d", bltd.bltd_num);
2587 task = kthread_run(ldlm_bl_thread_main, &bltd, bltd.bltd_name);
2589 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %ld\n",
2590 atomic_read(&blp->blp_num_threads), PTR_ERR(task));
2591 return PTR_ERR(task);
2593 wait_for_completion(&bltd.bltd_comp);
2599 * Main blocking requests processing thread.
2601 * Callers put locks into its queue by calling ldlm_bl_to_thread.
2602 * This thread in the end ends up doing actual call to ->l_blocking_ast
2605 static int ldlm_bl_thread_main(void *arg)
2607 struct ldlm_bl_pool *blp;
2608 struct ldlm_bl_thread_data *bltd = arg;
2611 blp = bltd->bltd_blp;
2613 atomic_inc(&blp->blp_num_threads);
2614 atomic_inc(&blp->blp_busy_threads);
2616 complete(&bltd->bltd_comp);
2617 /* cannot use bltd after this, it is only on caller's stack */
2620 struct l_wait_info lwi = { 0 };
2621 struct ldlm_bl_work_item *blwi = NULL;
2624 blwi = ldlm_bl_get_work(blp);
2627 atomic_dec(&blp->blp_busy_threads);
2628 l_wait_event_exclusive(blp->blp_waitq,
2629 (blwi = ldlm_bl_get_work(blp)) != NULL,
2631 busy = atomic_inc_return(&blp->blp_busy_threads);
2633 busy = atomic_read(&blp->blp_busy_threads);
2636 if (blwi->blwi_ns == NULL)
2637 /* added by ldlm_cleanup() */
2640 /* Not fatal if racy and have a few too many threads */
2641 if (unlikely(busy < blp->blp_max_threads &&
2642 busy >= atomic_read(&blp->blp_num_threads) &&
2643 !blwi->blwi_mem_pressure))
2644 /* discard the return value, we tried */
2645 ldlm_bl_thread_start(blp);
2647 if (blwi->blwi_mem_pressure)
2648 memory_pressure_set();
2650 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL2, 4);
2652 if (blwi->blwi_count) {
2654 /* The special case when we cancel locks in LRU
2655 * asynchronously, we pass the list of locks here.
2656 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2657 * canceled locally yet. */
2658 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2661 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL,
2664 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2667 if (blwi->blwi_mem_pressure)
2668 memory_pressure_clr();
2670 if (blwi->blwi_flags & LCF_ASYNC)
2671 OBD_FREE(blwi, sizeof(*blwi));
2673 complete(&blwi->blwi_comp);
2676 atomic_dec(&blp->blp_busy_threads);
2677 atomic_dec(&blp->blp_num_threads);
2678 complete(&blp->blp_comp);
2683 static int ldlm_setup(void);
2684 static int ldlm_cleanup(void);
2686 int ldlm_get_ref(void)
2690 mutex_lock(&ldlm_ref_mutex);
2691 if (++ldlm_refcount == 1) {
2696 mutex_unlock(&ldlm_ref_mutex);
2700 EXPORT_SYMBOL(ldlm_get_ref);
2702 void ldlm_put_ref(void)
2705 mutex_lock(&ldlm_ref_mutex);
2706 if (ldlm_refcount == 1) {
2707 int rc = ldlm_cleanup();
2709 CERROR("ldlm_cleanup failed: %d\n", rc);
2715 mutex_unlock(&ldlm_ref_mutex);
2719 EXPORT_SYMBOL(ldlm_put_ref);
2722 * Export handle<->lock hash operations.
2725 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2727 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2731 ldlm_export_lock_key(struct hlist_node *hnode)
2733 struct ldlm_lock *lock;
2735 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2736 return &lock->l_remote_handle;
2740 ldlm_export_lock_keycpy(struct hlist_node *hnode, void *key)
2742 struct ldlm_lock *lock;
2744 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2745 lock->l_remote_handle = *(struct lustre_handle *)key;
2749 ldlm_export_lock_keycmp(const void *key, struct hlist_node *hnode)
2751 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2755 ldlm_export_lock_object(struct hlist_node *hnode)
2757 return hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2761 ldlm_export_lock_get(cfs_hash_t *hs, struct hlist_node *hnode)
2763 struct ldlm_lock *lock;
2765 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2766 LDLM_LOCK_GET(lock);
2770 ldlm_export_lock_put(cfs_hash_t *hs, struct hlist_node *hnode)
2772 struct ldlm_lock *lock;
2774 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2775 LDLM_LOCK_RELEASE(lock);
2778 static cfs_hash_ops_t ldlm_export_lock_ops = {
2779 .hs_hash = ldlm_export_lock_hash,
2780 .hs_key = ldlm_export_lock_key,
2781 .hs_keycmp = ldlm_export_lock_keycmp,
2782 .hs_keycpy = ldlm_export_lock_keycpy,
2783 .hs_object = ldlm_export_lock_object,
2784 .hs_get = ldlm_export_lock_get,
2785 .hs_put = ldlm_export_lock_put,
2786 .hs_put_locked = ldlm_export_lock_put,
2789 int ldlm_init_export(struct obd_export *exp)
2794 exp->exp_lock_hash =
2795 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2796 HASH_EXP_LOCK_CUR_BITS,
2797 HASH_EXP_LOCK_MAX_BITS,
2798 HASH_EXP_LOCK_BKT_BITS, 0,
2799 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2800 &ldlm_export_lock_ops,
2801 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2802 CFS_HASH_NBLK_CHANGE);
2804 if (!exp->exp_lock_hash)
2807 rc = ldlm_init_flock_export(exp);
2813 ldlm_destroy_export(exp);
2816 EXPORT_SYMBOL(ldlm_init_export);
2818 void ldlm_destroy_export(struct obd_export *exp)
2821 cfs_hash_putref(exp->exp_lock_hash);
2822 exp->exp_lock_hash = NULL;
2824 ldlm_destroy_flock_export(exp);
2827 EXPORT_SYMBOL(ldlm_destroy_export);
2829 static int ldlm_setup(void)
2831 static struct ptlrpc_service_conf conf;
2832 struct ldlm_bl_pool *blp = NULL;
2833 #ifdef HAVE_SERVER_SUPPORT
2834 struct task_struct *task;
2835 #endif /* HAVE_SERVER_SUPPORT */
2841 if (ldlm_state != NULL)
2844 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2845 if (ldlm_state == NULL)
2849 rc = ldlm_proc_setup();
2852 #endif /* LPROCFS */
2854 memset(&conf, 0, sizeof(conf));
2855 conf = (typeof(conf)) {
2856 .psc_name = "ldlm_cbd",
2857 .psc_watchdog_factor = 2,
2859 .bc_nbufs = LDLM_CLIENT_NBUFS,
2860 .bc_buf_size = LDLM_BUFSIZE,
2861 .bc_req_max_size = LDLM_MAXREQSIZE,
2862 .bc_rep_max_size = LDLM_MAXREPSIZE,
2863 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2864 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2867 .tc_thr_name = "ldlm_cb",
2868 .tc_thr_factor = LDLM_THR_FACTOR,
2869 .tc_nthrs_init = LDLM_NTHRS_INIT,
2870 .tc_nthrs_base = LDLM_NTHRS_BASE,
2871 .tc_nthrs_max = LDLM_NTHRS_MAX,
2872 .tc_nthrs_user = ldlm_num_threads,
2873 .tc_cpu_affinity = 1,
2874 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2877 .cc_pattern = ldlm_cpts,
2880 .so_req_handler = ldlm_callback_handler,
2883 ldlm_state->ldlm_cb_service = \
2884 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2885 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2886 CERROR("failed to start service\n");
2887 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2888 ldlm_state->ldlm_cb_service = NULL;
2892 #ifdef HAVE_SERVER_SUPPORT
2893 memset(&conf, 0, sizeof(conf));
2894 conf = (typeof(conf)) {
2895 .psc_name = "ldlm_canceld",
2896 .psc_watchdog_factor = 6,
2898 .bc_nbufs = LDLM_SERVER_NBUFS,
2899 .bc_buf_size = LDLM_BUFSIZE,
2900 .bc_req_max_size = LDLM_MAXREQSIZE,
2901 .bc_rep_max_size = LDLM_MAXREPSIZE,
2902 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2903 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2907 .tc_thr_name = "ldlm_cn",
2908 .tc_thr_factor = LDLM_THR_FACTOR,
2909 .tc_nthrs_init = LDLM_NTHRS_INIT,
2910 .tc_nthrs_base = LDLM_NTHRS_BASE,
2911 .tc_nthrs_max = LDLM_NTHRS_MAX,
2912 .tc_nthrs_user = ldlm_num_threads,
2913 .tc_cpu_affinity = 1,
2914 .tc_ctx_tags = LCT_MD_THREAD | \
2919 .cc_pattern = ldlm_cpts,
2922 .so_req_handler = ldlm_cancel_handler,
2923 .so_hpreq_handler = ldlm_hpreq_handler,
2926 ldlm_state->ldlm_cancel_service = \
2927 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2928 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2929 CERROR("failed to start service\n");
2930 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2931 ldlm_state->ldlm_cancel_service = NULL;
2934 #endif /* HAVE_SERVER_SUPPORT */
2936 OBD_ALLOC(blp, sizeof(*blp));
2938 GOTO(out, rc = -ENOMEM);
2939 ldlm_state->ldlm_bl_pool = blp;
2941 spin_lock_init(&blp->blp_lock);
2942 INIT_LIST_HEAD(&blp->blp_list);
2943 INIT_LIST_HEAD(&blp->blp_prio_list);
2944 init_waitqueue_head(&blp->blp_waitq);
2945 atomic_set(&blp->blp_num_threads, 0);
2946 atomic_set(&blp->blp_busy_threads, 0);
2948 if (ldlm_num_threads == 0) {
2949 blp->blp_min_threads = LDLM_NTHRS_INIT;
2950 blp->blp_max_threads = LDLM_NTHRS_MAX;
2952 blp->blp_min_threads = blp->blp_max_threads = \
2953 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2957 for (i = 0; i < blp->blp_min_threads; i++) {
2958 rc = ldlm_bl_thread_start(blp);
2963 #ifdef HAVE_SERVER_SUPPORT
2964 INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2965 expired_lock_thread.elt_state = ELT_STOPPED;
2966 init_waitqueue_head(&expired_lock_thread.elt_waitq);
2968 INIT_LIST_HEAD(&waiting_locks_list);
2969 spin_lock_init(&waiting_locks_spinlock);
2970 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2972 task = kthread_run(expired_lock_main, NULL, "ldlm_elt");
2975 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2979 wait_event(expired_lock_thread.elt_waitq,
2980 expired_lock_thread.elt_state == ELT_READY);
2981 #endif /* HAVE_SERVER_SUPPORT */
2983 rc = ldlm_pools_init();
2985 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2995 static int ldlm_cleanup(void)
2999 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
3000 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
3001 CERROR("ldlm still has namespaces; clean these up first.\n");
3002 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
3003 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
3009 if (ldlm_state->ldlm_bl_pool != NULL) {
3010 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
3012 while (atomic_read(&blp->blp_num_threads) > 0) {
3013 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
3015 init_completion(&blp->blp_comp);
3017 spin_lock(&blp->blp_lock);
3018 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
3019 wake_up(&blp->blp_waitq);
3020 spin_unlock(&blp->blp_lock);
3022 wait_for_completion(&blp->blp_comp);
3025 OBD_FREE(blp, sizeof(*blp));
3028 if (ldlm_state->ldlm_cb_service != NULL)
3029 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
3030 #ifdef HAVE_SERVER_SUPPORT
3031 if (ldlm_state->ldlm_cancel_service != NULL)
3032 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
3035 ldlm_proc_cleanup();
3037 #ifdef HAVE_SERVER_SUPPORT
3038 if (expired_lock_thread.elt_state != ELT_STOPPED) {
3039 expired_lock_thread.elt_state = ELT_TERMINATE;
3040 wake_up(&expired_lock_thread.elt_waitq);
3041 wait_event(expired_lock_thread.elt_waitq,
3042 expired_lock_thread.elt_state == ELT_STOPPED);
3046 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
3054 mutex_init(&ldlm_ref_mutex);
3055 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
3056 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
3058 INIT_LIST_HEAD(&ldlm_srv_namespace_list);
3059 INIT_LIST_HEAD(&ldlm_cli_active_namespace_list);
3060 INIT_LIST_HEAD(&ldlm_cli_inactive_namespace_list);
3062 ldlm_resource_slab = kmem_cache_create("ldlm_resources",
3063 sizeof(struct ldlm_resource), 0,
3064 SLAB_HWCACHE_ALIGN, NULL);
3065 if (ldlm_resource_slab == NULL)
3068 ldlm_lock_slab = kmem_cache_create("ldlm_locks",
3069 sizeof(struct ldlm_lock), 0,
3070 SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU, NULL);
3071 if (ldlm_lock_slab == NULL) {
3072 kmem_cache_destroy(ldlm_resource_slab);
3076 ldlm_interval_slab = kmem_cache_create("interval_node",
3077 sizeof(struct ldlm_interval),
3078 0, SLAB_HWCACHE_ALIGN, NULL);
3079 if (ldlm_interval_slab == NULL) {
3080 kmem_cache_destroy(ldlm_resource_slab);
3081 kmem_cache_destroy(ldlm_lock_slab);
3084 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3085 class_export_dump_hook = ldlm_dump_export_locks;
3090 void ldlm_exit(void)
3093 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
3094 kmem_cache_destroy(ldlm_resource_slab);
3095 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
3096 * synchronize_rcu() to wait a grace period elapsed, so that
3097 * ldlm_lock_free() get a chance to be called. */
3099 kmem_cache_destroy(ldlm_lock_slab);
3100 kmem_cache_destroy(ldlm_interval_slab);