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 cfs_list_t blp_prio_list;
106 * blp_list is used for all other callbacks which are likely
107 * to take longer to process.
111 wait_queue_head_t blp_waitq;
112 struct completion blp_comp;
113 cfs_atomic_t blp_num_threads;
114 cfs_atomic_t blp_busy_threads;
119 struct ldlm_bl_work_item {
120 cfs_list_t blwi_entry;
121 struct ldlm_namespace *blwi_ns;
122 struct ldlm_lock_desc blwi_ld;
123 struct ldlm_lock *blwi_lock;
124 cfs_list_t 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 cfs_list_t waiting_locks_list;
150 static cfs_timer_t waiting_locks_timer;
152 static struct expired_lock_thread {
153 wait_queue_head_t elt_waitq;
156 cfs_list_t 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 = !cfs_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 cfs_list_t *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 (!cfs_list_empty(expired)) {
210 struct obd_export *export;
211 struct ldlm_lock *lock;
213 lock = cfs_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 cfs_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 (lock->l_flags & LDLM_FL_DESTROYED) {
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 cfs_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 (!cfs_list_empty(&waiting_locks_list)) {
308 lock = cfs_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 cfs_list_del(&lock->l_pending_chain);
350 cfs_list_add(&lock->l_pending_chain,
351 &expired_lock_thread.elt_expired_locks);
355 if (!cfs_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 (!cfs_list_empty(&waiting_locks_list)) {
367 cfs_time_t timeout_rounded;
368 lock = cfs_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 (!cfs_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 cfs_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(lock->l_flags & LDLM_FL_RES_LOCKED);
425 lock->l_flags |= LDLM_FL_WAITED;
427 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
429 spin_lock_bh(&waiting_locks_spinlock);
430 if (lock->l_flags & LDLM_FL_DESTROYED) {
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 (cfs_list_empty(&lock->l_exp_list))
452 cfs_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 cfs_list_t *list_next;
476 if (cfs_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 = cfs_list_entry(list_next, struct ldlm_lock,
489 cfs_timer_arm(&waiting_locks_timer,
490 round_timeout(next->l_callback_timeout));
493 cfs_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 cfs_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 (cfs_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((lock->l_flags & (LDLM_FL_RES_LOCKED|LDLM_FL_CANCEL_ON_BLOCK))
576 == LDLM_FL_RES_LOCKED);
581 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
583 #ifdef HAVE_SERVER_SUPPORT
586 * Perform lock cleanup if AST sending failed.
588 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
589 const char *ast_type)
591 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
592 "to a lock %s callback time out: rc %d\n",
593 lock->l_export->exp_obd->obd_name,
594 obd_export_nid2str(lock->l_export), ast_type, rc);
596 if (obd_dump_on_timeout)
597 libcfs_debug_dumplog();
599 spin_lock_bh(&waiting_locks_spinlock);
600 if (__ldlm_del_waiting_lock(lock) == 0)
601 /* the lock was not in any list, grab an extra ref before adding
602 * the lock to the expired list */
604 cfs_list_add(&lock->l_pending_chain,
605 &expired_lock_thread.elt_expired_locks);
606 wake_up(&expired_lock_thread.elt_waitq);
607 spin_unlock_bh(&waiting_locks_spinlock);
609 class_fail_export(lock->l_export);
614 * Perform lock cleanup if AST reply came with error.
616 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
617 struct ptlrpc_request *req, int rc,
618 const char *ast_type)
620 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
622 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
623 LASSERT(lock->l_export);
624 if (lock->l_export->exp_libclient) {
625 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
626 " timeout, just cancelling lock", ast_type,
627 libcfs_nid2str(peer.nid));
628 ldlm_lock_cancel(lock);
630 } else if (lock->l_flags & LDLM_FL_CANCEL) {
631 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
632 "cancel was received (AST reply lost?)",
633 ast_type, libcfs_nid2str(peer.nid));
634 ldlm_lock_cancel(lock);
637 ldlm_del_waiting_lock(lock);
638 ldlm_failed_ast(lock, rc, ast_type);
642 struct ldlm_resource *res = lock->l_resource;
643 LDLM_DEBUG(lock, "client (nid %s) returned %d"
644 " from %s AST - normal race",
645 libcfs_nid2str(peer.nid),
647 lustre_msg_get_status(req->rq_repmsg) : -1,
650 /* update lvbo to return proper attributes.
652 ldlm_resource_getref(res);
653 ldlm_res_lvbo_update(res, NULL, 1);
654 ldlm_resource_putref(res);
658 LDLM_ERROR(lock, "client (nid %s) returned %d "
659 "from %s AST", libcfs_nid2str(peer.nid),
660 (req->rq_repmsg != NULL) ?
661 lustre_msg_get_status(req->rq_repmsg) : 0,
664 ldlm_lock_cancel(lock);
665 /* Server-side AST functions are called from ldlm_reprocess_all,
666 * which needs to be told to please restart its reprocessing. */
673 static int ldlm_cb_interpret(const struct lu_env *env,
674 struct ptlrpc_request *req, void *data, int rc)
676 struct ldlm_cb_async_args *ca = data;
677 struct ldlm_lock *lock = ca->ca_lock;
678 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
681 LASSERT(lock != NULL);
684 case LDLM_GL_CALLBACK:
685 /* Update the LVB from disk if the AST failed
686 * (this is a legal race)
688 * - Glimpse callback of local lock just returns
689 * -ELDLM_NO_LOCK_DATA.
690 * - Glimpse callback of remote lock might return
691 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
693 if (rc == -ELDLM_NO_LOCK_DATA) {
694 LDLM_DEBUG(lock, "lost race - client has a lock but no "
696 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
697 } else if (rc != 0) {
698 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
700 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
703 case LDLM_BL_CALLBACK:
705 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
707 case LDLM_CP_CALLBACK:
709 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
712 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
717 /* release extra reference taken in ldlm_ast_fini() */
718 LDLM_LOCK_RELEASE(lock);
721 cfs_atomic_inc(&arg->restart);
726 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
727 struct ldlm_cb_set_arg *arg,
728 struct ldlm_lock *lock,
734 if (unlikely(instant_cancel)) {
735 rc = ptl_send_rpc(req, 1);
736 ptlrpc_req_finished(req);
738 cfs_atomic_inc(&arg->restart);
741 ptlrpc_set_add_req(arg->set, req);
748 * Check if there are requests in the export request list which prevent
749 * the lock canceling and make these requests high priority ones.
751 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
753 struct ptlrpc_request *req;
756 if (lock->l_export == NULL) {
757 LDLM_DEBUG(lock, "client lock: no-op");
761 spin_lock_bh(&lock->l_export->exp_rpc_lock);
762 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
764 /* Do not process requests that were not yet added to there
765 * incoming queue or were already removed from there for
766 * processing. We evaluate ptlrpc_nrs_req_can_move() without
767 * holding svcpt->scp_req_lock, and then redo the check with
768 * the lock held once we need to obtain a reliable result.
770 if (ptlrpc_nrs_req_can_move(req) &&
771 req->rq_ops->hpreq_lock_match &&
772 req->rq_ops->hpreq_lock_match(req, lock))
773 ptlrpc_nrs_req_hp_move(req);
775 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
780 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
781 * enqueued server lock conflicts with given one.
783 * Sends blocking AST RPC to the client owning that lock; arms timeout timer
784 * to wait for client response.
786 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
787 struct ldlm_lock_desc *desc,
788 void *data, int flag)
790 struct ldlm_cb_async_args *ca;
791 struct ldlm_cb_set_arg *arg = data;
792 struct ldlm_request *body;
793 struct ptlrpc_request *req;
794 int instant_cancel = 0;
798 if (flag == LDLM_CB_CANCELING)
799 /* Don't need to do anything here. */
803 LASSERT(data != NULL);
804 if (lock->l_export->exp_obd->obd_recovering != 0)
805 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
807 ldlm_lock_reorder_req(lock);
809 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
810 &RQF_LDLM_BL_CALLBACK,
811 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
815 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
816 ca = ptlrpc_req_async_args(req);
817 ca->ca_set_arg = arg;
820 req->rq_interpret_reply = ldlm_cb_interpret;
821 req->rq_no_resend = 1;
823 lock_res_and_lock(lock);
824 if (lock->l_granted_mode != lock->l_req_mode) {
825 /* this blocking AST will be communicated as part of the
826 * completion AST instead */
827 unlock_res_and_lock(lock);
829 ptlrpc_req_finished(req);
830 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
834 if (lock->l_flags & LDLM_FL_DESTROYED) {
835 /* What's the point? */
836 unlock_res_and_lock(lock);
837 ptlrpc_req_finished(req);
841 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
844 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
845 body->lock_handle[0] = lock->l_remote_handle;
846 body->lock_desc = *desc;
847 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags & LDLM_AST_FLAGS);
849 LDLM_DEBUG(lock, "server preparing blocking AST");
851 ptlrpc_request_set_replen(req);
852 if (instant_cancel) {
853 unlock_res_and_lock(lock);
854 ldlm_lock_cancel(lock);
856 LASSERT(lock->l_granted_mode == lock->l_req_mode);
857 ldlm_add_waiting_lock(lock);
858 unlock_res_and_lock(lock);
861 req->rq_send_state = LUSTRE_IMP_FULL;
862 /* ptlrpc_request_alloc_pack already set timeout */
864 req->rq_timeout = ldlm_get_rq_timeout();
866 if (lock->l_export && lock->l_export->exp_nid_stats &&
867 lock->l_export->exp_nid_stats->nid_ldlm_stats)
868 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
869 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
871 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
875 EXPORT_SYMBOL(ldlm_server_blocking_ast);
878 * ->l_completion_ast callback for a remote lock in server namespace.
880 * Sends AST to the client notifying it of lock granting. If initial
881 * lock response was not sent yet, instead of sending another RPC, just
882 * mark the lock as granted and client will understand
884 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
886 struct ldlm_cb_set_arg *arg = data;
887 struct ldlm_request *body;
888 struct ptlrpc_request *req;
889 struct ldlm_cb_async_args *ca;
890 long total_enqueue_wait;
891 int instant_cancel = 0;
896 LASSERT(lock != NULL);
897 LASSERT(data != NULL);
899 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
900 lock->l_last_activity);
902 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
903 &RQF_LDLM_CP_CALLBACK);
907 /* server namespace, doesn't need lock */
908 lvb_len = ldlm_lvbo_size(lock);
909 /* LU-3124 & LU-2187: to not return layout in completion AST because
910 * it may deadlock for LU-2187, or client may not have enough space
911 * for large layout. The layout will be returned to client with an
912 * extra RPC to fetch xattr.lov */
913 if (ldlm_has_layout(lock))
916 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT, lvb_len);
917 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
919 ptlrpc_request_free(req);
923 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
924 ca = ptlrpc_req_async_args(req);
925 ca->ca_set_arg = arg;
928 req->rq_interpret_reply = ldlm_cb_interpret;
929 req->rq_no_resend = 1;
930 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
932 body->lock_handle[0] = lock->l_remote_handle;
933 body->lock_flags = ldlm_flags_to_wire(flags);
934 ldlm_lock2desc(lock, &body->lock_desc);
936 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
938 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
940 /* We still need to send the RPC to wake up the blocked
941 * enqueue thread on the client.
943 * Consider old client, there is no better way to notify
944 * the failure, just zero-sized the LVB, then the client
945 * will fail out as "-EPROTO". */
946 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, 0,
950 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len,
955 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
958 /* Server-side enqueue wait time estimate, used in
959 __ldlm_add_waiting_lock to set future enqueue timers */
960 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
961 at_measured(ldlm_lock_to_ns_at(lock),
964 /* bz18618. Don't add lock enqueue time we spend waiting for a
965 previous callback to fail. Locks waiting legitimately will
966 get extended by ldlm_refresh_waiting_lock regardless of the
967 estimate, so it's okay to underestimate here. */
968 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
969 "It is likely that a previous callback timed out.",
971 at_get(ldlm_lock_to_ns_at(lock)));
973 ptlrpc_request_set_replen(req);
975 req->rq_send_state = LUSTRE_IMP_FULL;
976 /* ptlrpc_request_pack already set timeout */
978 req->rq_timeout = ldlm_get_rq_timeout();
980 /* We only send real blocking ASTs after the lock is granted */
981 lock_res_and_lock(lock);
982 if (lock->l_flags & LDLM_FL_AST_SENT) {
983 body->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
984 /* Copy AST flags like LDLM_FL_DISCARD_DATA. */
985 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags &
988 /* We might get here prior to ldlm_handle_enqueue setting
989 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
990 * into waiting list, but this is safe and similar code in
991 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
992 * that would not only cancel the lock, but will also remove
993 * it from waiting list */
994 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
995 unlock_res_and_lock(lock);
996 ldlm_lock_cancel(lock);
998 lock_res_and_lock(lock);
1000 /* start the lock-timeout clock */
1001 ldlm_add_waiting_lock(lock);
1004 unlock_res_and_lock(lock);
1006 if (lock->l_export && lock->l_export->exp_nid_stats &&
1007 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1008 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1009 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1011 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1013 RETURN(lvb_len < 0 ? lvb_len : rc);
1015 EXPORT_SYMBOL(ldlm_server_completion_ast);
1018 * Server side ->l_glimpse_ast handler for client locks.
1020 * Sends glimpse AST to the client and waits for reply. Then updates
1021 * lvbo with the result.
1023 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1025 struct ldlm_cb_set_arg *arg = data;
1026 struct ldlm_request *body;
1027 struct ptlrpc_request *req;
1028 struct ldlm_cb_async_args *ca;
1030 struct req_format *req_fmt;
1033 LASSERT(lock != NULL);
1035 if (arg->gl_desc != NULL)
1036 /* There is a glimpse descriptor to pack */
1037 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1039 req_fmt = &RQF_LDLM_GL_CALLBACK;
1041 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1042 req_fmt, LUSTRE_DLM_VERSION,
1048 if (arg->gl_desc != NULL) {
1049 /* copy the GL descriptor */
1050 union ldlm_gl_desc *desc;
1051 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1052 *desc = *arg->gl_desc;
1055 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1056 body->lock_handle[0] = lock->l_remote_handle;
1057 ldlm_lock2desc(lock, &body->lock_desc);
1059 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1060 ca = ptlrpc_req_async_args(req);
1061 ca->ca_set_arg = arg;
1064 /* server namespace, doesn't need lock */
1065 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1066 ldlm_lvbo_size(lock));
1067 ptlrpc_request_set_replen(req);
1069 req->rq_send_state = LUSTRE_IMP_FULL;
1070 /* ptlrpc_request_alloc_pack already set timeout */
1072 req->rq_timeout = ldlm_get_rq_timeout();
1074 req->rq_interpret_reply = ldlm_cb_interpret;
1076 if (lock->l_export && lock->l_export->exp_nid_stats &&
1077 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1078 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1079 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1081 rc = ldlm_ast_fini(req, arg, lock, 0);
1085 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1087 int ldlm_glimpse_locks(struct ldlm_resource *res, cfs_list_t *gl_work_list)
1092 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1094 if (rc == -ERESTART)
1095 ldlm_reprocess_all(res);
1099 EXPORT_SYMBOL(ldlm_glimpse_locks);
1101 /* return LDLM lock associated with a lock callback request */
1102 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req)
1104 struct ldlm_cb_async_args *ca;
1105 struct ldlm_lock *lock;
1108 ca = ptlrpc_req_async_args(req);
1111 RETURN(ERR_PTR(-EFAULT));
1115 EXPORT_SYMBOL(ldlm_request_lock);
1117 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1118 struct lprocfs_stats *srv_stats)
1120 int lock_type = 0, op = 0;
1122 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1124 switch (lock_type) {
1126 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1129 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1130 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1132 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1135 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1138 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1146 lprocfs_counter_incr(srv_stats, op);
1152 * Main server-side entry point into LDLM for enqueue. This is called by ptlrpc
1153 * service threads to carry out client lock enqueueing requests.
1155 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1156 struct ptlrpc_request *req,
1157 const struct ldlm_request *dlm_req,
1158 const struct ldlm_callback_suite *cbs)
1160 struct ldlm_reply *dlm_rep;
1162 ldlm_error_t err = ELDLM_OK;
1163 struct ldlm_lock *lock = NULL;
1164 void *cookie = NULL;
1168 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1170 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1171 flags = ldlm_flags_from_wire(dlm_req->lock_flags);
1173 LASSERT(req->rq_export);
1175 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1176 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1178 if (req->rq_export && req->rq_export->exp_nid_stats &&
1179 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1180 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1181 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1183 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1184 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1185 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1186 dlm_req->lock_desc.l_resource.lr_type);
1187 GOTO(out, rc = -EFAULT);
1190 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1191 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1192 dlm_req->lock_desc.l_req_mode &
1193 (dlm_req->lock_desc.l_req_mode-1))) {
1194 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1195 dlm_req->lock_desc.l_req_mode);
1196 GOTO(out, rc = -EFAULT);
1199 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) {
1200 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1202 DEBUG_REQ(D_ERROR, req,
1203 "PLAIN lock request from IBITS client?");
1204 GOTO(out, rc = -EPROTO);
1206 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1208 DEBUG_REQ(D_ERROR, req,
1209 "IBITS lock request from unaware client?");
1210 GOTO(out, rc = -EPROTO);
1214 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1215 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1216 ibits for mgc/mgs) */
1218 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1219 * inodebits lock if client does not support them. */
1220 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) &&
1221 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1222 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1223 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1224 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1225 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1226 dlm_req->lock_desc.l_req_mode = LCK_CR;
1230 if (unlikely(flags & LDLM_FL_REPLAY)) {
1231 /* Find an existing lock in the per-export lock hash */
1232 /* In the function below, .hs_keycmp resolves to
1233 * ldlm_export_lock_keycmp() */
1234 /* coverity[overrun-buffer-val] */
1235 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1236 (void *)&dlm_req->lock_handle[0]);
1238 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1239 LPX64, lock->l_handle.h_cookie);
1240 GOTO(existing_lock, rc = 0);
1244 /* The lock's callback data might be set in the policy function */
1245 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1246 dlm_req->lock_desc.l_resource.lr_type,
1247 dlm_req->lock_desc.l_req_mode,
1248 cbs, NULL, 0, LVB_T_NONE);
1250 GOTO(out, rc = -ENOMEM);
1252 lock->l_last_activity = cfs_time_current_sec();
1253 lock->l_remote_handle = dlm_req->lock_handle[0];
1254 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1256 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1257 /* Don't enqueue a lock onto the export if it is been disonnected
1258 * due to eviction (bug 3822) or server umount (bug 24324).
1259 * Cancel it now instead. */
1260 if (req->rq_export->exp_disconnected) {
1261 LDLM_ERROR(lock, "lock on disconnected export %p",
1263 GOTO(out, rc = -ENOTCONN);
1266 lock->l_export = class_export_lock_get(req->rq_export, lock);
1267 if (lock->l_export->exp_lock_hash)
1268 cfs_hash_add(lock->l_export->exp_lock_hash,
1269 &lock->l_remote_handle,
1274 if (flags & LDLM_FL_HAS_INTENT) {
1275 /* In this case, the reply buffer is allocated deep in
1276 * local_lock_enqueue by the policy function. */
1279 /* based on the assumption that lvb size never changes during
1280 * resource life time otherwise it need resource->lr_lock's
1282 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1283 RCL_SERVER, ldlm_lvbo_size(lock));
1285 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1286 GOTO(out, rc = -ENOMEM);
1288 rc = req_capsule_server_pack(&req->rq_pill);
1293 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1294 ldlm_convert_policy_to_local(req->rq_export,
1295 dlm_req->lock_desc.l_resource.lr_type,
1296 &dlm_req->lock_desc.l_policy_data,
1297 &lock->l_policy_data);
1298 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1299 lock->l_req_extent = lock->l_policy_data.l_extent;
1301 err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
1308 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1309 dlm_rep->lock_flags = ldlm_flags_to_wire(flags);
1311 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1312 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1314 /* We never send a blocking AST until the lock is granted, but
1315 * we can tell it right now */
1316 lock_res_and_lock(lock);
1318 /* Now take into account flags to be inherited from original lock
1319 request both in reply to client and in our own lock flags. */
1320 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1321 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
1322 LDLM_INHERIT_FLAGS);
1324 /* Don't move a pending lock onto the export if it has already been
1325 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1326 * Cancel it now instead. */
1327 if (unlikely(req->rq_export->exp_disconnected ||
1328 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1329 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1331 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1332 dlm_rep->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1333 if (lock->l_granted_mode == lock->l_req_mode) {
1335 * Only cancel lock if it was granted, because it would
1336 * be destroyed immediately and would never be granted
1337 * in the future, causing timeouts on client. Not
1338 * granted lock will be cancelled immediately after
1339 * sending completion AST.
1341 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1342 unlock_res_and_lock(lock);
1343 ldlm_lock_cancel(lock);
1344 lock_res_and_lock(lock);
1346 ldlm_add_waiting_lock(lock);
1349 /* Make sure we never ever grant usual metadata locks to liblustre
1351 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1352 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1353 req->rq_export->exp_libclient) {
1354 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1355 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1356 CERROR("Granting sync lock to libclient. "
1357 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1358 dlm_req->lock_flags, dlm_rep->lock_flags,
1360 LDLM_ERROR(lock, "sync lock");
1361 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1362 struct ldlm_intent *it;
1364 it = req_capsule_client_get(&req->rq_pill,
1367 CERROR("This is intent %s ("LPU64")\n",
1368 ldlm_it2str(it->opc), it->opc);
1374 unlock_res_and_lock(lock);
1378 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1379 if (!req->rq_packed_final) {
1380 err = lustre_pack_reply(req, 1, NULL, NULL);
1385 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1386 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1388 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1389 "(err=%d, rc=%d)", err, rc);
1392 if (req_capsule_has_field(&req->rq_pill, &RMF_DLM_LVB,
1394 ldlm_lvbo_size(lock) > 0) {
1398 buf = req_capsule_server_get(&req->rq_pill,
1400 LASSERTF(buf != NULL, "req %p, lock %p\n",
1402 buflen = req_capsule_get_size(&req->rq_pill,
1403 &RMF_DLM_LVB, RCL_SERVER);
1405 buflen = ldlm_lvbo_fill(lock, buf,
1411 buflen, RCL_SERVER);
1419 lock_res_and_lock(lock);
1420 ldlm_resource_unlink_lock(lock);
1421 ldlm_lock_destroy_nolock(lock);
1422 unlock_res_and_lock(lock);
1425 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1426 ldlm_reprocess_all(lock->l_resource);
1428 LDLM_LOCK_RELEASE(lock);
1431 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1436 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1439 * Old-style LDLM main entry point for server code enqueue.
1441 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1442 ldlm_completion_callback completion_callback,
1443 ldlm_blocking_callback blocking_callback,
1444 ldlm_glimpse_callback glimpse_callback)
1446 struct ldlm_request *dlm_req;
1447 struct ldlm_callback_suite cbs = {
1448 .lcs_completion = completion_callback,
1449 .lcs_blocking = blocking_callback,
1450 .lcs_glimpse = glimpse_callback
1454 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1455 if (dlm_req != NULL) {
1456 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1457 req, dlm_req, &cbs);
1463 EXPORT_SYMBOL(ldlm_handle_enqueue);
1466 * Main LDLM entry point for server code to process lock conversion requests.
1468 int ldlm_handle_convert0(struct ptlrpc_request *req,
1469 const struct ldlm_request *dlm_req)
1471 struct ldlm_reply *dlm_rep;
1472 struct ldlm_lock *lock;
1476 if (req->rq_export && req->rq_export->exp_nid_stats &&
1477 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1478 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1479 LDLM_CONVERT - LDLM_FIRST_OPC);
1481 rc = req_capsule_server_pack(&req->rq_pill);
1485 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1486 dlm_rep->lock_flags = dlm_req->lock_flags;
1488 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1490 req->rq_status = LUSTRE_EINVAL;
1494 LDLM_DEBUG(lock, "server-side convert handler START");
1496 lock->l_last_activity = cfs_time_current_sec();
1497 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1498 &dlm_rep->lock_flags);
1500 if (ldlm_del_waiting_lock(lock))
1501 LDLM_DEBUG(lock, "converted waiting lock");
1504 req->rq_status = LUSTRE_EDEADLK;
1509 if (!req->rq_status)
1510 ldlm_reprocess_all(lock->l_resource);
1511 LDLM_DEBUG(lock, "server-side convert handler END");
1512 LDLM_LOCK_PUT(lock);
1514 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1518 EXPORT_SYMBOL(ldlm_handle_convert0);
1521 * Old-style main LDLM entry point for server code to process lock conversion
1524 int ldlm_handle_convert(struct ptlrpc_request *req)
1527 struct ldlm_request *dlm_req;
1529 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1530 if (dlm_req != NULL) {
1531 rc = ldlm_handle_convert0(req, dlm_req);
1533 CERROR ("Can't unpack dlm_req\n");
1538 EXPORT_SYMBOL(ldlm_handle_convert);
1541 * Cancel all the locks whose handles are packed into ldlm_request
1543 * Called by server code expecting such combined cancel activity
1546 int ldlm_request_cancel(struct ptlrpc_request *req,
1547 const struct ldlm_request *dlm_req, int first)
1549 struct ldlm_resource *res, *pres = NULL;
1550 struct ldlm_lock *lock;
1551 int i, count, done = 0;
1554 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1558 if (count == 1 && dlm_req->lock_handle[0].cookie == 0)
1561 /* There is no lock on the server at the replay time,
1562 * skip lock cancelling to make replay tests to pass. */
1563 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1566 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1567 "starting at %d", count, first);
1569 for (i = first; i < count; i++) {
1570 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1572 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1573 "lock (cookie "LPU64")",
1574 dlm_req->lock_handle[i].cookie);
1578 res = lock->l_resource;
1581 /* This code is an optimization to only attempt lock
1582 * granting on the resource (that could be CPU-expensive)
1583 * after we are done cancelling lock in that resource. */
1586 ldlm_reprocess_all(pres);
1587 LDLM_RESOURCE_DELREF(pres);
1588 ldlm_resource_putref(pres);
1591 ldlm_resource_getref(res);
1592 LDLM_RESOURCE_ADDREF(res);
1593 ldlm_res_lvbo_update(res, NULL, 1);
1597 ldlm_lock_cancel(lock);
1598 LDLM_LOCK_PUT(lock);
1601 ldlm_reprocess_all(pres);
1602 LDLM_RESOURCE_DELREF(pres);
1603 ldlm_resource_putref(pres);
1605 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1608 EXPORT_SYMBOL(ldlm_request_cancel);
1611 * Main LDLM entry point for server code to cancel locks.
1613 * Typically gets called from service handler on LDLM_CANCEL opc.
1615 int ldlm_handle_cancel(struct ptlrpc_request *req)
1617 struct ldlm_request *dlm_req;
1621 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1622 if (dlm_req == NULL) {
1623 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1627 if (req->rq_export && req->rq_export->exp_nid_stats &&
1628 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1629 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1630 LDLM_CANCEL - LDLM_FIRST_OPC);
1632 rc = req_capsule_server_pack(&req->rq_pill);
1636 if (!ldlm_request_cancel(req, dlm_req, 0))
1637 req->rq_status = LUSTRE_ESTALE;
1639 RETURN(ptlrpc_reply(req));
1641 EXPORT_SYMBOL(ldlm_handle_cancel);
1642 #endif /* HAVE_SERVER_SUPPORT */
1645 * Callback handler for receiving incoming blocking ASTs.
1647 * This can only happen on client side.
1649 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1650 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1655 LDLM_DEBUG(lock, "client blocking AST callback handler");
1657 lock_res_and_lock(lock);
1658 lock->l_flags |= LDLM_FL_CBPENDING;
1660 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1661 lock->l_flags |= LDLM_FL_CANCEL;
1663 do_ast = (!lock->l_readers && !lock->l_writers);
1664 unlock_res_and_lock(lock);
1667 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1668 lock, lock->l_blocking_ast);
1669 if (lock->l_blocking_ast != NULL)
1670 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1673 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1677 LDLM_DEBUG(lock, "client blocking callback handler END");
1678 LDLM_LOCK_RELEASE(lock);
1683 * Callback handler for receiving incoming completion ASTs.
1685 * This only can happen on client side.
1687 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1688 struct ldlm_namespace *ns,
1689 struct ldlm_request *dlm_req,
1690 struct ldlm_lock *lock)
1693 CFS_LIST_HEAD(ast_list);
1697 LDLM_DEBUG(lock, "client completion callback handler START");
1699 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1700 int to = cfs_time_seconds(1);
1702 schedule_timeout_and_set_state(
1703 TASK_INTERRUPTIBLE, to);
1704 if (lock->l_granted_mode == lock->l_req_mode ||
1705 lock->l_flags & LDLM_FL_DESTROYED)
1710 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1712 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", lvb_len);
1713 GOTO(out, rc = lvb_len);
1714 } else if (lvb_len > 0) {
1715 if (lock->l_lvb_len > 0) {
1716 /* for extent lock, lvb contains ost_lvb{}. */
1717 LASSERT(lock->l_lvb_data != NULL);
1719 if (unlikely(lock->l_lvb_len < lvb_len)) {
1720 LDLM_ERROR(lock, "Replied LVB is larger than "
1721 "expectation, expected = %d, "
1723 lock->l_lvb_len, lvb_len);
1724 GOTO(out, rc = -EINVAL);
1726 } else if (ldlm_has_layout(lock)) { /* for layout lock, lvb has
1727 * variable length */
1730 OBD_ALLOC(lvb_data, lvb_len);
1731 if (lvb_data == NULL) {
1732 LDLM_ERROR(lock, "No memory: %d.\n", lvb_len);
1733 GOTO(out, rc = -ENOMEM);
1736 lock_res_and_lock(lock);
1737 LASSERT(lock->l_lvb_data == NULL);
1738 lock->l_lvb_data = lvb_data;
1739 lock->l_lvb_len = lvb_len;
1740 unlock_res_and_lock(lock);
1744 lock_res_and_lock(lock);
1745 if ((lock->l_flags & LDLM_FL_DESTROYED) ||
1746 lock->l_granted_mode == lock->l_req_mode) {
1747 /* bug 11300: the lock has already been granted */
1748 unlock_res_and_lock(lock);
1749 LDLM_DEBUG(lock, "Double grant race happened");
1753 /* If we receive the completion AST before the actual enqueue returned,
1754 * then we might need to switch lock modes, resources, or extents. */
1755 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1756 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1757 LDLM_DEBUG(lock, "completion AST, new lock mode");
1760 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1761 ldlm_convert_policy_to_local(req->rq_export,
1762 dlm_req->lock_desc.l_resource.lr_type,
1763 &dlm_req->lock_desc.l_policy_data,
1764 &lock->l_policy_data);
1765 LDLM_DEBUG(lock, "completion AST, new policy data");
1768 ldlm_resource_unlink_lock(lock);
1769 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1770 &lock->l_resource->lr_name,
1771 sizeof(lock->l_resource->lr_name)) != 0) {
1772 unlock_res_and_lock(lock);
1773 rc = ldlm_lock_change_resource(ns, lock,
1774 &dlm_req->lock_desc.l_resource.lr_name);
1776 LDLM_ERROR(lock, "Failed to allocate resource");
1779 LDLM_DEBUG(lock, "completion AST, new resource");
1780 CERROR("change resource!\n");
1781 lock_res_and_lock(lock);
1784 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1785 /* BL_AST locks are not needed in LRU.
1786 * Let ldlm_cancel_lru() be fast. */
1787 ldlm_lock_remove_from_lru(lock);
1788 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1789 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1792 if (lock->l_lvb_len > 0) {
1793 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_CLIENT,
1794 lock->l_lvb_data, lvb_len);
1796 unlock_res_and_lock(lock);
1801 ldlm_grant_lock(lock, &ast_list);
1802 unlock_res_and_lock(lock);
1804 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1806 /* Let Enqueue to call osc_lock_upcall() and initialize
1808 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1810 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1812 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1818 lock_res_and_lock(lock);
1819 lock->l_flags |= LDLM_FL_FAILED;
1820 unlock_res_and_lock(lock);
1821 wake_up(&lock->l_waitq);
1823 LDLM_LOCK_RELEASE(lock);
1827 * Callback handler for receiving incoming glimpse ASTs.
1829 * This only can happen on client side. After handling the glimpse AST
1830 * we also consider dropping the lock here if it is unused locally for a
1833 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1834 struct ldlm_namespace *ns,
1835 struct ldlm_request *dlm_req,
1836 struct ldlm_lock *lock)
1841 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1843 if (lock->l_glimpse_ast != NULL)
1844 rc = lock->l_glimpse_ast(lock, req);
1846 if (req->rq_repmsg != NULL) {
1849 req->rq_status = rc;
1853 lock_res_and_lock(lock);
1854 if (lock->l_granted_mode == LCK_PW &&
1855 !lock->l_readers && !lock->l_writers &&
1856 cfs_time_after(cfs_time_current(),
1857 cfs_time_add(lock->l_last_used,
1858 cfs_time_seconds(10)))) {
1859 unlock_res_and_lock(lock);
1860 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1861 ldlm_handle_bl_callback(ns, NULL, lock);
1866 unlock_res_and_lock(lock);
1867 LDLM_LOCK_RELEASE(lock);
1871 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1873 if (req->rq_no_reply)
1876 req->rq_status = rc;
1877 if (!req->rq_packed_final) {
1878 rc = lustre_pack_reply(req, 1, NULL, NULL);
1882 return ptlrpc_reply(req);
1886 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi,
1887 ldlm_cancel_flags_t cancel_flags)
1889 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1892 spin_lock(&blp->blp_lock);
1893 if (blwi->blwi_lock &&
1894 blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1895 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1896 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1898 /* other blocking callbacks are added to the regular list */
1899 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1901 spin_unlock(&blp->blp_lock);
1903 wake_up(&blp->blp_waitq);
1905 /* can not check blwi->blwi_flags as blwi could be already freed in
1907 if (!(cancel_flags & LCF_ASYNC))
1908 wait_for_completion(&blwi->blwi_comp);
1913 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1914 struct ldlm_namespace *ns,
1915 struct ldlm_lock_desc *ld,
1916 cfs_list_t *cancels, int count,
1917 struct ldlm_lock *lock,
1918 ldlm_cancel_flags_t cancel_flags)
1920 init_completion(&blwi->blwi_comp);
1921 CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1923 if (memory_pressure_get())
1924 blwi->blwi_mem_pressure = 1;
1927 blwi->blwi_flags = cancel_flags;
1929 blwi->blwi_ld = *ld;
1931 cfs_list_add(&blwi->blwi_head, cancels);
1932 cfs_list_del_init(cancels);
1933 blwi->blwi_count = count;
1935 blwi->blwi_lock = lock;
1940 * Queues a list of locks \a cancels containing \a count locks
1941 * for later processing by a blocking thread. If \a count is zero,
1942 * then the lock referenced as \a lock is queued instead.
1944 * The blocking thread would then call ->l_blocking_ast callback in the lock.
1945 * If list addition fails an error is returned and caller is supposed to
1946 * call ->l_blocking_ast itself.
1948 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1949 struct ldlm_lock_desc *ld,
1950 struct ldlm_lock *lock,
1951 cfs_list_t *cancels, int count,
1952 ldlm_cancel_flags_t cancel_flags)
1956 if (cancels && count == 0)
1959 if (cancel_flags & LCF_ASYNC) {
1960 struct ldlm_bl_work_item *blwi;
1962 OBD_ALLOC(blwi, sizeof(*blwi));
1965 init_blwi(blwi, ns, ld, cancels, count, lock, cancel_flags);
1967 RETURN(__ldlm_bl_to_thread(blwi, cancel_flags));
1969 /* if it is synchronous call do minimum mem alloc, as it could
1970 * be triggered from kernel shrinker
1972 struct ldlm_bl_work_item blwi;
1974 memset(&blwi, 0, sizeof(blwi));
1975 init_blwi(&blwi, ns, ld, cancels, count, lock, cancel_flags);
1976 RETURN(__ldlm_bl_to_thread(&blwi, cancel_flags));
1982 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1983 struct ldlm_lock *lock)
1986 return ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LCF_ASYNC);
1992 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1993 cfs_list_t *cancels, int count,
1994 ldlm_cancel_flags_t cancel_flags)
1997 return ldlm_bl_to_thread(ns, ld, NULL, cancels, count, cancel_flags);
2003 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
2004 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
2006 struct obd_device *obd = req->rq_export->exp_obd;
2013 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
2015 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2017 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
2019 DEBUG_REQ(D_IOCTL, req, "no set_info key");
2022 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
2024 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
2026 DEBUG_REQ(D_IOCTL, req, "no set_info val");
2029 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
2032 /* We are responsible for swabbing contents of val */
2034 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
2035 /* Pass it on to mdc (the "export" in this case) */
2036 rc = obd_set_info_async(req->rq_svc_thread->t_env,
2038 sizeof(KEY_HSM_COPYTOOL_SEND),
2039 KEY_HSM_COPYTOOL_SEND,
2042 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
2047 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
2048 const char *msg, int rc,
2049 struct lustre_handle *handle)
2051 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
2052 "%s: [nid %s] [rc %d] [lock "LPX64"]",
2053 msg, libcfs_id2str(req->rq_peer), rc,
2054 handle ? handle->cookie : 0);
2055 if (req->rq_no_reply)
2056 CWARN("No reply was sent, maybe cause bug 21636.\n");
2058 CWARN("Send reply failed, maybe cause bug 21636.\n");
2061 static int ldlm_handle_qc_callback(struct ptlrpc_request *req)
2063 struct obd_quotactl *oqctl;
2064 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2066 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2067 if (oqctl == NULL) {
2068 CERROR("Can't unpack obd_quotactl\n");
2072 oqctl->qc_stat = ptlrpc_status_ntoh(oqctl->qc_stat);
2074 cli->cl_qchk_stat = oqctl->qc_stat;
2078 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2079 static int ldlm_callback_handler(struct ptlrpc_request *req)
2081 struct ldlm_namespace *ns;
2082 struct ldlm_request *dlm_req;
2083 struct ldlm_lock *lock;
2087 /* Requests arrive in sender's byte order. The ptlrpc service
2088 * handler has already checked and, if necessary, byte-swapped the
2089 * incoming request message body, but I am responsible for the
2090 * message buffers. */
2092 /* do nothing for sec context finalize */
2093 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
2096 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2098 if (req->rq_export == NULL) {
2099 rc = ldlm_callback_reply(req, -ENOTCONN);
2100 ldlm_callback_errmsg(req, "Operate on unconnected server",
2105 LASSERT(req->rq_export != NULL);
2106 LASSERT(req->rq_export->exp_obd != NULL);
2108 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2109 case LDLM_BL_CALLBACK:
2110 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
2113 case LDLM_CP_CALLBACK:
2114 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
2117 case LDLM_GL_CALLBACK:
2118 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
2122 rc = ldlm_handle_setinfo(req);
2123 ldlm_callback_reply(req, rc);
2125 case LLOG_ORIGIN_HANDLE_CREATE:
2126 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2127 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2129 rc = llog_origin_handle_open(req);
2130 ldlm_callback_reply(req, rc);
2132 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2133 req_capsule_set(&req->rq_pill,
2134 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2135 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2137 rc = llog_origin_handle_next_block(req);
2138 ldlm_callback_reply(req, rc);
2140 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2141 req_capsule_set(&req->rq_pill,
2142 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2143 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2145 rc = llog_origin_handle_read_header(req);
2146 ldlm_callback_reply(req, rc);
2148 case LLOG_ORIGIN_HANDLE_CLOSE:
2149 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2151 rc = llog_origin_handle_close(req);
2152 ldlm_callback_reply(req, rc);
2154 case OBD_QC_CALLBACK:
2155 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
2156 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
2158 rc = ldlm_handle_qc_callback(req);
2159 ldlm_callback_reply(req, rc);
2162 CERROR("unknown opcode %u\n",
2163 lustre_msg_get_opc(req->rq_reqmsg));
2164 ldlm_callback_reply(req, -EPROTO);
2168 ns = req->rq_export->exp_obd->obd_namespace;
2169 LASSERT(ns != NULL);
2171 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2173 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2174 if (dlm_req == NULL) {
2175 rc = ldlm_callback_reply(req, -EPROTO);
2176 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2181 /* Force a known safe race, send a cancel to the server for a lock
2182 * which the server has already started a blocking callback on. */
2183 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2184 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2185 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0], 0);
2187 CERROR("ldlm_cli_cancel: %d\n", rc);
2190 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2192 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2193 "disappeared\n", dlm_req->lock_handle[0].cookie);
2194 rc = ldlm_callback_reply(req, -EINVAL);
2195 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2196 &dlm_req->lock_handle[0]);
2200 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2201 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2202 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2204 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2205 lock_res_and_lock(lock);
2206 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
2208 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2209 /* If somebody cancels lock and cache is already dropped,
2210 * or lock is failed before cp_ast received on client,
2211 * we can tell the server we have no lock. Otherwise, we
2212 * should send cancel after dropping the cache. */
2213 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2214 (lock->l_flags & LDLM_FL_BL_DONE)) ||
2215 (lock->l_flags & LDLM_FL_FAILED)) {
2216 LDLM_DEBUG(lock, "callback on lock "
2217 LPX64" - lock disappeared\n",
2218 dlm_req->lock_handle[0].cookie);
2219 unlock_res_and_lock(lock);
2220 LDLM_LOCK_RELEASE(lock);
2221 rc = ldlm_callback_reply(req, -EINVAL);
2222 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2223 &dlm_req->lock_handle[0]);
2226 /* BL_AST locks are not needed in LRU.
2227 * Let ldlm_cancel_lru() be fast. */
2228 ldlm_lock_remove_from_lru(lock);
2229 lock->l_flags |= LDLM_FL_BL_AST;
2231 unlock_res_and_lock(lock);
2233 /* We want the ost thread to get this reply so that it can respond
2234 * to ost requests (write cache writeback) that might be triggered
2237 * But we'd also like to be able to indicate in the reply that we're
2238 * cancelling right now, because it's unused, or have an intent result
2239 * in the reply, so we might have to push the responsibility for sending
2240 * the reply down into the AST handlers, alas. */
2242 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2243 case LDLM_BL_CALLBACK:
2244 CDEBUG(D_INODE, "blocking ast\n");
2245 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2246 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2247 rc = ldlm_callback_reply(req, 0);
2248 if (req->rq_no_reply || rc)
2249 ldlm_callback_errmsg(req, "Normal process", rc,
2250 &dlm_req->lock_handle[0]);
2252 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2253 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2255 case LDLM_CP_CALLBACK:
2256 CDEBUG(D_INODE, "completion ast\n");
2257 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2258 ldlm_callback_reply(req, 0);
2259 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2261 case LDLM_GL_CALLBACK:
2262 CDEBUG(D_INODE, "glimpse ast\n");
2263 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2264 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2267 LBUG(); /* checked above */
2273 #ifdef HAVE_SERVER_SUPPORT
2275 * Main handler for canceld thread.
2277 * Separated into its own thread to avoid deadlocks.
2279 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2284 /* Requests arrive in sender's byte order. The ptlrpc service
2285 * handler has already checked and, if necessary, byte-swapped the
2286 * incoming request message body, but I am responsible for the
2287 * message buffers. */
2289 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2291 if (req->rq_export == NULL) {
2292 struct ldlm_request *dlm_req;
2294 CERROR("%s from %s arrived at %lu with bad export cookie "
2296 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2297 libcfs_nid2str(req->rq_peer.nid),
2298 req->rq_arrival_time.tv_sec,
2299 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2301 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2302 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2303 dlm_req = req_capsule_client_get(&req->rq_pill,
2305 if (dlm_req != NULL)
2306 ldlm_lock_dump_handle(D_ERROR,
2307 &dlm_req->lock_handle[0]);
2309 ldlm_callback_reply(req, -ENOTCONN);
2313 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2315 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2317 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2318 CDEBUG(D_INODE, "cancel\n");
2319 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET) ||
2320 CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))
2322 rc = ldlm_handle_cancel(req);
2327 CERROR("invalid opcode %d\n",
2328 lustre_msg_get_opc(req->rq_reqmsg));
2329 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2330 ldlm_callback_reply(req, -EINVAL);
2336 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2337 struct ldlm_lock *lock)
2339 struct ldlm_request *dlm_req;
2340 struct lustre_handle lockh;
2345 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2346 if (dlm_req == NULL)
2349 ldlm_lock2handle(lock, &lockh);
2350 for (i = 0; i < dlm_req->lock_count; i++) {
2351 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2353 DEBUG_REQ(D_RPCTRACE, req,
2354 "Prio raised by lock "LPX64".", lockh.cookie);
2365 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2367 struct ldlm_request *dlm_req;
2372 /* no prolong in recovery */
2373 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2376 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2377 if (dlm_req == NULL)
2380 for (i = 0; i < dlm_req->lock_count; i++) {
2381 struct ldlm_lock *lock;
2383 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2387 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2389 LDLM_DEBUG(lock, "hpreq cancel lock");
2390 LDLM_LOCK_PUT(lock);
2399 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2400 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2401 .hpreq_check = ldlm_cancel_hpreq_check,
2405 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2409 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2411 if (req->rq_export == NULL)
2414 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2415 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2416 req->rq_ops = &ldlm_cancel_hpreq_ops;
2421 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2422 cfs_hlist_node_t *hnode, void *data)
2425 cfs_list_t *rpc_list = data;
2426 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2428 lock_res_and_lock(lock);
2430 if (lock->l_req_mode != lock->l_granted_mode) {
2431 unlock_res_and_lock(lock);
2435 LASSERT(lock->l_resource);
2436 if (lock->l_resource->lr_type != LDLM_IBITS &&
2437 lock->l_resource->lr_type != LDLM_PLAIN) {
2438 unlock_res_and_lock(lock);
2442 if (lock->l_flags & LDLM_FL_AST_SENT) {
2443 unlock_res_and_lock(lock);
2447 LASSERT(lock->l_blocking_ast);
2448 LASSERT(!lock->l_blocking_lock);
2450 lock->l_flags |= LDLM_FL_AST_SENT;
2451 if (lock->l_export && lock->l_export->exp_lock_hash) {
2452 /* NB: it's safe to call cfs_hash_del() even lock isn't
2453 * in exp_lock_hash. */
2454 /* In the function below, .hs_keycmp resolves to
2455 * ldlm_export_lock_keycmp() */
2456 /* coverity[overrun-buffer-val] */
2457 cfs_hash_del(lock->l_export->exp_lock_hash,
2458 &lock->l_remote_handle, &lock->l_exp_hash);
2461 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2462 LDLM_LOCK_GET(lock);
2464 unlock_res_and_lock(lock);
2468 void ldlm_revoke_export_locks(struct obd_export *exp)
2470 cfs_list_t rpc_list;
2473 CFS_INIT_LIST_HEAD(&rpc_list);
2474 cfs_hash_for_each_empty(exp->exp_lock_hash,
2475 ldlm_revoke_lock_cb, &rpc_list);
2476 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2477 LDLM_WORK_REVOKE_AST);
2481 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2482 #endif /* HAVE_SERVER_SUPPORT */
2485 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2487 struct ldlm_bl_work_item *blwi = NULL;
2488 static unsigned int num_bl = 0;
2490 spin_lock(&blp->blp_lock);
2491 /* process a request from the blp_list at least every blp_num_threads */
2492 if (!cfs_list_empty(&blp->blp_list) &&
2493 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2494 blwi = cfs_list_entry(blp->blp_list.next,
2495 struct ldlm_bl_work_item, blwi_entry);
2497 if (!cfs_list_empty(&blp->blp_prio_list))
2498 blwi = cfs_list_entry(blp->blp_prio_list.next,
2499 struct ldlm_bl_work_item,
2503 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2505 cfs_list_del(&blwi->blwi_entry);
2507 spin_unlock(&blp->blp_lock);
2512 /* This only contains temporary data until the thread starts */
2513 struct ldlm_bl_thread_data {
2514 char bltd_name[CFS_CURPROC_COMM_MAX];
2515 struct ldlm_bl_pool *bltd_blp;
2516 struct completion bltd_comp;
2520 static int ldlm_bl_thread_main(void *arg);
2522 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2524 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2527 init_completion(&bltd.bltd_comp);
2528 bltd.bltd_num = cfs_atomic_read(&blp->blp_num_threads);
2529 snprintf(bltd.bltd_name, sizeof(bltd.bltd_name) - 1,
2530 "ldlm_bl_%02d", bltd.bltd_num);
2531 task = kthread_run(ldlm_bl_thread_main, &bltd, bltd.bltd_name);
2533 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %ld\n",
2534 cfs_atomic_read(&blp->blp_num_threads), PTR_ERR(task));
2535 return PTR_ERR(task);
2537 wait_for_completion(&bltd.bltd_comp);
2543 * Main blocking requests processing thread.
2545 * Callers put locks into its queue by calling ldlm_bl_to_thread.
2546 * This thread in the end ends up doing actual call to ->l_blocking_ast
2549 static int ldlm_bl_thread_main(void *arg)
2551 struct ldlm_bl_pool *blp;
2555 struct ldlm_bl_thread_data *bltd = arg;
2557 blp = bltd->bltd_blp;
2559 cfs_atomic_inc(&blp->blp_num_threads);
2560 cfs_atomic_inc(&blp->blp_busy_threads);
2562 complete(&bltd->bltd_comp);
2563 /* cannot use bltd after this, it is only on caller's stack */
2567 struct l_wait_info lwi = { 0 };
2568 struct ldlm_bl_work_item *blwi = NULL;
2571 blwi = ldlm_bl_get_work(blp);
2574 cfs_atomic_dec(&blp->blp_busy_threads);
2575 l_wait_event_exclusive(blp->blp_waitq,
2576 (blwi = ldlm_bl_get_work(blp)) != NULL,
2578 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2580 busy = cfs_atomic_read(&blp->blp_busy_threads);
2583 if (blwi->blwi_ns == NULL)
2584 /* added by ldlm_cleanup() */
2587 /* Not fatal if racy and have a few too many threads */
2588 if (unlikely(busy < blp->blp_max_threads &&
2589 busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2590 !blwi->blwi_mem_pressure))
2591 /* discard the return value, we tried */
2592 ldlm_bl_thread_start(blp);
2594 if (blwi->blwi_mem_pressure)
2595 memory_pressure_set();
2597 if (blwi->blwi_count) {
2599 /* The special case when we cancel locks in LRU
2600 * asynchronously, we pass the list of locks here.
2601 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2602 * canceled locally yet. */
2603 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2606 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL,
2609 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2612 if (blwi->blwi_mem_pressure)
2613 memory_pressure_clr();
2615 if (blwi->blwi_flags & LCF_ASYNC)
2616 OBD_FREE(blwi, sizeof(*blwi));
2618 complete(&blwi->blwi_comp);
2621 cfs_atomic_dec(&blp->blp_busy_threads);
2622 cfs_atomic_dec(&blp->blp_num_threads);
2623 complete(&blp->blp_comp);
2629 static int ldlm_setup(void);
2630 static int ldlm_cleanup(void);
2632 int ldlm_get_ref(void)
2636 mutex_lock(&ldlm_ref_mutex);
2637 if (++ldlm_refcount == 1) {
2642 mutex_unlock(&ldlm_ref_mutex);
2646 EXPORT_SYMBOL(ldlm_get_ref);
2648 void ldlm_put_ref(void)
2651 mutex_lock(&ldlm_ref_mutex);
2652 if (ldlm_refcount == 1) {
2653 int rc = ldlm_cleanup();
2655 CERROR("ldlm_cleanup failed: %d\n", rc);
2661 mutex_unlock(&ldlm_ref_mutex);
2665 EXPORT_SYMBOL(ldlm_put_ref);
2668 * Export handle<->lock hash operations.
2671 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2673 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2677 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2679 struct ldlm_lock *lock;
2681 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2682 return &lock->l_remote_handle;
2686 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2688 struct ldlm_lock *lock;
2690 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2691 lock->l_remote_handle = *(struct lustre_handle *)key;
2695 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2697 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2701 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2703 return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2707 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2709 struct ldlm_lock *lock;
2711 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2712 LDLM_LOCK_GET(lock);
2716 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2718 struct ldlm_lock *lock;
2720 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2721 LDLM_LOCK_RELEASE(lock);
2724 static cfs_hash_ops_t ldlm_export_lock_ops = {
2725 .hs_hash = ldlm_export_lock_hash,
2726 .hs_key = ldlm_export_lock_key,
2727 .hs_keycmp = ldlm_export_lock_keycmp,
2728 .hs_keycpy = ldlm_export_lock_keycpy,
2729 .hs_object = ldlm_export_lock_object,
2730 .hs_get = ldlm_export_lock_get,
2731 .hs_put = ldlm_export_lock_put,
2732 .hs_put_locked = ldlm_export_lock_put,
2735 int ldlm_init_export(struct obd_export *exp)
2740 exp->exp_lock_hash =
2741 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2742 HASH_EXP_LOCK_CUR_BITS,
2743 HASH_EXP_LOCK_MAX_BITS,
2744 HASH_EXP_LOCK_BKT_BITS, 0,
2745 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2746 &ldlm_export_lock_ops,
2747 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2748 CFS_HASH_NBLK_CHANGE);
2750 if (!exp->exp_lock_hash)
2753 rc = ldlm_init_flock_export(exp);
2759 ldlm_destroy_export(exp);
2762 EXPORT_SYMBOL(ldlm_init_export);
2764 void ldlm_destroy_export(struct obd_export *exp)
2767 cfs_hash_putref(exp->exp_lock_hash);
2768 exp->exp_lock_hash = NULL;
2770 ldlm_destroy_flock_export(exp);
2773 EXPORT_SYMBOL(ldlm_destroy_export);
2775 static int ldlm_setup(void)
2777 static struct ptlrpc_service_conf conf;
2778 struct ldlm_bl_pool *blp = NULL;
2785 if (ldlm_state != NULL)
2788 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2789 if (ldlm_state == NULL)
2793 rc = ldlm_proc_setup();
2798 memset(&conf, 0, sizeof(conf));
2799 conf = (typeof(conf)) {
2800 .psc_name = "ldlm_cbd",
2801 .psc_watchdog_factor = 2,
2803 .bc_nbufs = LDLM_CLIENT_NBUFS,
2804 .bc_buf_size = LDLM_BUFSIZE,
2805 .bc_req_max_size = LDLM_MAXREQSIZE,
2806 .bc_rep_max_size = LDLM_MAXREPSIZE,
2807 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2808 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2811 .tc_thr_name = "ldlm_cb",
2812 .tc_thr_factor = LDLM_THR_FACTOR,
2813 .tc_nthrs_init = LDLM_NTHRS_INIT,
2814 .tc_nthrs_base = LDLM_NTHRS_BASE,
2815 .tc_nthrs_max = LDLM_NTHRS_MAX,
2816 .tc_nthrs_user = ldlm_num_threads,
2817 .tc_cpu_affinity = 1,
2818 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2821 .cc_pattern = ldlm_cpts,
2824 .so_req_handler = ldlm_callback_handler,
2827 ldlm_state->ldlm_cb_service = \
2828 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2829 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2830 CERROR("failed to start service\n");
2831 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2832 ldlm_state->ldlm_cb_service = NULL;
2836 #ifdef HAVE_SERVER_SUPPORT
2837 memset(&conf, 0, sizeof(conf));
2838 conf = (typeof(conf)) {
2839 .psc_name = "ldlm_canceld",
2840 .psc_watchdog_factor = 6,
2842 .bc_nbufs = LDLM_SERVER_NBUFS,
2843 .bc_buf_size = LDLM_BUFSIZE,
2844 .bc_req_max_size = LDLM_MAXREQSIZE,
2845 .bc_rep_max_size = LDLM_MAXREPSIZE,
2846 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2847 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2851 .tc_thr_name = "ldlm_cn",
2852 .tc_thr_factor = LDLM_THR_FACTOR,
2853 .tc_nthrs_init = LDLM_NTHRS_INIT,
2854 .tc_nthrs_base = LDLM_NTHRS_BASE,
2855 .tc_nthrs_max = LDLM_NTHRS_MAX,
2856 .tc_nthrs_user = ldlm_num_threads,
2857 .tc_cpu_affinity = 1,
2858 .tc_ctx_tags = LCT_MD_THREAD | \
2863 .cc_pattern = ldlm_cpts,
2866 .so_req_handler = ldlm_cancel_handler,
2867 .so_hpreq_handler = ldlm_hpreq_handler,
2870 ldlm_state->ldlm_cancel_service = \
2871 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2872 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2873 CERROR("failed to start service\n");
2874 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2875 ldlm_state->ldlm_cancel_service = NULL;
2880 OBD_ALLOC(blp, sizeof(*blp));
2882 GOTO(out, rc = -ENOMEM);
2883 ldlm_state->ldlm_bl_pool = blp;
2885 spin_lock_init(&blp->blp_lock);
2886 CFS_INIT_LIST_HEAD(&blp->blp_list);
2887 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2888 init_waitqueue_head(&blp->blp_waitq);
2889 cfs_atomic_set(&blp->blp_num_threads, 0);
2890 cfs_atomic_set(&blp->blp_busy_threads, 0);
2893 if (ldlm_num_threads == 0) {
2894 blp->blp_min_threads = LDLM_NTHRS_INIT;
2895 blp->blp_max_threads = LDLM_NTHRS_MAX;
2897 blp->blp_min_threads = blp->blp_max_threads = \
2898 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2902 for (i = 0; i < blp->blp_min_threads; i++) {
2903 rc = ldlm_bl_thread_start(blp);
2908 # ifdef HAVE_SERVER_SUPPORT
2909 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2910 expired_lock_thread.elt_state = ELT_STOPPED;
2911 init_waitqueue_head(&expired_lock_thread.elt_waitq);
2913 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2914 spin_lock_init(&waiting_locks_spinlock);
2915 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2917 rc = PTR_ERR(kthread_run(expired_lock_main, NULL, "ldlm_elt"));
2918 if (IS_ERR_VALUE(rc)) {
2919 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2923 wait_event(expired_lock_thread.elt_waitq,
2924 expired_lock_thread.elt_state == ELT_READY);
2925 # endif /* HAVE_SERVER_SUPPORT */
2927 rc = ldlm_pools_init();
2929 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2940 static int ldlm_cleanup(void)
2944 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2945 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2946 CERROR("ldlm still has namespaces; clean these up first.\n");
2947 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2948 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2955 if (ldlm_state->ldlm_bl_pool != NULL) {
2956 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2958 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2959 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2961 init_completion(&blp->blp_comp);
2963 spin_lock(&blp->blp_lock);
2964 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2965 wake_up(&blp->blp_waitq);
2966 spin_unlock(&blp->blp_lock);
2968 wait_for_completion(&blp->blp_comp);
2971 OBD_FREE(blp, sizeof(*blp));
2973 #endif /* __KERNEL__ */
2975 if (ldlm_state->ldlm_cb_service != NULL)
2976 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2977 # ifdef HAVE_SERVER_SUPPORT
2978 if (ldlm_state->ldlm_cancel_service != NULL)
2979 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2983 ldlm_proc_cleanup();
2985 # ifdef HAVE_SERVER_SUPPORT
2986 if (expired_lock_thread.elt_state != ELT_STOPPED) {
2987 expired_lock_thread.elt_state = ELT_TERMINATE;
2988 wake_up(&expired_lock_thread.elt_waitq);
2989 wait_event(expired_lock_thread.elt_waitq,
2990 expired_lock_thread.elt_state == ELT_STOPPED);
2993 #endif /* __KERNEL__ */
2995 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
3003 mutex_init(&ldlm_ref_mutex);
3004 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
3005 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
3006 ldlm_resource_slab = kmem_cache_create("ldlm_resources",
3007 sizeof(struct ldlm_resource), 0,
3008 SLAB_HWCACHE_ALIGN, NULL);
3009 if (ldlm_resource_slab == NULL)
3012 ldlm_lock_slab = kmem_cache_create("ldlm_locks",
3013 sizeof(struct ldlm_lock), 0,
3014 SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU, NULL);
3015 if (ldlm_lock_slab == NULL) {
3016 kmem_cache_destroy(ldlm_resource_slab);
3020 ldlm_interval_slab = kmem_cache_create("interval_node",
3021 sizeof(struct ldlm_interval),
3022 0, SLAB_HWCACHE_ALIGN, NULL);
3023 if (ldlm_interval_slab == NULL) {
3024 kmem_cache_destroy(ldlm_resource_slab);
3025 kmem_cache_destroy(ldlm_lock_slab);
3028 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3029 class_export_dump_hook = ldlm_dump_export_locks;
3034 void ldlm_exit(void)
3037 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
3038 kmem_cache_destroy(ldlm_resource_slab);
3040 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
3041 * synchronize_rcu() to wait a grace period elapsed, so that
3042 * ldlm_lock_free() get a chance to be called. */
3045 kmem_cache_destroy(ldlm_lock_slab);
3046 kmem_cache_destroy(ldlm_interval_slab);