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
45 # include <libcfs/libcfs.h>
47 # include <liblustre.h>
50 #include <lustre_dlm.h>
51 #include <obd_class.h>
52 #include <libcfs/list.h>
53 #include "ldlm_internal.h"
55 static int ldlm_num_threads;
56 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
57 "number of DLM service threads to start");
59 static char *ldlm_cpts;
60 CFS_MODULE_PARM(ldlm_cpts, "s", charp, 0444,
61 "CPU partitions ldlm threads should run on");
63 extern struct kmem_cache *ldlm_resource_slab;
64 extern struct kmem_cache *ldlm_lock_slab;
65 static struct mutex ldlm_ref_mutex;
66 static int ldlm_refcount;
68 struct ldlm_cb_async_args {
69 struct ldlm_cb_set_arg *ca_set_arg;
70 struct ldlm_lock *ca_lock;
75 static struct ldlm_state *ldlm_state;
77 inline cfs_time_t round_timeout(cfs_time_t timeout)
79 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
82 /* timeout for initial callback (AST) reply (bz10399) */
83 static inline unsigned int ldlm_get_rq_timeout(void)
86 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
88 return timeout < 1 ? 1 : timeout;
93 #define ELT_TERMINATE 2
99 * blp_prio_list is used for callbacks that should be handled
100 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
103 struct list_head blp_prio_list;
106 * blp_list is used for all other callbacks which are likely
107 * to take longer to process.
109 struct list_head blp_list;
111 wait_queue_head_t blp_waitq;
112 struct completion blp_comp;
113 atomic_t blp_num_threads;
114 atomic_t blp_busy_threads;
119 struct ldlm_bl_work_item {
120 struct list_head blwi_entry;
121 struct ldlm_namespace *blwi_ns;
122 struct ldlm_lock_desc blwi_ld;
123 struct ldlm_lock *blwi_lock;
124 struct list_head blwi_head;
126 struct completion blwi_comp;
127 ldlm_cancel_flags_t blwi_flags;
128 int blwi_mem_pressure;
131 #if defined(HAVE_SERVER_SUPPORT) && defined(__KERNEL__)
134 * Protects both waiting_locks_list and expired_lock_thread.
136 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
139 * List for contended locks.
141 * As soon as a lock is contended, it gets placed on this list and
142 * expected time to get a response is filled in the lock. A special
143 * thread walks the list looking for locks that should be released and
144 * schedules client evictions for those that have not been released in
147 * All access to it should be under waiting_locks_spinlock.
149 static struct list_head waiting_locks_list;
150 static struct timer_list waiting_locks_timer;
152 static struct expired_lock_thread {
153 wait_queue_head_t elt_waitq;
156 struct list_head elt_expired_locks;
157 } expired_lock_thread;
159 static inline int have_expired_locks(void)
164 spin_lock_bh(&waiting_locks_spinlock);
165 need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
166 spin_unlock_bh(&waiting_locks_spinlock);
172 * Check expired lock list for expired locks and time them out.
174 static int expired_lock_main(void *arg)
176 struct list_head *expired = &expired_lock_thread.elt_expired_locks;
177 struct l_wait_info lwi = { 0 };
182 expired_lock_thread.elt_state = ELT_READY;
183 wake_up(&expired_lock_thread.elt_waitq);
186 l_wait_event(expired_lock_thread.elt_waitq,
187 have_expired_locks() ||
188 expired_lock_thread.elt_state == ELT_TERMINATE,
191 spin_lock_bh(&waiting_locks_spinlock);
192 if (expired_lock_thread.elt_dump) {
193 struct libcfs_debug_msg_data msgdata = {
194 .msg_file = __FILE__,
195 .msg_fn = "waiting_locks_callback",
196 .msg_line = expired_lock_thread.elt_dump };
197 spin_unlock_bh(&waiting_locks_spinlock);
199 /* from waiting_locks_callback, but not in timer */
200 libcfs_debug_dumplog();
201 libcfs_run_lbug_upcall(&msgdata);
203 spin_lock_bh(&waiting_locks_spinlock);
204 expired_lock_thread.elt_dump = 0;
209 while (!list_empty(expired)) {
210 struct obd_export *export;
211 struct ldlm_lock *lock;
213 lock = list_entry(expired->next, struct ldlm_lock,
215 if ((void *)lock < LP_POISON + PAGE_CACHE_SIZE &&
216 (void *)lock >= LP_POISON) {
217 spin_unlock_bh(&waiting_locks_spinlock);
218 CERROR("free lock on elt list %p\n", lock);
221 list_del_init(&lock->l_pending_chain);
222 if ((void *)lock->l_export <
223 LP_POISON + PAGE_CACHE_SIZE &&
224 (void *)lock->l_export >= LP_POISON) {
225 CERROR("lock with free export on elt list %p\n",
227 lock->l_export = NULL;
228 LDLM_ERROR(lock, "free export");
229 /* release extra ref grabbed by
230 * ldlm_add_waiting_lock() or
231 * ldlm_failed_ast() */
232 LDLM_LOCK_RELEASE(lock);
236 if (ldlm_is_destroyed(lock)) {
237 /* release the lock refcount where
238 * waiting_locks_callback() founds */
239 LDLM_LOCK_RELEASE(lock);
242 export = class_export_lock_get(lock->l_export, lock);
243 spin_unlock_bh(&waiting_locks_spinlock);
246 class_fail_export(export);
247 class_export_lock_put(export, lock);
249 /* release extra ref grabbed by ldlm_add_waiting_lock()
250 * or ldlm_failed_ast() */
251 LDLM_LOCK_RELEASE(lock);
253 spin_lock_bh(&waiting_locks_spinlock);
255 spin_unlock_bh(&waiting_locks_spinlock);
257 if (do_dump && obd_dump_on_eviction) {
258 CERROR("dump the log upon eviction\n");
259 libcfs_debug_dumplog();
262 if (expired_lock_thread.elt_state == ELT_TERMINATE)
266 expired_lock_thread.elt_state = ELT_STOPPED;
267 wake_up(&expired_lock_thread.elt_waitq);
271 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
272 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
275 * Check if there is a request in the export request list
276 * which prevents the lock canceling.
278 static int ldlm_lock_busy(struct ldlm_lock *lock)
280 struct ptlrpc_request *req;
284 if (lock->l_export == NULL)
287 spin_lock_bh(&lock->l_export->exp_rpc_lock);
288 list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
290 if (req->rq_ops->hpreq_lock_match) {
291 match = req->rq_ops->hpreq_lock_match(req, lock);
296 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
300 /* This is called from within a timer interrupt and cannot schedule */
301 static void waiting_locks_callback(unsigned long unused)
303 struct ldlm_lock *lock;
306 spin_lock_bh(&waiting_locks_spinlock);
307 while (!list_empty(&waiting_locks_list)) {
308 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
310 if (cfs_time_after(lock->l_callback_timeout,
311 cfs_time_current()) ||
312 (lock->l_req_mode == LCK_GROUP))
315 /* Check if we need to prolong timeout */
316 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
317 ldlm_lock_busy(lock)) {
320 if (lock->l_pending_chain.next == &waiting_locks_list)
325 spin_unlock_bh(&waiting_locks_spinlock);
326 LDLM_DEBUG(lock, "prolong the busy lock");
327 ldlm_refresh_waiting_lock(lock,
328 ldlm_get_enq_timeout(lock));
329 spin_lock_bh(&waiting_locks_spinlock);
332 LDLM_LOCK_RELEASE(lock);
336 LDLM_LOCK_RELEASE(lock);
339 ldlm_lock_to_ns(lock)->ns_timeouts++;
340 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
341 "evicting client at %s ",
342 cfs_time_current_sec() - lock->l_last_activity,
344 lock->l_export->exp_connection->c_peer.nid));
346 /* no needs to take an extra ref on the lock since it was in
347 * the waiting_locks_list and ldlm_add_waiting_lock()
348 * already grabbed a ref */
349 list_del(&lock->l_pending_chain);
350 list_add(&lock->l_pending_chain,
351 &expired_lock_thread.elt_expired_locks);
355 if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
356 if (obd_dump_on_timeout && need_dump)
357 expired_lock_thread.elt_dump = __LINE__;
359 wake_up(&expired_lock_thread.elt_waitq);
363 * Make sure the timer will fire again if we have any locks
366 if (!list_empty(&waiting_locks_list)) {
367 cfs_time_t timeout_rounded;
368 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
370 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
371 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
373 spin_unlock_bh(&waiting_locks_spinlock);
377 * Add lock to the list of contended locks.
379 * Indicate that we're waiting for a client to call us back cancelling a given
380 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
381 * timer to fire appropriately. (We round up to the next second, to avoid
382 * floods of timer firings during periods of high lock contention and traffic).
383 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
384 * if it has been added to the waiting list (1 is returned).
386 * Called with the namespace lock held.
388 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
391 cfs_time_t timeout_rounded;
393 if (!list_empty(&lock->l_pending_chain))
396 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
397 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
400 timeout = cfs_time_shift(seconds);
401 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
402 lock->l_callback_timeout = timeout;
404 timeout_rounded = round_timeout(lock->l_callback_timeout);
406 if (cfs_time_before(timeout_rounded,
407 cfs_timer_deadline(&waiting_locks_timer)) ||
408 !cfs_timer_is_armed(&waiting_locks_timer)) {
409 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
411 /* if the new lock has a shorter timeout than something earlier on
412 the list, we'll wait the longer amount of time; no big deal. */
414 list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
418 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
421 int timeout = ldlm_get_enq_timeout(lock);
423 /* NB: must be called with hold of lock_res_and_lock() */
424 LASSERT(ldlm_is_res_locked(lock));
425 ldlm_set_waited(lock);
427 LASSERT(!ldlm_is_cancel_on_block(lock));
429 spin_lock_bh(&waiting_locks_spinlock);
430 if (ldlm_is_destroyed(lock)) {
431 static cfs_time_t next;
432 spin_unlock_bh(&waiting_locks_spinlock);
433 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
434 if (cfs_time_after(cfs_time_current(), next)) {
435 next = cfs_time_shift(14400);
436 libcfs_debug_dumpstack(NULL);
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 || !__KERNEL__ */
562 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
567 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
572 # ifdef HAVE_SERVER_SUPPORT
573 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
575 LASSERT(ldlm_is_res_locked(lock) && !ldlm_is_cancel_on_block(lock));
580 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
582 #ifdef HAVE_SERVER_SUPPORT
585 * Perform lock cleanup if AST sending failed.
587 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
588 const char *ast_type)
590 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
591 "to a lock %s callback time out: rc %d\n",
592 lock->l_export->exp_obd->obd_name,
593 obd_export_nid2str(lock->l_export), ast_type, rc);
595 if (obd_dump_on_timeout)
596 libcfs_debug_dumplog();
598 spin_lock_bh(&waiting_locks_spinlock);
599 if (__ldlm_del_waiting_lock(lock) == 0)
600 /* the lock was not in any list, grab an extra ref before adding
601 * the lock to the expired list */
603 list_add(&lock->l_pending_chain,
604 &expired_lock_thread.elt_expired_locks);
605 wake_up(&expired_lock_thread.elt_waitq);
606 spin_unlock_bh(&waiting_locks_spinlock);
608 class_fail_export(lock->l_export);
613 * Perform lock cleanup if AST reply came with error.
615 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
616 struct ptlrpc_request *req, int rc,
617 const char *ast_type)
619 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
621 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
622 LASSERT(lock->l_export);
623 if (lock->l_export->exp_libclient) {
624 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
625 " timeout, just cancelling lock", ast_type,
626 libcfs_nid2str(peer.nid));
627 ldlm_lock_cancel(lock);
629 } else if (ldlm_is_cancel(lock)) {
630 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
631 "cancel was received (AST reply lost?)",
632 ast_type, libcfs_nid2str(peer.nid));
633 ldlm_lock_cancel(lock);
636 ldlm_del_waiting_lock(lock);
637 ldlm_failed_ast(lock, rc, ast_type);
641 struct ldlm_resource *res = lock->l_resource;
642 LDLM_DEBUG(lock, "client (nid %s) returned %d"
643 " from %s AST - normal race",
644 libcfs_nid2str(peer.nid),
646 lustre_msg_get_status(req->rq_repmsg) : -1,
649 /* update lvbo to return proper attributes.
651 ldlm_resource_getref(res);
652 ldlm_res_lvbo_update(res, NULL, 1);
653 ldlm_resource_putref(res);
657 LDLM_ERROR(lock, "client (nid %s) returned %d "
658 "from %s AST", libcfs_nid2str(peer.nid),
659 (req->rq_repmsg != NULL) ?
660 lustre_msg_get_status(req->rq_repmsg) : 0,
663 ldlm_lock_cancel(lock);
664 /* Server-side AST functions are called from ldlm_reprocess_all,
665 * which needs to be told to please restart its reprocessing. */
672 static int ldlm_cb_interpret(const struct lu_env *env,
673 struct ptlrpc_request *req, void *data, int rc)
675 struct ldlm_cb_async_args *ca = data;
676 struct ldlm_lock *lock = ca->ca_lock;
677 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
680 LASSERT(lock != NULL);
683 case LDLM_GL_CALLBACK:
684 /* Update the LVB from disk if the AST failed
685 * (this is a legal race)
687 * - Glimpse callback of local lock just returns
688 * -ELDLM_NO_LOCK_DATA.
689 * - Glimpse callback of remote lock might return
690 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
692 if (rc == -ELDLM_NO_LOCK_DATA) {
693 LDLM_DEBUG(lock, "lost race - client has a lock but no "
695 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
696 } else if (rc != 0) {
697 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
699 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
702 case LDLM_BL_CALLBACK:
704 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
706 case LDLM_CP_CALLBACK:
708 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
711 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
716 /* release extra reference taken in ldlm_ast_fini() */
717 LDLM_LOCK_RELEASE(lock);
720 atomic_inc(&arg->restart);
725 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
726 struct ldlm_cb_set_arg *arg,
727 struct ldlm_lock *lock,
733 if (unlikely(instant_cancel)) {
734 rc = ptl_send_rpc(req, 1);
735 ptlrpc_req_finished(req);
737 atomic_inc(&arg->restart);
740 ptlrpc_set_add_req(arg->set, req);
747 * Check if there are requests in the export request list which prevent
748 * the lock canceling and make these requests high priority ones.
750 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
752 struct ptlrpc_request *req;
755 if (lock->l_export == NULL) {
756 LDLM_DEBUG(lock, "client lock: no-op");
760 spin_lock_bh(&lock->l_export->exp_rpc_lock);
761 list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
763 /* Do not process requests that were not yet added to there
764 * incoming queue or were already removed from there for
765 * processing. We evaluate ptlrpc_nrs_req_can_move() without
766 * holding svcpt->scp_req_lock, and then redo the check with
767 * the lock held once we need to obtain a reliable result.
769 if (ptlrpc_nrs_req_can_move(req) &&
770 req->rq_ops->hpreq_lock_match &&
771 req->rq_ops->hpreq_lock_match(req, lock))
772 ptlrpc_nrs_req_hp_move(req);
774 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
779 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
780 * enqueued server lock conflicts with given one.
782 * Sends blocking AST RPC to the client owning that lock; arms timeout timer
783 * to wait for client response.
785 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
786 struct ldlm_lock_desc *desc,
787 void *data, int flag)
789 struct ldlm_cb_async_args *ca;
790 struct ldlm_cb_set_arg *arg = data;
791 struct ldlm_request *body;
792 struct ptlrpc_request *req;
793 int instant_cancel = 0;
797 if (flag == LDLM_CB_CANCELING)
798 /* Don't need to do anything here. */
802 LASSERT(data != NULL);
803 if (lock->l_export->exp_obd->obd_recovering != 0)
804 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
806 ldlm_lock_reorder_req(lock);
808 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
809 &RQF_LDLM_BL_CALLBACK,
810 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
814 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
815 ca = ptlrpc_req_async_args(req);
816 ca->ca_set_arg = arg;
819 req->rq_interpret_reply = ldlm_cb_interpret;
820 req->rq_no_resend = 1;
822 lock_res_and_lock(lock);
823 if (lock->l_granted_mode != lock->l_req_mode) {
824 /* this blocking AST will be communicated as part of the
825 * completion AST instead */
826 unlock_res_and_lock(lock);
828 ptlrpc_req_finished(req);
829 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
833 if (ldlm_is_destroyed(lock)) {
834 /* What's the point? */
835 unlock_res_and_lock(lock);
836 ptlrpc_req_finished(req);
840 if (ldlm_is_cancel_on_block(lock))
843 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
844 body->lock_handle[0] = lock->l_remote_handle;
845 body->lock_desc = *desc;
846 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags & LDLM_FL_AST_MASK);
848 LDLM_DEBUG(lock, "server preparing blocking AST");
850 ptlrpc_request_set_replen(req);
851 if (instant_cancel) {
852 unlock_res_and_lock(lock);
853 ldlm_lock_cancel(lock);
855 LASSERT(lock->l_granted_mode == lock->l_req_mode);
856 ldlm_add_waiting_lock(lock);
857 unlock_res_and_lock(lock);
860 req->rq_send_state = LUSTRE_IMP_FULL;
861 /* ptlrpc_request_alloc_pack already set timeout */
863 req->rq_timeout = ldlm_get_rq_timeout();
865 lock->l_last_activity = cfs_time_current_sec();
867 if (lock->l_export && lock->l_export->exp_nid_stats &&
868 lock->l_export->exp_nid_stats->nid_ldlm_stats)
869 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
870 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
872 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
876 EXPORT_SYMBOL(ldlm_server_blocking_ast);
879 * ->l_completion_ast callback for a remote lock in server namespace.
881 * Sends AST to the client notifying it of lock granting. If initial
882 * lock response was not sent yet, instead of sending another RPC, just
883 * mark the lock as granted and client will understand
885 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
887 struct ldlm_cb_set_arg *arg = data;
888 struct ldlm_request *body;
889 struct ptlrpc_request *req;
890 struct ldlm_cb_async_args *ca;
891 long total_enqueue_wait;
892 int instant_cancel = 0;
897 LASSERT(lock != NULL);
898 LASSERT(data != NULL);
900 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
901 lock->l_last_activity);
903 if (OBD_FAIL_PRECHECK(OBD_FAIL_OST_LDLM_REPLY_NET)) {
904 LDLM_DEBUG(lock, "dropping CP AST");
908 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
909 &RQF_LDLM_CP_CALLBACK);
913 /* server namespace, doesn't need lock */
914 lvb_len = ldlm_lvbo_size(lock);
915 /* LU-3124 & LU-2187: to not return layout in completion AST because
916 * it may deadlock for LU-2187, or client may not have enough space
917 * for large layout. The layout will be returned to client with an
918 * extra RPC to fetch xattr.lov */
919 if (ldlm_has_layout(lock))
922 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT, lvb_len);
923 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
925 ptlrpc_request_free(req);
929 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
930 ca = ptlrpc_req_async_args(req);
931 ca->ca_set_arg = arg;
934 req->rq_interpret_reply = ldlm_cb_interpret;
935 req->rq_no_resend = 1;
936 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
938 body->lock_handle[0] = lock->l_remote_handle;
939 body->lock_flags = ldlm_flags_to_wire(flags);
940 ldlm_lock2desc(lock, &body->lock_desc);
942 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
944 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
946 /* We still need to send the RPC to wake up the blocked
947 * enqueue thread on the client.
949 * Consider old client, there is no better way to notify
950 * the failure, just zero-sized the LVB, then the client
951 * will fail out as "-EPROTO". */
952 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, 0,
956 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len,
961 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
964 lock->l_last_activity = cfs_time_current_sec();
966 /* Server-side enqueue wait time estimate, used in
967 __ldlm_add_waiting_lock to set future enqueue timers */
968 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
969 at_measured(ldlm_lock_to_ns_at(lock),
972 /* bz18618. Don't add lock enqueue time we spend waiting for a
973 previous callback to fail. Locks waiting legitimately will
974 get extended by ldlm_refresh_waiting_lock regardless of the
975 estimate, so it's okay to underestimate here. */
976 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
977 "It is likely that a previous callback timed out.",
979 at_get(ldlm_lock_to_ns_at(lock)));
981 ptlrpc_request_set_replen(req);
983 req->rq_send_state = LUSTRE_IMP_FULL;
984 /* ptlrpc_request_pack already set timeout */
986 req->rq_timeout = ldlm_get_rq_timeout();
988 /* We only send real blocking ASTs after the lock is granted */
989 lock_res_and_lock(lock);
990 if (ldlm_is_ast_sent(lock)) {
991 body->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
992 /* Copy AST flags like LDLM_FL_DISCARD_DATA. */
993 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags &
996 /* We might get here prior to ldlm_handle_enqueue setting
997 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
998 * into waiting list, but this is safe and similar code in
999 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
1000 * that would not only cancel the lock, but will also remove
1001 * it from waiting list */
1002 if (ldlm_is_cancel_on_block(lock)) {
1003 unlock_res_and_lock(lock);
1004 ldlm_lock_cancel(lock);
1006 lock_res_and_lock(lock);
1008 /* start the lock-timeout clock */
1009 ldlm_add_waiting_lock(lock);
1012 unlock_res_and_lock(lock);
1014 if (lock->l_export && lock->l_export->exp_nid_stats &&
1015 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1016 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1017 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1019 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1021 RETURN(lvb_len < 0 ? lvb_len : rc);
1023 EXPORT_SYMBOL(ldlm_server_completion_ast);
1026 * Server side ->l_glimpse_ast handler for client locks.
1028 * Sends glimpse AST to the client and waits for reply. Then updates
1029 * lvbo with the result.
1031 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1033 struct ldlm_cb_set_arg *arg = data;
1034 struct ldlm_request *body;
1035 struct ptlrpc_request *req;
1036 struct ldlm_cb_async_args *ca;
1038 struct req_format *req_fmt;
1041 LASSERT(lock != NULL);
1043 if (arg->gl_desc != NULL)
1044 /* There is a glimpse descriptor to pack */
1045 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1047 req_fmt = &RQF_LDLM_GL_CALLBACK;
1049 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1050 req_fmt, LUSTRE_DLM_VERSION,
1056 if (arg->gl_desc != NULL) {
1057 /* copy the GL descriptor */
1058 union ldlm_gl_desc *desc;
1059 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1060 *desc = *arg->gl_desc;
1063 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1064 body->lock_handle[0] = lock->l_remote_handle;
1065 ldlm_lock2desc(lock, &body->lock_desc);
1067 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1068 ca = ptlrpc_req_async_args(req);
1069 ca->ca_set_arg = arg;
1072 /* server namespace, doesn't need lock */
1073 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1074 ldlm_lvbo_size(lock));
1075 ptlrpc_request_set_replen(req);
1077 req->rq_send_state = LUSTRE_IMP_FULL;
1078 /* ptlrpc_request_alloc_pack already set timeout */
1080 req->rq_timeout = ldlm_get_rq_timeout();
1082 lock->l_last_activity = cfs_time_current_sec();
1084 req->rq_interpret_reply = ldlm_cb_interpret;
1086 if (lock->l_export && lock->l_export->exp_nid_stats &&
1087 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1088 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1089 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1091 rc = ldlm_ast_fini(req, arg, lock, 0);
1095 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1097 int ldlm_glimpse_locks(struct ldlm_resource *res,
1098 struct list_head *gl_work_list)
1103 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1105 if (rc == -ERESTART)
1106 ldlm_reprocess_all(res);
1110 EXPORT_SYMBOL(ldlm_glimpse_locks);
1112 /* return LDLM lock associated with a lock callback request */
1113 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req)
1115 struct ldlm_cb_async_args *ca;
1116 struct ldlm_lock *lock;
1119 ca = ptlrpc_req_async_args(req);
1122 RETURN(ERR_PTR(-EFAULT));
1126 EXPORT_SYMBOL(ldlm_request_lock);
1128 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1129 struct lprocfs_stats *srv_stats)
1131 int lock_type = 0, op = 0;
1133 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1135 switch (lock_type) {
1137 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1140 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1141 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1143 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1146 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1149 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1157 lprocfs_counter_incr(srv_stats, op);
1163 * Main server-side entry point into LDLM for enqueue. This is called by ptlrpc
1164 * service threads to carry out client lock enqueueing requests.
1166 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1167 struct ptlrpc_request *req,
1168 const struct ldlm_request *dlm_req,
1169 const struct ldlm_callback_suite *cbs)
1171 struct ldlm_reply *dlm_rep;
1173 ldlm_error_t err = ELDLM_OK;
1174 struct ldlm_lock *lock = NULL;
1175 void *cookie = NULL;
1177 struct ldlm_resource *res = NULL;
1180 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1182 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1183 flags = ldlm_flags_from_wire(dlm_req->lock_flags);
1185 LASSERT(req->rq_export);
1187 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1188 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1190 if (req->rq_export && req->rq_export->exp_nid_stats &&
1191 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1192 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1193 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1195 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1196 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1197 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1198 dlm_req->lock_desc.l_resource.lr_type);
1199 GOTO(out, rc = -EFAULT);
1202 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1203 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1204 dlm_req->lock_desc.l_req_mode &
1205 (dlm_req->lock_desc.l_req_mode-1))) {
1206 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1207 dlm_req->lock_desc.l_req_mode);
1208 GOTO(out, rc = -EFAULT);
1211 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) {
1212 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1214 DEBUG_REQ(D_ERROR, req,
1215 "PLAIN lock request from IBITS client?");
1216 GOTO(out, rc = -EPROTO);
1218 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1220 DEBUG_REQ(D_ERROR, req,
1221 "IBITS lock request from unaware client?");
1222 GOTO(out, rc = -EPROTO);
1226 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1227 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1228 ibits for mgc/mgs) */
1230 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1231 * inodebits lock if client does not support them. */
1232 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) &&
1233 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1234 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1235 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1236 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1237 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1238 dlm_req->lock_desc.l_req_mode = LCK_CR;
1242 if (unlikely((flags & LDLM_FL_REPLAY) ||
1243 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))) {
1244 /* Find an existing lock in the per-export lock hash */
1245 /* In the function below, .hs_keycmp resolves to
1246 * ldlm_export_lock_keycmp() */
1247 /* coverity[overrun-buffer-val] */
1248 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1249 (void *)&dlm_req->lock_handle[0]);
1251 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1252 LPX64, lock->l_handle.h_cookie);
1253 flags |= LDLM_FL_RESENT;
1254 GOTO(existing_lock, rc = 0);
1258 /* The lock's callback data might be set in the policy function */
1259 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1260 dlm_req->lock_desc.l_resource.lr_type,
1261 dlm_req->lock_desc.l_req_mode,
1262 cbs, NULL, 0, LVB_T_NONE);
1269 lock->l_last_activity = cfs_time_current_sec();
1270 lock->l_remote_handle = dlm_req->lock_handle[0];
1271 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1273 /* Initialize resource lvb but not for a lock being replayed since
1274 * Client already got lvb sent in this case.
1275 * This must occur early since some policy methods assume resource
1276 * lvb is available (lr_lvb_data != NULL).
1278 res = lock->l_resource;
1279 if (!(flags & LDLM_FL_REPLAY)) {
1280 /* non-replayed lock, delayed lvb init may need to be done */
1281 rc = ldlm_lvbo_init(res);
1283 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", rc);
1288 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1289 /* Don't enqueue a lock onto the export if it is been disonnected
1290 * due to eviction (bug 3822) or server umount (bug 24324).
1291 * Cancel it now instead. */
1292 if (req->rq_export->exp_disconnected) {
1293 LDLM_ERROR(lock, "lock on disconnected export %p",
1295 GOTO(out, rc = -ENOTCONN);
1298 lock->l_export = class_export_lock_get(req->rq_export, lock);
1299 if (lock->l_export->exp_lock_hash)
1300 cfs_hash_add(lock->l_export->exp_lock_hash,
1301 &lock->l_remote_handle,
1306 if (flags & LDLM_FL_HAS_INTENT) {
1307 /* In this case, the reply buffer is allocated deep in
1308 * local_lock_enqueue by the policy function. */
1311 /* based on the assumption that lvb size never changes during
1312 * resource life time otherwise it need resource->lr_lock's
1314 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1315 RCL_SERVER, ldlm_lvbo_size(lock));
1317 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1318 GOTO(out, rc = -ENOMEM);
1320 rc = req_capsule_server_pack(&req->rq_pill);
1325 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1326 ldlm_convert_policy_to_local(req->rq_export,
1327 dlm_req->lock_desc.l_resource.lr_type,
1328 &dlm_req->lock_desc.l_policy_data,
1329 &lock->l_policy_data);
1330 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1331 lock->l_req_extent = lock->l_policy_data.l_extent;
1333 err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
1340 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1341 dlm_rep->lock_flags = ldlm_flags_to_wire(flags);
1343 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1344 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1346 /* We never send a blocking AST until the lock is granted, but
1347 * we can tell it right now */
1348 lock_res_and_lock(lock);
1350 /* Now take into account flags to be inherited from original lock
1351 request both in reply to client and in our own lock flags. */
1352 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_FL_INHERIT_MASK;
1353 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
1354 LDLM_FL_INHERIT_MASK);
1356 /* Don't move a pending lock onto the export if it has already been
1357 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1358 * Cancel it now instead. */
1359 if (unlikely(req->rq_export->exp_disconnected ||
1360 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1361 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1363 } else if (ldlm_is_ast_sent(lock)) {
1364 dlm_rep->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1365 if (lock->l_granted_mode == lock->l_req_mode) {
1367 * Only cancel lock if it was granted, because it would
1368 * be destroyed immediately and would never be granted
1369 * in the future, causing timeouts on client. Not
1370 * granted lock will be cancelled immediately after
1371 * sending completion AST.
1373 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1374 unlock_res_and_lock(lock);
1375 ldlm_lock_cancel(lock);
1376 lock_res_and_lock(lock);
1378 ldlm_add_waiting_lock(lock);
1381 /* Make sure we never ever grant usual metadata locks to liblustre
1383 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1384 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1385 req->rq_export->exp_libclient) {
1386 if (unlikely(!ldlm_is_cancel_on_block(lock) ||
1387 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1388 CERROR("Granting sync lock to libclient. "
1389 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1390 dlm_req->lock_flags, dlm_rep->lock_flags,
1392 LDLM_ERROR(lock, "sync lock");
1393 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1394 struct ldlm_intent *it;
1396 it = req_capsule_client_get(&req->rq_pill,
1399 CERROR("This is intent %s ("LPU64")\n",
1400 ldlm_it2str(it->opc), it->opc);
1406 unlock_res_and_lock(lock);
1410 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1411 if (!req->rq_packed_final) {
1412 err = lustre_pack_reply(req, 1, NULL, NULL);
1417 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1418 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1420 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1421 "(err=%d, rc=%d)", err, rc);
1424 if (req_capsule_has_field(&req->rq_pill, &RMF_DLM_LVB,
1426 ldlm_lvbo_size(lock) > 0) {
1430 buf = req_capsule_server_get(&req->rq_pill,
1432 LASSERTF(buf != NULL, "req %p, lock %p\n",
1434 buflen = req_capsule_get_size(&req->rq_pill,
1435 &RMF_DLM_LVB, RCL_SERVER);
1436 /* non-replayed lock, delayed lvb init may
1437 * need to be occur now */
1438 if ((buflen > 0) && !(flags & LDLM_FL_REPLAY)) {
1439 buflen = ldlm_lvbo_fill(lock, buf,
1445 buflen, RCL_SERVER);
1448 } else if (flags & LDLM_FL_REPLAY) {
1449 /* no LVB resend upon replay */
1464 lock_res_and_lock(lock);
1465 ldlm_resource_unlink_lock(lock);
1466 ldlm_lock_destroy_nolock(lock);
1467 unlock_res_and_lock(lock);
1470 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1471 ldlm_reprocess_all(lock->l_resource);
1473 LDLM_LOCK_RELEASE(lock);
1476 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1481 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1484 * Old-style LDLM main entry point for server code enqueue.
1486 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1487 ldlm_completion_callback completion_callback,
1488 ldlm_blocking_callback blocking_callback,
1489 ldlm_glimpse_callback glimpse_callback)
1491 struct ldlm_request *dlm_req;
1492 struct ldlm_callback_suite cbs = {
1493 .lcs_completion = completion_callback,
1494 .lcs_blocking = blocking_callback,
1495 .lcs_glimpse = glimpse_callback
1499 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1500 if (dlm_req != NULL) {
1501 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1502 req, dlm_req, &cbs);
1508 EXPORT_SYMBOL(ldlm_handle_enqueue);
1511 * Main LDLM entry point for server code to process lock conversion requests.
1513 int ldlm_handle_convert0(struct ptlrpc_request *req,
1514 const struct ldlm_request *dlm_req)
1516 struct ldlm_reply *dlm_rep;
1517 struct ldlm_lock *lock;
1521 if (req->rq_export && req->rq_export->exp_nid_stats &&
1522 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1523 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1524 LDLM_CONVERT - LDLM_FIRST_OPC);
1526 rc = req_capsule_server_pack(&req->rq_pill);
1530 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1531 dlm_rep->lock_flags = dlm_req->lock_flags;
1533 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1535 req->rq_status = LUSTRE_EINVAL;
1539 LDLM_DEBUG(lock, "server-side convert handler START");
1541 lock->l_last_activity = cfs_time_current_sec();
1542 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1543 &dlm_rep->lock_flags);
1545 if (ldlm_del_waiting_lock(lock))
1546 LDLM_DEBUG(lock, "converted waiting lock");
1549 req->rq_status = LUSTRE_EDEADLK;
1554 if (!req->rq_status)
1555 ldlm_reprocess_all(lock->l_resource);
1556 LDLM_DEBUG(lock, "server-side convert handler END");
1557 LDLM_LOCK_PUT(lock);
1559 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1563 EXPORT_SYMBOL(ldlm_handle_convert0);
1566 * Old-style main LDLM entry point for server code to process lock conversion
1569 int ldlm_handle_convert(struct ptlrpc_request *req)
1572 struct ldlm_request *dlm_req;
1574 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1575 if (dlm_req != NULL) {
1576 rc = ldlm_handle_convert0(req, dlm_req);
1578 CERROR ("Can't unpack dlm_req\n");
1583 EXPORT_SYMBOL(ldlm_handle_convert);
1586 * Cancel all the locks whose handles are packed into ldlm_request
1588 * Called by server code expecting such combined cancel activity
1591 int ldlm_request_cancel(struct ptlrpc_request *req,
1592 const struct ldlm_request *dlm_req, int first)
1594 struct ldlm_resource *res, *pres = NULL;
1595 struct ldlm_lock *lock;
1596 int i, count, done = 0;
1599 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1603 if (count == 1 && dlm_req->lock_handle[0].cookie == 0)
1606 /* There is no lock on the server at the replay time,
1607 * skip lock cancelling to make replay tests to pass. */
1608 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1611 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1612 "starting at %d", count, first);
1614 for (i = first; i < count; i++) {
1615 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1617 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1618 "lock (cookie "LPU64")",
1619 dlm_req->lock_handle[i].cookie);
1623 res = lock->l_resource;
1626 /* This code is an optimization to only attempt lock
1627 * granting on the resource (that could be CPU-expensive)
1628 * after we are done cancelling lock in that resource. */
1631 ldlm_reprocess_all(pres);
1632 LDLM_RESOURCE_DELREF(pres);
1633 ldlm_resource_putref(pres);
1636 ldlm_resource_getref(res);
1637 LDLM_RESOURCE_ADDREF(res);
1638 ldlm_res_lvbo_update(res, NULL, 1);
1642 ldlm_lock_cancel(lock);
1643 LDLM_LOCK_PUT(lock);
1646 ldlm_reprocess_all(pres);
1647 LDLM_RESOURCE_DELREF(pres);
1648 ldlm_resource_putref(pres);
1650 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1653 EXPORT_SYMBOL(ldlm_request_cancel);
1656 * Main LDLM entry point for server code to cancel locks.
1658 * Typically gets called from service handler on LDLM_CANCEL opc.
1660 int ldlm_handle_cancel(struct ptlrpc_request *req)
1662 struct ldlm_request *dlm_req;
1666 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1667 if (dlm_req == NULL) {
1668 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1672 if (req->rq_export && req->rq_export->exp_nid_stats &&
1673 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1674 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1675 LDLM_CANCEL - LDLM_FIRST_OPC);
1677 rc = req_capsule_server_pack(&req->rq_pill);
1681 if (!ldlm_request_cancel(req, dlm_req, 0))
1682 req->rq_status = LUSTRE_ESTALE;
1684 RETURN(ptlrpc_reply(req));
1686 EXPORT_SYMBOL(ldlm_handle_cancel);
1687 #endif /* HAVE_SERVER_SUPPORT */
1690 * Callback handler for receiving incoming blocking ASTs.
1692 * This can only happen on client side.
1694 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1695 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1700 LDLM_DEBUG(lock, "client blocking AST callback handler");
1702 lock_res_and_lock(lock);
1703 ldlm_set_cbpending(lock);
1705 if (ldlm_is_cancel_on_block(lock))
1706 ldlm_set_cancel(lock);
1708 do_ast = (!lock->l_readers && !lock->l_writers);
1709 unlock_res_and_lock(lock);
1712 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1713 lock, lock->l_blocking_ast);
1714 if (lock->l_blocking_ast != NULL)
1715 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1718 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1722 LDLM_DEBUG(lock, "client blocking callback handler END");
1723 LDLM_LOCK_RELEASE(lock);
1728 * Callback handler for receiving incoming completion ASTs.
1730 * This only can happen on client side.
1732 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1733 struct ldlm_namespace *ns,
1734 struct ldlm_request *dlm_req,
1735 struct ldlm_lock *lock)
1737 struct list_head ast_list;
1742 LDLM_DEBUG(lock, "client completion callback handler START");
1744 INIT_LIST_HEAD(&ast_list);
1745 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1746 int to = cfs_time_seconds(1);
1748 schedule_timeout_and_set_state(
1749 TASK_INTERRUPTIBLE, to);
1750 if (lock->l_granted_mode == lock->l_req_mode ||
1751 ldlm_is_destroyed(lock))
1756 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1758 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", lvb_len);
1759 GOTO(out, rc = lvb_len);
1760 } else if (lvb_len > 0) {
1761 if (lock->l_lvb_len > 0) {
1762 /* for extent lock, lvb contains ost_lvb{}. */
1763 LASSERT(lock->l_lvb_data != NULL);
1765 if (unlikely(lock->l_lvb_len < lvb_len)) {
1766 LDLM_ERROR(lock, "Replied LVB is larger than "
1767 "expectation, expected = %d, "
1769 lock->l_lvb_len, lvb_len);
1770 GOTO(out, rc = -EINVAL);
1772 } else if (ldlm_has_layout(lock)) { /* for layout lock, lvb has
1773 * variable length */
1776 OBD_ALLOC_LARGE(lvb_data, lvb_len);
1777 if (lvb_data == NULL) {
1778 LDLM_ERROR(lock, "No memory: %d.\n", lvb_len);
1779 GOTO(out, rc = -ENOMEM);
1782 lock_res_and_lock(lock);
1783 LASSERT(lock->l_lvb_data == NULL);
1784 lock->l_lvb_type = LVB_T_LAYOUT;
1785 lock->l_lvb_data = lvb_data;
1786 lock->l_lvb_len = lvb_len;
1787 unlock_res_and_lock(lock);
1791 lock_res_and_lock(lock);
1792 if (ldlm_is_destroyed(lock) ||
1793 lock->l_granted_mode == lock->l_req_mode) {
1794 /* bug 11300: the lock has already been granted */
1795 unlock_res_and_lock(lock);
1796 LDLM_DEBUG(lock, "Double grant race happened");
1800 /* If we receive the completion AST before the actual enqueue returned,
1801 * then we might need to switch lock modes, resources, or extents. */
1802 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1803 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1804 LDLM_DEBUG(lock, "completion AST, new lock mode");
1807 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1808 ldlm_convert_policy_to_local(req->rq_export,
1809 dlm_req->lock_desc.l_resource.lr_type,
1810 &dlm_req->lock_desc.l_policy_data,
1811 &lock->l_policy_data);
1812 LDLM_DEBUG(lock, "completion AST, new policy data");
1815 ldlm_resource_unlink_lock(lock);
1816 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1817 &lock->l_resource->lr_name,
1818 sizeof(lock->l_resource->lr_name)) != 0) {
1819 unlock_res_and_lock(lock);
1820 rc = ldlm_lock_change_resource(ns, lock,
1821 &dlm_req->lock_desc.l_resource.lr_name);
1823 LDLM_ERROR(lock, "Failed to allocate resource");
1826 LDLM_DEBUG(lock, "completion AST, new resource");
1827 CERROR("change resource!\n");
1828 lock_res_and_lock(lock);
1831 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1832 /* BL_AST locks are not needed in LRU.
1833 * Let ldlm_cancel_lru() be fast. */
1834 ldlm_lock_remove_from_lru(lock);
1835 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1836 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1839 if (lock->l_lvb_len > 0) {
1840 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_CLIENT,
1841 lock->l_lvb_data, lvb_len);
1843 unlock_res_and_lock(lock);
1848 ldlm_grant_lock(lock, &ast_list);
1849 unlock_res_and_lock(lock);
1851 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1853 /* Let Enqueue to call osc_lock_upcall() and initialize
1855 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1857 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1859 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1865 lock_res_and_lock(lock);
1866 ldlm_set_failed(lock);
1867 unlock_res_and_lock(lock);
1868 wake_up(&lock->l_waitq);
1870 LDLM_LOCK_RELEASE(lock);
1874 * Callback handler for receiving incoming glimpse ASTs.
1876 * This only can happen on client side. After handling the glimpse AST
1877 * we also consider dropping the lock here if it is unused locally for a
1880 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1881 struct ldlm_namespace *ns,
1882 struct ldlm_request *dlm_req,
1883 struct ldlm_lock *lock)
1888 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1890 if (lock->l_glimpse_ast != NULL)
1891 rc = lock->l_glimpse_ast(lock, req);
1893 if (req->rq_repmsg != NULL) {
1896 req->rq_status = rc;
1900 lock_res_and_lock(lock);
1901 if (lock->l_granted_mode == LCK_PW &&
1902 !lock->l_readers && !lock->l_writers &&
1903 cfs_time_after(cfs_time_current(),
1904 cfs_time_add(lock->l_last_used,
1905 cfs_time_seconds(10)))) {
1906 unlock_res_and_lock(lock);
1907 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1908 ldlm_handle_bl_callback(ns, NULL, lock);
1913 unlock_res_and_lock(lock);
1914 LDLM_LOCK_RELEASE(lock);
1918 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1920 if (req->rq_no_reply)
1923 req->rq_status = rc;
1924 if (!req->rq_packed_final) {
1925 rc = lustre_pack_reply(req, 1, NULL, NULL);
1929 return ptlrpc_reply(req);
1933 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi,
1934 ldlm_cancel_flags_t cancel_flags)
1936 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1939 spin_lock(&blp->blp_lock);
1940 if (blwi->blwi_lock &&
1941 ldlm_is_discard_data(blwi->blwi_lock)) {
1942 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1943 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1945 /* other blocking callbacks are added to the regular list */
1946 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1948 spin_unlock(&blp->blp_lock);
1950 wake_up(&blp->blp_waitq);
1952 /* can not check blwi->blwi_flags as blwi could be already freed in
1954 if (!(cancel_flags & LCF_ASYNC))
1955 wait_for_completion(&blwi->blwi_comp);
1960 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1961 struct ldlm_namespace *ns,
1962 struct ldlm_lock_desc *ld,
1963 struct list_head *cancels, int count,
1964 struct ldlm_lock *lock,
1965 ldlm_cancel_flags_t cancel_flags)
1967 init_completion(&blwi->blwi_comp);
1968 INIT_LIST_HEAD(&blwi->blwi_head);
1970 if (memory_pressure_get())
1971 blwi->blwi_mem_pressure = 1;
1974 blwi->blwi_flags = cancel_flags;
1976 blwi->blwi_ld = *ld;
1978 list_add(&blwi->blwi_head, cancels);
1979 list_del_init(cancels);
1980 blwi->blwi_count = count;
1982 blwi->blwi_lock = lock;
1987 * Queues a list of locks \a cancels containing \a count locks
1988 * for later processing by a blocking thread. If \a count is zero,
1989 * then the lock referenced as \a lock is queued instead.
1991 * The blocking thread would then call ->l_blocking_ast callback in the lock.
1992 * If list addition fails an error is returned and caller is supposed to
1993 * call ->l_blocking_ast itself.
1995 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1996 struct ldlm_lock_desc *ld,
1997 struct ldlm_lock *lock,
1998 struct list_head *cancels, int count,
1999 ldlm_cancel_flags_t cancel_flags)
2003 if (cancels && count == 0)
2006 if (cancel_flags & LCF_ASYNC) {
2007 struct ldlm_bl_work_item *blwi;
2009 OBD_ALLOC(blwi, sizeof(*blwi));
2012 init_blwi(blwi, ns, ld, cancels, count, lock, cancel_flags);
2014 RETURN(__ldlm_bl_to_thread(blwi, cancel_flags));
2016 /* if it is synchronous call do minimum mem alloc, as it could
2017 * be triggered from kernel shrinker
2019 struct ldlm_bl_work_item blwi;
2021 memset(&blwi, 0, sizeof(blwi));
2022 init_blwi(&blwi, ns, ld, cancels, count, lock, cancel_flags);
2023 RETURN(__ldlm_bl_to_thread(&blwi, cancel_flags));
2029 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
2030 struct ldlm_lock *lock)
2033 return ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LCF_ASYNC);
2039 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
2040 struct list_head *cancels, int count,
2041 ldlm_cancel_flags_t cancel_flags)
2044 return ldlm_bl_to_thread(ns, ld, NULL, cancels, count, cancel_flags);
2050 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
2051 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
2053 struct obd_device *obd = req->rq_export->exp_obd;
2060 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
2062 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2064 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
2066 DEBUG_REQ(D_IOCTL, req, "no set_info key");
2069 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
2071 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
2073 DEBUG_REQ(D_IOCTL, req, "no set_info val");
2076 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
2079 /* We are responsible for swabbing contents of val */
2081 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
2082 /* Pass it on to mdc (the "export" in this case) */
2083 rc = obd_set_info_async(req->rq_svc_thread->t_env,
2085 sizeof(KEY_HSM_COPYTOOL_SEND),
2086 KEY_HSM_COPYTOOL_SEND,
2089 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
2094 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
2095 const char *msg, int rc,
2096 struct lustre_handle *handle)
2098 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
2099 "%s: [nid %s] [rc %d] [lock "LPX64"]",
2100 msg, libcfs_id2str(req->rq_peer), rc,
2101 handle ? handle->cookie : 0);
2102 if (req->rq_no_reply)
2103 CWARN("No reply was sent, maybe cause bug 21636.\n");
2105 CWARN("Send reply failed, maybe cause bug 21636.\n");
2108 static int ldlm_handle_qc_callback(struct ptlrpc_request *req)
2110 struct obd_quotactl *oqctl;
2111 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2113 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2114 if (oqctl == NULL) {
2115 CERROR("Can't unpack obd_quotactl\n");
2119 oqctl->qc_stat = ptlrpc_status_ntoh(oqctl->qc_stat);
2121 cli->cl_qchk_stat = oqctl->qc_stat;
2125 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2126 static int ldlm_callback_handler(struct ptlrpc_request *req)
2128 struct ldlm_namespace *ns;
2129 struct ldlm_request *dlm_req;
2130 struct ldlm_lock *lock;
2134 /* Requests arrive in sender's byte order. The ptlrpc service
2135 * handler has already checked and, if necessary, byte-swapped the
2136 * incoming request message body, but I am responsible for the
2137 * message buffers. */
2139 /* do nothing for sec context finalize */
2140 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
2143 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2145 if (req->rq_export == NULL) {
2146 rc = ldlm_callback_reply(req, -ENOTCONN);
2147 ldlm_callback_errmsg(req, "Operate on unconnected server",
2152 LASSERT(req->rq_export != NULL);
2153 LASSERT(req->rq_export->exp_obd != NULL);
2155 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2156 case LDLM_BL_CALLBACK:
2157 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
2160 case LDLM_CP_CALLBACK:
2161 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
2164 case LDLM_GL_CALLBACK:
2165 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
2169 rc = ldlm_handle_setinfo(req);
2170 ldlm_callback_reply(req, rc);
2172 case LLOG_ORIGIN_HANDLE_CREATE:
2173 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2174 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2176 rc = llog_origin_handle_open(req);
2177 ldlm_callback_reply(req, rc);
2179 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2180 req_capsule_set(&req->rq_pill,
2181 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2182 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2184 rc = llog_origin_handle_next_block(req);
2185 ldlm_callback_reply(req, rc);
2187 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2188 req_capsule_set(&req->rq_pill,
2189 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2190 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2192 rc = llog_origin_handle_read_header(req);
2193 ldlm_callback_reply(req, rc);
2195 case LLOG_ORIGIN_HANDLE_CLOSE:
2196 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2198 rc = llog_origin_handle_close(req);
2199 ldlm_callback_reply(req, rc);
2201 case OBD_QC_CALLBACK:
2202 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
2203 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
2205 rc = ldlm_handle_qc_callback(req);
2206 ldlm_callback_reply(req, rc);
2209 CERROR("unknown opcode %u\n",
2210 lustre_msg_get_opc(req->rq_reqmsg));
2211 ldlm_callback_reply(req, -EPROTO);
2215 ns = req->rq_export->exp_obd->obd_namespace;
2216 LASSERT(ns != NULL);
2218 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2220 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2221 if (dlm_req == NULL) {
2222 rc = ldlm_callback_reply(req, -EPROTO);
2223 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2228 /* Force a known safe race, send a cancel to the server for a lock
2229 * which the server has already started a blocking callback on. */
2230 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2231 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2232 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0], 0);
2234 CERROR("ldlm_cli_cancel: %d\n", rc);
2237 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2239 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2240 "disappeared\n", dlm_req->lock_handle[0].cookie);
2241 rc = ldlm_callback_reply(req, -EINVAL);
2242 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2243 &dlm_req->lock_handle[0]);
2247 if (ldlm_is_fail_loc(lock) &&
2248 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2249 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2251 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2252 lock_res_and_lock(lock);
2253 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
2255 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2256 /* If somebody cancels lock and cache is already dropped,
2257 * or lock is failed before cp_ast received on client,
2258 * we can tell the server we have no lock. Otherwise, we
2259 * should send cancel after dropping the cache. */
2260 if ((ldlm_is_canceling(lock) && ldlm_is_bl_done(lock)) ||
2261 ldlm_is_failed(lock)) {
2262 LDLM_DEBUG(lock, "callback on lock "
2263 LPX64" - lock disappeared\n",
2264 dlm_req->lock_handle[0].cookie);
2265 unlock_res_and_lock(lock);
2266 LDLM_LOCK_RELEASE(lock);
2267 rc = ldlm_callback_reply(req, -EINVAL);
2268 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2269 &dlm_req->lock_handle[0]);
2272 /* BL_AST locks are not needed in LRU.
2273 * Let ldlm_cancel_lru() be fast. */
2274 ldlm_lock_remove_from_lru(lock);
2275 ldlm_set_bl_ast(lock);
2277 unlock_res_and_lock(lock);
2279 /* We want the ost thread to get this reply so that it can respond
2280 * to ost requests (write cache writeback) that might be triggered
2283 * But we'd also like to be able to indicate in the reply that we're
2284 * cancelling right now, because it's unused, or have an intent result
2285 * in the reply, so we might have to push the responsibility for sending
2286 * the reply down into the AST handlers, alas. */
2288 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2289 case LDLM_BL_CALLBACK:
2290 CDEBUG(D_INODE, "blocking ast\n");
2291 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2292 if (!ldlm_is_cancel_on_block(lock)) {
2293 rc = ldlm_callback_reply(req, 0);
2294 if (req->rq_no_reply || rc)
2295 ldlm_callback_errmsg(req, "Normal process", rc,
2296 &dlm_req->lock_handle[0]);
2298 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2299 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2301 case LDLM_CP_CALLBACK:
2302 CDEBUG(D_INODE, "completion ast\n");
2303 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2304 ldlm_callback_reply(req, 0);
2305 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2307 case LDLM_GL_CALLBACK:
2308 CDEBUG(D_INODE, "glimpse ast\n");
2309 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2310 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2313 LBUG(); /* checked above */
2319 #ifdef HAVE_SERVER_SUPPORT
2321 * Main handler for canceld thread.
2323 * Separated into its own thread to avoid deadlocks.
2325 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2330 /* Requests arrive in sender's byte order. The ptlrpc service
2331 * handler has already checked and, if necessary, byte-swapped the
2332 * incoming request message body, but I am responsible for the
2333 * message buffers. */
2335 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2337 if (req->rq_export == NULL) {
2338 struct ldlm_request *dlm_req;
2340 CERROR("%s from %s arrived at %lu with bad export cookie "
2342 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2343 libcfs_nid2str(req->rq_peer.nid),
2344 req->rq_arrival_time.tv_sec,
2345 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2347 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2348 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2349 dlm_req = req_capsule_client_get(&req->rq_pill,
2351 if (dlm_req != NULL)
2352 ldlm_lock_dump_handle(D_ERROR,
2353 &dlm_req->lock_handle[0]);
2355 ldlm_callback_reply(req, -ENOTCONN);
2359 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2361 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2363 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2364 CDEBUG(D_INODE, "cancel\n");
2365 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET) ||
2366 CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))
2368 rc = ldlm_handle_cancel(req);
2373 CERROR("invalid opcode %d\n",
2374 lustre_msg_get_opc(req->rq_reqmsg));
2375 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2376 ldlm_callback_reply(req, -EINVAL);
2382 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2383 struct ldlm_lock *lock)
2385 struct ldlm_request *dlm_req;
2386 struct lustre_handle lockh;
2391 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2392 if (dlm_req == NULL)
2395 ldlm_lock2handle(lock, &lockh);
2396 for (i = 0; i < dlm_req->lock_count; i++) {
2397 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2399 DEBUG_REQ(D_RPCTRACE, req,
2400 "Prio raised by lock "LPX64".", lockh.cookie);
2411 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2413 struct ldlm_request *dlm_req;
2418 /* no prolong in recovery */
2419 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2422 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2423 if (dlm_req == NULL)
2426 for (i = 0; i < dlm_req->lock_count; i++) {
2427 struct ldlm_lock *lock;
2429 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2433 rc = ldlm_is_ast_sent(lock) ? 1 : 0;
2435 LDLM_DEBUG(lock, "hpreq cancel lock");
2436 LDLM_LOCK_PUT(lock);
2445 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2446 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2447 .hpreq_check = ldlm_cancel_hpreq_check,
2451 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2455 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2457 if (req->rq_export == NULL)
2460 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2461 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2462 req->rq_ops = &ldlm_cancel_hpreq_ops;
2467 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2468 struct hlist_node *hnode, void *data)
2471 struct list_head *rpc_list = data;
2472 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2474 lock_res_and_lock(lock);
2476 if (lock->l_req_mode != lock->l_granted_mode) {
2477 unlock_res_and_lock(lock);
2481 LASSERT(lock->l_resource);
2482 if (lock->l_resource->lr_type != LDLM_IBITS &&
2483 lock->l_resource->lr_type != LDLM_PLAIN) {
2484 unlock_res_and_lock(lock);
2488 if (ldlm_is_ast_sent(lock)) {
2489 unlock_res_and_lock(lock);
2493 LASSERT(lock->l_blocking_ast);
2494 LASSERT(!lock->l_blocking_lock);
2496 ldlm_set_ast_sent(lock);
2497 if (lock->l_export && lock->l_export->exp_lock_hash) {
2498 /* NB: it's safe to call cfs_hash_del() even lock isn't
2499 * in exp_lock_hash. */
2500 /* In the function below, .hs_keycmp resolves to
2501 * ldlm_export_lock_keycmp() */
2502 /* coverity[overrun-buffer-val] */
2503 cfs_hash_del(lock->l_export->exp_lock_hash,
2504 &lock->l_remote_handle, &lock->l_exp_hash);
2507 list_add_tail(&lock->l_rk_ast, rpc_list);
2508 LDLM_LOCK_GET(lock);
2510 unlock_res_and_lock(lock);
2514 void ldlm_revoke_export_locks(struct obd_export *exp)
2516 struct list_head rpc_list;
2519 INIT_LIST_HEAD(&rpc_list);
2520 cfs_hash_for_each_empty(exp->exp_lock_hash,
2521 ldlm_revoke_lock_cb, &rpc_list);
2522 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2523 LDLM_WORK_REVOKE_AST);
2527 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2528 #endif /* HAVE_SERVER_SUPPORT */
2531 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2533 struct ldlm_bl_work_item *blwi = NULL;
2534 static unsigned int num_bl = 0;
2536 spin_lock(&blp->blp_lock);
2537 /* process a request from the blp_list at least every blp_num_threads */
2538 if (!list_empty(&blp->blp_list) &&
2539 (list_empty(&blp->blp_prio_list) || num_bl == 0))
2540 blwi = list_entry(blp->blp_list.next,
2541 struct ldlm_bl_work_item, blwi_entry);
2543 if (!list_empty(&blp->blp_prio_list))
2544 blwi = list_entry(blp->blp_prio_list.next,
2545 struct ldlm_bl_work_item,
2549 if (++num_bl >= atomic_read(&blp->blp_num_threads))
2551 list_del(&blwi->blwi_entry);
2553 spin_unlock(&blp->blp_lock);
2558 /* This only contains temporary data until the thread starts */
2559 struct ldlm_bl_thread_data {
2560 char bltd_name[CFS_CURPROC_COMM_MAX];
2561 struct ldlm_bl_pool *bltd_blp;
2562 struct completion bltd_comp;
2566 static int ldlm_bl_thread_main(void *arg);
2568 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2570 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2571 struct task_struct *task;
2573 init_completion(&bltd.bltd_comp);
2574 bltd.bltd_num = atomic_read(&blp->blp_num_threads);
2575 snprintf(bltd.bltd_name, sizeof(bltd.bltd_name) - 1,
2576 "ldlm_bl_%02d", bltd.bltd_num);
2577 task = kthread_run(ldlm_bl_thread_main, &bltd, bltd.bltd_name);
2579 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %ld\n",
2580 atomic_read(&blp->blp_num_threads), PTR_ERR(task));
2581 return PTR_ERR(task);
2583 wait_for_completion(&bltd.bltd_comp);
2589 * Main blocking requests processing thread.
2591 * Callers put locks into its queue by calling ldlm_bl_to_thread.
2592 * This thread in the end ends up doing actual call to ->l_blocking_ast
2595 static int ldlm_bl_thread_main(void *arg)
2597 struct ldlm_bl_pool *blp;
2598 struct ldlm_bl_thread_data *bltd = arg;
2601 blp = bltd->bltd_blp;
2603 atomic_inc(&blp->blp_num_threads);
2604 atomic_inc(&blp->blp_busy_threads);
2606 complete(&bltd->bltd_comp);
2607 /* cannot use bltd after this, it is only on caller's stack */
2610 struct l_wait_info lwi = { 0 };
2611 struct ldlm_bl_work_item *blwi = NULL;
2614 blwi = ldlm_bl_get_work(blp);
2617 atomic_dec(&blp->blp_busy_threads);
2618 l_wait_event_exclusive(blp->blp_waitq,
2619 (blwi = ldlm_bl_get_work(blp)) != NULL,
2621 busy = atomic_inc_return(&blp->blp_busy_threads);
2623 busy = atomic_read(&blp->blp_busy_threads);
2626 if (blwi->blwi_ns == NULL)
2627 /* added by ldlm_cleanup() */
2630 /* Not fatal if racy and have a few too many threads */
2631 if (unlikely(busy < blp->blp_max_threads &&
2632 busy >= atomic_read(&blp->blp_num_threads) &&
2633 !blwi->blwi_mem_pressure))
2634 /* discard the return value, we tried */
2635 ldlm_bl_thread_start(blp);
2637 if (blwi->blwi_mem_pressure)
2638 memory_pressure_set();
2640 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL2, 4);
2642 if (blwi->blwi_count) {
2644 /* The special case when we cancel locks in LRU
2645 * asynchronously, we pass the list of locks here.
2646 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2647 * canceled locally yet. */
2648 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2651 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL,
2654 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2657 if (blwi->blwi_mem_pressure)
2658 memory_pressure_clr();
2660 if (blwi->blwi_flags & LCF_ASYNC)
2661 OBD_FREE(blwi, sizeof(*blwi));
2663 complete(&blwi->blwi_comp);
2666 atomic_dec(&blp->blp_busy_threads);
2667 atomic_dec(&blp->blp_num_threads);
2668 complete(&blp->blp_comp);
2674 static int ldlm_setup(void);
2675 static int ldlm_cleanup(void);
2677 int ldlm_get_ref(void)
2681 mutex_lock(&ldlm_ref_mutex);
2682 if (++ldlm_refcount == 1) {
2687 mutex_unlock(&ldlm_ref_mutex);
2691 EXPORT_SYMBOL(ldlm_get_ref);
2693 void ldlm_put_ref(void)
2696 mutex_lock(&ldlm_ref_mutex);
2697 if (ldlm_refcount == 1) {
2698 int rc = ldlm_cleanup();
2700 CERROR("ldlm_cleanup failed: %d\n", rc);
2706 mutex_unlock(&ldlm_ref_mutex);
2710 EXPORT_SYMBOL(ldlm_put_ref);
2713 * Export handle<->lock hash operations.
2716 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2718 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2722 ldlm_export_lock_key(struct hlist_node *hnode)
2724 struct ldlm_lock *lock;
2726 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2727 return &lock->l_remote_handle;
2731 ldlm_export_lock_keycpy(struct hlist_node *hnode, void *key)
2733 struct ldlm_lock *lock;
2735 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2736 lock->l_remote_handle = *(struct lustre_handle *)key;
2740 ldlm_export_lock_keycmp(const void *key, struct hlist_node *hnode)
2742 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2746 ldlm_export_lock_object(struct hlist_node *hnode)
2748 return hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2752 ldlm_export_lock_get(cfs_hash_t *hs, struct hlist_node *hnode)
2754 struct ldlm_lock *lock;
2756 lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2757 LDLM_LOCK_GET(lock);
2761 ldlm_export_lock_put(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_RELEASE(lock);
2769 static cfs_hash_ops_t ldlm_export_lock_ops = {
2770 .hs_hash = ldlm_export_lock_hash,
2771 .hs_key = ldlm_export_lock_key,
2772 .hs_keycmp = ldlm_export_lock_keycmp,
2773 .hs_keycpy = ldlm_export_lock_keycpy,
2774 .hs_object = ldlm_export_lock_object,
2775 .hs_get = ldlm_export_lock_get,
2776 .hs_put = ldlm_export_lock_put,
2777 .hs_put_locked = ldlm_export_lock_put,
2780 int ldlm_init_export(struct obd_export *exp)
2785 exp->exp_lock_hash =
2786 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2787 HASH_EXP_LOCK_CUR_BITS,
2788 HASH_EXP_LOCK_MAX_BITS,
2789 HASH_EXP_LOCK_BKT_BITS, 0,
2790 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2791 &ldlm_export_lock_ops,
2792 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2793 CFS_HASH_NBLK_CHANGE);
2795 if (!exp->exp_lock_hash)
2798 rc = ldlm_init_flock_export(exp);
2804 ldlm_destroy_export(exp);
2807 EXPORT_SYMBOL(ldlm_init_export);
2809 void ldlm_destroy_export(struct obd_export *exp)
2812 cfs_hash_putref(exp->exp_lock_hash);
2813 exp->exp_lock_hash = NULL;
2815 ldlm_destroy_flock_export(exp);
2818 EXPORT_SYMBOL(ldlm_destroy_export);
2820 static int ldlm_setup(void)
2822 static struct ptlrpc_service_conf conf;
2823 struct ldlm_bl_pool *blp = NULL;
2825 # ifdef HAVE_SERVER_SUPPORT
2826 struct task_struct *task;
2834 if (ldlm_state != NULL)
2837 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2838 if (ldlm_state == NULL)
2842 rc = ldlm_proc_setup();
2847 memset(&conf, 0, sizeof(conf));
2848 conf = (typeof(conf)) {
2849 .psc_name = "ldlm_cbd",
2850 .psc_watchdog_factor = 2,
2852 .bc_nbufs = LDLM_CLIENT_NBUFS,
2853 .bc_buf_size = LDLM_BUFSIZE,
2854 .bc_req_max_size = LDLM_MAXREQSIZE,
2855 .bc_rep_max_size = LDLM_MAXREPSIZE,
2856 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2857 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2860 .tc_thr_name = "ldlm_cb",
2861 .tc_thr_factor = LDLM_THR_FACTOR,
2862 .tc_nthrs_init = LDLM_NTHRS_INIT,
2863 .tc_nthrs_base = LDLM_NTHRS_BASE,
2864 .tc_nthrs_max = LDLM_NTHRS_MAX,
2865 .tc_nthrs_user = ldlm_num_threads,
2866 .tc_cpu_affinity = 1,
2867 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2870 .cc_pattern = ldlm_cpts,
2873 .so_req_handler = ldlm_callback_handler,
2876 ldlm_state->ldlm_cb_service = \
2877 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2878 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2879 CERROR("failed to start service\n");
2880 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2881 ldlm_state->ldlm_cb_service = NULL;
2885 #ifdef HAVE_SERVER_SUPPORT
2886 memset(&conf, 0, sizeof(conf));
2887 conf = (typeof(conf)) {
2888 .psc_name = "ldlm_canceld",
2889 .psc_watchdog_factor = 6,
2891 .bc_nbufs = LDLM_SERVER_NBUFS,
2892 .bc_buf_size = LDLM_BUFSIZE,
2893 .bc_req_max_size = LDLM_MAXREQSIZE,
2894 .bc_rep_max_size = LDLM_MAXREPSIZE,
2895 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2896 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2900 .tc_thr_name = "ldlm_cn",
2901 .tc_thr_factor = LDLM_THR_FACTOR,
2902 .tc_nthrs_init = LDLM_NTHRS_INIT,
2903 .tc_nthrs_base = LDLM_NTHRS_BASE,
2904 .tc_nthrs_max = LDLM_NTHRS_MAX,
2905 .tc_nthrs_user = ldlm_num_threads,
2906 .tc_cpu_affinity = 1,
2907 .tc_ctx_tags = LCT_MD_THREAD | \
2912 .cc_pattern = ldlm_cpts,
2915 .so_req_handler = ldlm_cancel_handler,
2916 .so_hpreq_handler = ldlm_hpreq_handler,
2919 ldlm_state->ldlm_cancel_service = \
2920 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2921 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2922 CERROR("failed to start service\n");
2923 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2924 ldlm_state->ldlm_cancel_service = NULL;
2929 OBD_ALLOC(blp, sizeof(*blp));
2931 GOTO(out, rc = -ENOMEM);
2932 ldlm_state->ldlm_bl_pool = blp;
2934 spin_lock_init(&blp->blp_lock);
2935 INIT_LIST_HEAD(&blp->blp_list);
2936 INIT_LIST_HEAD(&blp->blp_prio_list);
2937 init_waitqueue_head(&blp->blp_waitq);
2938 atomic_set(&blp->blp_num_threads, 0);
2939 atomic_set(&blp->blp_busy_threads, 0);
2942 if (ldlm_num_threads == 0) {
2943 blp->blp_min_threads = LDLM_NTHRS_INIT;
2944 blp->blp_max_threads = LDLM_NTHRS_MAX;
2946 blp->blp_min_threads = blp->blp_max_threads = \
2947 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2951 for (i = 0; i < blp->blp_min_threads; i++) {
2952 rc = ldlm_bl_thread_start(blp);
2957 # ifdef HAVE_SERVER_SUPPORT
2958 INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2959 expired_lock_thread.elt_state = ELT_STOPPED;
2960 init_waitqueue_head(&expired_lock_thread.elt_waitq);
2962 INIT_LIST_HEAD(&waiting_locks_list);
2963 spin_lock_init(&waiting_locks_spinlock);
2964 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2966 task = kthread_run(expired_lock_main, NULL, "ldlm_elt");
2969 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2973 wait_event(expired_lock_thread.elt_waitq,
2974 expired_lock_thread.elt_state == ELT_READY);
2975 # endif /* HAVE_SERVER_SUPPORT */
2977 rc = ldlm_pools_init();
2979 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2990 static int ldlm_cleanup(void)
2994 if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2995 !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2996 CERROR("ldlm still has namespaces; clean these up first.\n");
2997 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2998 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
3005 if (ldlm_state->ldlm_bl_pool != NULL) {
3006 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
3008 while (atomic_read(&blp->blp_num_threads) > 0) {
3009 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
3011 init_completion(&blp->blp_comp);
3013 spin_lock(&blp->blp_lock);
3014 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
3015 wake_up(&blp->blp_waitq);
3016 spin_unlock(&blp->blp_lock);
3018 wait_for_completion(&blp->blp_comp);
3021 OBD_FREE(blp, sizeof(*blp));
3023 #endif /* __KERNEL__ */
3025 if (ldlm_state->ldlm_cb_service != NULL)
3026 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
3027 # ifdef HAVE_SERVER_SUPPORT
3028 if (ldlm_state->ldlm_cancel_service != NULL)
3029 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
3033 ldlm_proc_cleanup();
3035 # ifdef HAVE_SERVER_SUPPORT
3036 if (expired_lock_thread.elt_state != ELT_STOPPED) {
3037 expired_lock_thread.elt_state = ELT_TERMINATE;
3038 wake_up(&expired_lock_thread.elt_waitq);
3039 wait_event(expired_lock_thread.elt_waitq,
3040 expired_lock_thread.elt_state == ELT_STOPPED);
3043 #endif /* __KERNEL__ */
3045 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
3053 mutex_init(&ldlm_ref_mutex);
3054 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
3055 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
3057 INIT_LIST_HEAD(&ldlm_srv_namespace_list);
3058 INIT_LIST_HEAD(&ldlm_cli_active_namespace_list);
3059 INIT_LIST_HEAD(&ldlm_cli_inactive_namespace_list);
3061 ldlm_resource_slab = kmem_cache_create("ldlm_resources",
3062 sizeof(struct ldlm_resource), 0,
3063 SLAB_HWCACHE_ALIGN, NULL);
3064 if (ldlm_resource_slab == NULL)
3067 ldlm_lock_slab = kmem_cache_create("ldlm_locks",
3068 sizeof(struct ldlm_lock), 0,
3069 SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU, NULL);
3070 if (ldlm_lock_slab == NULL) {
3071 kmem_cache_destroy(ldlm_resource_slab);
3075 ldlm_interval_slab = kmem_cache_create("interval_node",
3076 sizeof(struct ldlm_interval),
3077 0, SLAB_HWCACHE_ALIGN, NULL);
3078 if (ldlm_interval_slab == NULL) {
3079 kmem_cache_destroy(ldlm_resource_slab);
3080 kmem_cache_destroy(ldlm_lock_slab);
3083 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3084 class_export_dump_hook = ldlm_dump_export_locks;
3089 void ldlm_exit(void)
3092 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
3093 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. */
3100 kmem_cache_destroy(ldlm_lock_slab);
3101 kmem_cache_destroy(ldlm_interval_slab);